Improve mac_address_from_name() function to avoid double discovery of Shelly devices (#153343)

This commit is contained in:
Maciej Bieniek
2025-10-01 16:49:27 +02:00
committed by GitHub
parent d76e947021
commit fd8ccb8d8f
2 changed files with 24 additions and 2 deletions

View File

@@ -552,8 +552,15 @@ def percentage_to_brightness(percentage: int) -> int:
def mac_address_from_name(name: str) -> str | None:
"""Convert a name to a mac address."""
mac = name.partition(".")[0].partition("-")[-1]
return mac.upper() if len(mac) == 12 else None
base = name.split(".", 1)[0]
if "-" not in base:
return None
mac = base.rsplit("-", 1)[-1]
if len(mac) != 12 or not all(char in "0123456789abcdefABCDEF" for char in mac):
return None
return mac.upper()
def get_release_url(gen: int, model: str, beta: bool) -> str | None:

View File

@@ -34,6 +34,7 @@ from homeassistant.components.shelly.utils import (
get_rpc_channel_name,
get_rpc_input_triggers,
is_block_momentary_input,
mac_address_from_name,
)
from homeassistant.util import dt as dt_util
@@ -327,3 +328,17 @@ def test_get_release_url(
def test_get_host(host: str, expected: str) -> None:
"""Test get_host function."""
assert get_host(host) == expected
@pytest.mark.parametrize(
("name", "result"),
[
("shelly1pm-AABBCCDDEEFF", "AABBCCDDEEFF"),
("Shelly Plus 1 [DDEEFF]", None),
("S11-Schlafzimmer", None),
("22-Kueche-links", None),
],
)
def test_mac_address_from_name(name: str, result: str | None) -> None:
"""Test mac_address_from_name() function."""
assert mac_address_from_name(name) == result