Skip to content

RFC: Event platform #419

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

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions zha/application/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
Platform.CLIMATE,
Platform.COVER,
Platform.DEVICE_TRACKER,
Platform.EVENT,
Platform.FAN,
Platform.LIGHT,
Platform.LOCK,
Expand Down
65 changes: 65 additions & 0 deletions zha/application/platforms/event/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
"""Support for the ZHA event platform."""

from __future__ import annotations

Check warning on line 3 in zha/application/platforms/event/__init__.py

View check run for this annotation

Codecov / codecov/patch

zha/application/platforms/event/__init__.py#L3

Added line #L3 was not covered by tests

import dataclasses
import functools
import logging
from typing import Any, Final

Check warning on line 8 in zha/application/platforms/event/__init__.py

View check run for this annotation

Codecov / codecov/patch

zha/application/platforms/event/__init__.py#L5-L8

Added lines #L5 - L8 were not covered by tests

from zigpy.quirks.v2 import EventMetadata

Check warning on line 10 in zha/application/platforms/event/__init__.py

View check run for this annotation

Codecov / codecov/patch

zha/application/platforms/event/__init__.py#L10

Added line #L10 was not covered by tests

from zha.application import Platform
from zha.application.platforms import PlatformEntity
from zha.application.platforms.helpers import validate_device_class

Check warning on line 14 in zha/application/platforms/event/__init__.py

View check run for this annotation

Codecov / codecov/patch

zha/application/platforms/event/__init__.py#L12-L14

Added lines #L12 - L14 were not covered by tests

from .const import EventDeviceClass

Check warning on line 16 in zha/application/platforms/event/__init__.py

View check run for this annotation

Codecov / codecov/patch

zha/application/platforms/event/__init__.py#L16

Added line #L16 was not covered by tests

_LOGGER = logging.getLogger(__name__)

Check warning on line 18 in zha/application/platforms/event/__init__.py

View check run for this annotation

Codecov / codecov/patch

zha/application/platforms/event/__init__.py#L18

Added line #L18 was not covered by tests


@dataclasses.dataclass(frozen=True, kw_only=True)
class EventPlatformEvent:

Check warning on line 22 in zha/application/platforms/event/__init__.py

View check run for this annotation

Codecov / codecov/patch

zha/application/platforms/event/__init__.py#L21-L22

Added lines #L21 - L22 were not covered by tests
"""Event for the event platform trigger."""

event: Final[str] = "trigger_event"
event_type: str
event_attributes: dict[str, Any] | None

Check warning on line 27 in zha/application/platforms/event/__init__.py

View check run for this annotation

Codecov / codecov/patch

zha/application/platforms/event/__init__.py#L25-L27

Added lines #L25 - L27 were not covered by tests


class EventEntity(PlatformEntity):

Check warning on line 30 in zha/application/platforms/event/__init__.py

View check run for this annotation

Codecov / codecov/patch

zha/application/platforms/event/__init__.py#L30

Added line #L30 was not covered by tests
"""Represent an event entity."""

PLATFORM = Platform.EVENT
_attr_event_types: tuple[str] = ()

Check warning on line 34 in zha/application/platforms/event/__init__.py

View check run for this annotation

Codecov / codecov/patch

zha/application/platforms/event/__init__.py#L33-L34

Added lines #L33 - L34 were not covered by tests

def _init_from_quirks_metadata(self, entity_metadata: EventMetadata) -> None:

Check warning on line 36 in zha/application/platforms/event/__init__.py

View check run for this annotation

Codecov / codecov/patch

zha/application/platforms/event/__init__.py#L36

Added line #L36 was not covered by tests
"""Init this entity from the quirks metadata."""
super()._init_from_quirks_metadata(entity_metadata)

Check warning on line 38 in zha/application/platforms/event/__init__.py

View check run for this annotation

Codecov / codecov/patch

zha/application/platforms/event/__init__.py#L38

Added line #L38 was not covered by tests

self._attr_event_types = entity_metadata.event_types

Check warning on line 40 in zha/application/platforms/event/__init__.py

