From 050caa71684e1b4aa4c639ad8717b257118970f6 Mon Sep 17 00:00:00 2001 From: Fredrik Erlandsson Date: Thu, 29 Nov 2018 10:27:04 +0100 Subject: [PATCH 1/7] move component to a package --- .../components/{tellduslive.py => tellduslive/__init__.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename homeassistant/components/{tellduslive.py => tellduslive/__init__.py} (100%) diff --git a/homeassistant/components/tellduslive.py b/homeassistant/components/tellduslive/__init__.py similarity index 100% rename from homeassistant/components/tellduslive.py rename to homeassistant/components/tellduslive/__init__.py From 81fb76b1a5426e9182f8740af7f105f4cb1d11e0 Mon Sep 17 00:00:00 2001 From: Fredrik Erlandsson Date: Thu, 29 Nov 2018 11:12:49 +0100 Subject: [PATCH 2/7] move TelldusLiveEntry to separate file --- .../components/binary_sensor/tellduslive.py | 2 +- homeassistant/components/cover/tellduslive.py | 2 +- homeassistant/components/light/tellduslive.py | 2 +- .../components/sensor/tellduslive.py | 2 +- .../components/switch/tellduslive.py | 2 +- .../components/tellduslive/__init__.py | 100 +----------------- homeassistant/components/tellduslive/const.py | 3 + homeassistant/components/tellduslive/entry.py | 100 ++++++++++++++++++ 8 files changed, 112 insertions(+), 101 deletions(-) create mode 100644 homeassistant/components/tellduslive/const.py create mode 100644 homeassistant/components/tellduslive/entry.py diff --git a/homeassistant/components/binary_sensor/tellduslive.py b/homeassistant/components/binary_sensor/tellduslive.py index 450a5e580bdc09..9770299db71ae3 100644 --- a/homeassistant/components/binary_sensor/tellduslive.py +++ b/homeassistant/components/binary_sensor/tellduslive.py @@ -9,7 +9,7 @@ """ import logging -from homeassistant.components.tellduslive import TelldusLiveEntity +from homeassistant.components.tellduslive.entry import TelldusLiveEntity from homeassistant.components.binary_sensor import BinarySensorDevice _LOGGER = logging.getLogger(__name__) diff --git a/homeassistant/components/cover/tellduslive.py b/homeassistant/components/cover/tellduslive.py index 9d292d9e8b5bb3..73f3df972d2523 100644 --- a/homeassistant/components/cover/tellduslive.py +++ b/homeassistant/components/cover/tellduslive.py @@ -9,7 +9,7 @@ import logging from homeassistant.components.cover import CoverDevice -from homeassistant.components.tellduslive import TelldusLiveEntity +from homeassistant.components.tellduslive.entry import TelldusLiveEntity _LOGGER = logging.getLogger(__name__) diff --git a/homeassistant/components/light/tellduslive.py b/homeassistant/components/light/tellduslive.py index 07b5458fa4506c..c61a23fe7e1eaf 100644 --- a/homeassistant/components/light/tellduslive.py +++ b/homeassistant/components/light/tellduslive.py @@ -10,7 +10,7 @@ from homeassistant.components.light import ( ATTR_BRIGHTNESS, SUPPORT_BRIGHTNESS, Light) -from homeassistant.components.tellduslive import TelldusLiveEntity +from homeassistant.components.tellduslive.entry import TelldusLiveEntity _LOGGER = logging.getLogger(__name__) diff --git a/homeassistant/components/sensor/tellduslive.py b/homeassistant/components/sensor/tellduslive.py index 9bd5a1d8413192..0928ea7cfec9ad 100644 --- a/homeassistant/components/sensor/tellduslive.py +++ b/homeassistant/components/sensor/tellduslive.py @@ -6,7 +6,7 @@ """ import logging -from homeassistant.components.tellduslive import TelldusLiveEntity +from homeassistant.components.tellduslive.entry import TelldusLiveEntity from homeassistant.const import ( DEVICE_CLASS_HUMIDITY, DEVICE_CLASS_ILLUMINANCE, DEVICE_CLASS_TEMPERATURE, TEMP_CELSIUS) diff --git a/homeassistant/components/switch/tellduslive.py b/homeassistant/components/switch/tellduslive.py index 0263dfd8198c97..9b1caa6963b5a4 100644 --- a/homeassistant/components/switch/tellduslive.py +++ b/homeassistant/components/switch/tellduslive.py @@ -9,7 +9,7 @@ """ import logging -from homeassistant.components.tellduslive import TelldusLiveEntity +from homeassistant.components.tellduslive.entry import TelldusLiveEntity from homeassistant.helpers.entity import ToggleEntity _LOGGER = logging.getLogger(__name__) diff --git a/homeassistant/components/tellduslive/__init__.py b/homeassistant/components/tellduslive/__init__.py index a6ba248b99b4e8..117e21610f3d3c 100644 --- a/homeassistant/components/tellduslive/__init__.py +++ b/homeassistant/components/tellduslive/__init__.py @@ -4,25 +4,23 @@ For more details about this component, please refer to the documentation at https://home-assistant.io/components/tellduslive/ """ -from datetime import datetime, timedelta +from datetime import timedelta import logging import voluptuous as vol from homeassistant.components.discovery import SERVICE_TELLDUSLIVE from homeassistant.const import ( - ATTR_BATTERY_LEVEL, CONF_HOST, CONF_TOKEN, DEVICE_DEFAULT_NAME, - EVENT_HOMEASSISTANT_START) + CONF_HOST, CONF_TOKEN, EVENT_HOMEASSISTANT_START) from homeassistant.helpers import discovery import homeassistant.helpers.config_validation as cv -from homeassistant.helpers.entity import Entity from homeassistant.helpers.event import track_point_in_utc_time from homeassistant.util.dt import utcnow from homeassistant.util.json import load_json, save_json -APPLICATION_NAME = 'Home Assistant' +from .const import DOMAIN -DOMAIN = 'tellduslive' +APPLICATION_NAME = 'Home Assistant' REQUIREMENTS = ['tellduslive==0.10.4'] @@ -49,8 +47,6 @@ }, extra=vol.ALLOW_EXTRA) -ATTR_LAST_UPDATED = 'time_last_updated' - CONFIG_INSTRUCTIONS = """ To link your TelldusLive account: @@ -276,91 +272,3 @@ def device(self, device_id): def is_available(self, device_id): """Return device availability.""" return device_id in self._client.device_ids - - -class TelldusLiveEntity(Entity): - """Base class for all Telldus Live entities.""" - - def __init__(self, hass, device_id): - """Initialize the entity.""" - self._id = device_id - self._client = hass.data[DOMAIN] - self._client.entities.append(self) - self._name = self.device.name - _LOGGER.debug('Created device %s', self) - - def changed(self): - """Return the property of the device might have changed.""" - if self.device.name: - self._name = self.device.name - self.schedule_update_ha_state() - - @property - def device_id(self): - """Return the id of the device.""" - return self._id - - @property - def device(self): - """Return the representation of the device.""" - return self._client.device(self.device_id) - - @property - def _state(self): - """Return the state of the device.""" - return self.device.state - - @property - def should_poll(self): - """Return the polling state.""" - return False - - @property - def assumed_state(self): - """Return true if unable to access real state of entity.""" - return True - - @property - def name(self): - """Return name of device.""" - return self._name or DEVICE_DEFAULT_NAME - - @property - def available(self): - """Return true if device is not offline.""" - return self._client.is_available(self.device_id) - - @property - def device_state_attributes(self): - """Return the state attributes.""" - attrs = {} - if self._battery_level: - attrs[ATTR_BATTERY_LEVEL] = self._battery_level - if self._last_updated: - attrs[ATTR_LAST_UPDATED] = self._last_updated - return attrs - - @property - def _battery_level(self): - """Return the battery level of a device.""" - from tellduslive import (BATTERY_LOW, - BATTERY_UNKNOWN, - BATTERY_OK) - if self.device.battery == BATTERY_LOW: - return 1 - if self.device.battery == BATTERY_UNKNOWN: - return None - if self.device.battery == BATTERY_OK: - return 100 - return self.device.battery # Percentage - - @property - def _last_updated(self): - """Return the last update of a device.""" - return str(datetime.fromtimestamp(self.device.lastUpdated)) \ - if self.device.lastUpdated else None - - @property - def unique_id(self) -> str: - """Return a unique ID.""" - return self._id diff --git a/homeassistant/components/tellduslive/const.py b/homeassistant/components/tellduslive/const.py new file mode 100644 index 00000000000000..1c76e027b428c4 --- /dev/null +++ b/homeassistant/components/tellduslive/const.py @@ -0,0 +1,3 @@ +"""Consts used by TelldusLive.""" + +DOMAIN = 'tellduslive' diff --git a/homeassistant/components/tellduslive/entry.py b/homeassistant/components/tellduslive/entry.py new file mode 100644 index 00000000000000..238acd091f18d0 --- /dev/null +++ b/homeassistant/components/tellduslive/entry.py @@ -0,0 +1,100 @@ +"""Base Entity for all TelldusLiveEntities.""" +from datetime import datetime +import logging + +from homeassistant.const import ( + ATTR_BATTERY_LEVEL, DEVICE_DEFAULT_NAME) +from homeassistant.helpers.entity import Entity +from .const import DOMAIN + +_LOGGER = logging.getLogger(__name__) + +ATTR_LAST_UPDATED = 'time_last_updated' + + +class TelldusLiveEntity(Entity): + """Base class for all Telldus Live entities.""" + + def __init__(self, hass, device_id): + """Initialize the entity.""" + self._id = device_id + self._client = hass.data[DOMAIN] + self._client.entities.append(self) + self._name = self.device.name + _LOGGER.debug('Created device %s', self) + + def changed(self): + """Return the property of the device might have changed.""" + if self.device.name: + self._name = self.device.name + self.schedule_update_ha_state() + + @property + def device_id(self): + """Return the id of the device.""" + return self._id + + @property + def device(self): + """Return the representation of the device.""" + return self._client.device(self.device_id) + + @property + def _state(self): + """Return the state of the device.""" + return self.device.state + + @property + def should_poll(self): + """Return the polling state.""" + return False + + @property + def assumed_state(self): + """Return true if unable to access real state of entity.""" + return True + + @property + def name(self): + """Return name of device.""" + return self._name or DEVICE_DEFAULT_NAME + + @property + def available(self): + """Return true if device is not offline.""" + return self._client.is_available(self.device_id) + + @property + def device_state_attributes(self): + """Return the state attributes.""" + attrs = {} + if self._battery_level: + attrs[ATTR_BATTERY_LEVEL] = self._battery_level + if self._last_updated: + attrs[ATTR_LAST_UPDATED] = self._last_updated + return attrs + + @property + def _battery_level(self): + """Return the battery level of a device.""" + from tellduslive import (BATTERY_LOW, + BATTERY_UNKNOWN, + BATTERY_OK) + if self.device.battery == BATTERY_LOW: + return 1 + if self.device.battery == BATTERY_UNKNOWN: + return None + if self.device.battery == BATTERY_OK: + return 100 + return self.device.battery # Percentage + + @property + def _last_updated(self): + """Return the last update of a device.""" + return str(datetime.fromtimestamp(self.device.lastUpdated)) \ + if self.device.lastUpdated else None + + @property + def unique_id(self) -> str: + """Return a unique ID.""" + return self._id From aa047f0b12db133148eb23e74506242820470627 Mon Sep 17 00:00:00 2001 From: Fredrik Erlandsson Date: Thu, 29 Nov 2018 11:32:45 +0100 Subject: [PATCH 3/7] refactor * move entities from a shared container * using the dispatch helper instead for communication between component and platforms --- .../components/binary_sensor/tellduslive.py | 2 +- homeassistant/components/cover/tellduslive.py | 3 --- homeassistant/components/light/tellduslive.py | 1 - .../components/switch/tellduslive.py | 2 -- .../components/tellduslive/__init__.py | 13 +++++----- homeassistant/components/tellduslive/const.py | 2 ++ homeassistant/components/tellduslive/entry.py | 25 +++++++++++++++---- 7 files changed, 29 insertions(+), 19 deletions(-) diff --git a/homeassistant/components/binary_sensor/tellduslive.py b/homeassistant/components/binary_sensor/tellduslive.py index 9770299db71ae3..9bb743fc40963b 100644 --- a/homeassistant/components/binary_sensor/tellduslive.py +++ b/homeassistant/components/binary_sensor/tellduslive.py @@ -9,8 +9,8 @@ """ import logging -from homeassistant.components.tellduslive.entry import TelldusLiveEntity from homeassistant.components.binary_sensor import BinarySensorDevice +from homeassistant.components.tellduslive.entry import TelldusLiveEntity _LOGGER = logging.getLogger(__name__) diff --git a/homeassistant/components/cover/tellduslive.py b/homeassistant/components/cover/tellduslive.py index 73f3df972d2523..f8fb154f4fc4c6 100644 --- a/homeassistant/components/cover/tellduslive.py +++ b/homeassistant/components/cover/tellduslive.py @@ -33,14 +33,11 @@ def is_closed(self): def close_cover(self, **kwargs): """Close the cover.""" self.device.down() - self.changed() def open_cover(self, **kwargs): """Open the cover.""" self.device.up() - self.changed() def stop_cover(self, **kwargs): """Stop the cover.""" self.device.stop() - self.changed() diff --git a/homeassistant/components/light/tellduslive.py b/homeassistant/components/light/tellduslive.py index c61a23fe7e1eaf..92b87a93a430e0 100644 --- a/homeassistant/components/light/tellduslive.py +++ b/homeassistant/components/light/tellduslive.py @@ -33,7 +33,6 @@ def __init__(self, hass, device_id): def changed(self): """Define a property of the device that might have changed.""" self._last_brightness = self.brightness - super().changed() @property def brightness(self): diff --git a/homeassistant/components/switch/tellduslive.py b/homeassistant/components/switch/tellduslive.py index 9b1caa6963b5a4..dc12fafcfafdcd 100644 --- a/homeassistant/components/switch/tellduslive.py +++ b/homeassistant/components/switch/tellduslive.py @@ -33,9 +33,7 @@ def is_on(self): def turn_on(self, **kwargs): """Turn the switch on.""" self.device.turn_on() - self.changed() def turn_off(self, **kwargs): """Turn the switch off.""" self.device.turn_off() - self.changed() diff --git a/homeassistant/components/tellduslive/__init__.py b/homeassistant/components/tellduslive/__init__.py index 117e21610f3d3c..f14963332d6180 100644 --- a/homeassistant/components/tellduslive/__init__.py +++ b/homeassistant/components/tellduslive/__init__.py @@ -14,11 +14,12 @@ CONF_HOST, CONF_TOKEN, EVENT_HOMEASSISTANT_START) from homeassistant.helpers import discovery import homeassistant.helpers.config_validation as cv +from homeassistant.helpers.dispatcher import async_dispatcher_send from homeassistant.helpers.event import track_point_in_utc_time from homeassistant.util.dt import utcnow from homeassistant.util.json import load_json, save_json -from .const import DOMAIN +from .const import DOMAIN, SIGNAL_UPDATE_ENTITY APPLICATION_NAME = 'Home Assistant' @@ -46,7 +47,6 @@ }), }, extra=vol.ALLOW_EXTRA) - CONFIG_INSTRUCTIONS = """ To link your TelldusLive account: @@ -206,7 +206,7 @@ class TelldusLiveClient: def __init__(self, hass, config, session): """Initialize the Tellus data object.""" - self.entities = [] + self._known_devices = set() self._hass = hass self._config = config @@ -250,9 +250,8 @@ def discover(device_id, component): discovery.load_platform( self._hass, component, DOMAIN, [device_id], self._config) - known_ids = {entity.device_id for entity in self.entities} for device in self._client.devices: - if device.device_id in known_ids: + if device.device_id in self._known_devices: continue if device.is_sensor: for item in device.items: @@ -261,9 +260,9 @@ def discover(device_id, component): else: discover(device.device_id, identify_device(device)) + self._known_devices.add(device.device_id) - for entity in self.entities: - entity.changed() + async_dispatcher_send(self._hass, SIGNAL_UPDATE_ENTITY) def device(self, device_id): """Return device representation.""" diff --git a/homeassistant/components/tellduslive/const.py b/homeassistant/components/tellduslive/const.py index 1c76e027b428c4..a4ef33af5186d6 100644 --- a/homeassistant/components/tellduslive/const.py +++ b/homeassistant/components/tellduslive/const.py @@ -1,3 +1,5 @@ """Consts used by TelldusLive.""" DOMAIN = 'tellduslive' + +SIGNAL_UPDATE_ENTITY = 'tellduslive_update' diff --git a/homeassistant/components/tellduslive/entry.py b/homeassistant/components/tellduslive/entry.py index 238acd091f18d0..854f3bac8b246e 100644 --- a/homeassistant/components/tellduslive/entry.py +++ b/homeassistant/components/tellduslive/entry.py @@ -2,10 +2,12 @@ from datetime import datetime import logging -from homeassistant.const import ( - ATTR_BATTERY_LEVEL, DEVICE_DEFAULT_NAME) +from homeassistant.const import ATTR_BATTERY_LEVEL, DEVICE_DEFAULT_NAME +from homeassistant.core import callback +from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.entity import Entity -from .const import DOMAIN + +from .const import DOMAIN, SIGNAL_UPDATE_ENTITY _LOGGER = logging.getLogger(__name__) @@ -19,11 +21,24 @@ def __init__(self, hass, device_id): """Initialize the entity.""" self._id = device_id self._client = hass.data[DOMAIN] - self._client.entities.append(self) self._name = self.device.name + self._async_unsub_dispatcher_connect = None + _LOGGER.debug('Created device %s', self) + + async def async_added_to_hass(self): + """Call when entity is added to hass.""" _LOGGER.debug('Created device %s', self) + self._async_unsub_dispatcher_connect = async_dispatcher_connect( + self.hass, SIGNAL_UPDATE_ENTITY, self._update_callback) + self._update_callback() + + async def async_will_remove_from_hass(self): + """Disconnect dispatcher listener when removed.""" + if self._async_unsub_dispatcher_connect: + self._async_unsub_dispatcher_connect() - def changed(self): + @callback + def _update_callback(self): """Return the property of the device might have changed.""" if self.device.name: self._name = self.device.name From ef825f430338d2d75bd183ef8679c2f27fa1a9ad Mon Sep 17 00:00:00 2001 From: Fredrik Erlandsson Date: Fri, 30 Nov 2018 06:45:06 +0100 Subject: [PATCH 4/7] updated covereagerc and codeowners --- .coveragerc | 3 ++- CODEOWNERS | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/.coveragerc b/.coveragerc index f894d1edd4a6b6..d7a87c48f2af2a 100644 --- a/.coveragerc +++ b/.coveragerc @@ -323,7 +323,8 @@ omit = homeassistant/components/tahoma.py homeassistant/components/*/tahoma.py - homeassistant/components/tellduslive.py + homeassistant/components/tellduslive/__init__.py + homeassistant/components/tellduslive/entry.py homeassistant/components/*/tellduslive.py homeassistant/components/tellstick.py diff --git a/CODEOWNERS b/CODEOWNERS index 85f8d996fac0cf..2e623d0d9bd223 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -227,8 +227,8 @@ homeassistant/components/*/simplisafe.py @bachya # T homeassistant/components/tahoma.py @philklei homeassistant/components/*/tahoma.py @philklei -homeassistant/components/tellduslive.py @molobrakos @fredrike -homeassistant/components/*/tellduslive.py @molobrakos @fredrike +homeassistant/components/tellduslive/*.py @fredrike +homeassistant/components/*/tellduslive.py @fredrike homeassistant/components/tesla.py @zabuldon homeassistant/components/*/tesla.py @zabuldon homeassistant/components/thethingsnetwork.py @fabaff From dcd5b56bdac2aae08dd60730a5a1b1fa588cf2e2 Mon Sep 17 00:00:00 2001 From: Fredrik Erlandsson Date: Fri, 30 Nov 2018 12:02:11 +0100 Subject: [PATCH 5/7] suggestions from MartinHjelmare --- .../components/binary_sensor/tellduslive.py | 4 +- homeassistant/components/cover/tellduslive.py | 4 +- homeassistant/components/light/tellduslive.py | 8 ++-- .../components/sensor/tellduslive.py | 15 +++---- .../components/switch/tellduslive.py | 5 ++- .../components/tellduslive/__init__.py | 41 +++++++------------ homeassistant/components/tellduslive/entry.py | 9 ++-- 7 files changed, 42 insertions(+), 44 deletions(-) diff --git a/homeassistant/components/binary_sensor/tellduslive.py b/homeassistant/components/binary_sensor/tellduslive.py index 9bb743fc40963b..7f60e40c68bb43 100644 --- a/homeassistant/components/binary_sensor/tellduslive.py +++ b/homeassistant/components/binary_sensor/tellduslive.py @@ -9,6 +9,7 @@ """ import logging +from homeassistant.components import tellduslive from homeassistant.components.binary_sensor import BinarySensorDevice from homeassistant.components.tellduslive.entry import TelldusLiveEntity @@ -19,8 +20,9 @@ def setup_platform(hass, config, add_entities, discovery_info=None): """Set up Tellstick sensors.""" if discovery_info is None: return + client = hass.data[tellduslive.DOMAIN] add_entities( - TelldusLiveSensor(hass, binary_sensor) + TelldusLiveSensor(client, binary_sensor) for binary_sensor in discovery_info ) diff --git a/homeassistant/components/cover/tellduslive.py b/homeassistant/components/cover/tellduslive.py index f8fb154f4fc4c6..67affdae04e4b9 100644 --- a/homeassistant/components/cover/tellduslive.py +++ b/homeassistant/components/cover/tellduslive.py @@ -8,6 +8,7 @@ """ import logging +from homeassistant.components import tellduslive from homeassistant.components.cover import CoverDevice from homeassistant.components.tellduslive.entry import TelldusLiveEntity @@ -19,7 +20,8 @@ def setup_platform(hass, config, add_entities, discovery_info=None): if discovery_info is None: return - add_entities(TelldusLiveCover(hass, cover) for cover in discovery_info) + client = hass.data[tellduslive.DOMAIN] + add_entities(TelldusLiveCover(client, cover) for cover in discovery_info) class TelldusLiveCover(TelldusLiveEntity, CoverDevice): diff --git a/homeassistant/components/light/tellduslive.py b/homeassistant/components/light/tellduslive.py index 92b87a93a430e0..8601fe3cf1f581 100644 --- a/homeassistant/components/light/tellduslive.py +++ b/homeassistant/components/light/tellduslive.py @@ -8,6 +8,7 @@ """ import logging +from homeassistant.components import tellduslive from homeassistant.components.light import ( ATTR_BRIGHTNESS, SUPPORT_BRIGHTNESS, Light) from homeassistant.components.tellduslive.entry import TelldusLiveEntity @@ -19,15 +20,16 @@ def setup_platform(hass, config, add_entities, discovery_info=None): """Set up the Tellstick Net lights.""" if discovery_info is None: return - add_entities(TelldusLiveLight(hass, light) for light in discovery_info) + client = hass.data[tellduslive.DOMAIN] + add_entities(TelldusLiveLight(client, light) for light in discovery_info) class TelldusLiveLight(TelldusLiveEntity, Light): """Representation of a Tellstick Net light.""" - def __init__(self, hass, device_id): + def __init__(self, client, device_id): """Initialize the Tellstick Net light.""" - super().__init__(hass, device_id) + super().__init__(client, device_id) self._last_brightness = self.brightness def changed(self): diff --git a/homeassistant/components/sensor/tellduslive.py b/homeassistant/components/sensor/tellduslive.py index 0928ea7cfec9ad..9f9815acc25f24 100644 --- a/homeassistant/components/sensor/tellduslive.py +++ b/homeassistant/components/sensor/tellduslive.py @@ -6,6 +6,7 @@ """ import logging +from homeassistant.components import tellduslive from homeassistant.components.tellduslive.entry import TelldusLiveEntity from homeassistant.const import ( DEVICE_CLASS_HUMIDITY, DEVICE_CLASS_ILLUMINANCE, DEVICE_CLASS_TEMPERATURE, @@ -27,8 +28,8 @@ SENSOR_TYPE_BAROMETRIC_PRESSURE = 'barpress' SENSOR_TYPES = { - SENSOR_TYPE_TEMPERATURE: ['Temperature', TEMP_CELSIUS, None, - DEVICE_CLASS_TEMPERATURE], + SENSOR_TYPE_TEMPERATURE: + ['Temperature', TEMP_CELSIUS, None, DEVICE_CLASS_TEMPERATURE], SENSOR_TYPE_HUMIDITY: ['Humidity', '%', None, DEVICE_CLASS_HUMIDITY], SENSOR_TYPE_RAINRATE: ['Rain rate', 'mm/h', 'mdi:water', None], SENSOR_TYPE_RAINTOTAL: ['Rain total', 'mm', 'mdi:water', None], @@ -39,7 +40,7 @@ SENSOR_TYPE_WATT: ['Power', 'W', '', None], SENSOR_TYPE_LUMINANCE: ['Luminance', 'lx', None, DEVICE_CLASS_ILLUMINANCE], SENSOR_TYPE_DEW_POINT: - ['Dew Point', TEMP_CELSIUS, None, DEVICE_CLASS_TEMPERATURE], + ['Dew Point', TEMP_CELSIUS, None, DEVICE_CLASS_TEMPERATURE], SENSOR_TYPE_BAROMETRIC_PRESSURE: ['Barometric Pressure', 'kPa', '', None], } @@ -48,7 +49,9 @@ def setup_platform(hass, config, add_entities, discovery_info=None): """Set up the Tellstick sensors.""" if discovery_info is None: return - add_entities(TelldusLiveSensor(hass, sensor) for sensor in discovery_info) + client = hass.data[tellduslive.DOMAIN] + add_entities( + TelldusLiveSensor(client, sensor) for sensor in discovery_info) class TelldusLiveSensor(TelldusLiveEntity): @@ -87,9 +90,7 @@ def _value_as_humidity(self): @property def name(self): """Return the name of the sensor.""" - return '{} {}'.format( - super().name, - self.quantity_name or '') + return '{} {}'.format(super().name, self.quantity_name or '') @property def state(self): diff --git a/homeassistant/components/switch/tellduslive.py b/homeassistant/components/switch/tellduslive.py index dc12fafcfafdcd..ed4f825f5ac6b3 100644 --- a/homeassistant/components/switch/tellduslive.py +++ b/homeassistant/components/switch/tellduslive.py @@ -9,6 +9,7 @@ """ import logging +from homeassistant.components import tellduslive from homeassistant.components.tellduslive.entry import TelldusLiveEntity from homeassistant.helpers.entity import ToggleEntity @@ -19,7 +20,9 @@ def setup_platform(hass, config, add_entities, discovery_info=None): """Set up Tellstick switches.""" if discovery_info is None: return - add_entities(TelldusLiveSwitch(hass, switch) for switch in discovery_info) + client = hass.data[tellduslive.DOMAIN] + add_entities( + TelldusLiveSwitch(client, switch) for switch in discovery_info) class TelldusLiveSwitch(TelldusLiveEntity, ToggleEntity): diff --git a/homeassistant/components/tellduslive/__init__.py b/homeassistant/components/tellduslive/__init__.py index f14963332d6180..8c10e8b469e19f 100644 --- a/homeassistant/components/tellduslive/__init__.py +++ b/homeassistant/components/tellduslive/__init__.py @@ -10,14 +10,13 @@ import voluptuous as vol from homeassistant.components.discovery import SERVICE_TELLDUSLIVE -from homeassistant.const import ( - CONF_HOST, CONF_TOKEN, EVENT_HOMEASSISTANT_START) +from homeassistant.const import CONF_HOST, CONF_TOKEN from homeassistant.helpers import discovery import homeassistant.helpers.config_validation as cv from homeassistant.helpers.dispatcher import async_dispatcher_send -from homeassistant.helpers.event import track_point_in_utc_time -from homeassistant.util.dt import utcnow +from homeassistant.helpers.event import async_track_time_interval from homeassistant.util.json import load_json, save_json +from homeassistant.util.async_ import run_coroutine_threadsafe from .const import DOMAIN, SIGNAL_UPDATE_ENTITY @@ -142,7 +141,7 @@ def tellstick_discovered(service, info): if not supports_local_api(device): _LOGGER.debug('Tellstick does not support local API') # Configure the cloud service - hass.async_add_job(request_configuration) + hass.add_job(request_configuration) return _LOGGER.debug('Tellstick does support local API') @@ -185,18 +184,12 @@ def tellstick_discovered(service, info): return True if not session.is_authorized: - _LOGGER.error( - 'Authentication Error') + _LOGGER.error('Authentication Error') return False client = TelldusLiveClient(hass, config, session) - hass.data[DOMAIN] = client - - if session: - client.update() - else: - hass.bus.listen(EVENT_HOMEASSISTANT_START, client.update) + run_coroutine_threadsafe(client.update(), hass.loop) return True @@ -210,23 +203,19 @@ def __init__(self, hass, config, session): self._hass = hass self._config = config - - self._interval = config.get(DOMAIN, {}).get( - CONF_UPDATE_INTERVAL, DEFAULT_UPDATE_INTERVAL) - _LOGGER.debug('Update interval %s', self._interval) self._client = session - def update(self, *args): - """Periodically poll the servers for current state.""" - _LOGGER.debug('Updating') - try: - self._sync() - finally: - track_point_in_utc_time( - self._hass, self.update, utcnow() + self._interval) + interval = config.get(DOMAIN, {}).get(CONF_UPDATE_INTERVAL, + DEFAULT_UPDATE_INTERVAL) + _LOGGER.debug('Update interval %s', interval) + async_track_time_interval(self._hass, self.update, interval) - def _sync(self): + async def update(self, *args): """Update local list of devices.""" + if DOMAIN not in self._hass.data: + return + + _LOGGER.debug('Updating') if not self._client.update(): _LOGGER.warning('Failed request') diff --git a/homeassistant/components/tellduslive/entry.py b/homeassistant/components/tellduslive/entry.py index 854f3bac8b246e..2cd14b23d33fa2 100644 --- a/homeassistant/components/tellduslive/entry.py +++ b/homeassistant/components/tellduslive/entry.py @@ -7,7 +7,7 @@ from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.entity import Entity -from .const import DOMAIN, SIGNAL_UPDATE_ENTITY +from .const import SIGNAL_UPDATE_ENTITY _LOGGER = logging.getLogger(__name__) @@ -17,13 +17,12 @@ class TelldusLiveEntity(Entity): """Base class for all Telldus Live entities.""" - def __init__(self, hass, device_id): + def __init__(self, client, device_id): """Initialize the entity.""" self._id = device_id - self._client = hass.data[DOMAIN] + self._client = client self._name = self.device.name self._async_unsub_dispatcher_connect = None - _LOGGER.debug('Created device %s', self) async def async_added_to_hass(self): """Call when entity is added to hass.""" @@ -42,7 +41,7 @@ def _update_callback(self): """Return the property of the device might have changed.""" if self.device.name: self._name = self.device.name - self.schedule_update_ha_state() + self.async_schedule_update_ha_state() @property def device_id(self): From 2871a06b6959326492286cc90bdc9dd4cad440db Mon Sep 17 00:00:00 2001 From: Fredrik Erlandsson Date: Fri, 30 Nov 2018 18:31:18 +0100 Subject: [PATCH 6/7] don't make update async --- .../components/tellduslive/__init__.py | 24 ++++++++----------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/homeassistant/components/tellduslive/__init__.py b/homeassistant/components/tellduslive/__init__.py index 8c10e8b469e19f..89e7446448999e 100644 --- a/homeassistant/components/tellduslive/__init__.py +++ b/homeassistant/components/tellduslive/__init__.py @@ -13,10 +13,9 @@ from homeassistant.const import CONF_HOST, CONF_TOKEN from homeassistant.helpers import discovery import homeassistant.helpers.config_validation as cv -from homeassistant.helpers.dispatcher import async_dispatcher_send -from homeassistant.helpers.event import async_track_time_interval +from homeassistant.helpers.dispatcher import dispatcher_send +from homeassistant.helpers.event import track_time_interval from homeassistant.util.json import load_json, save_json -from homeassistant.util.async_ import run_coroutine_threadsafe from .const import DOMAIN, SIGNAL_UPDATE_ENTITY @@ -189,7 +188,12 @@ def tellstick_discovered(service, info): client = TelldusLiveClient(hass, config, session) hass.data[DOMAIN] = client - run_coroutine_threadsafe(client.update(), hass.loop) + client.update() + + interval = config.get(DOMAIN, {}).get(CONF_UPDATE_INTERVAL, + DEFAULT_UPDATE_INTERVAL) + _LOGGER.debug('Update interval %s', interval) + track_time_interval(hass, client.update, interval) return True @@ -205,16 +209,8 @@ def __init__(self, hass, config, session): self._config = config self._client = session - interval = config.get(DOMAIN, {}).get(CONF_UPDATE_INTERVAL, - DEFAULT_UPDATE_INTERVAL) - _LOGGER.debug('Update interval %s', interval) - async_track_time_interval(self._hass, self.update, interval) - - async def update(self, *args): + def update(self, *args): """Update local list of devices.""" - if DOMAIN not in self._hass.data: - return - _LOGGER.debug('Updating') if not self._client.update(): _LOGGER.warning('Failed request') @@ -251,7 +247,7 @@ def discover(device_id, component): identify_device(device)) self._known_devices.add(device.device_id) - async_dispatcher_send(self._hass, SIGNAL_UPDATE_ENTITY) + dispatcher_send(self._hass, SIGNAL_UPDATE_ENTITY) def device(self, device_id): """Return device representation.""" From 295aa2944386718381ffd986b42d2e3186253805 Mon Sep 17 00:00:00 2001 From: Fredrik Erlandsson Date: Fri, 30 Nov 2018 20:49:54 +0100 Subject: [PATCH 7/7] "Strip is good!" --- homeassistant/components/sensor/tellduslive.py | 2 +- homeassistant/components/tellduslive/entry.py | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/homeassistant/components/sensor/tellduslive.py b/homeassistant/components/sensor/tellduslive.py index 9f9815acc25f24..4afff115b9df4a 100644 --- a/homeassistant/components/sensor/tellduslive.py +++ b/homeassistant/components/sensor/tellduslive.py @@ -90,7 +90,7 @@ def _value_as_humidity(self): @property def name(self): """Return the name of the sensor.""" - return '{} {}'.format(super().name, self.quantity_name or '') + return '{} {}'.format(super().name, self.quantity_name or '').strip() @property def state(self): diff --git a/homeassistant/components/tellduslive/entry.py b/homeassistant/components/tellduslive/entry.py index 2cd14b23d33fa2..88b7d47ad9dc27 100644 --- a/homeassistant/components/tellduslive/entry.py +++ b/homeassistant/components/tellduslive/entry.py @@ -29,7 +29,6 @@ async def async_added_to_hass(self): _LOGGER.debug('Created device %s', self) self._async_unsub_dispatcher_connect = async_dispatcher_connect( self.hass, SIGNAL_UPDATE_ENTITY, self._update_callback) - self._update_callback() async def async_will_remove_from_hass(self): """Disconnect dispatcher listener when removed."""