Skip to content

Commit a113219

Browse files
authored
Refactor Shelly RPC event platform to use base class (home-assistant#157499)
1 parent 708b3dc commit a113219

File tree

1 file changed

+30
-29
lines changed

1 file changed

+30
-29
lines changed

homeassistant/components/shelly/event.py

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,14 @@
1818
)
1919
from homeassistant.core import HomeAssistant, callback
2020
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
21-
from homeassistant.helpers.update_coordinator import CoordinatorEntity
2221

2322
from .const import (
2423
BASIC_INPUTS_EVENTS_TYPES,
2524
RPC_INPUTS_EVENTS_TYPES,
2625
SHIX3_1_INPUTS_EVENTS_TYPES,
2726
)
2827
from .coordinator import ShellyBlockCoordinator, ShellyConfigEntry, ShellyRpcCoordinator
29-
from .entity import ShellyBlockEntity, get_entity_rpc_device_info
28+
from .entity import ShellyBlockEntity, ShellyRpcEntity
3029
from .utils import (
3130
async_remove_orphaned_entities,
3231
async_remove_shelly_entity,
@@ -136,7 +135,7 @@ def _async_setup_rpc_entry(
136135
async_add_entities: AddConfigEntryEntitiesCallback,
137136
) -> None:
138137
"""Set up entities for RPC device."""
139-
entities: list[ShellyRpcEvent] = []
138+
entities: list[ShellyRpcEvent | ShellyRpcScriptEvent] = []
140139

141140
coordinator = config_entry.runtime_data.rpc
142141
if TYPE_CHECKING:
@@ -162,7 +161,9 @@ def _async_setup_rpc_entry(
162161
continue
163162

164163
if script_events and (event_types := script_events[get_rpc_key_id(script)]):
165-
entities.append(ShellyRpcScriptEvent(coordinator, script, event_types))
164+
entities.append(
165+
ShellyRpcScriptEvent(coordinator, script, SCRIPT_EVENT, event_types)
166+
)
166167

167168
# If a script is removed, from the device configuration, we need to remove orphaned entities
168169
async_remove_orphaned_entities(
@@ -227,7 +228,7 @@ def _async_handle_event(self, event: dict[str, Any]) -> None:
227228
self.async_write_ha_state()
228229

229230

230-
class ShellyRpcEvent(CoordinatorEntity[ShellyRpcCoordinator], EventEntity):
231+
class ShellyRpcEvent(ShellyRpcEntity, EventEntity):
231232
"""Represent RPC event entity."""
232233

233234
_attr_has_entity_name = True
@@ -240,25 +241,19 @@ def __init__(
240241
description: ShellyRpcEventDescription,
241242
) -> None:
242243
"""Initialize Shelly entity."""
243-
super().__init__(coordinator)
244-
self._attr_device_info = get_entity_rpc_device_info(coordinator, key)
245-
self._attr_unique_id = f"{coordinator.mac}-{key}"
244+
super().__init__(coordinator, key)
246245
self.entity_description = description
247246

248-
if description.key == "input":
249-
_, component, component_id = get_rpc_key(key)
250-
if custom_name := get_rpc_custom_name(coordinator.device, key):
251-
self._attr_name = custom_name
252-
else:
253-
self._attr_translation_placeholders = {
254-
"input_number": component_id
255-
if get_rpc_number_of_channels(coordinator.device, component) > 1
256-
else ""
257-
}
258-
self.event_id = int(component_id)
259-
elif description.key == "script":
260-
self._attr_name = get_rpc_custom_name(coordinator.device, key)
261-
self.event_id = get_rpc_key_id(key)
247+
_, component, component_id = get_rpc_key(key)
248+
if custom_name := get_rpc_custom_name(coordinator.device, key):
249+
self._attr_name = custom_name
250+
else:
251+
self._attr_translation_placeholders = {
252+
"input_number": component_id
253+
if get_rpc_number_of_channels(coordinator.device, component) > 1
254+
else ""
255+
}
256+
self.event_id = int(component_id)
262257

263258
async def async_added_to_hass(self) -> None:
264259
"""When entity is added to hass."""
@@ -270,30 +265,36 @@ async def async_added_to_hass(self) -> None:
270265

271266
@callback
272267
def _async_handle_event(self, event: dict[str, Any]) -> None:
273-
"""Handle the demo button event."""
268+
"""Handle the event."""
274269
if event["id"] == self.event_id:
275270
self._trigger_event(event["event"])
276271
self.async_write_ha_state()
277272

278273

279-
class ShellyRpcScriptEvent(ShellyRpcEvent):
274+
class ShellyRpcScriptEvent(ShellyRpcEntity, EventEntity):
280275
"""Represent RPC script event entity."""
281276

277+
_attr_has_entity_name = True
278+
entity_description: ShellyRpcEventDescription
279+
282280
def __init__(
283281
self,
284282
coordinator: ShellyRpcCoordinator,
285283
key: str,
284+
description: ShellyRpcEventDescription,
286285
event_types: list[str],
287286
) -> None:
288287
"""Initialize Shelly script event entity."""
289-
super().__init__(coordinator, key, SCRIPT_EVENT)
290-
291-
self.component = key
288+
super().__init__(coordinator, key)
289+
self.entity_description = description
292290
self._attr_event_types = event_types
293291

292+
self._attr_name = get_rpc_custom_name(coordinator.device, key)
293+
self.event_id = get_rpc_key_id(key)
294+
294295
async def async_added_to_hass(self) -> None:
295296
"""When entity is added to hass."""
296-
await super(CoordinatorEntity, self).async_added_to_hass()
297+
await super().async_added_to_hass()
297298

298299
self.async_on_remove(
299300
self.coordinator.async_subscribe_events(self._async_handle_event)
@@ -302,7 +303,7 @@ async def async_added_to_hass(self) -> None:
302303
@callback
303304
def _async_handle_event(self, event: dict[str, Any]) -> None:
304305
"""Handle script event."""
305-
if event.get("component") == self.component:
306+
if event.get("component") == self.key:
306307
event_type = event.get("event")
307308
if event_type not in self.event_types:
308309
# This can happen if we didn't find this event type in the script

0 commit comments

Comments
 (0)