mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 03:07:37 +00:00
Register Alert services as entity services (#80213)
This commit is contained in:
parent
466c4656ca
commit
1e75c3829e
@ -14,7 +14,6 @@ from homeassistant.components.notify import (
|
||||
DOMAIN as DOMAIN_NOTIFY,
|
||||
)
|
||||
from homeassistant.const import (
|
||||
ATTR_ENTITY_ID,
|
||||
CONF_ENTITY_ID,
|
||||
CONF_NAME,
|
||||
CONF_REPEAT,
|
||||
@ -26,10 +25,10 @@ from homeassistant.const import (
|
||||
STATE_OFF,
|
||||
STATE_ON,
|
||||
)
|
||||
from homeassistant.core import Event, HomeAssistant, ServiceCall
|
||||
from homeassistant.helpers import service
|
||||
from homeassistant.core import Event, HomeAssistant
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers.entity import Entity
|
||||
from homeassistant.helpers.entity_component import EntityComponent
|
||||
from homeassistant.helpers.event import (
|
||||
async_track_point_in_time,
|
||||
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
|
||||
)
|
||||
|
||||
ALERT_SERVICE_SCHEMA = vol.Schema({vol.Required(ATTR_ENTITY_ID): cv.entity_ids})
|
||||
|
||||
|
||||
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
||||
"""Set up the Alert component."""
|
||||
component = EntityComponent[Alert](LOGGER, DOMAIN, hass)
|
||||
|
||||
entities: list[Alert] = []
|
||||
|
||||
for object_id, cfg in config[DOMAIN].items():
|
||||
@ -121,45 +120,11 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
||||
if not entities:
|
||||
return False
|
||||
|
||||
async def async_handle_alert_service(service_call: ServiceCall) -> None:
|
||||
"""Handle calls to alert services."""
|
||||
alert_ids = await service.async_extract_entity_ids(hass, service_call)
|
||||
component.async_register_entity_service(SERVICE_TURN_OFF, {}, "async_turn_off")
|
||||
component.async_register_entity_service(SERVICE_TURN_ON, {}, "async_turn_on")
|
||||
component.async_register_entity_service(SERVICE_TOGGLE, {}, "async_toggle")
|
||||
|
||||
for alert_id in alert_ids:
|
||||
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()
|
||||
await component.async_add_entities(entities)
|
||||
|
||||
return True
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user