mirror of
https://github.com/home-assistant/core.git
synced 2025-08-02 18:18:21 +00:00
One more
This commit is contained in:
parent
2f36311b54
commit
cc5c569a67
@ -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.
|
|
||||||
"""
|
|
||||||
|
@ -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
|
||||||
|
@ -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")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user