This commit is contained in:
epenet 2025-07-02 07:34:59 +00:00
parent 2f36311b54
commit cc5c569a67
3 changed files with 28 additions and 21 deletions

View File

@ -18,9 +18,3 @@ class DhcpServiceInfo(BaseServiceInfo):
as a lowercase string without colons. as a lowercase string without colons.
eg. "AA:BB:CC:12:34:56" is stored as "aabbcc123456" eg. "AA:BB:CC:12:34:56" is stored as "aabbcc123456"
""" """
def __post_init__(self) -> None:
"""Post init checks.
Needed so it can be overridden in tests.
"""

View File

@ -156,18 +156,6 @@ asyncio.set_event_loop_policy(runner.HassEventLoopPolicy(False))
asyncio.set_event_loop_policy = lambda policy: None asyncio.set_event_loop_policy = lambda policy: None
def _dhcp_service_info_post_init(self: DhcpServiceInfo) -> None:
"""Post-init processing for DhcpServiceInfo.
Ensure that the macaddress is always in lowercase and without colons to match DHCP service.
"""
if self.macaddress != self.macaddress.lower().replace(":", ""):
raise ValueError("macaddress is not correctly formatted")
DhcpServiceInfo.__post_init__ = _dhcp_service_info_post_init
def pytest_addoption(parser: pytest.Parser) -> None: def pytest_addoption(parser: pytest.Parser) -> None:
"""Register custom pytest options.""" """Register custom pytest options."""
parser.addoption("--dburl", action="store", default="sqlite://") parser.addoption("--dburl", action="store", default="sqlite://")
@ -2091,3 +2079,19 @@ def disable_block_async_io() -> Generator[None]:
blocking_call.object, blocking_call.function, blocking_call.original_func blocking_call.object, blocking_call.function, blocking_call.original_func
) )
calls.clear() calls.clear()
_real_dhcp_service_info_init = DhcpServiceInfo.__init__
def _dhcp_service_info_init(self: DhcpServiceInfo, *args: Any, **kwargs: Any) -> None:
"""Override __init__ for DhcpServiceInfo.
Ensure that the macaddress is always in lowercase and without colons to match DHCP service.
"""
_real_dhcp_service_info_init(self, *args, **kwargs)
if self.macaddress != self.macaddress.lower().replace(":", ""):
raise ValueError("macaddress is not correctly formatted")
DhcpServiceInfo.__init__ = _dhcp_service_info_init

View File

@ -1,14 +1,23 @@
"""Test service_info helpers.""" """Test service_info helpers."""
import pytest
from homeassistant.helpers.service_info.dhcp import DhcpServiceInfo from homeassistant.helpers.service_info.dhcp import DhcpServiceInfo
# Ensure that DhcpServiceInfo.__post_init__ is called, even on a constant outside of a test # Ensure that incorrectly formatted mac addresses are rejected, even
# on a constant outside of a test
try: try:
_ = DhcpServiceInfo(ip="", hostname="", macaddress="AA:BB:CC:DD:EE:FF") _ = DhcpServiceInfo(ip="", hostname="", macaddress="AA:BB:CC:DD:EE:FF")
except ValueError: except ValueError:
pass pass
else: else:
raise RuntimeError( raise RuntimeError(
"DhcpServiceInfo.__post_init__ was not called. " "DhcpServiceInfo incorrectly formatted mac address was not rejected. "
"Please ensure that the __post_init__ method is correctly defined." "Please ensure that the DhcpServiceInfo is correctly patched."
) )
def test_invalid_macaddress() -> None:
"""Test that DhcpServiceInfo raises ValueError for unformatted macaddress."""
with pytest.raises(ValueError):
DhcpServiceInfo(ip="", hostname="", macaddress="AA:BB:CC:DD:EE:FF")