Dynalite listener for config entry update (#32116)
* added entry listener that reloads the component * fixed update with existing entry * fixed import in test * fixes * library version * removed unique_id * fix * fixed for no entries in hass.data * fixed return value on abort * moved to use async_entries * removed unused import
This commit is contained in:
9 files changed
+54
-20
No files matched your search
@@ -108,22 +108,28 @@ async def async_setup(hass, config):
|
||||
return True
|
||||
|
||||
|
||||
async def async_entry_changed(hass, entry):
|
||||
"""Reload entry since the data has changed."""
|
||||
LOGGER.debug("Reconfiguring entry %s", entry.data)
|
||||
bridge = hass.data[DOMAIN][entry.entry_id]
|
||||
await bridge.reload_config(entry.data)
|
||||
LOGGER.debug("Reconfiguring entry finished %s", entry.data)
|
||||
|
||||
|
||||
async def async_setup_entry(hass, entry):
|
||||
"""Set up a bridge from a config entry."""
|
||||
LOGGER.debug("Setting up entry %s", entry.data)
|
||||
|
||||
bridge = DynaliteBridge(hass, entry.data)
|
||||
|
||||
hass.data[DOMAIN][entry.entry_id] = bridge
|
||||
entry.add_update_listener(async_entry_changed)
|
||||
if not await bridge.async_setup():
|
||||
LOGGER.error("Could not set up bridge for entry %s", entry.data)
|
||||
hass.data[DOMAIN].pop(entry.entry_id)
|
||||
return False
|
||||
|
||||
if not await bridge.try_connection():
|
||||
LOGGER.errot("Could not connect with entry %s", entry)
|
||||
LOGGER.error("Could not connect with entry %s", entry)
|
||||
hass.data[DOMAIN].pop(entry.entry_id)
|
||||
raise ConfigEntryNotReady
|
||||
|
||||
hass.data[DOMAIN][entry.entry_id] = bridge
|
||||
|
||||
hass.async_create_task(
|
||||
hass.config_entries.async_forward_entry_setup(entry, "light")
|
||||
)
|
||||
|
||||
@@ -25,16 +25,22 @@ class DynaliteBridge:
|
||||
self.host = config[CONF_HOST]
|
||||
# Configure the dynalite devices
|
||||
self.dynalite_devices = DynaliteDevices(
|
||||
config=config,
|
||||
newDeviceFunc=self.add_devices_when_registered,
|
||||
updateDeviceFunc=self.update_device,
|
||||
)
|
||||
self.dynalite_devices.configure(config)
|
||||
|
||||
async def async_setup(self):
|
||||
"""Set up a Dynalite bridge."""
|
||||
# Configure the dynalite devices
|
||||
LOGGER.debug("Setting up bridge - host %s", self.host)
|
||||
return await self.dynalite_devices.async_setup()
|
||||
|
||||
async def reload_config(self, config):
|
||||
"""Reconfigure a bridge when config changes."""
|
||||
LOGGER.debug("Setting up bridge - host %s, config %s", self.host, config)
|
||||
self.dynalite_devices.configure(config)
|
||||
|
||||
def update_signal(self, device=None):
|
||||
"""Create signal to use to trigger entity update."""
|
||||
if device:
|
||||
|
||||
@@ -22,8 +22,11 @@ class DynaliteFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
"""Import a new bridge as a config entry."""
|
||||
LOGGER.debug("Starting async_step_import - %s", import_info)
|
||||
host = import_info[CONF_HOST]
|
||||
await self.async_set_unique_id(host)
|
||||
self._abort_if_unique_id_configured(import_info)
|
||||
for entry in self.hass.config_entries.async_entries(DOMAIN):
|
||||
if entry.data[CONF_HOST] == host:
|
||||
if entry.data != import_info:
|
||||
self.hass.config_entries.async_update_entry(entry, data=import_info)
|
||||
return self.async_abort(reason="already_configured")
|
||||
# New entry
|
||||
bridge = DynaliteBridge(self.hass, import_info)
|
||||
if not await bridge.async_setup():
|
||||
|
||||
@@ -5,5 +5,5 @@
|
||||
"documentation": "https://www.home-assistant.io/integrations/dynalite",
|
||||
"dependencies": [],
|
||||
"codeowners": ["@ziv1234"],
|
||||
"requirements": ["dynalite_devices==0.1.22"]
|
||||
"requirements": ["dynalite_devices==0.1.26"]
|
||||
}
|
||||
Reference in new issue
Block a user