Skip to content

Commit

Permalink
Add hardware model to onvif config flow discovery (#93676)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco committed May 28, 2023
1 parent f3037d0 commit 5f5951e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 deletions.
20 changes: 12 additions & 8 deletions homeassistant/components/onvif/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
from .const import (
CONF_DEVICE_ID,
CONF_ENABLE_WEBHOOKS,
CONF_HARDWARE,
DEFAULT_ARGUMENTS,
DEFAULT_ENABLE_WEBHOOKS,
DEFAULT_PORT,
Expand Down Expand Up @@ -71,11 +72,14 @@ async def async_discovery(hass: HomeAssistant) -> list[dict[str, Any]]:
CONF_NAME: service.getEPR(),
CONF_HOST: url.hostname,
CONF_PORT: url.port or 80,
CONF_HARDWARE: None,
}
for scope in service.getScopes():
scope_str = scope.getValue()
if scope_str.lower().startswith("onvif://www.onvif.org/name"):
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"):
device[CONF_DEVICE_ID] = scope_str.split("/")[-1]
devices.append(device)
Expand Down Expand Up @@ -192,8 +196,7 @@ async def async_step_device(self, user_input=None):
return await self.async_step_configure()

for device in self.devices:
name = f"{device[CONF_NAME]} ({device[CONF_HOST]})"
if name == user_input[CONF_HOST]:
if device[CONF_HOST] == user_input[CONF_HOST]:
self.device_id = device[CONF_DEVICE_ID]
self.onvif_config = {
CONF_NAME: device[CONF_NAME],
Expand All @@ -215,15 +218,16 @@ async def async_step_device(self, user_input=None):
LOGGER.debug("Discovered ONVIF devices %s", pformat(self.devices))

if self.devices:
names = [
f"{device[CONF_NAME]} ({device[CONF_HOST]})" for device in self.devices
]

names.append(CONF_MANUAL_INPUT)
devices = {CONF_MANUAL_INPUT: CONF_MANUAL_INPUT}
for device in self.devices:
description = f"{device[CONF_NAME]} ({device[CONF_HOST]})"
if hardware := device[CONF_HARDWARE]:
description += f" [{hardware}]"
devices[device[CONF_HOST]] = description

return self.async_show_form(
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()
Expand Down
1 change: 1 addition & 0 deletions homeassistant/components/onvif/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
DEFAULT_ARGUMENTS = "-pred 1"

CONF_DEVICE_ID = "deviceid"
CONF_HARDWARE = "hardware"
CONF_SNAPSHOT_AUTH = "snapshot_auth"
CONF_ENABLE_WEBHOOKS = "enable_webhooks"
DEFAULT_ENABLE_WEBHOOKS = True
Expand Down
23 changes: 20 additions & 3 deletions tests/components/onvif/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
config_flow.CONF_HOST: HOST,
config_flow.CONF_PORT: PORT,
"MAC": MAC,
"HARDWARE": "IPC model",
},
{
"EPR": "urn:uuid:987654321",
Expand All @@ -54,7 +55,11 @@


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."""
services = []
Expand All @@ -79,6 +84,12 @@ def setup_mock_discovery(
return_value=f"onvif://www.onvif.org/mac/{item['MAC']}"
)
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)
services.append(service)
mock_discovery.return_value = services
Expand Down Expand Up @@ -111,10 +122,16 @@ async def test_flow_discovered_devices(hass: HomeAssistant) -> None:

assert result["type"] == data_entry_flow.FlowResultType.FORM
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["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
Expand Down

0 comments on commit 5f5951e

Please sign in to comment.