added dict get() instead of assuming keys exist; now handles config_flow setup failures without crashing

This commit is contained in:
hello@theauthtoken.com
2022-10-09 19:54:31 -07:00
committed by rospogrigio
parent 7b37f07fd0
commit d2fa4dc2a0
2 changed files with 11 additions and 8 deletions

View File

@@ -139,15 +139,16 @@ async def async_setup(hass: HomeAssistant, config: dict):
)
new_data[ATTR_UPDATED_AT] = str(int(time.time() * 1000))
hass.config_entries.async_update_entry(entry, data=new_data)
device = hass.data[DOMAIN][TUYA_DEVICES][device_id]
if not device.connected:
device.async_connect()
elif device_id in hass.data[DOMAIN][TUYA_DEVICES]:
# _LOGGER.debug("Device %s found with IP %s", device_id, device_ip)
_LOGGER.debug("Device %s found with IP %s", device_id, device_ip)
device = hass.data[DOMAIN][TUYA_DEVICES].get(device_id)
if not device:
_LOGGER.warning(f"Could not find device for device_id {device_id}")
elif not device.connected:
device.async_connect()
device = hass.data[DOMAIN][TUYA_DEVICES][device_id]
if not device.connected:
device.async_connect()
def _shutdown(event):
"""Clean up resources when shutting down."""