diff --git a/homeassistant/components/ifttt/alarm_control_panel.py b/homeassistant/components/ifttt/alarm_control_panel.py index bfecec261a7f..b46cda95dceb 100644 --- a/homeassistant/components/ifttt/alarm_control_panel.py +++ b/homeassistant/components/ifttt/alarm_control_panel.py @@ -12,19 +12,13 @@ from homeassistant.components.alarm_control_panel import ( AlarmControlPanelState, CodeFormat, ) -from homeassistant.const import ( - ATTR_ENTITY_ID, - ATTR_STATE, - CONF_CODE, - CONF_NAME, - CONF_OPTIMISTIC, -) -from homeassistant.core import HomeAssistant, ServiceCall +from homeassistant.const import CONF_CODE, CONF_NAME, CONF_OPTIMISTIC +from homeassistant.core import HomeAssistant from homeassistant.helpers import config_validation as cv from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType -from .const import ATTR_EVENT, DOMAIN, SERVICE_PUSH_ALARM_STATE, SERVICE_TRIGGER +from .const import ATTR_EVENT, DATA_IFTTT_ALARM, DOMAIN, SERVICE_TRIGGER _LOGGER = logging.getLogger(__name__) @@ -35,7 +29,6 @@ ALLOWED_STATES = [ AlarmControlPanelState.ARMED_HOME, ] -DATA_IFTTT_ALARM = "ifttt_alarm" DEFAULT_NAME = "Home" CONF_EVENT_AWAY = "event_arm_away" @@ -63,13 +56,6 @@ PLATFORM_SCHEMA = ALARM_CONTROL_PANEL_PLATFORM_SCHEMA.extend( } ) -PUSH_ALARM_STATE_SERVICE_SCHEMA = probatio.Schema( - { - probatio.Required(ATTR_ENTITY_ID): cv.entity_ids, - probatio.Required(ATTR_STATE): cv.string, - } -) - def setup_platform( hass: HomeAssistant, @@ -103,25 +89,6 @@ def setup_platform( hass.data[DATA_IFTTT_ALARM].append(alarmpanel) add_entities([alarmpanel]) - async def push_state_update(service: ServiceCall) -> None: - """Set the service state as device state attribute.""" - entity_ids = service.data.get(ATTR_ENTITY_ID) - state = service.data.get(ATTR_STATE) - devices = hass.data[DATA_IFTTT_ALARM] - if entity_ids: - devices = [d for d in devices if d.entity_id in entity_ids] - - for device in devices: - device.push_alarm_state(state) - device.async_schedule_update_ha_state() - - hass.services.register( - DOMAIN, - SERVICE_PUSH_ALARM_STATE, - push_state_update, - schema=PUSH_ALARM_STATE_SERVICE_SCHEMA, - ) - class IFTTTAlarmPanel(AlarmControlPanelEntity): """Representation of an alarm control panel controlled through IFTTT.""" diff --git a/homeassistant/components/ifttt/const.py b/homeassistant/components/ifttt/const.py index f81c6c178986..b58e83f2f079 100644 --- a/homeassistant/components/ifttt/const.py +++ b/homeassistant/components/ifttt/const.py @@ -14,3 +14,5 @@ ATTR_VALUE3 = "value3" SERVICE_PUSH_ALARM_STATE = "push_alarm_state" SERVICE_TRIGGER = "trigger" + +DATA_IFTTT_ALARM = "ifttt_alarm" diff --git a/homeassistant/components/ifttt/services.py b/homeassistant/components/ifttt/services.py index 8b518257686d..8b6de4bce720 100644 --- a/homeassistant/components/ifttt/services.py +++ b/homeassistant/components/ifttt/services.py @@ -7,6 +7,7 @@ import probatio import pyfttt import requests +from homeassistant.const import ATTR_ENTITY_ID, ATTR_STATE from homeassistant.core import HomeAssistant, ServiceCall, callback from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers import config_validation as cv @@ -18,7 +19,9 @@ from .const import ( ATTR_VALUE2, ATTR_VALUE3, DATA_API_KEYS, + DATA_IFTTT_ALARM, DOMAIN, + SERVICE_PUSH_ALARM_STATE, SERVICE_TRIGGER, ) @@ -36,6 +39,13 @@ SERVICE_TRIGGER_SCHEMA = probatio.Schema( } ) +PUSH_ALARM_STATE_SERVICE_SCHEMA = probatio.Schema( + { + probatio.Required(ATTR_ENTITY_ID): cv.entity_ids, + probatio.Required(ATTR_STATE): cv.string, + } +) + def _trigger_service(call: ServiceCall) -> None: """Handle IFTTT trigger service calls.""" @@ -65,9 +75,28 @@ def _trigger_service(call: ServiceCall) -> None: ) from err +async def _push_state_update(service: ServiceCall) -> None: + """Set the service state as device state attribute.""" + entity_ids = service.data.get(ATTR_ENTITY_ID) + state = service.data.get(ATTR_STATE) + devices = service.hass.data.get(DATA_IFTTT_ALARM, []) + if entity_ids: + devices = [d for d in devices if d.entity_id in entity_ids] + + for device in devices: + device.push_alarm_state(state) + device.async_schedule_update_ha_state() + + @callback def async_setup_services(hass: HomeAssistant) -> None: """Set up the services for the IFTTT integration.""" hass.services.async_register( DOMAIN, SERVICE_TRIGGER, _trigger_service, schema=SERVICE_TRIGGER_SCHEMA ) + hass.services.async_register( + DOMAIN, + SERVICE_PUSH_ALARM_STATE, + _push_state_update, + schema=PUSH_ALARM_STATE_SERVICE_SCHEMA, + )