From f700bbe6d64ad6407edfe37c979fe98dedf729bb Mon Sep 17 00:00:00 2001 From: niceboy Date: Sun, 12 Dec 2021 05:06:48 +0000 Subject: [PATCH] discovery_info['hostname'] will be failed soon, and instead of discovery_info.hostname --- .../aqara_gateway/config_flow.py | 34 +++++++++++++------ 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/custom_components/aqara_gateway/config_flow.py b/custom_components/aqara_gateway/config_flow.py index 9700eb5..af343b1 100755 --- a/custom_components/aqara_gateway/config_flow.py +++ b/custom_components/aqara_gateway/config_flow.py @@ -154,13 +154,22 @@ async def async_step_discovery_confirm(self, user_input=None): async def async_step_zeroconf(self, discovery_info: DiscoveryInfoType): """Handle zeroconf discovery.""" # Hostname is format: _aqara._tcp.local., _aqara-setup._tcp.local. - local_name = discovery_info["hostname"][:-1] - node_name = local_name[: -len(".local")] - address = discovery_info["properties"].get( - "address", discovery_info["host"]) - model = discovery_info["properties"].get("md", "unknow") - fwversion = discovery_info["properties"].get("fw", "unknow") - fwcloud = discovery_info["properties"].get("cl", "unknow") + if getattr(discovery_info, "hostname"): + local_name = discovery_info.hostname[:-1] + node_name = local_name[: -len(".local")] + address = discovery_info.properties.get( + "address", discovery_info.host) + model = discovery_info.properties.get("md", "unknow") + fwversion = discovery_info.properties.get("fw", "unknow") + fwcloud = discovery_info.properties.get("cl", "unknow") + else: + local_name = discovery_info["hostname"][:-1] + node_name = local_name[: -len(".local")] + address = discovery_info["properties"].get( + "address", discovery_info[CONF_HOST]) + model = discovery_info["properties"].get("md", "unknow") + fwversion = discovery_info["properties"].get("fw", "unknow") + fwcloud = discovery_info["properties"].get("cl", "unknow") self._device_info = ( f"Name: {node_name}\n" f"Model: {model}\n" @@ -170,7 +179,10 @@ async def async_step_zeroconf(self, discovery_info: DiscoveryInfoType): f"Cloud: {fwcloud}" ) - self._host = discovery_info[CONF_HOST] + if getattr(discovery_info, "hostname"): + self._host = discovery_info.hostname + else: + self._host = discovery_info[CONF_HOST] self._name = node_name self._password = '' self._token = '' @@ -180,13 +192,13 @@ async def async_step_zeroconf(self, discovery_info: DiscoveryInfoType): already_configured = False if CONF_HOST in entry.data and entry.data[CONF_HOST] in [ address, - discovery_info[CONF_HOST], + self._host, ]: # Is this address or IP address already configured? already_configured = True elif CONF_HOST in entry.options and entry.options[CONF_HOST] in [ address, - discovery_info[CONF_HOST], + self._host, ]: # Is this address or IP address already configured? already_configured = True @@ -200,7 +212,7 @@ async def async_step_zeroconf(self, discovery_info: DiscoveryInfoType): await self.async_set_unique_id(node_name) self._abort_if_unique_id_configured( - updates={CONF_HOST: discovery_info[CONF_HOST]} + updates={CONF_HOST: self._host} ) if (fwcloud == "miot" and