Compare commits

...

3 Commits

Author SHA1 Message Date
epenet
0dd090ba20 Simplify 2025-05-19 06:51:33 +00:00
epenet
864882750d Move to service_info 2025-05-14 06:31:43 +00:00
epenet
594769aa95 Add MockDhcpServiceInfo common test helper 2025-05-14 06:11:24 +00:00
5 changed files with 41 additions and 21 deletions

View File

@@ -10,9 +10,9 @@ from homeassistant.components.airthings.const import CONF_SECRET, DOMAIN
from homeassistant.const import CONF_ID
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
from homeassistant.helpers.service_info.dhcp import DhcpServiceInfo
from tests.common import MockConfigEntry
from tests.service_info import MockDhcpServiceInfo
TEST_DATA = {
CONF_ID: "client_id",
@@ -20,17 +20,17 @@ TEST_DATA = {
}
DHCP_SERVICE_INFO = [
DhcpServiceInfo(
MockDhcpServiceInfo(
hostname="airthings-view",
ip="192.168.1.100",
macaddress="00:00:00:00:00:00",
),
DhcpServiceInfo(
MockDhcpServiceInfo(
hostname="airthings-hub",
ip="192.168.1.101",
macaddress="D0:14:11:90:00:00",
),
DhcpServiceInfo(
MockDhcpServiceInfo(
hostname="airthings-hub",
ip="192.168.1.102",
macaddress="70:B3:D5:2A:00:00",
@@ -147,7 +147,7 @@ async def test_flow_entry_already_exists(hass: HomeAssistant) -> None:
@pytest.mark.parametrize("dhcp_service_info", DHCP_SERVICE_INFO)
async def test_dhcp_flow(
hass: HomeAssistant, dhcp_service_info: DhcpServiceInfo
hass: HomeAssistant, dhcp_service_info: MockDhcpServiceInfo
) -> None:
"""Test the DHCP discovery flow."""

View File

@@ -18,8 +18,6 @@ from homeassistant.config_entries import SOURCE_USER, ConfigEntryState
from homeassistant.const import CONF_HOST, CONF_ID, CONF_PORT
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.service_info.dhcp import DhcpServiceInfo
from .util import (
CONFIG,
@@ -32,11 +30,12 @@ from .util import (
)
from tests.common import MockConfigEntry
from tests.service_info import MockDhcpServiceInfo
DHCP_SERVICE_INFO = DhcpServiceInfo(
DHCP_SERVICE_INFO = MockDhcpServiceInfo(
hostname="airzone",
ip="192.168.1.100",
macaddress=dr.format_mac("E84F25000000").replace(":", ""),
macaddress="E84F25000000",
)
TEST_ID = 1

View File

@@ -19,11 +19,11 @@ from homeassistant.components.roborock.const import CONF_ENTRY_CODE, DOMAIN, DRA
from homeassistant.const import CONF_USERNAME, Platform
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
from homeassistant.helpers.service_info.dhcp import DhcpServiceInfo
from .mock_data import MOCK_CONFIG, NETWORK_INFO, ROBOROCK_RRUID, USER_DATA, USER_EMAIL
from tests.common import MockConfigEntry
from tests.service_info import MockDhcpServiceInfo
@pytest.fixture
@@ -349,9 +349,9 @@ async def test_discovery_not_setup(
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_DHCP},
data=DhcpServiceInfo(
data=MockDhcpServiceInfo(
ip=NETWORK_INFO.ip,
macaddress=NETWORK_INFO.mac.replace(":", ""),
macaddress=NETWORK_INFO.mac,
hostname="roborock-vacuum-a72",
),
)
@@ -394,9 +394,9 @@ async def test_discovery_already_setup(
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_DHCP},
data=DhcpServiceInfo(
data=MockDhcpServiceInfo(
ip=NETWORK_INFO.ip,
macaddress=NETWORK_INFO.mac.replace(":", ""),
macaddress=NETWORK_INFO.mac,
hostname="roborock-vacuum-a72",
),
)

View File

@@ -9,11 +9,11 @@ from homeassistant.config_entries import SOURCE_DHCP, SOURCE_USER, ConfigEntrySt
from homeassistant.const import CONF_HOST
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
from homeassistant.helpers.service_info.dhcp import DhcpServiceInfo
from . import setup_config_entry
from tests.common import MockConfigEntry
from tests.service_info import MockDhcpServiceInfo
async def test_config_flow(
@@ -49,7 +49,7 @@ async def test_config_flow_from_dhcp(
hass: HomeAssistant, mock_setup_entry: AsyncMock, mock_hub_refresh: AsyncMock
) -> None:
"""Test we can handle DHCP discovery to create a config entry."""
info = DhcpServiceInfo(
info = MockDhcpServiceInfo(
ip="1.2.3.4", hostname="webcontrol", macaddress="00:11:22:33:44:55"
)
result = await hass.config_entries.flow.async_init(
@@ -108,7 +108,7 @@ async def test_config_flow_from_dhcp_add_mac(
assert len(mock_setup_entry.mock_calls) == 1
assert hass.config_entries.async_entries(DOMAIN)[0].unique_id is None
info = DhcpServiceInfo(
info = MockDhcpServiceInfo(
ip="1.2.3.4", hostname="webcontrol", macaddress="00:11:22:33:44:55"
)
result = await hass.config_entries.flow.async_init(
@@ -125,7 +125,7 @@ async def test_config_flow_from_dhcp_ip_update(
mock_hub_refresh: AsyncMock,
) -> None:
"""Test we can use DHCP discovery to update IP in a config entry."""
info = DhcpServiceInfo(
info = MockDhcpServiceInfo(
ip="1.2.3.4", hostname="webcontrol", macaddress="00:11:22:33:44:55"
)
result = await hass.config_entries.flow.async_init(
@@ -153,7 +153,7 @@ async def test_config_flow_from_dhcp_ip_update(
assert len(mock_setup_entry.mock_calls) == 1
assert hass.config_entries.async_entries(DOMAIN)[0].unique_id == "00:11:22:33:44:55"
info = DhcpServiceInfo(
info = MockDhcpServiceInfo(
ip="5.6.7.8", hostname="webcontrol", macaddress="00:11:22:33:44:55"
)
result = await hass.config_entries.flow.async_init(
@@ -171,7 +171,7 @@ async def test_config_flow_from_dhcp_no_update(
mock_hub_refresh: AsyncMock,
) -> None:
"""Test we do not use DHCP discovery to overwrite hostname with IP in config entry."""
info = DhcpServiceInfo(
info = MockDhcpServiceInfo(
ip="1.2.3.4", hostname="webcontrol", macaddress="00:11:22:33:44:55"
)
result = await hass.config_entries.flow.async_init(
@@ -199,7 +199,7 @@ async def test_config_flow_from_dhcp_no_update(
assert len(mock_setup_entry.mock_calls) == 1
assert hass.config_entries.async_entries(DOMAIN)[0].unique_id == "00:11:22:33:44:55"
info = DhcpServiceInfo(
info = MockDhcpServiceInfo(
ip="5.6.7.8", hostname="webcontrol", macaddress="00:11:22:33:44:55"
)
result = await hass.config_entries.flow.async_init(

21
tests/service_info.py Normal file
View File

@@ -0,0 +1,21 @@
"""Service info test helpers."""
from __future__ import annotations
from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.service_info.dhcp import DhcpServiceInfo
class MockDhcpServiceInfo(DhcpServiceInfo):
"""Mocked DHCP service info."""
def __init__(self, ip: str, hostname: str, macaddress: str) -> None:
"""Initialize the mock service info."""
# Historically, the MAC address was formatted without colons
# and since all consumers of this data are expecting it to be
# formatted without colons we will continue to do so
super().__init__(
ip=ip,
hostname=hostname,
macaddress=dr.format_mac(macaddress).replace(":", ""),
)