From 3cc45697cb379b72149ec2ab20b4530d74d8e4c9 Mon Sep 17 00:00:00 2001 From: Robert Svensson Date: Sat, 16 Jan 2021 18:59:57 +0100 Subject: [PATCH] Prettier Zeroconf discovery name for Axis devices (#45216) * Change zeroconf name to be based of prettier name rather than hostname to help user understand what device is discovered Have a full zeroconf discovery message in tests Clean up unusued globals * Use non-formatted serial number for config entry title as well --- homeassistant/components/axis/config_flow.py | 17 ++++------- homeassistant/components/axis/strings.json | 2 +- tests/components/axis/test_config_flow.py | 30 +++++++++++--------- tests/components/axis/test_device.py | 2 +- 4 files changed, 24 insertions(+), 27 deletions(-) diff --git a/homeassistant/components/axis/config_flow.py b/homeassistant/components/axis/config_flow.py index 26b8d8212eb..6a5543a284c 100644 --- a/homeassistant/components/axis/config_flow.py +++ b/homeassistant/components/axis/config_flow.py @@ -29,15 +29,6 @@ from .device import get_device from .errors import AuthenticationRequired, CannotConnect AXIS_OUI = {"00:40:8c", "ac:cc:8e", "b8:a4:4f"} - -CONFIG_FILE = "axis.conf" - -EVENT_TYPES = ["motion", "vmd3", "pir", "sound", "daynight", "tampering", "input"] - -PLATFORMS = ["camera"] - -AXIS_INCLUDE = EVENT_TYPES + PLATFORMS - DEFAULT_PORT = 80 @@ -58,6 +49,7 @@ class AxisFlowHandler(config_entries.ConfigFlow, domain=AXIS_DOMAIN): self.device_config = {} self.discovery_schema = {} self.import_schema = {} + self.serial = None async def async_step_user(self, user_input=None): """Handle a Axis config flow start. @@ -76,7 +68,8 @@ class AxisFlowHandler(config_entries.ConfigFlow, domain=AXIS_DOMAIN): password=user_input[CONF_PASSWORD], ) - await self.async_set_unique_id(format_mac(device.vapix.serial_number)) + self.serial = device.vapix.serial_number + await self.async_set_unique_id(format_mac(self.serial)) self._abort_if_unique_id_configured( updates={ @@ -135,7 +128,7 @@ class AxisFlowHandler(config_entries.ConfigFlow, domain=AXIS_DOMAIN): self.device_config[CONF_NAME] = name - title = f"{model} - {self.unique_id}" + title = f"{model} - {self.serial}" return self.async_create_entry(title=title, data=self.device_config) async def async_step_dhcp(self, discovery_info: dict): @@ -155,7 +148,7 @@ class AxisFlowHandler(config_entries.ConfigFlow, domain=AXIS_DOMAIN): { CONF_HOST: discovery_info[CONF_HOST], CONF_MAC: format_mac(discovery_info["properties"]["macaddress"]), - CONF_NAME: discovery_info["hostname"][:-7], + CONF_NAME: discovery_info["name"].split(".", 1)[0], CONF_PORT: discovery_info[CONF_PORT], } ) diff --git a/homeassistant/components/axis/strings.json b/homeassistant/components/axis/strings.json index 046b7fee475..47a25b542a7 100644 --- a/homeassistant/components/axis/strings.json +++ b/homeassistant/components/axis/strings.json @@ -1,6 +1,6 @@ { "config": { - "flow_title": "Axis device: {name} ({host})", + "flow_title": "{name} ({host})", "step": { "user": { "title": "Set up Axis device", diff --git a/tests/components/axis/test_config_flow.py b/tests/components/axis/test_config_flow.py index 33d19bbf328..2520b06696f 100644 --- a/tests/components/axis/test_config_flow.py +++ b/tests/components/axis/test_config_flow.py @@ -33,7 +33,6 @@ from homeassistant.data_entry_flow import ( ) from .test_device import ( - FORMATTED_MAC, MAC, MODEL, NAME, @@ -68,7 +67,7 @@ async def test_flow_manual_configuration(hass): ) assert result["type"] == RESULT_TYPE_CREATE_ENTRY - assert result["title"] == f"M1065-LW - {FORMATTED_MAC}" + assert result["title"] == f"M1065-LW - {MAC}" assert result["data"] == { CONF_HOST: "1.2.3.4", CONF_USERNAME: "user", @@ -225,7 +224,7 @@ async def test_flow_create_entry_multiple_existing_entries_of_same_model(hass): ) assert result["type"] == RESULT_TYPE_CREATE_ENTRY - assert result["title"] == f"M1065-LW - {FORMATTED_MAC}" + assert result["title"] == f"M1065-LW - {MAC}" assert result["data"] == { CONF_HOST: "1.2.3.4", CONF_USERNAME: "user", @@ -266,7 +265,7 @@ async def test_dhcp_flow(hass): ) assert result["type"] == RESULT_TYPE_CREATE_ENTRY - assert result["title"] == f"M1065-LW - {FORMATTED_MAC}" + assert result["title"] == f"M1065-LW - {MAC}" assert result["data"] == { CONF_HOST: "1.2.3.4", CONF_USERNAME: "user", @@ -376,10 +375,15 @@ async def test_zeroconf_flow(hass): result = await hass.config_entries.flow.async_init( AXIS_DOMAIN, data={ - CONF_HOST: "1.2.3.4", - CONF_PORT: 80, - "hostname": "name", - "properties": {"macaddress": MAC}, + "host": "1.2.3.4", + "port": 80, + "hostname": f"axis-{MAC.lower()}.local.", + "type": "_axis-video._tcp.local.", + "name": f"AXIS M1065-L - {MAC}._axis-video._tcp.local.", + "properties": { + "_raw": {"macaddress": MAC.encode()}, + "macaddress": MAC, + }, }, context={"source": SOURCE_ZEROCONF}, ) @@ -400,7 +404,7 @@ async def test_zeroconf_flow(hass): ) assert result["type"] == RESULT_TYPE_CREATE_ENTRY - assert result["title"] == f"M1065-LW - {FORMATTED_MAC}" + assert result["title"] == f"M1065-LW - {MAC}" assert result["data"] == { CONF_HOST: "1.2.3.4", CONF_USERNAME: "user", @@ -424,7 +428,7 @@ async def test_zeroconf_flow_already_configured(hass): data={ CONF_HOST: "1.2.3.4", CONF_PORT: 80, - "hostname": "name", + "name": "name", "properties": {"macaddress": MAC}, }, context={"source": SOURCE_ZEROCONF}, @@ -459,7 +463,7 @@ async def test_zeroconf_flow_updated_configuration(hass): data={ CONF_HOST: "2.3.4.5", CONF_PORT: 8080, - "hostname": "name", + "name": "name", "properties": {"macaddress": MAC}, }, context={"source": SOURCE_ZEROCONF}, @@ -486,7 +490,7 @@ async def test_zeroconf_flow_ignore_non_axis_device(hass): data={ CONF_HOST: "169.254.3.4", CONF_PORT: 80, - "hostname": "", + "name": "", "properties": {"macaddress": "01234567890"}, }, context={"source": SOURCE_ZEROCONF}, @@ -503,7 +507,7 @@ async def test_zeroconf_flow_ignore_link_local_address(hass): data={ CONF_HOST: "169.254.3.4", CONF_PORT: 80, - "hostname": "", + "name": "", "properties": {"macaddress": MAC}, }, context={"source": SOURCE_ZEROCONF}, diff --git a/tests/components/axis/test_device.py b/tests/components/axis/test_device.py index 99ac739b4d4..d750a5bdded 100644 --- a/tests/components/axis/test_device.py +++ b/tests/components/axis/test_device.py @@ -378,7 +378,7 @@ async def test_update_address(hass): data={ "host": "2.3.4.5", "port": 80, - "hostname": "name", + "name": "name", "properties": {"macaddress": MAC}, }, context={"source": SOURCE_ZEROCONF},