Make harmony handle IP address changes (#33173)
If the IP address of the harmony hub changed it would not be rediscovered. We now connect to get the unique id and then update config entries with the correct ip if it is already setup.
This commit is contained in:
1 parent
087b672449
commit
0b2a8bf79a
4 files changed
+70
-41
No files matched your search
@@ -26,24 +26,32 @@ DATA_SCHEMA = vol.Schema(
|
||||
)
|
||||
|
||||
|
||||
async def get_harmony_client_if_available(hass: core.HomeAssistant, ip_address):
|
||||
"""Connect to a harmony hub and fetch info."""
|
||||
harmony = HarmonyAPI(ip_address=ip_address)
|
||||
|
||||
try:
|
||||
if not await harmony.connect():
|
||||
await harmony.close()
|
||||
return None
|
||||
except harmony_exceptions.TimeOut:
|
||||
return None
|
||||
|
||||
await harmony.close()
|
||||
|
||||
return harmony
|
||||
|
||||
|
||||
async def validate_input(hass: core.HomeAssistant, data):
|
||||
"""Validate the user input allows us to connect.
|
||||
|
||||
Data has the keys from DATA_SCHEMA with values provided by the user.
|
||||
"""
|
||||
harmony = HarmonyAPI(ip_address=data[CONF_HOST])
|
||||
|
||||
_LOGGER.debug("harmony:%s", harmony)
|
||||
|
||||
try:
|
||||
if not await harmony.connect():
|
||||
await harmony.close()
|
||||
raise CannotConnect
|
||||
except harmony_exceptions.TimeOut:
|
||||
harmony = await get_harmony_client_if_available(hass, data[CONF_HOST])
|
||||
if not harmony:
|
||||
raise CannotConnect
|
||||
|
||||
unique_id = find_unique_id_for_remote(harmony)
|
||||
await harmony.close()
|
||||
|
||||
# As a last resort we get the name from the harmony client
|
||||
# in the event a name was not provided. harmony.name is
|
||||
@@ -74,7 +82,7 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
if user_input is not None:
|
||||
|
||||
try:
|
||||
info = await validate_input(self.hass, user_input)
|
||||
validated = await validate_input(self.hass, user_input)
|
||||
except CannotConnect:
|
||||
errors["base"] = "cannot_connect"
|
||||
except Exception: # pylint: disable=broad-except
|
||||
@@ -82,7 +90,11 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
errors["base"] = "unknown"
|
||||
|
||||
if "base" not in errors:
|
||||
return await self._async_create_entry_from_valid_input(info, user_input)
|
||||
await self.async_set_unique_id(validated[UNIQUE_ID])
|
||||
self._abort_if_unique_id_configured()
|
||||
return await self._async_create_entry_from_valid_input(
|
||||
validated, user_input
|
||||
)
|
||||
|
||||
# Return form
|
||||
return self.async_show_form(
|
||||
@@ -104,8 +116,17 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
CONF_NAME: friendly_name,
|
||||
}
|
||||
|
||||
if self._host_already_configured(self.harmony_config):
|
||||
return self.async_abort(reason="already_configured")
|
||||
harmony = await get_harmony_client_if_available(
|
||||
self.hass, self.harmony_config[CONF_HOST]
|
||||
)
|
||||
|
||||
if harmony:
|
||||
unique_id = find_unique_id_for_remote(harmony)
|
||||
await self.async_set_unique_id(unique_id)
|
||||
self._abort_if_unique_id_configured(
|
||||
updates={CONF_HOST: self.harmony_config[CONF_HOST]}
|
||||
)
|
||||
self.harmony_config[UNIQUE_ID] = unique_id
|
||||
|
||||
return await self.async_step_link()
|
||||
|
||||
@@ -114,16 +135,11 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
errors = {}
|
||||
|
||||
if user_input is not None:
|
||||
try:
|
||||
info = await validate_input(self.hass, self.harmony_config)
|
||||
except CannotConnect:
|
||||
errors["base"] = "cannot_connect"
|
||||
except Exception: # pylint: disable=broad-except
|
||||
_LOGGER.exception("Unexpected exception")
|
||||
errors["base"] = "unknown"
|
||||
|
||||
if "base" not in errors:
|
||||
return await self._async_create_entry_from_valid_input(info, user_input)
|
||||
# Everything was validated in async_step_ssdp
|
||||
# all we do now is create.
|
||||
return await self._async_create_entry_from_valid_input(
|
||||
self.harmony_config, {}
|
||||
)
|
||||
|
||||
return self.async_show_form(
|
||||
step_id="link",
|
||||
@@ -146,8 +162,7 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
|
||||
async def _async_create_entry_from_valid_input(self, validated, user_input):
|
||||
"""Single path to create the config entry from validated input."""
|
||||
await self.async_set_unique_id(validated[UNIQUE_ID])
|
||||
self._abort_if_unique_id_configured()
|
||||
|
||||
data = {CONF_NAME: validated[CONF_NAME], CONF_HOST: validated[CONF_HOST]}
|
||||
# Options from yaml are preserved, we will pull them out when
|
||||
# we setup the config entry
|
||||
|
||||
Reference in new issue
Block a user