mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +00:00
Ensure that HomeKit names start and end with alphanumeric character (#126413)
This commit is contained in:
parent
d81e836b37
commit
0bf90d18ef
@ -114,6 +114,7 @@ _LOGGER = logging.getLogger(__name__)
|
|||||||
|
|
||||||
NUMBERS_ONLY_RE = re.compile(r"[^\d.]+")
|
NUMBERS_ONLY_RE = re.compile(r"[^\d.]+")
|
||||||
VERSION_RE = re.compile(r"([0-9]+)(\.[0-9]+)?(\.[0-9]+)?")
|
VERSION_RE = re.compile(r"([0-9]+)(\.[0-9]+)?(\.[0-9]+)?")
|
||||||
|
INVALID_END_CHARS = "-_"
|
||||||
MAX_VERSION_PART = 2**32 - 1
|
MAX_VERSION_PART = 2**32 - 1
|
||||||
|
|
||||||
|
|
||||||
@ -414,7 +415,11 @@ def cleanup_name_for_homekit(name: str | None) -> str:
|
|||||||
# likely isn't a problem
|
# likely isn't a problem
|
||||||
if name is None:
|
if name is None:
|
||||||
return "None" # None crashes apple watches
|
return "None" # None crashes apple watches
|
||||||
return name.translate(HOMEKIT_CHAR_TRANSLATIONS)[:MAX_NAME_LENGTH]
|
return (
|
||||||
|
name.translate(HOMEKIT_CHAR_TRANSLATIONS)
|
||||||
|
.lstrip(INVALID_END_CHARS)[:MAX_NAME_LENGTH]
|
||||||
|
.rstrip(INVALID_END_CHARS)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def temperature_to_homekit(temperature: float, unit: str) -> float:
|
def temperature_to_homekit(temperature: float, unit: str) -> float:
|
||||||
|
@ -655,7 +655,7 @@ async def test_bad_name(hass: HomeAssistant, hk_driver) -> None:
|
|||||||
assert acc.category == 10 # Sensor
|
assert acc.category == 10 # Sensor
|
||||||
|
|
||||||
assert acc.char_humidity.value == 20
|
assert acc.char_humidity.value == 20
|
||||||
assert acc.display_name == "--Humid--"
|
assert acc.display_name == "Humid"
|
||||||
|
|
||||||
|
|
||||||
async def test_empty_name(hass: HomeAssistant, hk_driver) -> None:
|
async def test_empty_name(hass: HomeAssistant, hk_driver) -> None:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user