1
0
mirror of https://github.com/home-assistant/core.git synced 2026-05-19 23:10:15 +01:00
Files
2026-04-30 21:14:48 +02:00

64 lines
1.9 KiB
Python

"""Support for SwitchBot event entities."""
from homeassistant.components.event import (
EventDeviceClass,
EventEntity,
EventEntityDescription,
)
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from .coordinator import SwitchbotConfigEntry, SwitchbotDataUpdateCoordinator
from .entity import SwitchbotEntity
PARALLEL_UPDATES = 0
EVENT_TYPES = {
"doorbell": EventEntityDescription(
key="doorbell",
device_class=EventDeviceClass.DOORBELL,
event_types=["ring"],
),
}
async def async_setup_entry(
hass: HomeAssistant,
config_entry: SwitchbotConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
"""Set up the SwitchBot event platform."""
coordinator = config_entry.runtime_data
async_add_entities(
SwitchbotEventEntity(coordinator, event, description)
for event, description in EVENT_TYPES.items()
if event in coordinator.device.parsed_data
)
class SwitchbotEventEntity(SwitchbotEntity, EventEntity):
"""Representation of a SwitchBot event."""
def __init__(
self,
coordinator: SwitchbotDataUpdateCoordinator,
event: str,
description: EventEntityDescription,
) -> None:
"""Initialize the SwitchBot event."""
super().__init__(coordinator)
self._event = event
self.entity_description = description
self._attr_unique_id = f"{coordinator.base_unique_id}-{event}"
self._previous_doorbell_seq = int(
coordinator.device.parsed_data.get("doorbell_seq", 0)
)
@callback
def _async_update_attrs(self) -> None:
"""Update the entity attributes."""
seq = int(self.parsed_data.get("doorbell_seq", 0))
if seq not in (0, self._previous_doorbell_seq):
self._trigger_event("ring")
self._previous_doorbell_seq = seq