View check run for this annotation

Codecov / codecov/patch

zha/application/platforms/event/__init__.py#L40

Added line #L40 was not covered by tests

if entity_metadata.device_class is not None:
self._attr_device_class = validate_device_class(

Check warning on line 43 in zha/application/platforms/event/__init__.py

View check run for this annotation

Codecov / codecov/patch

zha/application/platforms/event/__init__.py#L42-L43

Added lines #L42 - L43 were not covered by tests
EventDeviceClass,
entity_metadata.device_class,
Platform.EVENT.value,
_LOGGER,
)

@functools.cached_property
def event_types(self) -> list[str]:

Check warning on line 51 in zha/application/platforms/event/__init__.py

View check run for this annotation

Codecov / codecov/patch

zha/application/platforms/event/__init__.py#L50-L51

Added lines #L50 - L51 were not covered by tests
"""Return a list of possible events."""
return list(self._attr_event_types)

Check warning on line 53 in zha/application/platforms/event/__init__.py

View check run for this annotation

Codecov / codecov/patch

zha/application/platforms/event/__init__.py#L53

Added line #L53 was not covered by tests

def trigger_event(

Check warning on line 55 in zha/application/platforms/event/__init__.py

View check run for this annotation

Codecov / codecov/patch

zha/application/platforms/event/__init__.py#L55

Added line #L55 was not covered by tests
self, event_type: str, event_attributes: dict[str, Any] | None = None
) -> None:
"""Trigger an event."""
self.emit(

Check warning on line 59 in zha/application/platforms/event/__init__.py

View check run for this annotation

Codecov / codecov/patch

zha/application/platforms/event/__init__.py#L59

Added line #L59 was not covered by tests
EventPlatformEvent.event_type,
EventPlatformEvent(
event_type=event_type,
event_attributes=event_attributes,
),
)
11 changes: 11 additions & 0 deletions zha/application/platforms/event/const.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"""Constants for the ZHA event platform."""

from enum import StrEnum

Check warning on line 3 in zha/application/platforms/event/const.py

View check run for this annotation

Codecov / codecov/patch

zha/application/platforms/event/const.py#L3

Added line #L3 was not covered by tests


class EventDeviceClass(StrEnum):

Check warning on line 6 in zha/application/platforms/event/const.py

View check run for this annotation

Codecov / codecov/patch

zha/application/platforms/event/const.py#L6

Added line #L6 was not covered by tests
"""Device class for event entities."""

DOORBELL = "doorbell"
BUTTON = "button"
MOTION = "motion"

Check warning on line 11 in zha/application/platforms/event/const.py

View check run for this annotation

Codecov / codecov/patch

zha/application/platforms/event/const.py#L9-L11

Added lines #L9 - L11 were not covered by tests
19 changes: 18 additions & 1 deletion zha/application/platforms/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

if TYPE_CHECKING:
from zha.application.platforms.binary_sensor.const import BinarySensorDeviceClass
from zha.application.platforms.event.const import EventDeviceClass
from zha.application.platforms.number.const import NumberDeviceClass
from zha.application.platforms.sensor.const import SensorDeviceClass

Expand Down Expand Up @@ -78,16 +79,32 @@ def validate_device_class(
) -> NumberDeviceClass | None: ...


@overload
def validate_device_class(
device_class_enum: type[EventDeviceClass],
metadata_value: enum.Enum,
platform: str,
logger: logging.Logger,
) -> EventDeviceClass | None: ...


def validate_device_class(
device_class_enum: (
type[BinarySensorDeviceClass]
| type[SensorDeviceClass]
| type[NumberDeviceClass]
| type[EventDeviceClass]
),
metadata_value: enum.Enum,
platform: str,
logger: logging.Logger,
) -> BinarySensorDeviceClass | SensorDeviceClass | NumberDeviceClass | None:
) -> (
BinarySensorDeviceClass
| SensorDeviceClass
| NumberDeviceClass
| EventDeviceClass
| None
):
"""Validate and return a device class."""
try:
return device_class_enum(metadata_value.value)
Expand Down
Loading