Fix zeroconf when location name has a period (#81022)

pull/81035/head
J. Nick Koston 2022-10-26 10:39:13 -05:00 committed by GitHub
parent 8645e47b07
commit a72e906ac1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 1 deletions

View File

@ -234,12 +234,20 @@ def _get_announced_addresses(
return address_list
def _filter_disallowed_characters(name: str) -> str:
"""Filter disallowed characters from a string.
. is a reversed character for zeroconf.
"""
return name.replace(".", " ")
async def _async_register_hass_zc_service(
hass: HomeAssistant, aio_zc: HaAsyncZeroconf, uuid: str
) -> None:
# Get instance UUID
valid_location_name = _truncate_location_name_to_valid(
hass.config.location_name or "Home"
_filter_disallowed_characters(hass.config.location_name or "Home")
)
params = {

View File

@ -1162,6 +1162,25 @@ async def test_no_name(hass, mock_async_zeroconf):
assert info.name == "Home._home-assistant._tcp.local."
async def test_setup_with_disallowed_characters_in_local_name(
hass, mock_async_zeroconf, caplog
):
"""Test we still setup with disallowed characters in the location name."""
with patch.object(hass.config_entries.flow, "async_init"), patch.object(
zeroconf, "HaAsyncServiceBrowser", side_effect=service_update_mock
), patch.object(
hass.config,
"location_name",
"My.House",
):
assert await async_setup_component(hass, zeroconf.DOMAIN, {zeroconf.DOMAIN: {}})
hass.bus.async_fire(EVENT_HOMEASSISTANT_START)
await hass.async_block_till_done()
calls = mock_async_zeroconf.async_register_service.mock_calls
assert calls[0][1][0].name == "My House._home-assistant._tcp.local."
async def test_start_with_frontend(hass, mock_async_zeroconf):
"""Test we start with the frontend."""
with patch("homeassistant.components.zeroconf.HaZeroconf"):