Use config entry to setup platforms (#13752)
* Use config entry to setup platforms * Rename to async_forward_entry * Add tests * Catch if platform not exists for entry
This commit is contained in:
1 parent
cb51553c2d
commit
73de749411
12 files changed
+271
-45
No files matched your search
@@ -39,18 +39,17 @@ class HueBridge(object):
|
||||
async def async_setup(self, tries=0):
|
||||
"""Set up a phue bridge based on host parameter."""
|
||||
host = self.host
|
||||
hass = self.hass
|
||||
|
||||
try:
|
||||
self.api = await get_bridge(
|
||||
self.hass, host,
|
||||
self.config_entry.data['username']
|
||||
)
|
||||
hass, host, self.config_entry.data['username'])
|
||||
except AuthenticationRequired:
|
||||
# usernames can become invalid if hub is reset or user removed.
|
||||
# We are going to fail the config entry setup and initiate a new
|
||||
# linking procedure. When linking succeeds, it will remove the
|
||||
# old config entry.
|
||||
self.hass.async_add_job(self.hass.config_entries.flow.async_init(
|
||||
hass.async_add_job(hass.config_entries.flow.async_init(
|
||||
DOMAIN, source='import', data={
|
||||
'host': host,
|
||||
}
|
||||
@@ -69,7 +68,7 @@ class HueBridge(object):
|
||||
self.config_entry.state = config_entries.ENTRY_STATE_LOADED
|
||||
|
||||
# Unhandled edge case: cancel this if we discover bridge on new IP
|
||||
self.hass.helpers.event.async_call_later(retry_delay, retry_setup)
|
||||
hass.helpers.event.async_call_later(retry_delay, retry_setup)
|
||||
|
||||
return False
|
||||
|
||||
@@ -78,11 +77,10 @@ class HueBridge(object):
|
||||
host)
|
||||
return False
|
||||
|
||||
self.hass.async_add_job(
|
||||
self.hass.helpers.discovery.async_load_platform(
|
||||
'light', DOMAIN, {'host': host}))
|
||||
hass.async_add_job(hass.config_entries.async_forward_entry(
|
||||
self.config_entry, 'light'))
|
||||
|
||||
self.hass.services.async_register(
|
||||
hass.services.async_register(
|
||||
DOMAIN, SERVICE_HUE_SCENE, self.hue_activate_scene,
|
||||
schema=SCENE_SCHEMA)
|
||||
|
||||
|
||||
@@ -334,7 +334,7 @@ class SetIntentHandler(intent.IntentHandler):
|
||||
|
||||
async def async_setup(hass, config):
|
||||
"""Expose light control via state machine and services."""
|
||||
component = EntityComponent(
|
||||
component = hass.data[DOMAIN] = EntityComponent(
|
||||
_LOGGER, DOMAIN, hass, SCAN_INTERVAL, GROUP_NAME_ALL_LIGHTS)
|
||||
await component.async_setup(config)
|
||||
|
||||
@@ -388,6 +388,11 @@ async def async_setup(hass, config):
|
||||
return True
|
||||
|
||||
|
||||
async def async_setup_entry(hass, entry):
|
||||
"""Setup a config entry."""
|
||||
return await hass.data[DOMAIN].async_setup_entry(entry)
|
||||
|
||||
|
||||
class Profiles:
|
||||
"""Representation of available color profiles."""
|
||||
|
||||
|
||||
@@ -49,11 +49,17 @@ GROUP_MIN_API_VERSION = (1, 13, 0)
|
||||
|
||||
async def async_setup_platform(hass, config, async_add_devices,
|
||||
discovery_info=None):
|
||||
"""Set up the Hue lights."""
|
||||
if discovery_info is None:
|
||||
return
|
||||
"""Old way of setting up Hue lights.
|
||||
|
||||
bridge = hass.data[hue.DOMAIN][discovery_info['host']]
|
||||
Can only be called when a user accidentally mentions hue platform in their
|
||||
config. But even in that case it would have been ignored.
|
||||
"""
|
||||
pass
|
||||
|
||||
|
||||
async def async_setup_entry(hass, config_entry, async_add_devices):
|
||||
"""Set up the Hue lights from a config entry."""
|
||||
bridge = hass.data[hue.DOMAIN][config_entry.data['host']]
|
||||
cur_lights = {}
|
||||
cur_groups = {}
|
||||
|
||||
|
||||
Reference in new issue
Block a user