1 """Event for Shelly."""
3 from __future__
import annotations
5 from collections.abc
import Callable
6 from dataclasses
import dataclass
7 from typing
import TYPE_CHECKING, Any, Final
9 from aioshelly.block_device
import Block
10 from aioshelly.const
import MODEL_I3, RPC_GENERATIONS
13 DOMAIN
as EVENT_DOMAIN,
16 EventEntityDescription,
24 BASIC_INPUTS_EVENTS_TYPES,
25 RPC_INPUTS_EVENTS_TYPES,
26 SHIX3_1_INPUTS_EVENTS_TYPES,
28 from .coordinator
import ShellyBlockCoordinator, ShellyConfigEntry, ShellyRpcCoordinator
29 from .entity
import ShellyBlockEntity
31 async_remove_shelly_entity,
34 get_rpc_key_instances,
35 is_block_momentary_input,
36 is_rpc_momentary_input,
40 @dataclass(frozen=True, kw_only=True)
42 """Class to describe Shelly event."""
44 removal_condition: Callable[[dict, Block], bool] |
None =
None
47 @dataclass(frozen=True, kw_only=True)
49 """Class to describe Shelly event."""
51 removal_condition: Callable[[dict, dict, str], bool] |
None =
None
56 translation_key=
"input",
57 device_class=EventDeviceClass.BUTTON,
64 translation_key=
"input",
65 device_class=EventDeviceClass.BUTTON,
66 event_types=
list(RPC_INPUTS_EVENTS_TYPES),
75 config_entry: ShellyConfigEntry,
76 async_add_entities: AddEntitiesCallback,
78 """Set up sensors for device."""
79 entities: list[ShellyBlockEvent | ShellyRpcEvent] = []
81 coordinator: ShellyRpcCoordinator | ShellyBlockCoordinator |
None =
None
84 coordinator = config_entry.runtime_data.rpc
90 for key
in key_instances:
91 if RPC_EVENT.removal_condition
and RPC_EVENT.removal_condition(
92 coordinator.device.config, coordinator.device.status, key
94 unique_id = f
"{coordinator.mac}-{key}"
99 coordinator = config_entry.runtime_data.block
102 assert coordinator.device.blocks
104 for block
in coordinator.device.blocks:
106 "inputEvent" not in block.sensor_ids
107 or "inputEventCnt" not in block.sensor_ids
111 if BLOCK_EVENT.removal_condition
and BLOCK_EVENT.removal_condition(
112 coordinator.device.settings, block
114 channel =
int(block.channel
or 0) + 1
115 unique_id = f
"{coordinator.mac}-{block.description}-{channel}"
124 """Represent Block event entity."""
126 entity_description: ShellyBlockEventDescription
130 coordinator: ShellyBlockCoordinator,
132 description: ShellyBlockEventDescription,
134 """Initialize Shelly entity."""
135 super().
__init__(coordinator, block)
139 if coordinator.model == MODEL_I3:
146 """When entity is added to hass."""
154 """Handle the demo button event."""
155 if event[
"channel"] == self.
channelchannel:
161 """Represent RPC event entity."""
163 entity_description: ShellyRpcEventDescription
167 coordinator: ShellyRpcCoordinator,
169 description: ShellyRpcEventDescription,
171 """Initialize Shelly entity."""
175 connections={(CONNECTION_NETWORK_MAC, coordinator.mac)}
182 """When entity is added to hass."""
190 """Handle the demo button event."""
None _trigger_event(self, str event_type, dict[str, Any]|None event_attributes=None)
CALLBACK_TYPE async_subscribe_input_events(self, Callable[[dict[str, Any]], None] input_event_callback)
CALLBACK_TYPE async_subscribe_input_events(self, Callable[[dict[str, Any]], None] input_event_callback)
None async_added_to_hass(self)
None __init__(self, ShellyBlockCoordinator coordinator, Block block, ShellyBlockEventDescription description)
None _async_handle_event(self, dict[str, Any] event)
None _async_handle_event(self, dict[str, Any] event)
None async_added_to_hass(self)
None __init__(self, ShellyRpcCoordinator coordinator, str key, ShellyRpcEventDescription description)
None async_write_ha_state(self)
None async_on_remove(self, CALLBACK_TYPE func)
None async_setup_entry(HomeAssistant hass, ShellyConfigEntry config_entry, AddEntitiesCallback async_add_entities)
bool is_rpc_momentary_input(dict[str, Any] config, dict[str, Any] status, str key)
list[str] get_rpc_key_instances(dict[str, Any] keys_dict, str key)
bool is_block_momentary_input(dict[str, Any] settings, Block block, bool include_detached=False)
int get_device_entry_gen(ConfigEntry entry)
None async_remove_shelly_entity(HomeAssistant hass, str domain, str unique_id)
str get_rpc_entity_name(RpcDevice device, str key, str|None description=None)