Add Aqara c1 pet feeder support to ZHA (#82340)

* Add Aqara c1 pet feeder support to ZHA

* clean up

* cleanup

* state classes for daily measurements

* cleanups

* cleanups

* restore the refreshing of the inverted value cache

* cleanup
This commit is contained in:
David F. Mulcahey 2022-11-21 18:03:17 -05:00 committed by GitHub
parent d47fe35a88
commit 5329a679bb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 165 additions and 10 deletions

View file

@ -174,7 +174,8 @@ class ZHASwitchConfigurationEntity(ZhaEntity, SwitchEntity):
_attr_entity_category = EntityCategory.CONFIG
_zcl_attribute: str
_zcl_inverter_attribute: str = ""
_zcl_inverter_attribute: str | None = None
_force_inverted: bool = False
@classmethod
def create_entity(
@ -225,19 +226,24 @@ class ZHASwitchConfigurationEntity(ZhaEntity, SwitchEntity):
"""Handle state update from channel."""
self.async_write_ha_state()
@property
def inverted(self) -> bool:
"""Return True if the switch is inverted."""
if self._zcl_inverter_attribute:
return bool(self._channel.cluster.get(self._zcl_inverter_attribute))
return self._force_inverted
@property
def is_on(self) -> bool:
"""Return if the switch is on based on the statemachine."""
val = bool(self._channel.cluster.get(self._zcl_attribute))
invert = bool(self._channel.cluster.get(self._zcl_inverter_attribute))
return (not val) if invert else val
return (not val) if self.inverted else val
async def async_turn_on_off(self, state: bool) -> None:
"""Turn the entity on or off."""
try:
invert = bool(self._channel.cluster.get(self._zcl_inverter_attribute))
result = await self._channel.cluster.write_attributes(
{self._zcl_attribute: not state if invert else state}
{self._zcl_attribute: not state if self.inverted else state}
)
except zigpy.exceptions.ZigbeeException as ex:
self.error("Could not set value: %s", ex)
@ -258,15 +264,15 @@ class ZHASwitchConfigurationEntity(ZhaEntity, SwitchEntity):
async def async_update(self) -> None:
"""Attempt to retrieve the state of the entity."""
await super().async_update()
_LOGGER.error("Polling current state")
self.error("Polling current state")
if self._channel:
value = await self._channel.get_attribute_value(
self._zcl_attribute, from_cache=False
)
invert = await self._channel.get_attribute_value(
await self._channel.get_attribute_value(
self._zcl_inverter_attribute, from_cache=False
)
_LOGGER.debug("read value=%s, inverter=%s", value, bool(invert))
self.debug("read value=%s, inverted=%s", value, self.inverted)
@CONFIG_DIAGNOSTIC_MATCH(
@ -430,3 +436,24 @@ class InovelliDisableDoubleTapClearNotificationsMode(
_zcl_attribute: str = "disable_clear_notifications_double_tap"
_attr_name: str = "Disable config 2x tap to clear notifications"
@CONFIG_DIAGNOSTIC_MATCH(channel_names="opple_cluster", models={"aqara.feeder.acn001"})
class AqaraPetFeederLEDIndicator(
ZHASwitchConfigurationEntity, id_suffix="disable_led_indicator"
):
"""Representation of a LED indicator configuration entity."""
_zcl_attribute: str = "disable_led_indicator"
_attr_name = "LED indicator"
_force_inverted = True
_attr_icon: str = "mdi:led-on"
@CONFIG_DIAGNOSTIC_MATCH(channel_names="opple_cluster", models={"aqara.feeder.acn001"})
class AqaraPetFeederChildLock(ZHASwitchConfigurationEntity, id_suffix="child_lock"):
"""Representation of a child lock configuration entity."""
_zcl_attribute: str = "child_lock"
_attr_name = "Child lock"
_attr_icon: str = "mdi:account-lock"