From 1325b3825678bb92a055eac70e633d2b6f94f7f0 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 20 Aug 2021 16:33:23 -0500 Subject: [PATCH] Handle case where location_name is set to "" for zeroconf (#54880) --- homeassistant/components/zeroconf/__init__.py | 4 +++- tests/components/zeroconf/test_init.py | 15 ++++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/zeroconf/__init__.py b/homeassistant/components/zeroconf/__init__.py index 8b1f482e05e..17cfb9d05de 100644 --- a/homeassistant/components/zeroconf/__init__.py +++ b/homeassistant/components/zeroconf/__init__.py @@ -215,7 +215,9 @@ 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) + valid_location_name = _truncate_location_name_to_valid( + hass.config.location_name or "Home" + ) params = { "location_name": valid_location_name, diff --git a/tests/components/zeroconf/test_init.py b/tests/components/zeroconf/test_init.py index a284c91e4f4..d13bbc97547 100644 --- a/tests/components/zeroconf/test_init.py +++ b/tests/components/zeroconf/test_init.py @@ -39,7 +39,7 @@ def service_update_mock(ipv6, zeroconf, services, handlers, *, limit_service=Non handlers[0](zeroconf, service, f"_name.{service}", ServiceStateChange.Added) -def get_service_info_mock(service_type, name): +def get_service_info_mock(service_type, name, *args, **kwargs): """Return service info for get_service_info.""" return AsyncServiceInfo( service_type, @@ -872,3 +872,16 @@ async def test_async_detect_interfaces_explicitly_set_ipv6(hass, mock_async_zero interfaces=["192.168.1.5", "fe80::dead:beef:dead:beef"], ip_version=IPVersion.All, ) + + +async def test_no_name(hass, mock_async_zeroconf): + """Test fallback to Home for mDNS announcement if the name is missing.""" + hass.config.location_name = "" + with patch("homeassistant.components.zeroconf.HaZeroconf"): + assert await async_setup_component(hass, zeroconf.DOMAIN, {zeroconf.DOMAIN: {}}) + hass.bus.async_fire(EVENT_HOMEASSISTANT_START) + await hass.async_block_till_done() + + register_call = mock_async_zeroconf.async_register_service.mock_calls[-1] + info = register_call.args[0] + assert info.name == "Home._home-assistant._tcp.local."