Kulersky cleanups (#43901)

This commit is contained in:
Emily Mills 2020-12-03 11:08:16 -06:00 committed by GitHub
parent 262e77d969
commit 54cb2d42af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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( 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 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( async def async_setup_entry(
hass: HomeAssistantType, hass: HomeAssistantType,
config_entry: ConfigEntry, config_entry: ConfigEntry,
@ -69,13 +75,12 @@ async def async_setup_entry(
for device in new_devices: for device in new_devices:
light = pykulersky.Light(device["address"], device["name"]) light = pykulersky.Light(device["address"], device["name"])
try: try:
# Attempt to connect to this light and read the color. If the # If the connection fails, either this is not a Kuler Sky
# connection fails, either this is not a Kuler Sky light, or # light, or it's bluetooth connection is currently locked
# it's bluetooth connection is currently locked by another # by another device. If the vendor's app is connected to
# device. If the vendor's app is connected to the light when # the light when home assistant tries to connect, this
# home assistant tries to connect, this connection will fail. # connection will fail.
await hass.async_add_executor_job(light.connect) await hass.async_add_executor_job(check_light, light)
await hass.async_add_executor_job(light.get_color)
except pykulersky.PykulerskyException: except pykulersky.PykulerskyException:
continue continue
# The light has successfully connected # The light has successfully connected
@ -83,7 +88,7 @@ async def async_setup_entry(
async_add_entities([KulerskyLight(light)], update_before_add=True) async_add_entities([KulerskyLight(light)], update_before_add=True)
# Start initial discovery # Start initial discovery
hass.async_add_job(discover) hass.async_create_task(discover())
# Perform recurring discovery of new devices # Perform recurring discovery of new devices
async_track_time_interval(hass, discover, DISCOVERY_INTERVAL) async_track_time_interval(hass, discover, DISCOVERY_INTERVAL)

View File

@ -56,11 +56,11 @@ async def mock_light(hass, mock_entry):
], ],
): ):
with patch( with patch(
"homeassistant.components.kulersky.light.pykulersky.Light" "homeassistant.components.kulersky.light.pykulersky.Light",
) as mockdevice, patch.object(light, "connect") as mock_connect, patch.object( return_value=light,
), patch.object(light, "connect") as mock_connect, patch.object(
light, "get_color", return_value=(0, 0, 0, 0) light, "get_color", return_value=(0, 0, 0, 0)
): ):
mockdevice.return_value = light
mock_entry.add_to_hass(hass) mock_entry.add_to_hass(hass)
await hass.config_entries.async_setup(mock_entry.entry_id) await hass.config_entries.async_setup(mock_entry.entry_id)
await hass.async_block_till_done() await hass.async_block_till_done()