diff --git a/homeassistant/components/device_tracker/mysensors.py b/homeassistant/components/device_tracker/mysensors.py index 8b10bc2b9bb..705dc9968c9 100644 --- a/homeassistant/components/device_tracker/mysensors.py +++ b/homeassistant/components/device_tracker/mysensors.py @@ -14,7 +14,7 @@ async def async_setup_scanner(hass, config, async_see, discovery_info=None): """Set up the MySensors device scanner.""" new_devices = mysensors.setup_mysensors_platform( hass, DOMAIN, discovery_info, MySensorsDeviceScanner, - device_args=(async_see, )) + device_args=(hass, async_see)) if not new_devices: return False @@ -37,12 +37,13 @@ async def async_setup_scanner(hass, config, async_see, discovery_info=None): class MySensorsDeviceScanner(mysensors.device.MySensorsDevice): """Represent a MySensors scanner.""" - def __init__(self, async_see, *args): + def __init__(self, hass, async_see, *args): """Set up instance.""" super().__init__(*args) self.async_see = async_see + self.hass = hass - async def async_update_callback(self): + async def _async_update_callback(self): """Update the device.""" await self.async_update() node = self.gateway.sensors[self.node_id] diff --git a/homeassistant/components/mysensors/const.py b/homeassistant/components/mysensors/const.py index ccb54bf647f..fbe3621a4af 100644 --- a/homeassistant/components/mysensors/const.py +++ b/homeassistant/components/mysensors/const.py @@ -23,6 +23,7 @@ SCHEMA = 'schema' CHILD_CALLBACK = 'mysensors_child_callback_{}_{}_{}_{}' NODE_CALLBACK = 'mysensors_node_callback_{}_{}' TYPE = 'type' +UPDATE_DELAY = 0.1 # MySensors const schemas BINARY_SENSOR_SCHEMA = {PLATFORM: 'binary_sensor', TYPE: 'V_TRIPPED'} diff --git a/homeassistant/components/mysensors/device.py b/homeassistant/components/mysensors/device.py index 07261b1c2a6..54333d8699b 100644 --- a/homeassistant/components/mysensors/device.py +++ b/homeassistant/components/mysensors/device.py @@ -1,5 +1,6 @@ """Handle MySensors devices.""" import logging +from functools import partial from homeassistant.const import ( ATTR_BATTERY_LEVEL, STATE_OFF, STATE_ON) @@ -7,7 +8,7 @@ from homeassistant.core import callback from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.entity import Entity -from .const import CHILD_CALLBACK, NODE_CALLBACK +from .const import CHILD_CALLBACK, NODE_CALLBACK, UPDATE_DELAY _LOGGER = logging.getLogger(__name__) @@ -39,6 +40,8 @@ class MySensorsDevice: child = gateway.sensors[node_id].children[child_id] self.child_type = child.type self._values = {} + self._update_scheduled = False + self.hass = None @property def name(self): @@ -84,6 +87,29 @@ class MySensorsDevice: else: self._values[value_type] = value + async def _async_update_callback(self): + """Update the device.""" + raise NotImplementedError + + @callback + def async_update_callback(self): + """Update the device after delay.""" + if self._update_scheduled: + return + + async def update(): + """Perform update.""" + try: + await self._async_update_callback() + except Exception: # pylint: disable=broad-except + _LOGGER.exception("Error updating %s", self.name) + finally: + self._update_scheduled = False + + self._update_scheduled = True + delayed_update = partial(self.hass.async_create_task, update()) + self.hass.loop.call_later(UPDATE_DELAY, delayed_update) + class MySensorsEntity(MySensorsDevice, Entity): """Representation of a MySensors entity.""" @@ -98,10 +124,9 @@ class MySensorsEntity(MySensorsDevice, Entity): """Return true if entity is available.""" return self.value_type in self._values - @callback - def async_update_callback(self): + async def _async_update_callback(self): """Update the entity.""" - self.async_schedule_update_ha_state(True) + await self.async_update_ha_state(True) async def async_added_to_hass(self): """Register update callback.""" diff --git a/homeassistant/components/mysensors/handler.py b/homeassistant/components/mysensors/handler.py index 39af1173706..886660baffe 100644 --- a/homeassistant/components/mysensors/handler.py +++ b/homeassistant/components/mysensors/handler.py @@ -99,7 +99,6 @@ def _handle_child_update(hass, hass_config, msg): for signal in set(signals): # Only one signal per device is needed. # A device can have multiple platforms, ie multiple schemas. - # FOR LATER: Add timer to not signal if another update comes in. async_dispatcher_send(hass, signal)