From 8954609f6bfaa88ef50b0ebf99238fbe1f677abb Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Tue, 30 Nov 2021 21:15:34 +0100 Subject: [PATCH] Use dataclass properties in axis discovery (#60558) Co-authored-by: epenet --- homeassistant/components/axis/config_flow.py | 24 +++++++++----------- tests/components/axis/test_device.py | 16 +++++++------ 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/homeassistant/components/axis/config_flow.py b/homeassistant/components/axis/config_flow.py index 74dbeb0b2c7..a47592fc877 100644 --- a/homeassistant/components/axis/config_flow.py +++ b/homeassistant/components/axis/config_flow.py @@ -156,21 +156,21 @@ class AxisFlowHandler(config_entries.ConfigFlow, domain=AXIS_DOMAIN): """Prepare configuration for a DHCP discovered Axis device.""" return await self._process_discovered_device( { - CONF_HOST: discovery_info[dhcp.IP_ADDRESS], - CONF_MAC: format_mac(discovery_info[dhcp.MAC_ADDRESS]), - CONF_NAME: discovery_info[dhcp.HOSTNAME], + CONF_HOST: discovery_info.ip, + CONF_MAC: format_mac(discovery_info.macaddress), + CONF_NAME: discovery_info.hostname, CONF_PORT: DEFAULT_PORT, } ) - async def async_step_ssdp(self, discovery_info: ssdp.SsdpServiceInfo): + async def async_step_ssdp(self, discovery_info: ssdp.SsdpServiceInfo) -> FlowResult: """Prepare configuration for a SSDP discovered Axis device.""" - url = urlsplit(discovery_info["presentationURL"]) + url = urlsplit(discovery_info.upnp[ssdp.ATTR_UPNP_PRESENTATION_URL]) return await self._process_discovered_device( { CONF_HOST: url.hostname, - CONF_MAC: format_mac(discovery_info["serialNumber"]), - CONF_NAME: f"{discovery_info['friendlyName']}", + CONF_MAC: format_mac(discovery_info.upnp[ssdp.ATTR_UPNP_SERIAL]), + CONF_NAME: f"{discovery_info.upnp[ssdp.ATTR_UPNP_FRIENDLY_NAME]}", CONF_PORT: url.port, } ) @@ -181,12 +181,10 @@ class AxisFlowHandler(config_entries.ConfigFlow, domain=AXIS_DOMAIN): """Prepare configuration for a Zeroconf discovered Axis device.""" return await self._process_discovered_device( { - CONF_HOST: discovery_info[zeroconf.ATTR_HOST], - CONF_MAC: format_mac( - discovery_info[zeroconf.ATTR_PROPERTIES]["macaddress"] - ), - CONF_NAME: discovery_info[zeroconf.ATTR_NAME].split(".", 1)[0], - CONF_PORT: discovery_info[zeroconf.ATTR_PORT], + CONF_HOST: discovery_info.host, + CONF_MAC: format_mac(discovery_info.properties["macaddress"]), + CONF_NAME: discovery_info.name.split(".", 1)[0], + CONF_PORT: discovery_info.port, } ) diff --git a/tests/components/axis/test_device.py b/tests/components/axis/test_device.py index 2d2ba83f633..d43845e01ad 100644 --- a/tests/components/axis/test_device.py +++ b/tests/components/axis/test_device.py @@ -8,7 +8,7 @@ from axis.event_stream import OPERATION_INITIALIZED import pytest import respx -from homeassistant.components import axis +from homeassistant.components import axis, zeroconf from homeassistant.components.axis.const import ( CONF_EVENTS, CONF_MODEL, @@ -383,12 +383,14 @@ async def test_update_address(hass): mock_default_vapix_requests(respx, "2.3.4.5") await hass.config_entries.flow.async_init( AXIS_DOMAIN, - data={ - "host": "2.3.4.5", - "port": 80, - "name": "name", - "properties": {"macaddress": MAC}, - }, + data=zeroconf.ZeroconfServiceInfo( + host="2.3.4.5", + hostname="mock_hostname", + name="name", + port=80, + properties={"macaddress": MAC}, + type="mock_type", + ), context={"source": SOURCE_ZEROCONF}, ) await hass.async_block_till_done()