2020-05-01 06:15:40 +00:00
|
|
|
"""Test ONVIF config flow."""
|
2022-01-24 14:07:06 +00:00
|
|
|
from unittest.mock import MagicMock, patch
|
2021-01-01 21:31:56 +00:00
|
|
|
|
2020-05-01 06:15:40 +00:00
|
|
|
from homeassistant import config_entries, data_entry_flow
|
2023-04-16 19:55:33 +00:00
|
|
|
from homeassistant.components import dhcp
|
|
|
|
from homeassistant.components.onvif import DOMAIN, config_flow
|
|
|
|
from homeassistant.config_entries import SOURCE_DHCP
|
|
|
|
from homeassistant.const import CONF_HOST
|
2023-02-08 15:48:54 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2023-04-16 19:55:33 +00:00
|
|
|
from homeassistant.data_entry_flow import FlowResultType
|
|
|
|
from homeassistant.helpers import device_registry as dr
|
2020-05-01 06:15:40 +00:00
|
|
|
|
2022-01-24 14:07:06 +00:00
|
|
|
from . import (
|
|
|
|
HOST,
|
|
|
|
MAC,
|
|
|
|
NAME,
|
|
|
|
PASSWORD,
|
|
|
|
PORT,
|
|
|
|
URN,
|
|
|
|
USERNAME,
|
|
|
|
setup_mock_device,
|
|
|
|
setup_mock_onvif_camera,
|
|
|
|
setup_onvif_integration,
|
|
|
|
)
|
2020-05-01 06:15:40 +00:00
|
|
|
|
|
|
|
DISCOVERY = [
|
|
|
|
{
|
|
|
|
"EPR": URN,
|
|
|
|
config_flow.CONF_NAME: NAME,
|
|
|
|
config_flow.CONF_HOST: HOST,
|
|
|
|
config_flow.CONF_PORT: PORT,
|
|
|
|
"MAC": MAC,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"EPR": "urn:uuid:987654321",
|
|
|
|
config_flow.CONF_NAME: "TestCamera2",
|
|
|
|
config_flow.CONF_HOST: "5.6.7.8",
|
|
|
|
config_flow.CONF_PORT: PORT,
|
|
|
|
"MAC": "ee:dd:cc:bb:aa",
|
|
|
|
},
|
|
|
|
]
|
2023-04-16 19:55:33 +00:00
|
|
|
DHCP_DISCOVERY = dhcp.DhcpServiceInfo(
|
|
|
|
hostname="any",
|
|
|
|
ip="5.6.7.8",
|
|
|
|
macaddress=MAC,
|
|
|
|
)
|
|
|
|
DHCP_DISCOVERY_SAME_IP = dhcp.DhcpServiceInfo(
|
|
|
|
hostname="any",
|
|
|
|
ip="1.2.3.4",
|
|
|
|
macaddress=MAC,
|
|
|
|
)
|
2020-05-01 06:15:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
def setup_mock_discovery(
|
|
|
|
mock_discovery, with_name=False, with_mac=False, two_devices=False
|
|
|
|
):
|
|
|
|
"""Prepare mock discovery result."""
|
|
|
|
services = []
|
|
|
|
for item in DISCOVERY:
|
|
|
|
service = MagicMock()
|
|
|
|
service.getXAddrs = MagicMock(
|
|
|
|
return_value=[
|
|
|
|
f"http://{item[config_flow.CONF_HOST]}:{item[config_flow.CONF_PORT]}/onvif/device_service"
|
|
|
|
]
|
|
|
|
)
|
|
|
|
service.getEPR = MagicMock(return_value=item["EPR"])
|
|
|
|
scopes = []
|
|
|
|
if with_name:
|
|
|
|
scope = MagicMock()
|
|
|
|
scope.getValue = MagicMock(
|
|
|
|
return_value=f"onvif://www.onvif.org/name/{item[config_flow.CONF_NAME]}"
|
|
|
|
)
|
|
|
|
scopes.append(scope)
|
|
|
|
if with_mac:
|
|
|
|
scope = MagicMock()
|
|
|
|
scope.getValue = MagicMock(
|
|
|
|
return_value=f"onvif://www.onvif.org/mac/{item['MAC']}"
|
|
|
|
)
|
|
|
|
scopes.append(scope)
|
|
|
|
service.getScopes = MagicMock(return_value=scopes)
|
|
|
|
services.append(service)
|
|
|
|
mock_discovery.return_value = services
|
|
|
|
|
|
|
|
|
2023-02-08 15:48:54 +00:00
|
|
|
async def test_flow_discovered_devices(hass: HomeAssistant) -> None:
|
2020-05-01 06:15:40 +00:00
|
|
|
"""Test that config flow works for discovered devices."""
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
2021-04-25 09:27:40 +00:00
|
|
|
config_flow.DOMAIN, context={"source": config_entries.SOURCE_USER}
|
2020-05-01 06:15:40 +00:00
|
|
|
)
|
|
|
|
|
2022-07-07 16:57:36 +00:00
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
2020-05-01 06:15:40 +00:00
|
|
|
assert result["step_id"] == "user"
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.onvif.config_flow.get_device"
|
2020-05-06 16:29:59 +00:00
|
|
|
) as mock_onvif_camera, patch(
|
2020-05-01 06:15:40 +00:00
|
|
|
"homeassistant.components.onvif.config_flow.wsdiscovery"
|
|
|
|
) as mock_discovery, patch(
|
2020-05-06 16:29:59 +00:00
|
|
|
"homeassistant.components.onvif.ONVIFDevice"
|
|
|
|
) as mock_device:
|
|
|
|
setup_mock_onvif_camera(mock_onvif_camera)
|
2020-05-01 06:15:40 +00:00
|
|
|
setup_mock_discovery(mock_discovery)
|
2020-05-06 16:29:59 +00:00
|
|
|
setup_mock_device(mock_device)
|
2020-05-01 06:15:40 +00:00
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
2021-06-28 14:39:18 +00:00
|
|
|
result["flow_id"], user_input={"auto": True}
|
2020-05-01 06:15:40 +00:00
|
|
|
)
|
|
|
|
|
2022-07-07 16:57:36 +00:00
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
2020-05-01 06:15:40 +00:00
|
|
|
assert result["step_id"] == "device"
|
|
|
|
assert len(result["data_schema"].schema[config_flow.CONF_HOST].container) == 3
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"], user_input={config_flow.CONF_HOST: f"{URN} ({HOST})"}
|
|
|
|
)
|
|
|
|
|
2022-07-07 16:57:36 +00:00
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
2021-06-28 14:39:18 +00:00
|
|
|
assert result["step_id"] == "configure"
|
2020-05-01 06:15:40 +00:00
|
|
|
|
2020-10-15 07:19:08 +00:00
|
|
|
with patch(
|
|
|
|
"homeassistant.components.onvif.async_setup_entry", return_value=True
|
|
|
|
) as mock_setup_entry:
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
user_input={
|
|
|
|
config_flow.CONF_USERNAME: USERNAME,
|
|
|
|
config_flow.CONF_PASSWORD: PASSWORD,
|
|
|
|
},
|
|
|
|
)
|
2020-05-01 06:15:40 +00:00
|
|
|
|
2020-10-15 07:19:08 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert len(mock_setup_entry.mock_calls) == 1
|
2020-05-01 06:15:40 +00:00
|
|
|
|
2022-07-07 16:57:36 +00:00
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
|
2020-05-01 06:15:40 +00:00
|
|
|
assert result["title"] == f"{URN} - {MAC}"
|
|
|
|
assert result["data"] == {
|
|
|
|
config_flow.CONF_NAME: URN,
|
|
|
|
config_flow.CONF_HOST: HOST,
|
|
|
|
config_flow.CONF_PORT: PORT,
|
|
|
|
config_flow.CONF_USERNAME: USERNAME,
|
|
|
|
config_flow.CONF_PASSWORD: PASSWORD,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-02-08 15:48:54 +00:00
|
|
|
async def test_flow_discovered_devices_ignore_configured_manual_input(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
) -> None:
|
2020-05-01 06:15:40 +00:00
|
|
|
"""Test that config flow discovery ignores configured devices."""
|
|
|
|
await setup_onvif_integration(hass)
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
2021-04-25 09:27:40 +00:00
|
|
|
config_flow.DOMAIN, context={"source": config_entries.SOURCE_USER}
|
2020-05-01 06:15:40 +00:00
|
|
|
)
|
|
|
|
|
2022-07-07 16:57:36 +00:00
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
2020-05-01 06:15:40 +00:00
|
|
|
assert result["step_id"] == "user"
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.onvif.config_flow.get_device"
|
2020-05-06 16:29:59 +00:00
|
|
|
) as mock_onvif_camera, patch(
|
2020-05-01 06:15:40 +00:00
|
|
|
"homeassistant.components.onvif.config_flow.wsdiscovery"
|
|
|
|
) as mock_discovery, patch(
|
2020-05-06 16:29:59 +00:00
|
|
|
"homeassistant.components.onvif.ONVIFDevice"
|
|
|
|
) as mock_device:
|
|
|
|
setup_mock_onvif_camera(mock_onvif_camera)
|
2020-05-01 06:15:40 +00:00
|
|
|
setup_mock_discovery(mock_discovery, with_mac=True)
|
2020-05-06 16:29:59 +00:00
|
|
|
setup_mock_device(mock_device)
|
2020-05-01 06:15:40 +00:00
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
2021-06-28 14:39:18 +00:00
|
|
|
result["flow_id"], user_input={"auto": True}
|
2020-05-01 06:15:40 +00:00
|
|
|
)
|
|
|
|
|
2022-07-07 16:57:36 +00:00
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
2020-05-01 06:15:40 +00:00
|
|
|
assert result["step_id"] == "device"
|
|
|
|
assert len(result["data_schema"].schema[config_flow.CONF_HOST].container) == 2
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
user_input={config_flow.CONF_HOST: config_flow.CONF_MANUAL_INPUT},
|
|
|
|
)
|
|
|
|
|
2022-07-07 16:57:36 +00:00
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
2021-06-28 14:39:18 +00:00
|
|
|
assert result["step_id"] == "configure"
|
|
|
|
|
|
|
|
|
2023-02-08 15:48:54 +00:00
|
|
|
async def test_flow_discovered_no_device(hass: HomeAssistant) -> None:
|
2021-06-28 14:39:18 +00:00
|
|
|
"""Test that config flow discovery no device."""
|
|
|
|
await setup_onvif_integration(hass)
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
config_flow.DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
|
|
|
|
2022-07-07 16:57:36 +00:00
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
2021-06-28 14:39:18 +00:00
|
|
|
assert result["step_id"] == "user"
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.onvif.config_flow.get_device"
|
|
|
|
) as mock_onvif_camera, patch(
|
|
|
|
"homeassistant.components.onvif.config_flow.wsdiscovery"
|
|
|
|
) as mock_discovery, patch(
|
|
|
|
"homeassistant.components.onvif.ONVIFDevice"
|
|
|
|
) as mock_device:
|
|
|
|
setup_mock_onvif_camera(mock_onvif_camera)
|
|
|
|
mock_discovery.return_value = []
|
|
|
|
setup_mock_device(mock_device)
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"], user_input={"auto": True}
|
|
|
|
)
|
|
|
|
|
2022-07-07 16:57:36 +00:00
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
2021-06-28 14:39:18 +00:00
|
|
|
assert result["step_id"] == "configure"
|
2020-05-01 06:15:40 +00:00
|
|
|
|
|
|
|
|
2023-02-08 15:48:54 +00:00
|
|
|
async def test_flow_discovery_ignore_existing_and_abort(hass: HomeAssistant) -> None:
|
2020-05-01 06:15:40 +00:00
|
|
|
"""Test that config flow discovery ignores setup devices."""
|
|
|
|
await setup_onvif_integration(hass)
|
|
|
|
await setup_onvif_integration(
|
|
|
|
hass,
|
|
|
|
config={
|
|
|
|
config_flow.CONF_NAME: DISCOVERY[1]["EPR"],
|
|
|
|
config_flow.CONF_HOST: DISCOVERY[1][config_flow.CONF_HOST],
|
|
|
|
config_flow.CONF_PORT: DISCOVERY[1][config_flow.CONF_PORT],
|
|
|
|
config_flow.CONF_USERNAME: "",
|
|
|
|
config_flow.CONF_PASSWORD: "",
|
|
|
|
},
|
|
|
|
unique_id=DISCOVERY[1]["MAC"],
|
|
|
|
entry_id="2",
|
|
|
|
)
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
2021-04-25 09:27:40 +00:00
|
|
|
config_flow.DOMAIN, context={"source": config_entries.SOURCE_USER}
|
2020-05-01 06:15:40 +00:00
|
|
|
)
|
|
|
|
|
2022-07-07 16:57:36 +00:00
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
2020-05-01 06:15:40 +00:00
|
|
|
assert result["step_id"] == "user"
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.onvif.config_flow.get_device"
|
2020-05-06 16:29:59 +00:00
|
|
|
) as mock_onvif_camera, patch(
|
2020-05-01 06:15:40 +00:00
|
|
|
"homeassistant.components.onvif.config_flow.wsdiscovery"
|
|
|
|
) as mock_discovery, patch(
|
2020-05-06 16:29:59 +00:00
|
|
|
"homeassistant.components.onvif.ONVIFDevice"
|
|
|
|
) as mock_device:
|
|
|
|
setup_mock_onvif_camera(mock_onvif_camera)
|
2020-05-01 06:15:40 +00:00
|
|
|
setup_mock_discovery(mock_discovery, with_name=True, with_mac=True)
|
2020-05-06 16:29:59 +00:00
|
|
|
setup_mock_device(mock_device)
|
2020-05-01 06:15:40 +00:00
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
2021-06-28 14:39:18 +00:00
|
|
|
result["flow_id"], user_input={"auto": True}
|
2020-05-01 06:15:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# It should skip to manual entry if the only devices are already configured
|
2022-07-07 16:57:36 +00:00
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
2021-06-28 14:39:18 +00:00
|
|
|
assert result["step_id"] == "configure"
|
2020-05-01 06:15:40 +00:00
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
user_input={
|
|
|
|
config_flow.CONF_NAME: NAME,
|
|
|
|
config_flow.CONF_HOST: HOST,
|
|
|
|
config_flow.CONF_PORT: PORT,
|
|
|
|
config_flow.CONF_USERNAME: USERNAME,
|
|
|
|
config_flow.CONF_PASSWORD: PASSWORD,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
# It should abort if already configured and entered manually
|
2022-07-07 16:57:36 +00:00
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.ABORT
|
2020-05-01 06:15:40 +00:00
|
|
|
|
|
|
|
|
2023-02-08 15:48:54 +00:00
|
|
|
async def test_flow_manual_entry(hass: HomeAssistant) -> None:
|
2020-05-01 06:15:40 +00:00
|
|
|
"""Test that config flow works for discovered devices."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
2021-04-25 09:27:40 +00:00
|
|
|
config_flow.DOMAIN, context={"source": config_entries.SOURCE_USER}
|
2020-05-01 06:15:40 +00:00
|
|
|
)
|
|
|
|
|
2022-07-07 16:57:36 +00:00
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
2020-05-01 06:15:40 +00:00
|
|
|
assert result["step_id"] == "user"
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.onvif.config_flow.get_device"
|
2020-05-06 16:29:59 +00:00
|
|
|
) as mock_onvif_camera, patch(
|
2020-05-01 06:15:40 +00:00
|
|
|
"homeassistant.components.onvif.config_flow.wsdiscovery"
|
|
|
|
) as mock_discovery, patch(
|
2020-05-06 16:29:59 +00:00
|
|
|
"homeassistant.components.onvif.ONVIFDevice"
|
|
|
|
) as mock_device:
|
|
|
|
setup_mock_onvif_camera(mock_onvif_camera, two_profiles=True)
|
2020-05-01 06:15:40 +00:00
|
|
|
# no discovery
|
|
|
|
mock_discovery.return_value = []
|
2020-05-06 16:29:59 +00:00
|
|
|
setup_mock_device(mock_device)
|
2020-05-01 06:15:40 +00:00
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
2020-08-27 11:56:20 +00:00
|
|
|
result["flow_id"],
|
2021-06-28 14:39:18 +00:00
|
|
|
user_input={"auto": False},
|
2020-05-01 06:15:40 +00:00
|
|
|
)
|
|
|
|
|
2022-07-07 16:57:36 +00:00
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
2021-06-28 14:39:18 +00:00
|
|
|
assert result["step_id"] == "configure"
|
2020-05-01 06:15:40 +00:00
|
|
|
|
2020-10-15 07:19:08 +00:00
|
|
|
with patch(
|
|
|
|
"homeassistant.components.onvif.async_setup_entry", return_value=True
|
|
|
|
) as mock_setup_entry:
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
user_input={
|
2021-06-28 14:39:18 +00:00
|
|
|
config_flow.CONF_NAME: NAME,
|
|
|
|
config_flow.CONF_HOST: HOST,
|
|
|
|
config_flow.CONF_PORT: PORT,
|
2020-10-15 07:19:08 +00:00
|
|
|
config_flow.CONF_USERNAME: USERNAME,
|
|
|
|
config_flow.CONF_PASSWORD: PASSWORD,
|
|
|
|
},
|
|
|
|
)
|
2020-05-01 06:15:40 +00:00
|
|
|
|
2020-10-15 07:19:08 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert len(mock_setup_entry.mock_calls) == 1
|
2020-05-01 06:15:40 +00:00
|
|
|
|
2022-07-07 16:57:36 +00:00
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
|
2020-05-01 06:15:40 +00:00
|
|
|
assert result["title"] == f"{NAME} - {MAC}"
|
|
|
|
assert result["data"] == {
|
|
|
|
config_flow.CONF_NAME: NAME,
|
|
|
|
config_flow.CONF_HOST: HOST,
|
|
|
|
config_flow.CONF_PORT: PORT,
|
|
|
|
config_flow.CONF_USERNAME: USERNAME,
|
|
|
|
config_flow.CONF_PASSWORD: PASSWORD,
|
|
|
|
}
|
2021-05-24 19:27:40 +00:00
|
|
|
|
|
|
|
|
2023-02-08 15:48:54 +00:00
|
|
|
async def test_option_flow(hass: HomeAssistant) -> None:
|
2020-05-01 06:15:40 +00:00
|
|
|
"""Test config flow options."""
|
2022-01-24 14:07:06 +00:00
|
|
|
entry, _, _ = await setup_onvif_integration(hass)
|
2020-05-01 06:15:40 +00:00
|
|
|
|
2022-05-23 05:54:49 +00:00
|
|
|
result = await hass.config_entries.options.async_init(
|
|
|
|
entry.entry_id, context={"show_advanced_options": True}
|
|
|
|
)
|
2020-05-01 06:15:40 +00:00
|
|
|
|
2022-07-07 16:57:36 +00:00
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.FORM
|
2020-05-01 06:15:40 +00:00
|
|
|
assert result["step_id"] == "onvif_devices"
|
|
|
|
|
|
|
|
result = await hass.config_entries.options.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
user_input={
|
|
|
|
config_flow.CONF_EXTRA_ARGUMENTS: "",
|
2022-05-15 06:31:18 +00:00
|
|
|
config_flow.CONF_RTSP_TRANSPORT: list(config_flow.RTSP_TRANSPORTS)[1],
|
2022-05-23 05:54:49 +00:00
|
|
|
config_flow.CONF_USE_WALLCLOCK_AS_TIMESTAMPS: True,
|
2020-05-01 06:15:40 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2022-07-07 16:57:36 +00:00
|
|
|
assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
|
2020-05-01 06:15:40 +00:00
|
|
|
assert result["data"] == {
|
|
|
|
config_flow.CONF_EXTRA_ARGUMENTS: "",
|
2022-05-15 06:31:18 +00:00
|
|
|
config_flow.CONF_RTSP_TRANSPORT: list(config_flow.RTSP_TRANSPORTS)[1],
|
2022-05-23 05:54:49 +00:00
|
|
|
config_flow.CONF_USE_WALLCLOCK_AS_TIMESTAMPS: True,
|
2020-05-01 06:15:40 +00:00
|
|
|
}
|
2023-04-16 19:55:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_discovered_by_dhcp_updates_host(hass: HomeAssistant) -> None:
|
|
|
|
"""Test dhcp updates existing host."""
|
|
|
|
config_entry, _camera, device = await setup_onvif_integration(hass)
|
|
|
|
device.profiles = device.async_get_profiles()
|
|
|
|
registry = dr.async_get(hass)
|
|
|
|
devices = dr.async_entries_for_config_entry(registry, config_entry.entry_id)
|
|
|
|
assert len(devices) == 1
|
|
|
|
device = devices[0]
|
|
|
|
assert device.model == "TestModel"
|
|
|
|
assert device.connections == {(dr.CONNECTION_NETWORK_MAC, MAC)}
|
|
|
|
assert config_entry.data[CONF_HOST] == "1.2.3.4"
|
|
|
|
await hass.config_entries.async_unload(config_entry.entry_id)
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": SOURCE_DHCP}, data=DHCP_DISCOVERY
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert result["type"] == FlowResultType.ABORT
|
|
|
|
assert result["reason"] == "already_configured"
|
|
|
|
assert config_entry.data[CONF_HOST] == DHCP_DISCOVERY.ip
|
|
|
|
|
|
|
|
|
|
|
|
async def test_discovered_by_dhcp_does_nothing_if_host_is_the_same(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
) -> None:
|
|
|
|
"""Test dhcp update does nothing if host is the same."""
|
|
|
|
config_entry, _camera, device = await setup_onvif_integration(hass)
|
|
|
|
device.profiles = device.async_get_profiles()
|
|
|
|
registry = dr.async_get(hass)
|
|
|
|
devices = dr.async_entries_for_config_entry(registry, config_entry.entry_id)
|
|
|
|
assert len(devices) == 1
|
|
|
|
device = devices[0]
|
|
|
|
assert device.model == "TestModel"
|
|
|
|
assert device.connections == {(dr.CONNECTION_NETWORK_MAC, MAC)}
|
|
|
|
assert config_entry.data[CONF_HOST] == DHCP_DISCOVERY_SAME_IP.ip
|
|
|
|
await hass.config_entries.async_unload(config_entry.entry_id)
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": SOURCE_DHCP}, data=DHCP_DISCOVERY_SAME_IP
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert result["type"] == FlowResultType.ABORT
|
|
|
|
assert result["reason"] == "already_configured"
|
|
|
|
assert config_entry.data[CONF_HOST] == DHCP_DISCOVERY_SAME_IP.ip
|
|
|
|
|
|
|
|
|
|
|
|
async def test_discovered_by_dhcp_does_not_update_if_already_loaded(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
) -> None:
|
|
|
|
"""Test dhcp does not update existing host if its already loaded."""
|
|
|
|
config_entry, _camera, device = await setup_onvif_integration(hass)
|
|
|
|
device.profiles = device.async_get_profiles()
|
|
|
|
registry = dr.async_get(hass)
|
|
|
|
devices = dr.async_entries_for_config_entry(registry, config_entry.entry_id)
|
|
|
|
assert len(devices) == 1
|
|
|
|
device = devices[0]
|
|
|
|
assert device.model == "TestModel"
|
|
|
|
assert device.connections == {(dr.CONNECTION_NETWORK_MAC, MAC)}
|
|
|
|
assert config_entry.data[CONF_HOST] == "1.2.3.4"
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": SOURCE_DHCP}, data=DHCP_DISCOVERY
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert result["type"] == FlowResultType.ABORT
|
|
|
|
assert result["reason"] == "already_configured"
|
|
|
|
assert config_entry.data[CONF_HOST] != DHCP_DISCOVERY.ip
|
|
|
|
|
|
|
|
|
|
|
|
async def test_discovered_by_dhcp_does_not_update_if_no_matching_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
) -> None:
|
|
|
|
"""Test dhcp does not update existing host if there are no matching entries."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": SOURCE_DHCP}, data=DHCP_DISCOVERY
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert result["type"] == FlowResultType.ABORT
|
|
|
|
assert result["reason"] == "no_devices_found"
|