Register Alert services as entity services (#80213)

This commit is contained in:
Franck Nijhof 2022-10-13 09:04:24 +02:00 committed by GitHub
parent 466c4656ca
commit 1e75c3829e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -14,7 +14,6 @@ from homeassistant.components.notify import (
DOMAIN as DOMAIN_NOTIFY, DOMAIN as DOMAIN_NOTIFY,
) )
from homeassistant.const import ( from homeassistant.const import (
ATTR_ENTITY_ID,
CONF_ENTITY_ID, CONF_ENTITY_ID,
CONF_NAME, CONF_NAME,
CONF_REPEAT, CONF_REPEAT,
@ -26,10 +25,10 @@ from homeassistant.const import (
STATE_OFF, STATE_OFF,
STATE_ON, STATE_ON,
) )
from homeassistant.core import Event, HomeAssistant, ServiceCall from homeassistant.core import Event, HomeAssistant
from homeassistant.helpers import service
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity import Entity from homeassistant.helpers.entity import Entity
from homeassistant.helpers.entity_component import EntityComponent
from homeassistant.helpers.event import ( from homeassistant.helpers.event import (
async_track_point_in_time, async_track_point_in_time,
async_track_state_change_event, async_track_state_change_event,
@ -77,11 +76,11 @@ CONFIG_SCHEMA = vol.Schema(
{DOMAIN: cv.schema_with_slug_keys(ALERT_SCHEMA)}, extra=vol.ALLOW_EXTRA {DOMAIN: cv.schema_with_slug_keys(ALERT_SCHEMA)}, extra=vol.ALLOW_EXTRA
) )
ALERT_SERVICE_SCHEMA = vol.Schema({vol.Required(ATTR_ENTITY_ID): cv.entity_ids})
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Set up the Alert component.""" """Set up the Alert component."""
component = EntityComponent[Alert](LOGGER, DOMAIN, hass)
entities: list[Alert] = [] entities: list[Alert] = []
for object_id, cfg in config[DOMAIN].items(): for object_id, cfg in config[DOMAIN].items():
@ -121,45 +120,11 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
if not entities: if not entities:
return False return False
async def async_handle_alert_service(service_call: ServiceCall) -> None: component.async_register_entity_service(SERVICE_TURN_OFF, {}, "async_turn_off")
"""Handle calls to alert services.""" component.async_register_entity_service(SERVICE_TURN_ON, {}, "async_turn_on")
alert_ids = await service.async_extract_entity_ids(hass, service_call) component.async_register_entity_service(SERVICE_TOGGLE, {}, "async_toggle")
for alert_id in alert_ids: await component.async_add_entities(entities)
for alert in entities:
if alert.entity_id != alert_id:
continue
alert.async_set_context(service_call.context)
if service_call.service == SERVICE_TURN_ON:
await alert.async_turn_on()
elif service_call.service == SERVICE_TOGGLE:
await alert.async_toggle()
else:
await alert.async_turn_off()
# Setup service calls
hass.services.async_register(
DOMAIN,
SERVICE_TURN_OFF,
async_handle_alert_service,
schema=ALERT_SERVICE_SCHEMA,
)
hass.services.async_register(
DOMAIN,
SERVICE_TURN_ON,
async_handle_alert_service,
schema=ALERT_SERVICE_SCHEMA,
)
hass.services.async_register(
DOMAIN,
SERVICE_TOGGLE,
async_handle_alert_service,
schema=ALERT_SERVICE_SCHEMA,
)
for alert in entities:
alert.async_write_ha_state()
return True return True