Doorbell Event is fired just once in homematicip_cloud (#144357)

* fire event if event type if correct

* Fix requested changes
This commit is contained in:
hahn-th 2025-05-14 11:44:59 +02:00 committed by GitHub
parent 27798a6004
commit 063deab3cb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 63 additions and 3 deletions

View File

@ -1,8 +1,11 @@
"""Support for HomematicIP Cloud events.""" """Support for HomematicIP Cloud events."""
from collections.abc import Callable
from dataclasses import dataclass from dataclasses import dataclass
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
from homematicip.base.channel_event import ChannelEvent
from homematicip.base.functionalChannels import FunctionalChannel
from homematicip.device import Device from homematicip.device import Device
from homeassistant.components.event import ( from homeassistant.components.event import (
@ -23,6 +26,9 @@ from .hap import HomematicipHAP
class HmipEventEntityDescription(EventEntityDescription): class HmipEventEntityDescription(EventEntityDescription):
"""Description of a HomematicIP Cloud event.""" """Description of a HomematicIP Cloud event."""
channel_event_types: list[str] | None = None
channel_selector_fn: Callable[[FunctionalChannel], bool] | None = None
EVENT_DESCRIPTIONS = { EVENT_DESCRIPTIONS = {
"doorbell": HmipEventEntityDescription( "doorbell": HmipEventEntityDescription(
@ -30,6 +36,8 @@ EVENT_DESCRIPTIONS = {
translation_key="doorbell", translation_key="doorbell",
device_class=EventDeviceClass.DOORBELL, device_class=EventDeviceClass.DOORBELL,
event_types=["ring"], event_types=["ring"],
channel_event_types=["DOOR_BELL_SENSOR_EVENT"],
channel_selector_fn=lambda channel: channel.channelRole == "DOOR_BELL_INPUT",
), ),
} }
@ -41,24 +49,29 @@ async def async_setup_entry(
) -> None: ) -> None:
"""Set up the HomematicIP cover from a config entry.""" """Set up the HomematicIP cover from a config entry."""
hap = hass.data[DOMAIN][config_entry.unique_id] hap = hass.data[DOMAIN][config_entry.unique_id]
entities: list[HomematicipGenericEntity] = []
async_add_entities( entities.extend(
HomematicipDoorBellEvent( HomematicipDoorBellEvent(
hap, hap,
device, device,
channel.index, channel.index,
EVENT_DESCRIPTIONS["doorbell"], description,
) )
for description in EVENT_DESCRIPTIONS.values()
for device in hap.home.devices for device in hap.home.devices
for channel in device.functionalChannels for channel in device.functionalChannels
if channel.channelRole == "DOOR_BELL_INPUT" if description.channel_selector_fn and description.channel_selector_fn(channel)
) )
async_add_entities(entities)
class HomematicipDoorBellEvent(HomematicipGenericEntity, EventEntity): class HomematicipDoorBellEvent(HomematicipGenericEntity, EventEntity):
"""Event class for HomematicIP doorbell events.""" """Event class for HomematicIP doorbell events."""
_attr_device_class = EventDeviceClass.DOORBELL _attr_device_class = EventDeviceClass.DOORBELL
entity_description: HmipEventEntityDescription
def __init__( def __init__(
self, self,
@ -86,9 +99,27 @@ class HomematicipDoorBellEvent(HomematicipGenericEntity, EventEntity):
@callback @callback
def _async_handle_event(self, *args, **kwargs) -> None: def _async_handle_event(self, *args, **kwargs) -> None:
"""Handle the event fired by the functional channel.""" """Handle the event fired by the functional channel."""
raised_channel_event = self._get_channel_event_from_args(*args)
if not self._should_raise(raised_channel_event):
return
event_types = self.entity_description.event_types event_types = self.entity_description.event_types
if TYPE_CHECKING: if TYPE_CHECKING:
assert event_types is not None assert event_types is not None
self._trigger_event(event_type=event_types[0]) self._trigger_event(event_type=event_types[0])
self.async_write_ha_state() self.async_write_ha_state()
def _should_raise(self, event_type: str) -> bool:
"""Check if the event should be raised."""
if self.entity_description.channel_event_types is None:
return False
return event_type in self.entity_description.channel_event_types
def _get_channel_event_from_args(self, *args) -> str:
"""Get the channel event."""
if isinstance(args[0], ChannelEvent):
return args[0].channelEventType
return ""

View File

@ -35,3 +35,32 @@ async def test_door_bell_event(
ha_state = hass.states.get(entity_id) ha_state = hass.states.get(entity_id)
assert ha_state.state != STATE_UNKNOWN assert ha_state.state != STATE_UNKNOWN
async def test_door_bell_event_wrong_event_type(
hass: HomeAssistant,
default_mock_hap_factory: HomeFactory,
) -> None:
"""Test of door bell event of HmIP-DSD-PCB."""
entity_id = "event.dsdpcb_klingel_doorbell"
entity_name = "dsdpcb_klingel doorbell"
device_model = "HmIP-DSD-PCB"
mock_hap = await default_mock_hap_factory.async_get_mock_hap(
test_devices=["dsdpcb_klingel"]
)
ha_state, hmip_device = get_and_check_entity_basics(
hass, mock_hap, entity_id, entity_name, device_model
)
ch = hmip_device.functionalChannels[1]
channel_event = ChannelEvent(
channelEventType="KEY_PRESS", channelIndex=1, deviceId=ch.device.id
)
assert ha_state.state == STATE_UNKNOWN
ch.fire_channel_event(channel_event)
ha_state = hass.states.get(entity_id)
assert ha_state.state == STATE_UNKNOWN