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
This commit is contained in:
Robert Svensson 2021-01-16 18:59:57 +01:00 committed by GitHub
parent 8151721fbc
commit 3cc45697cb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 24 additions and 27 deletions

View File

@ -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],
}
)

View File

@ -1,6 +1,6 @@
{
"config": {
"flow_title": "Axis device: {name} ({host})",
"flow_title": "{name} ({host})",
"step": {
"user": {
"title": "Set up Axis device",

View File

@ -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},

View File

@ -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},