From 54cb2d42afb5563fbc28110b2181d9deb96308ed Mon Sep 17 00:00:00 2001 From: Emily Mills Date: Thu, 3 Dec 2020 11:08:16 -0600 Subject: [PATCH] Kulersky cleanups (#43901) --- .../components/kulersky/config_flow.py | 2 +- homeassistant/components/kulersky/light.py | 21 ++++++++++++------- tests/components/kulersky/test_light.py | 6 +++--- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/homeassistant/components/kulersky/config_flow.py b/homeassistant/components/kulersky/config_flow.py index 2b22fcdbd31..04f7719b8e6 100644 --- a/homeassistant/components/kulersky/config_flow.py +++ b/homeassistant/components/kulersky/config_flow.py @@ -25,5 +25,5 @@ async def _async_has_devices(hass) -> bool: config_entry_flow.register_discovery_flow( - DOMAIN, "Kuler Sky", _async_has_devices, config_entries.CONN_CLASS_UNKNOWN + DOMAIN, "Kuler Sky", _async_has_devices, config_entries.CONN_CLASS_LOCAL_POLL ) diff --git a/homeassistant/components/kulersky/light.py b/homeassistant/components/kulersky/light.py index 4c17d1bcba3..71dd4a158ca 100644 --- a/homeassistant/components/kulersky/light.py +++ b/homeassistant/components/kulersky/light.py @@ -33,6 +33,12 @@ DISCOVERY_INTERVAL = timedelta(seconds=60) PARALLEL_UPDATES = 0 +def check_light(light: pykulersky.Light): + """Attempt to connect to this light and read the color.""" + light.connect() + light.get_color() + + async def async_setup_entry( hass: HomeAssistantType, config_entry: ConfigEntry, @@ -69,13 +75,12 @@ async def async_setup_entry( for device in new_devices: light = pykulersky.Light(device["address"], device["name"]) try: - # Attempt to connect to this light and read the color. If the - # connection fails, either this is not a Kuler Sky light, or - # it's bluetooth connection is currently locked by another - # device. If the vendor's app is connected to the light when - # home assistant tries to connect, this connection will fail. - await hass.async_add_executor_job(light.connect) - await hass.async_add_executor_job(light.get_color) + # If the connection fails, either this is not a Kuler Sky + # light, or it's bluetooth connection is currently locked + # by another device. If the vendor's app is connected to + # the light when home assistant tries to connect, this + # connection will fail. + await hass.async_add_executor_job(check_light, light) except pykulersky.PykulerskyException: continue # The light has successfully connected @@ -83,7 +88,7 @@ async def async_setup_entry( async_add_entities([KulerskyLight(light)], update_before_add=True) # Start initial discovery - hass.async_add_job(discover) + hass.async_create_task(discover()) # Perform recurring discovery of new devices async_track_time_interval(hass, discover, DISCOVERY_INTERVAL) diff --git a/tests/components/kulersky/test_light.py b/tests/components/kulersky/test_light.py index 1b2472d7d7f..5403f7cedde 100644 --- a/tests/components/kulersky/test_light.py +++ b/tests/components/kulersky/test_light.py @@ -56,11 +56,11 @@ async def mock_light(hass, mock_entry): ], ): with patch( - "homeassistant.components.kulersky.light.pykulersky.Light" - ) as mockdevice, patch.object(light, "connect") as mock_connect, patch.object( + "homeassistant.components.kulersky.light.pykulersky.Light", + return_value=light, + ), patch.object(light, "connect") as mock_connect, patch.object( light, "get_color", return_value=(0, 0, 0, 0) ): - mockdevice.return_value = light mock_entry.add_to_hass(hass) await hass.config_entries.async_setup(mock_entry.entry_id) await hass.async_block_till_done()