Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new cases to is_valid* #1659

Merged
merged 1 commit into from
Aug 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions hahomematic/support.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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:
Expand Down
3 changes: 3 additions & 0 deletions tests/test_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down