Use voluptuous for Octoprint (#3111)
* Migrate to voluptuous * Fix pylint issues
This commit is contained in:
1 parent
000832a82c
commit
6a84b82663
3 files changed
+93
-64
No files matched your search
@@ -5,31 +5,44 @@ For more details about this platform, please refer to the documentation at
|
||||
https://home-assistant.io/components/sensor.octoprint/
|
||||
"""
|
||||
import logging
|
||||
import requests
|
||||
|
||||
from homeassistant.const import TEMP_CELSIUS, CONF_NAME
|
||||
import requests
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
||||
from homeassistant.const import (
|
||||
TEMP_CELSIUS, CONF_NAME, CONF_MONITORED_CONDITIONS)
|
||||
from homeassistant.helpers.entity import Entity
|
||||
from homeassistant.loader import get_component
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
|
||||
DEPENDENCIES = ["octoprint"]
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
DEPENDENCIES = ['octoprint']
|
||||
|
||||
DEFAULT_NAME = 'OctoPrint'
|
||||
|
||||
SENSOR_TYPES = {
|
||||
# API Endpoint, Group, Key, unit
|
||||
"Temperatures": ["printer", "temperature", "*", TEMP_CELSIUS],
|
||||
"Current State": ["printer", "state", "text", None],
|
||||
"Job Percentage": ["job", "progress", "completion", "%"],
|
||||
'Temperatures': ['printer', 'temperature', '*', TEMP_CELSIUS],
|
||||
'Current State': ['printer', 'state', 'text', None],
|
||||
'Job Percentage': ['job', 'progress', 'completion', '%'],
|
||||
}
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||
vol.Optional(CONF_MONITORED_CONDITIONS, default=SENSOR_TYPES):
|
||||
vol.All(cv.ensure_list, [vol.In(SENSOR_TYPES)]),
|
||||
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
||||
})
|
||||
|
||||
|
||||
# pylint: disable=unused-argument
|
||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||
"""Setup the available OctoPrint sensors."""
|
||||
octoprint = get_component('octoprint')
|
||||
name = config.get(CONF_NAME, "OctoPrint")
|
||||
monitored_conditions = config.get("monitored_conditions",
|
||||
SENSOR_TYPES.keys())
|
||||
name = config.get(CONF_NAME)
|
||||
monitored_conditions = config.get(CONF_MONITORED_CONDITIONS)
|
||||
|
||||
devices = []
|
||||
types = ["actual", "target"]
|
||||
@@ -46,19 +59,19 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||
SENSOR_TYPES[octo_type][1],
|
||||
tool)
|
||||
devices.append(new_sensor)
|
||||
elif octo_type in SENSOR_TYPES:
|
||||
new_sensor = OctoPrintSensor(octoprint.OCTOPRINT,
|
||||
octo_type,
|
||||
SENSOR_TYPES[octo_type][2],
|
||||
name,
|
||||
SENSOR_TYPES[octo_type][3],
|
||||
SENSOR_TYPES[octo_type][0],
|
||||
SENSOR_TYPES[octo_type][1])
|
||||
devices.append(new_sensor)
|
||||
else:
|
||||
_LOGGER.error("Unknown OctoPrint sensor type: %s", octo_type)
|
||||
|
||||
add_devices(devices)
|
||||
new_sensor = OctoPrintSensor(octoprint.OCTOPRINT,
|
||||
octo_type,
|
||||
SENSOR_TYPES[octo_type][2],
|
||||
name,
|
||||
SENSOR_TYPES[octo_type][3],
|
||||
SENSOR_TYPES[octo_type][0],
|
||||
SENSOR_TYPES[octo_type][1])
|
||||
devices.append(new_sensor)
|
||||
|
||||
add_devices(devices)
|
||||
|
||||
|
||||
# pylint: disable=too-many-instance-attributes
|
||||
@@ -66,14 +79,15 @@ class OctoPrintSensor(Entity):
|
||||
"""Representation of an OctoPrint sensor."""
|
||||
|
||||
# pylint: disable=too-many-arguments
|
||||
def __init__(self, api, condition, sensor_type, sensor_name,
|
||||
unit, endpoint, group, tool=None):
|
||||
def __init__(self, api, condition, sensor_type, sensor_name, unit,
|
||||
endpoint, group, tool=None):
|
||||
"""Initialize a new OctoPrint sensor."""
|
||||
self.sensor_name = sensor_name
|
||||
if tool is None:
|
||||
self._name = sensor_name + ' ' + condition
|
||||
self._name = '{} {}'.format(sensor_name, condition)
|
||||
else:
|
||||
self._name = sensor_name + ' ' + condition + ' ' + tool + ' temp'
|
||||
self._name = '{} {} {} {}'.format(
|
||||
sensor_name, condition, tool, ' temp')
|
||||
self.sensor_type = sensor_type
|
||||
self.api = api
|
||||
self._state = None
|
||||
|
||||
Reference in new issue
Block a user