From d790de1346a49fe7415c0a9068c796b0e94e4449 Mon Sep 17 00:00:00 2001 From: Bas Nijholt Date: Thu, 15 Oct 2020 23:20:23 +0200 Subject: [PATCH] Minor code improvement for KEF (#41885) --- homeassistant/components/kef/media_player.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/homeassistant/components/kef/media_player.py b/homeassistant/components/kef/media_player.py index 892bb0adac0..a36fe11ef65 100644 --- a/homeassistant/components/kef/media_player.py +++ b/homeassistant/components/kef/media_player.py @@ -85,6 +85,16 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend( ) +def get_ip_mode(host): + """Get the 'mode' used to retrieve the MAC address.""" + try: + if ipaddress.ip_address(host).version == 6: + return "ip6" + return "ip" + except ValueError: + return "hostname" + + async def async_setup_platform(hass, config, async_add_entities, discovery_info=None): """Set up the KEF platform.""" if DOMAIN not in hass.data: @@ -111,13 +121,7 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info= sources, ) - try: - if ipaddress.ip_address(host).version == 6: - mode = "ip6" - else: - mode = "ip" - except ValueError: - mode = "hostname" + mode = get_ip_mode(host) mac = await hass.async_add_executor_job(partial(get_mac_address, **{mode: host})) unique_id = f"kef-{mac}" if mac is not None else None