Use lambda in gree switch (#90316)

This commit is contained in:
epenet 2023-03-27 10:00:41 +02:00 committed by GitHub
parent 2041a0b4f9
commit 164482dc08
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 67 additions and 10 deletions

View File

@ -1,8 +1,12 @@
"""Support for interface with a Gree climate systems.""" """Support for interface with a Gree climate systems."""
from __future__ import annotations from __future__ import annotations
from collections.abc import Callable
from dataclasses import dataclass
from typing import Any, cast from typing import Any, cast
from greeclimate.device import Device
from homeassistant.components.switch import ( from homeassistant.components.switch import (
SwitchDeviceClass, SwitchDeviceClass,
SwitchEntity, SwitchEntity,
@ -16,25 +20,77 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import COORDINATORS, DISPATCH_DEVICE_DISCOVERED, DISPATCHERS, DOMAIN from .const import COORDINATORS, DISPATCH_DEVICE_DISCOVERED, DISPATCHERS, DOMAIN
from .entity import GreeEntity from .entity import GreeEntity
GREE_SWITCHES: tuple[SwitchEntityDescription, ...] = (
SwitchEntityDescription( @dataclass
class GreeRequiredKeysMixin:
"""Mixin for required keys."""
get_value_fn: Callable[[Device], bool]
set_value_fn: Callable[[Device, bool], None]
@dataclass
class GreeSwitchEntityDescription(SwitchEntityDescription, GreeRequiredKeysMixin):
"""Describes Gree switch entity."""
def _set_light(device: Device, value: bool) -> None:
"""Typed helper to set device light property."""
device.light = value
def _set_quiet(device: Device, value: bool) -> None:
"""Typed helper to set device quiet property."""
device.quiet = value
def _set_fresh_air(device: Device, value: bool) -> None:
"""Typed helper to set device fresh_air property."""
device.fresh_air = value
def _set_xfan(device: Device, value: bool) -> None:
"""Typed helper to set device xfan property."""
device.xfan = value
def _set_anion(device: Device, value: bool) -> None:
"""Typed helper to set device anion property."""
device.anion = value
GREE_SWITCHES: tuple[GreeSwitchEntityDescription, ...] = (
GreeSwitchEntityDescription(
icon="mdi:lightbulb", icon="mdi:lightbulb",
name="Panel Light", name="Panel Light",
key="light", key="light",
get_value_fn=lambda d: d.light,
set_value_fn=_set_light,
), ),
SwitchEntityDescription( GreeSwitchEntityDescription(
name="Quiet", name="Quiet",
key="quiet", key="quiet",
get_value_fn=lambda d: d.quiet,
set_value_fn=_set_quiet,
), ),
SwitchEntityDescription( GreeSwitchEntityDescription(
name="Fresh Air", name="Fresh Air",
key="fresh_air", key="fresh_air",
get_value_fn=lambda d: d.fresh_air,
set_value_fn=_set_fresh_air,
), ),
SwitchEntityDescription(name="XFan", key="xfan"), GreeSwitchEntityDescription(
SwitchEntityDescription( name="XFan",
key="xfan",
get_value_fn=lambda d: d.xfan,
set_value_fn=_set_xfan,
),
GreeSwitchEntityDescription(
icon="mdi:pine-tree", icon="mdi:pine-tree",
name="Health mode", name="Health mode",
key="anion", key="anion",
get_value_fn=lambda d: d.anion,
set_value_fn=_set_anion,
entity_registry_enabled_default=False, entity_registry_enabled_default=False,
), ),
) )
@ -68,8 +124,9 @@ class GreeSwitch(GreeEntity, SwitchEntity):
"""Generic Gree switch entity.""" """Generic Gree switch entity."""
_attr_device_class = SwitchDeviceClass.SWITCH _attr_device_class = SwitchDeviceClass.SWITCH
entity_description: GreeSwitchEntityDescription
def __init__(self, coordinator, description: SwitchEntityDescription) -> None: def __init__(self, coordinator, description: GreeSwitchEntityDescription) -> None:
"""Initialize the Gree device.""" """Initialize the Gree device."""
self.entity_description = description self.entity_description = description
@ -78,16 +135,16 @@ class GreeSwitch(GreeEntity, SwitchEntity):
@property @property
def is_on(self) -> bool: def is_on(self) -> bool:
"""Return if the state is turned on.""" """Return if the state is turned on."""
return getattr(self.coordinator.device, self.entity_description.key) return self.entity_description.get_value_fn(self.coordinator.device)
async def async_turn_on(self, **kwargs: Any) -> None: async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn the entity on.""" """Turn the entity on."""
setattr(self.coordinator.device, self.entity_description.key, True) self.entity_description.set_value_fn(self.coordinator.device, True)
await self.coordinator.push_state_update() await self.coordinator.push_state_update()
self.async_write_ha_state() self.async_write_ha_state()
async def async_turn_off(self, **kwargs: Any) -> None: async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn the entity off.""" """Turn the entity off."""
setattr(self.coordinator.device, self.entity_description.key, False) self.entity_description.set_value_fn(self.coordinator.device, False)
await self.coordinator.push_state_update() await self.coordinator.push_state_update()
self.async_write_ha_state() self.async_write_ha_state()