diff --git a/homeassistant/components/sensor/bitcoin.py b/homeassistant/components/sensor/bitcoin.py index 31c6c1809b3ba6..0a3f568a383c44 100644 --- a/homeassistant/components/sensor/bitcoin.py +++ b/homeassistant/components/sensor/bitcoin.py @@ -14,6 +14,7 @@ CONF_DISPLAY_OPTIONS, ATTR_ATTRIBUTION, CONF_CURRENCY) import homeassistant.helpers.config_validation as cv from homeassistant.helpers.entity import Entity +from homeassistant.util import Throttle REQUIREMENTS = ['blockchain==1.4.0'] @@ -178,6 +179,7 @@ def __init__(self): self.stats = None self.ticker = None + @Throttle(SCAN_INTERVAL - timedelta(seconds=5)) def update(self): """Get the latest data from blockchain.info.""" from blockchain import statistics, exchangerates diff --git a/homeassistant/helpers/entity_component.py b/homeassistant/helpers/entity_component.py index 2833010789ef5b..46c45a6fdec278 100644 --- a/homeassistant/helpers/entity_component.py +++ b/homeassistant/helpers/entity_component.py @@ -374,6 +374,16 @@ def async_add_entities(self, new_entities, update_before_add=False): if not new_entities: return + hass = self.component.hass + + # If the entities are not async, update them in sequence to avoid + # race conditions when component uses throttle. + if update_before_add and any(not hasattr(entity, 'async_update') + for entity in new_entities): + for entity in new_entities: + yield from hass.async_add_job(entity.update) + update_before_add = False + @asyncio.coroutine def async_process_entity(new_entity): """Add entities to StateMachine.""" @@ -385,7 +395,7 @@ def async_process_entity(new_entity): tasks = [async_process_entity(entity) for entity in new_entities] - yield from asyncio.wait(tasks, loop=self.component.hass.loop) + yield from asyncio.wait(tasks, loop=hass.loop) self.component.async_update_group() if self._async_unsub_polling is not None or \ @@ -394,7 +404,7 @@ def async_process_entity(new_entity): return self._async_unsub_polling = async_track_time_interval( - self.component.hass, self._update_entity_states, self.scan_interval + hass, self._update_entity_states, self.scan_interval ) @asyncio.coroutine