guard against invalid trigger and action scenarios (#32512)

This commit is contained in:
David F. Mulcahey 2020-03-05 15:52:09 -05:00 committed by GitHub
parent ae0ea0f088
commit b5022f5bcb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 5 deletions

View file

@ -56,7 +56,10 @@ async def async_call_action_from_config(
async def async_get_actions(hass: HomeAssistant, device_id: str) -> List[dict]:
"""List device actions."""
zha_device = await async_get_zha_device(hass, device_id)
try:
zha_device = await async_get_zha_device(hass, device_id)
except (KeyError, AttributeError):
return []
cluster_channels = [
ch.name
for pool in zha_device.channels.pools
@ -81,7 +84,10 @@ async def _execute_service_based_action(
) -> None:
action_type = config[CONF_TYPE]
service_name = SERVICE_NAMES[action_type]
zha_device = await async_get_zha_device(hass, config[CONF_DEVICE_ID])
try:
zha_device = await async_get_zha_device(hass, config[CONF_DEVICE_ID])
except (KeyError, AttributeError):
return
service_data = {ATTR_IEEE: str(zha_device.ieee)}

View file

@ -27,7 +27,10 @@ async def async_validate_trigger_config(hass, config):
if "zha" in hass.config.components:
trigger = (config[CONF_TYPE], config[CONF_SUBTYPE])
zha_device = await async_get_zha_device(hass, config[CONF_DEVICE_ID])
try:
zha_device = await async_get_zha_device(hass, config[CONF_DEVICE_ID])
except (KeyError, AttributeError):
raise InvalidDeviceAutomationConfig
if (
zha_device.device_automation_triggers is None
or trigger not in zha_device.device_automation_triggers
@ -40,8 +43,10 @@ async def async_validate_trigger_config(hass, config):
async def async_attach_trigger(hass, config, action, automation_info):
"""Listen for state changes based on configuration."""
trigger = (config[CONF_TYPE], config[CONF_SUBTYPE])
zha_device = await async_get_zha_device(hass, config[CONF_DEVICE_ID])
try:
zha_device = await async_get_zha_device(hass, config[CONF_DEVICE_ID])
except (KeyError, AttributeError):
return None
trigger = zha_device.device_automation_triggers[trigger]
event_config = {