Implement support for start_up_on_off in ZHA (#70110)

* Implement support for start_up_on_off

fix discovery issues

remove cover change

* add tests
This commit is contained in:
David F. Mulcahey 2022-04-24 12:50:06 -04:00 committed by GitHub
parent 8a73381b56
commit 9b8d217b0e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 273 additions and 19 deletions

View file

@ -4,6 +4,7 @@ from __future__ import annotations
from enum import Enum
import functools
from zigpy.zcl.clusters.general import OnOff
from zigpy.zcl.clusters.security import IasWd
from homeassistant.components.select import SelectEntity
@ -15,12 +16,20 @@ from homeassistant.helpers.entity import EntityCategory
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .core import discovery
from .core.const import CHANNEL_IAS_WD, DATA_ZHA, SIGNAL_ADD_ENTITIES, Strobe
from .core.const import (
CHANNEL_IAS_WD,
CHANNEL_ON_OFF,
DATA_ZHA,
SIGNAL_ADD_ENTITIES,
Strobe,
)
from .core.registries import ZHA_ENTITIES
from .core.typing import ChannelType, ZhaDeviceType
from .entity import ZhaEntity
MULTI_MATCH = functools.partial(ZHA_ENTITIES.multipass_match, Platform.SELECT)
CONFIG_DIAGNOSTIC_MATCH = functools.partial(
ZHA_ENTITIES.config_diagnostic_match, Platform.SELECT
)
async def async_setup_entry(
@ -100,7 +109,7 @@ class ZHANonZCLSelectEntity(ZHAEnumSelectEntity):
return True
@MULTI_MATCH(channel_names=CHANNEL_IAS_WD)
@CONFIG_DIAGNOSTIC_MATCH(channel_names=CHANNEL_IAS_WD)
class ZHADefaultToneSelectEntity(
ZHANonZCLSelectEntity, id_suffix=IasWd.Warning.WarningMode.__name__
):
@ -109,7 +118,7 @@ class ZHADefaultToneSelectEntity(
_enum: Enum = IasWd.Warning.WarningMode
@MULTI_MATCH(channel_names=CHANNEL_IAS_WD)
@CONFIG_DIAGNOSTIC_MATCH(channel_names=CHANNEL_IAS_WD)
class ZHADefaultSirenLevelSelectEntity(
ZHANonZCLSelectEntity, id_suffix=IasWd.Warning.SirenLevel.__name__
):
@ -118,7 +127,7 @@ class ZHADefaultSirenLevelSelectEntity(
_enum: Enum = IasWd.Warning.SirenLevel
@MULTI_MATCH(channel_names=CHANNEL_IAS_WD)
@CONFIG_DIAGNOSTIC_MATCH(channel_names=CHANNEL_IAS_WD)
class ZHADefaultStrobeLevelSelectEntity(
ZHANonZCLSelectEntity, id_suffix=IasWd.StrobeLevel.__name__
):
@ -127,8 +136,72 @@ class ZHADefaultStrobeLevelSelectEntity(
_enum: Enum = IasWd.StrobeLevel
@MULTI_MATCH(channel_names=CHANNEL_IAS_WD)
@CONFIG_DIAGNOSTIC_MATCH(channel_names=CHANNEL_IAS_WD)
class ZHADefaultStrobeSelectEntity(ZHANonZCLSelectEntity, id_suffix=Strobe.__name__):
"""Representation of a ZHA default siren strobe select entity."""
_enum: Enum = Strobe
class ZCLEnumSelectEntity(ZhaEntity, SelectEntity):
"""Representation of a ZHA ZCL enum select entity."""
_select_attr: str
_attr_entity_category = EntityCategory.CONFIG
_enum: Enum
@classmethod
def create_entity(
cls,
unique_id: str,
zha_device: ZhaDeviceType,
channels: list[ChannelType],
**kwargs,
) -> ZhaEntity | None:
"""Entity Factory.
Return entity if it is a supported configuration, otherwise return None
"""
channel = channels[0]
if cls._select_attr in channel.cluster.unsupported_attributes:
return None
return cls(unique_id, zha_device, channels, **kwargs)
def __init__(
self,
unique_id: str,
zha_device: ZhaDeviceType,
channels: list[ChannelType],
**kwargs,
) -> None:
"""Init this select entity."""
self._attr_options = [entry.name.replace("_", " ") for entry in self._enum]
self._channel: ChannelType = channels[0]
super().__init__(unique_id, zha_device, channels, **kwargs)
@property
def current_option(self) -> str | None:
"""Return the selected entity option to represent the entity state."""
option = self._channel.cluster.get(self._select_attr)
if option is None:
return None
option = self._enum(option)
return option.name.replace("_", " ")
async def async_select_option(self, option: str | int) -> None:
"""Change the selected option."""
await self._channel.cluster.write_attributes(
{self._select_attr: self._enum[option.replace(" ", "_")]}
)
self.async_write_ha_state()
@CONFIG_DIAGNOSTIC_MATCH(channel_names=CHANNEL_ON_OFF)
class ZHAStartupOnOffSelectEntity(
ZCLEnumSelectEntity, id_suffix=OnOff.StartUpOnOff.__name__
):
"""Representation of a ZHA startup onoff select entity."""
_select_attr = "start_up_on_off"
_enum: Enum = OnOff.StartUpOnOff