8000 Fix race condition for sync entities using throttle by balloob · Pull Request #9891 · home-assistant/core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix race condition for sync entities using throttle #9891

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions homeassistant/components/sensor/bitcoin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']

Expand Down Expand Up @@ -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
Expand Down
14 changes: 12 additions & 2 deletions homeassistant/helpers/entity_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand All @@ -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 \
Expand All @@ -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
Expand Down
0