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)
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)
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()
def _shutdown(event):
"""Clean up resources when shutting down."""

View File

@@ -124,8 +124,10 @@ def async_config_entry_by_device_id(hass, device_id):
"""Look up config entry by device id."""
current_entries = hass.config_entries.async_entries(DOMAIN)
for entry in current_entries:
if device_id in entry.data[CONF_DEVICES]:
if device_id in entry.data.get(CONF_DEVICES, []):
return entry
else:
_LOGGER.warning(f"Missing device configuration for device_id {device_id}")
return None