Kulersky cleanups (#43901)

pull/44175/head
Emily Mills 2020-12-03 11:08:16 -06:00 committed by Franck Nijhof
parent b174c1d4eb
commit eb6128b53e
No known key found for this signature in database
GPG Key ID: D62583BA8AB11CA3
3 changed files with 17 additions and 12 deletions

View File

@ -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
)

View File

@ -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)

View File

@ -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()