diff --git a/hahomematic/support.py b/hahomematic/support.py index f15c9f9a..6b47356e 100644 --- a/hahomematic/support.py +++ b/hahomematic/support.py @@ -247,8 +247,10 @@ def get_ip_addr(host: str, port: int) -> str | None: return local_ip -def is_valid_hostname(hostname: str) -> bool: +def is_valid_hostname(hostname: str | None) -> bool: """Return True if hostname is valid.""" + if not hostname: + return False if hostname[-1] == ".": # strip exactly one dot from the right, if present hostname = hostname[:-1] @@ -265,8 +267,10 @@ def is_valid_hostname(hostname: str) -> bool: return all(allowed.match(label) for label in labels) -def is_valid_ipv4_address(address: str) -> bool: +def is_valid_ipv4_address(address: str | None) -> bool: """Return True if ipv4_address is valid.""" + if not address: + return False try: IPv4Address(address=address) except ValueError: diff --git a/tests/test_support.py b/tests/test_support.py index 5fe4d869..ba3e717b 100644 --- a/tests/test_support.py +++ b/tests/test_support.py @@ -550,6 +550,8 @@ def test_converter( def test_is_valid_hostname() -> None: """Test is_valid_hostname.""" + assert is_valid_hostname(None) is False + assert is_valid_hostname("") is False assert is_valid_hostname(" ") is False assert is_valid_hostname("123") is False assert is_valid_hostname("ccu") is True @@ -562,6 +564,7 @@ def test_is_valid_hostname() -> None: def test_is_valid_ipv4_address() -> None: """Test is_valid_ipv4_address.""" + assert is_valid_ipv4_address(None) is False assert is_valid_ipv4_address("") is False assert is_valid_ipv4_address(" ") is False assert is_valid_ipv4_address("192.168.1782") is False