Skip to content

Commit

Permalink
guard against invalid trigger and action scenarios (home-assistant#32512
Browse files Browse the repository at this point in the history
)
  • Loading branch information
dmulcahey authored Mar 5, 2020
1 parent ae0ea0f commit b5022f5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
10 changes: 8 additions & 2 deletions homeassistant/components/zha/device_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)}

Expand Down
11 changes: 8 additions & 3 deletions homeassistant/components/zha/device_trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 = {
Expand Down

0 comments on commit b5022f5

Please sign in to comment.