-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
Better locking while setting up components + discovery #4463
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,16 @@ | ||
"""Helper methods to help with platform discovery.""" | ||
"""Helper methods to help with platform discovery. | ||
|
||
There are two different types of discoveries that can be fired/listened for. | ||
- listen/discover is for services. These are targetted at a component. | ||
- listen_platform/discover_platform is for platforms. These are used by | ||
components to allow discovery of their platofrms. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. platofrms -> platforms |
||
""" | ||
import asyncio | ||
|
||
from homeassistant import bootstrap, core | ||
from homeassistant.const import ( | ||
ATTR_DISCOVERED, ATTR_SERVICE, EVENT_PLATFORM_DISCOVERED) | ||
from homeassistant.util.async import ( | ||
run_callback_threadsafe, fire_coroutine_threadsafe) | ||
from homeassistant.util.async import run_callback_threadsafe | ||
|
||
EVENT_LOAD_PLATFORM = 'load_platform.{}' | ||
ATTR_PLATFORM = 'platform' | ||
|
@@ -43,8 +48,29 @@ def discovery_event_listener(event): | |
|
||
def discover(hass, service, discovered=None, component=None, hass_config=None): | ||
"""Fire discovery event. Can ensure a component is loaded.""" | ||
if component is not None: | ||
bootstrap.setup_component(hass, component, hass_config) | ||
hass.async_add_job( | ||
async_discover(hass, service, discovered, component, hass_config)) | ||
|
||
|
||
@asyncio.coroutine | ||
def async_discover(hass, service, discovered=None, component=None, | ||
hass_config=None): | ||
"""Fire discovery event. Can ensure a component is loaded.""" | ||
if component is not None and component not in hass.config.components: | ||
did_lock = False | ||
setup_lock = hass.data.get('setup_lock') | ||
if setup_lock and setup_lock.locked(): | ||
did_lock = True | ||
yield from setup_lock.acquire() | ||
|
||
try: | ||
# Could have been loaded while waiting for lock. | ||
if component not in hass.config.components: | ||
yield from bootstrap.async_setup_component(hass, component, | ||
hass_config) | ||
finally: | ||
if did_lock: | ||
setup_lock.release() | ||
|
||
data = { | ||
ATTR_SERVICE: service | ||
|
@@ -53,7 +79,7 @@ def discover(hass, service, discovered=None, component=None, hass_config=None): | |
if discovered is not None: | ||
data[ATTR_DISCOVERED] = discovered | ||
|
||
hass.bus.fire(EVENT_PLATFORM_DISCOVERED, data) | ||
hass.bus.async_fire(EVENT_PLATFORM_DISCOVERED, data) | ||
|
||
|
||
def listen_platform(hass, component, callback): | ||
|
@@ -101,9 +127,9 @@ def load_platform(hass, component, platform, discovered=None, | |
|
||
Use `listen_platform` to register a callback for these events. | ||
""" | ||
fire_coroutine_threadsafe( | ||
async_load_platform(hass, component, platform, | ||
discovered, hass_config), hass.loop) | ||
hass.async_add_job( | ||
async_load_platform(hass, component, platform, discovered, | ||
hass_config)) | ||
|
||
|
||
@asyncio.coroutine | ||
|
@@ -120,25 +146,30 @@ def async_load_platform(hass, component, platform, discovered=None, | |
Use `listen_platform` to register a callback for these events. | ||
|
||
Warning: Do not yield from this inside a setup method to avoid a dead lock. | ||
Use `hass.loop.create_task(async_load_platform(..))` instead. | ||
Use `hass.loop.async_add_job(async_load_platform(..))` instead. | ||
|
||
This method is a coroutine. | ||
""" | ||
did_lock = False | ||
setup_lock = hass.data.get('setup_lock') | ||
if setup_lock and setup_lock.locked(): | ||
did_lock = True | ||
yield from setup_lock.acquire() | ||
if component not in hass.config.components: | ||
setup_lock = hass.data.get('setup_lock') | ||
if setup_lock and setup_lock.locked(): | ||
did_lock = True | ||
yield from setup_lock.acquire() | ||
|
||
setup_success = True | ||
|
||
try: | ||
# No need to fire event if we could not setup component | ||
res = yield from bootstrap.async_setup_component( | ||
hass, component, hass_config) | ||
# Could have been loaded while waiting for lock. | ||
if component not in hass.config.components: | ||
setup_success = yield from bootstrap.async_setup_component( | ||
hass, component, hass_config) | ||
finally: | ||
if did_lock: | ||
setup_lock.release() | ||
|
||
if not res: | ||
# No need to fire event if we could not setup component | ||
if not setup_success: | ||
return | ||
|
||
data = { | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I always thought that we setup everything with an ordered dict but looks like I was wrong. Instead we converted everything to a normal dictionary, making each setup unpredictable.