Add hardware model to onvif config flow discovery (#93676)

This commit is contained in:
J. Nick Koston 2023-05-27 19:47:13 -05:00 committed by GitHub
parent f3037d0b84
commit 5f5951e71c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 11 deletions

View File

@ -35,6 +35,7 @@ from homeassistant.helpers import device_registry as dr
from .const import ( from .const import (
CONF_DEVICE_ID, CONF_DEVICE_ID,
CONF_ENABLE_WEBHOOKS, CONF_ENABLE_WEBHOOKS,
CONF_HARDWARE,
DEFAULT_ARGUMENTS, DEFAULT_ARGUMENTS,
DEFAULT_ENABLE_WEBHOOKS, DEFAULT_ENABLE_WEBHOOKS,
DEFAULT_PORT, DEFAULT_PORT,
@ -71,11 +72,14 @@ async def async_discovery(hass: HomeAssistant) -> list[dict[str, Any]]:
CONF_NAME: service.getEPR(), CONF_NAME: service.getEPR(),
CONF_HOST: url.hostname, CONF_HOST: url.hostname,
CONF_PORT: url.port or 80, CONF_PORT: url.port or 80,
CONF_HARDWARE: None,
} }
for scope in service.getScopes(): for scope in service.getScopes():
scope_str = scope.getValue() scope_str = scope.getValue()
if scope_str.lower().startswith("onvif://www.onvif.org/name"): if scope_str.lower().startswith("onvif://www.onvif.org/name"):
device[CONF_NAME] = scope_str.split("/")[-1] device[CONF_NAME] = scope_str.split("/")[-1]
if scope_str.lower().startswith("onvif://www.onvif.org/hardware"):
device[CONF_HARDWARE] = scope_str.split("/")[-1]
if scope_str.lower().startswith("onvif://www.onvif.org/mac"): if scope_str.lower().startswith("onvif://www.onvif.org/mac"):
device[CONF_DEVICE_ID] = scope_str.split("/")[-1] device[CONF_DEVICE_ID] = scope_str.split("/")[-1]
devices.append(device) devices.append(device)
@ -192,8 +196,7 @@ class OnvifFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
return await self.async_step_configure() return await self.async_step_configure()
for device in self.devices: for device in self.devices:
name = f"{device[CONF_NAME]} ({device[CONF_HOST]})" if device[CONF_HOST] == user_input[CONF_HOST]:
if name == user_input[CONF_HOST]:
self.device_id = device[CONF_DEVICE_ID] self.device_id = device[CONF_DEVICE_ID]
self.onvif_config = { self.onvif_config = {
CONF_NAME: device[CONF_NAME], CONF_NAME: device[CONF_NAME],
@ -215,15 +218,16 @@ class OnvifFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
LOGGER.debug("Discovered ONVIF devices %s", pformat(self.devices)) LOGGER.debug("Discovered ONVIF devices %s", pformat(self.devices))
if self.devices: if self.devices:
names = [ devices = {CONF_MANUAL_INPUT: CONF_MANUAL_INPUT}
f"{device[CONF_NAME]} ({device[CONF_HOST]})" for device in self.devices for device in self.devices:
] description = f"{device[CONF_NAME]} ({device[CONF_HOST]})"
if hardware := device[CONF_HARDWARE]:
names.append(CONF_MANUAL_INPUT) description += f" [{hardware}]"
devices[device[CONF_HOST]] = description
return self.async_show_form( return self.async_show_form(
step_id="device", step_id="device",
data_schema=vol.Schema({vol.Optional(CONF_HOST): vol.In(names)}), data_schema=vol.Schema({vol.Optional(CONF_HOST): vol.In(devices)}),
) )
return await self.async_step_configure() return await self.async_step_configure()

View File

@ -13,6 +13,7 @@ DEFAULT_PORT = 80
DEFAULT_ARGUMENTS = "-pred 1" DEFAULT_ARGUMENTS = "-pred 1"
CONF_DEVICE_ID = "deviceid" CONF_DEVICE_ID = "deviceid"
CONF_HARDWARE = "hardware"
CONF_SNAPSHOT_AUTH = "snapshot_auth" CONF_SNAPSHOT_AUTH = "snapshot_auth"
CONF_ENABLE_WEBHOOKS = "enable_webhooks" CONF_ENABLE_WEBHOOKS = "enable_webhooks"
DEFAULT_ENABLE_WEBHOOKS = True DEFAULT_ENABLE_WEBHOOKS = True

View File

@ -32,6 +32,7 @@ DISCOVERY = [
config_flow.CONF_HOST: HOST, config_flow.CONF_HOST: HOST,
config_flow.CONF_PORT: PORT, config_flow.CONF_PORT: PORT,
"MAC": MAC, "MAC": MAC,
"HARDWARE": "IPC model",
}, },
{ {
"EPR": "urn:uuid:987654321", "EPR": "urn:uuid:987654321",
@ -54,7 +55,11 @@ DHCP_DISCOVERY_SAME_IP = dhcp.DhcpServiceInfo(
def setup_mock_discovery( def setup_mock_discovery(
mock_discovery, with_name=False, with_mac=False, two_devices=False mock_discovery,
with_name=False,
with_mac=False,
two_devices=False,
with_hardware=True,
): ):
"""Prepare mock discovery result.""" """Prepare mock discovery result."""
services = [] services = []
@ -79,6 +84,12 @@ def setup_mock_discovery(
return_value=f"onvif://www.onvif.org/mac/{item['MAC']}" return_value=f"onvif://www.onvif.org/mac/{item['MAC']}"
) )
scopes.append(scope) scopes.append(scope)
if with_hardware and "HARDWARE" in item:
scope = MagicMock()
scope.getValue = MagicMock(
return_value=f"onvif://www.onvif.org/hardware/{item['HARDWARE']}"
)
scopes.append(scope)
service.getScopes = MagicMock(return_value=scopes) service.getScopes = MagicMock(return_value=scopes)
services.append(service) services.append(service)
mock_discovery.return_value = services mock_discovery.return_value = services
@ -111,10 +122,16 @@ async def test_flow_discovered_devices(hass: HomeAssistant) -> None:
assert result["type"] == data_entry_flow.FlowResultType.FORM assert result["type"] == data_entry_flow.FlowResultType.FORM
assert result["step_id"] == "device" assert result["step_id"] == "device"
assert len(result["data_schema"].schema[config_flow.CONF_HOST].container) == 3 container = result["data_schema"].schema[config_flow.CONF_HOST].container
assert len(container) == 3
assert container == {
"Manually configure ONVIF device": "Manually configure ONVIF device",
"1.2.3.4": "urn:uuid:123456789 (1.2.3.4) [IPC model]",
"5.6.7.8": "urn:uuid:987654321 (5.6.7.8)",
}
result = await hass.config_entries.flow.async_configure( result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input={config_flow.CONF_HOST: f"{URN} ({HOST})"} result["flow_id"], user_input={config_flow.CONF_HOST: HOST}
) )
assert result["type"] == data_entry_flow.FlowResultType.FORM assert result["type"] == data_entry_flow.FlowResultType.FORM