8000 Doorbell Event is fired just once in homematicip_cloud by hahn-th · Pull Request #144357 · home-assistant/core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Doorbell Event is fired just once in homematicip_cloud #144357

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 34 additions & 3 deletions homeassistant/components/homematicip_cloud/event.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
"""Support for HomematicIP Cloud events."""

from collections.abc import Callable
from dataclasses import dataclass
from typing import TYPE_CHECKING

from homematicip.base.channel_event import ChannelEvent
from homematicip.base.functionalChannels import FunctionalChannel
from homematicip.device import Device

from homeassistant.components.event import (
Expand All @@ -23,13 +26,18 @@
class HmipEventEntityDescription(EventEntityDescription):
"""Description of a HomematicIP Cloud event."""

channel_event_types: list[str] | None = None
channel_selector_fn: Callable[[FunctionalChannel], bool] | None = None


EVENT_DESCRIPTIONS = {
"doorbell": HmipEventEntityDescription(
key="doorbell",
translation_key="doorbell",
device_class=EventDeviceClass.DOORBELL,
event_types=["ring"],
channel_event_types=["DOOR_BELL_SENSOR_EVENT"],
channel_selector_fn=lambda channel: channel.channelRole == "DOOR_BELL_INPUT",
),
}

Expand All @@ -41,24 +49,29 @@
) -> None:
"""Set up the HomematicIP cover from a config entry."""
hap = hass.data[DOMAIN][config_entry.unique_id]
entities: list[HomematicipGenericEntity] = []

async_add_entities(
entities.extend(
HomematicipDoorBellEvent(
hap,
device,
channel.index,
EVENT_DESCRIPTIONS["doorbell"],
description,
)
for description in EVENT_DESCRIPTIONS.values()
for device in hap.home.devices
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):
"""Event class for HomematicIP doorbell events."""

_attr_device_class = EventDeviceClass.DOORBELL
entity_description: HmipEventEntityDescription

def __init__(
self,
Expand Down Expand Up @@ -86,9 +99,27 @@
@callback
def _async_handle_event(self, *args, **kwargs) -> None:
"""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
if TYPE_CHECKING:
assert event_types is not None

self._trigger_event(event_type=event_types[0])
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

Check warning on line 117 in homeassistant/components/homematicip_cloud/event.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/homematicip_cloud/event.py#L117

Added line #L117 was not covered by tests
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 ""

Check warning on line 125 in homeassistant/components/homematicip_cloud/event.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/homematicip_cloud/event.py#L125

Added line #L125 was not covered by tests
29 changes: 29 additions & 0 deletions tests/components/homematicip_cloud/test_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,32 @@ async def test_door_bell_event(

ha_state = hass.states.get(entity_id)
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
Loading
0