From 663735542bd37d1487b2173a86dfbe08ce0f9500 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Tue, 3 Mar 2015 22:17:47 -0800 Subject: [PATCH 1/2] Refactor tellstick_sensor to a sensor platform --- homeassistant/components/sensor/tellstick.py | 126 +++++++++++++++++ homeassistant/components/tellstick_sensor.py | 141 ------------------- 2 files changed, 126 insertions(+), 141 deletions(-) create mode 100644 homeassistant/components/sensor/tellstick.py delete mode 100644 homeassistant/components/tellstick_sensor.py diff --git a/homeassistant/components/sensor/tellstick.py b/homeassistant/components/sensor/tellstick.py new file mode 100644 index 00000000000..3e72b9d065e --- /dev/null +++ b/homeassistant/components/sensor/tellstick.py @@ -0,0 +1,126 @@ +""" +homeassistant.components.sensor.tellstick +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Shows sensor values from tellstick sensors. + +Possible config keys: + +id of the sensor: Name the sensor with ID +135=Outside + +only_named: Only show the named sensors +only_named=1 + +temperature_scale: The scale of the temperature value +temperature_scale=°C + +datatype_mask: mask to determine which sensor values to show based on +https://tellcore-py.readthedocs.org + /en/v1.0.4/constants.html#module-tellcore.constants + +datatype_mask=1 # only show temperature +datatype_mask=12 # only show rain rate and rain total +datatype_mask=127 # show all sensor values +""" +import logging +from collections import namedtuple + +import tellcore.telldus as telldus +import tellcore.constants as tellcore_constants + +from homeassistant.const import ( + ATTR_FRIENDLY_NAME, ATTR_UNIT_OF_MEASUREMENT, TEMP_CELCIUS) +from homeassistant.helpers import Device + +DatatypeDescription = namedtuple("DatatypeDescription", ['name', 'unit']) + + +# pylint: disable=unused-argument +def setup_platform(hass, config, add_devices, discovery_info=None): + """ Sets up Tellstick sensors. """ + sensor_value_descriptions = { + tellcore_constants.TELLSTICK_TEMPERATURE: + DatatypeDescription( + 'temperature', config.get('temperature_scale', TEMP_CELCIUS)), + + tellcore_constants.TELLSTICK_HUMIDITY: + DatatypeDescription('humidity', '%'), + + tellcore_constants.TELLSTICK_RAINRATE: + DatatypeDescription('rain rate', ''), + + tellcore_constants.TELLSTICK_RAINTOTAL: + DatatypeDescription('rain total', ''), + + tellcore_constants.TELLSTICK_WINDDIRECTION: + DatatypeDescription('wind direction', ''), + + tellcore_constants.TELLSTICK_WINDAVERAGE: + DatatypeDescription('wind average', ''), + + tellcore_constants.TELLSTICK_WINDGUST: + DatatypeDescription('wind gust', '') + } + + try: + core = telldus.TelldusCore() + except OSError: + logging.getLogger(__name__).exception( + 'Could not initialize Tellstick.') + return + + sensors = [] + + for ts_sensor in core.sensors(): + try: + sensor_name = config[str(ts_sensor.id)] + except KeyError: + if 'only_named' in config: + continue + sensor_name = str(ts_sensor.id) + + for datatype in sensor_value_descriptions.keys(): + if datatype & int(config['datatype_mask']) and \ + ts_sensor.has_value(datatype): + + sensor_info = sensor_value_descriptions[datatype] + + sensors.append( + TellstickSensor( + sensor_name, ts_sensor, datatype, sensor_info)) + + add_devices(sensors) + + +class TellstickSensor(Device): + """ Represents a Tellstick sensor. """ + + def __init__(self, name, sensor, datatype, sensor_info): + self.datatype = datatype + self.sensor = sensor + self.unit = sensor_info.unit or None + + self._name = "{} {}".format(name, sensor_info.name) + + @property + def name(self): + """ Returns the name of the device. """ + return self._name + + @property + def state(self): + """ Returns the state of the device. """ + return self.sensor.value(self.datatype).value + + @property + def state_attributes(self): + """ Returns the state attributes. """ + attrs = { + ATTR_FRIENDLY_NAME: self._name, + } + + if self.unit: + attrs[ATTR_UNIT_OF_MEASUREMENT] = self.unit + + return attrs diff --git a/homeassistant/components/tellstick_sensor.py b/homeassistant/components/tellstick_sensor.py deleted file mode 100644 index 5f1ad748900..00000000000 --- a/homeassistant/components/tellstick_sensor.py +++ /dev/null @@ -1,141 +0,0 @@ -""" -homeassistant.components.tellstick_sensor -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -Shows sensor values from tellstick sensors. - -Possible config keys: - -id of the sensor: Name the sensor with ID -135=Outside - -only_named: Only show the named sensors -only_named=1 - -temperature_scale: The scale of the temperature value -temperature_scale=°C - -datatype_mask: mask to determine which sensor values to show based on -https://tellcore-py.readthedocs.org - /en/v1.0.4/constants.html#module-tellcore.constants - -datatype_mask=1 # only show temperature -datatype_mask=12 # only show rain rate and rain total -datatype_mask=127 # show all sensor values -""" -import logging -from collections import namedtuple - -import homeassistant.util as util -from homeassistant.const import ATTR_FRIENDLY_NAME, ATTR_UNIT_OF_MEASUREMENT - -# The domain of your component. Should be equal to the name of your component -DOMAIN = "tellstick_sensor" - -# List of component names (string) your component depends upon -# If you are setting up a group but not using a group for anything, -# don't depend on group -DEPENDENCIES = [] - -ENTITY_ID_FORMAT = DOMAIN + '.{}' - -DatatypeDescription = namedtuple("DatatypeDescription", ['name', 'unit']) - - -def setup(hass, config): - """ Register services or listen for events that your component needs. """ - - logger = logging.getLogger(__name__) - - try: - import tellcore.telldus as telldus - import tellcore.constants as tellcore_constants - except ImportError: - logger.exception( - "Failed to import tellcore") - return False - - core = telldus.TelldusCore() - - sensors = core.sensors() - - if len(sensors) == 0: - logger.error("No Tellstick sensors found") - return False - - sensor_value_descriptions = { - tellcore_constants.TELLSTICK_TEMPERATURE: - DatatypeDescription( - 'temperature', config[DOMAIN]['temperature_scale']), - - tellcore_constants.TELLSTICK_HUMIDITY: - DatatypeDescription('humidity', ' %'), - - tellcore_constants.TELLSTICK_RAINRATE: - DatatypeDescription('rain rate', ''), - - tellcore_constants.TELLSTICK_RAINTOTAL: - DatatypeDescription('rain total', ''), - - tellcore_constants.TELLSTICK_WINDDIRECTION: - DatatypeDescription('wind direction', ''), - - tellcore_constants.TELLSTICK_WINDAVERAGE: - DatatypeDescription('wind average', ''), - - tellcore_constants.TELLSTICK_WINDGUST: - DatatypeDescription('wind gust', '') - } - - def update_sensor_value_state(sensor_name, sensor_value): - """ Update the state of a sensor value """ - sensor_value_description = \ - sensor_value_descriptions[sensor_value.datatype] - sensor_value_name = '{} {}'.format( - sensor_name, sensor_value_description.name) - - entity_id = ENTITY_ID_FORMAT.format( - util.slugify(sensor_value_name)) - - state = sensor_value.value - - state_attr = { - ATTR_FRIENDLY_NAME: sensor_value_name, - ATTR_UNIT_OF_MEASUREMENT: sensor_value_description.unit - } - - hass.states.set(entity_id, state, state_attr) - - sensor_value_datatypes = [ - tellcore_constants.TELLSTICK_TEMPERATURE, - tellcore_constants.TELLSTICK_HUMIDITY, - tellcore_constants.TELLSTICK_RAINRATE, - tellcore_constants.TELLSTICK_RAINTOTAL, - tellcore_constants.TELLSTICK_WINDDIRECTION, - tellcore_constants.TELLSTICK_WINDAVERAGE, - tellcore_constants.TELLSTICK_WINDGUST - ] - - def update_sensor_state(sensor): - """ Updates all the sensor values from the sensor """ - try: - sensor_name = config[DOMAIN][str(sensor.id)] - except KeyError: - if 'only_named' in config[DOMAIN]: - return - sensor_name = str(sensor.id) - - for datatype in sensor_value_datatypes: - if datatype & int(config[DOMAIN]['datatype_mask']) and \ - sensor.has_value(datatype): - update_sensor_value_state(sensor_name, sensor.value(datatype)) - - def update_sensors_state(time): - """ Update the state of all sensors """ - for sensor in sensors: - update_sensor_state(sensor) - - update_sensors_state(None) - - hass.track_time_change(update_sensors_state, second=[0, 30]) - - return True From fc3375508e0aa0b191289e49e159e7c7de9292fa Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Tue, 3 Mar 2015 22:20:48 -0800 Subject: [PATCH 2/2] Frontend: Remove tellstick_sensor domain icon --- homeassistant/components/frontend/version.py | 2 +- homeassistant/components/frontend/www_static/frontend.html | 2 +- .../frontend/www_static/polymer/components/domain-icon.html | 3 --- 3 files changed, 2 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/frontend/version.py b/homeassistant/components/frontend/version.py index a9b8d548069..329a90d0156 100644 --- a/homeassistant/components/frontend/version.py +++ b/homeassistant/components/frontend/version.py @@ -1,2 +1,2 @@ """ DO NOT MODIFY. Auto-generated by build_frontend script """ -VERSION = "30729f23c19a66d9364d78b2379867cc" +VERSION = "d55e35f81749356431666fe86ebe005c" diff --git a/homeassistant/components/frontend/www_static/frontend.html b/homeassistant/components/frontend/www_static/frontend.html index 9a708fe13cd..0c63ac93ee4 100644 --- a/homeassistant/components/frontend/www_static/frontend.html +++ b/homeassistant/components/frontend/www_static/frontend.html @@ -168,7 +168,7 @@ i.addChangeListener(u),n.push({store:i,listener:u}),t&&u()}}.bind(this)),this._s } -