Fix configuration_url for Shelly device using IPv6 (#121939)

Co-authored-by: Maciej Bieniek <478555+bieniu@users.noreply.github.com>
This commit is contained in:
Maciej Bieniek 2024-07-15 08:31:44 +02:00 committed by GitHub
parent 9f53d0ccd9
commit 50751574b4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 34 additions and 2 deletions

View file

@ -3,7 +3,7 @@
from __future__ import annotations
from datetime import datetime, timedelta
from ipaddress import IPv4Address
from ipaddress import IPv4Address, IPv6Address, ip_address
import re
from types import MappingProxyType
from typing import Any, cast
@ -486,6 +486,20 @@ def get_http_port(data: MappingProxyType[str, Any]) -> int:
return cast(int, data.get(CONF_PORT, DEFAULT_HTTP_PORT))
def get_host(host: str) -> str:
"""Get the device IP address or hostname."""
try:
ip_object = ip_address(host)
except ValueError:
# host contains hostname
return host
if isinstance(ip_object, IPv6Address):
return f"[{host}]"
return host
@callback
def async_remove_shelly_rpc_entities(
hass: HomeAssistant, domain: str, mac: str, keys: list[str]