|
| 1 | +"""Event platform for Telegram bot integration.""" |
| 2 | + |
| 3 | +from typing import Any |
| 4 | + |
| 5 | +from homeassistant.components.event import EventEntity, EventEntityDescription |
| 6 | +from homeassistant.core import HomeAssistant, callback |
| 7 | +from homeassistant.helpers.dispatcher import async_dispatcher_connect |
| 8 | +from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback |
| 9 | + |
| 10 | +from .bot import TelegramBotConfigEntry |
| 11 | +from .const import ( |
| 12 | + EVENT_TELEGRAM_ATTACHMENT, |
| 13 | + EVENT_TELEGRAM_CALLBACK, |
| 14 | + EVENT_TELEGRAM_COMMAND, |
| 15 | + EVENT_TELEGRAM_SENT, |
| 16 | + EVENT_TELEGRAM_TEXT, |
| 17 | + SIGNAL_UPDATE_EVENT, |
| 18 | +) |
| 19 | +from .entity import TelegramBotEntity |
| 20 | + |
| 21 | + |
| 22 | +async def async_setup_entry( |
| 23 | + hass: HomeAssistant, |
| 24 | + config_entry: TelegramBotConfigEntry, |
| 25 | + async_add_entities: AddConfigEntryEntitiesCallback, |
| 26 | +) -> None: |
| 27 | + """Set up the event platform.""" |
| 28 | + async_add_entities([TelegramBotEventEntity(config_entry)]) |
| 29 | + |
| 30 | + |
| 31 | +class TelegramBotEventEntity(TelegramBotEntity, EventEntity): |
| 32 | + """An event entity.""" |
| 33 | + |
| 34 | + _attr_event_types = [ |
| 35 | + EVENT_TELEGRAM_ATTACHMENT, |
| 36 | + EVENT_TELEGRAM_CALLBACK, |
| 37 | + EVENT_TELEGRAM_COMMAND, |
| 38 | + EVENT_TELEGRAM_TEXT, |
| 39 | + EVENT_TELEGRAM_SENT, |
| 40 | + ] |
| 41 | + |
| 42 | + def __init__( |
| 43 | + self, |
| 44 | + config_entry: TelegramBotConfigEntry, |
| 45 | + ) -> None: |
| 46 | + """Initialize the entity.""" |
| 47 | + |
| 48 | + super().__init__( |
| 49 | + config_entry, |
| 50 | + EventEntityDescription(key="update_event", translation_key="update_event"), |
| 51 | + ) |
| 52 | + |
| 53 | + async def async_added_to_hass(self) -> None: |
| 54 | + """Register callbacks.""" |
| 55 | + self.async_on_remove( |
| 56 | + async_dispatcher_connect( |
| 57 | + self.hass, |
| 58 | + SIGNAL_UPDATE_EVENT, |
| 59 | + self._async_handle_event, |
| 60 | + ) |
| 61 | + ) |
| 62 | + |
| 63 | + @callback |
| 64 | + def _async_handle_event(self, event_type: str, event_data: dict[str, Any]) -> None: |
| 65 | + """Handle the event.""" |
| 66 | + self._trigger_event(event_type, event_data) |
| 67 | + self.async_write_ha_state() |
0 commit comments