Code styling tweaks to the zeroconf integration (#86048)

pull/86061/head^2
Franck Nijhof 2023-01-17 05:35:23 +01:00 committed by GitHub
parent 2ed6df9003
commit 4aba3fdad6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 17 deletions

View File

@ -141,7 +141,7 @@ async def _async_get_instance(hass: HomeAssistant, **zcargs: Any) -> HaAsyncZero
@callback
def _async_zc_has_functional_dual_stack() -> bool:
"""Return true for platforms that not support IP_ADD_MEMBERSHIP on an AF_INET6 socket.
"""Return true for platforms not supporting IP_ADD_MEMBERSHIP on an AF_INET6 socket.
Zeroconf only supports a single listen socket at this time.
"""
@ -275,7 +275,8 @@ async def _async_register_hass_zc_service(
adapters = await network.async_get_adapters(hass)
# Puts the default IPv4 address first in the list to preserve compatibility,
# because some mDNS implementations ignores anything but the first announced address.
# because some mDNS implementations ignores anything but the first announced
# address.
host_ip = await async_get_source_ip(hass, target_ip=MDNS_TARGET_IP)
host_ip_pton = None
if host_ip:
@ -429,15 +430,17 @@ class ZeroconfDiscovery:
integration: Integration = await async_get_integration(
self.hass, domain
)
# Since we prefer local control, if the integration that is being discovered
# is cloud AND the homekit device is UNPAIRED we still want to discovery it.
# Since we prefer local control, if the integration that is being
# discovered is cloud AND the homekit device is UNPAIRED we still
# want to discovery it.
#
# Additionally if the integration is polling, HKC offers a local push
# experience for the user to control the device so we want to offer that
# as well.
# Additionally if the integration is polling, HKC offers a local
# push experience for the user to control the device so we want
# to offer that as well.
#
# As soon as the device becomes paired, the config flow will be dismissed
# in the event the user does not want to pair with Home Assistant.
# As soon as the device becomes paired, the config flow will be
# dismissed in the event the user does not want to pair
# with Home Assistant.
#
if not integration.iot_class or (
not integration.iot_class.startswith("cloud")
@ -468,7 +471,8 @@ class ZeroconfDiscovery:
"source": config_entries.SOURCE_ZEROCONF,
}
if domain:
# Domain of integration that offers alternative API to handle this device.
# Domain of integration that offers alternative API to handle
# this device.
context["alternative_domain"] = domain
discovery_flow.async_create_flow(

View File

@ -10,7 +10,10 @@ from .models import HaZeroconf
def install_multiple_zeroconf_catcher(hass_zc: HaZeroconf) -> None:
"""Wrap the Zeroconf class to return the shared instance if multiple instances are detected."""
"""Wrap the Zeroconf class to return the shared instance.
Only if if multiple instances are detected.
"""
def new_zeroconf_new(self: zeroconf.Zeroconf, *k: Any, **kw: Any) -> HaZeroconf:
report(

View File

@ -192,11 +192,26 @@ async def test_setup_with_overly_long_url_and_name(hass, mock_async_zeroconf, ca
zeroconf, "HaAsyncServiceBrowser", side_effect=service_update_mock
), patch(
"homeassistant.components.zeroconf.get_url",
return_value="https://this.url.is.way.too.long/very/deep/path/that/will/make/us/go/over/the/maximum/string/length/and/would/cause/zeroconf/to/fail/to/startup/because/the/key/and/value/can/only/be/255/bytes/and/this/string/is/a/bit/longer/than/the/maximum/length/that/we/allow/for/a/value",
return_value=(
"https://this.url.is.way.too.long/very/deep/path/that/will/make/us/go/over"
"/the/maximum/string/length/and/would/cause/zeroconf/to/fail/to/startup"
"/because/the/key/and/value/can/only/be/255/bytes/and/this/string/is/a"
"/bit/longer/than/the/maximum/length/that/we/allow/for/a/value"
),
), patch.object(
hass.config,
"location_name",
"\u00dcBER \u00dcber German Umlaut long string long string long string long string long string long string long string long string long string long string long string long string long string long string long string long string long string long string long string long string long string long string long string long string long string long string long string long string long string long string long string long string long string long string long string long string long string long string long string long string long string long string long string long string long string long string long string long string long string long string",
(
"\u00dcBER \u00dcber German Umlaut long string long string long string long"
" string long string long string long string long string long string long"
" string long string long string long string long string long string long"
" string long string long string long string long string long string long"
" string long string long string long string long string long string long"
" string long string long string long string long string long string long"
" string long string long string long string long string long string long"
" string long string long string long string long string long string long"
" string long string long string long string long string"
),
), patch(
"homeassistant.components.zeroconf.AsyncServiceInfo.request",
):
@ -717,7 +732,9 @@ async def test_homekit_not_paired(hass, mock_async_zeroconf):
async def test_homekit_controller_still_discovered_unpaired_for_cloud(
hass, mock_async_zeroconf
):
"""Test discovery is still passed to homekit controller when unpaired and discovered by cloud integration.
"""Test discovery is still passed to homekit controller when unpaired.
When unpaired and discovered by cloud integration.
Since we prefer local control, if the integration that is being discovered
is cloud AND the homekit device is unpaired we still want to discovery it
@ -751,7 +768,9 @@ async def test_homekit_controller_still_discovered_unpaired_for_cloud(
async def test_homekit_controller_still_discovered_unpaired_for_polling(
hass, mock_async_zeroconf
):
"""Test discovery is still passed to homekit controller when unpaired and discovered by polling integration.
"""Test discovery is still passed to homekit controller when unpaired.
When unpaired and discovered by polling integration.
Since we prefer local push, if the integration that is being discovered
is polling AND the homekit device is unpaired we still want to discovery it
@ -938,7 +957,7 @@ _ADAPTER_WITH_DEFAULT_ENABLED = [
async def test_async_detect_interfaces_setting_non_loopback_route(
hass, mock_async_zeroconf
):
"""Test without default interface config and the route returns a non-loopback address."""
"""Test without default interface and the route returns a non-loopback address."""
with patch("homeassistant.components.zeroconf.HaZeroconf") as mock_zc, patch.object(
hass.config_entries.flow, "async_init"
), patch.object(
@ -1052,7 +1071,7 @@ async def test_async_detect_interfaces_setting_empty_route_linux(
async def test_async_detect_interfaces_setting_empty_route_freebsd(
hass, mock_async_zeroconf
):
"""Test without default interface config and the route returns nothing on freebsd."""
"""Test without default interface and the route returns nothing on freebsd."""
with patch("homeassistant.components.zeroconf.sys.platform", "freebsd"), patch(
"homeassistant.components.zeroconf.HaZeroconf"
) as mock_zc, patch.object(hass.config_entries.flow, "async_init"), patch.object(