From cc5c569a67b4ef8fb4fcedfcd7c73e8c01179b32 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Wed, 2 Jul 2025 07:34:59 +0000 Subject: [PATCH] One more --- homeassistant/helpers/service_info/dhcp.py | 6 ----- tests/conftest.py | 28 ++++++++++++---------- tests/helpers/test_service_info.py | 15 +++++++++--- 3 files changed, 28 insertions(+), 21 deletions(-) diff --git a/homeassistant/helpers/service_info/dhcp.py b/homeassistant/helpers/service_info/dhcp.py index eb9fe629123..d46c7a59004 100644 --- a/homeassistant/helpers/service_info/dhcp.py +++ b/homeassistant/helpers/service_info/dhcp.py @@ -18,9 +18,3 @@ class DhcpServiceInfo(BaseServiceInfo): as a lowercase string without colons. 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. - """ diff --git a/tests/conftest.py b/tests/conftest.py index 4fc3996bc7c..15f9243312c 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -156,18 +156,6 @@ asyncio.set_event_loop_policy(runner.HassEventLoopPolicy(False)) 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: """Register custom pytest options.""" 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 ) 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 diff --git a/tests/helpers/test_service_info.py b/tests/helpers/test_service_info.py index c84be7af38f..249ceb0e637 100644 --- a/tests/helpers/test_service_info.py +++ b/tests/helpers/test_service_info.py @@ -1,14 +1,23 @@ """Test service_info helpers.""" +import pytest + 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: _ = DhcpServiceInfo(ip="", hostname="", macaddress="AA:BB:CC:DD:EE:FF") except ValueError: pass else: raise RuntimeError( - "DhcpServiceInfo.__post_init__ was not called. " - "Please ensure that the __post_init__ method is correctly defined." + "DhcpServiceInfo incorrectly formatted mac address was not rejected. " + "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")