- genericized the ErdSwitch

- import cleanup
This commit is contained in:
Jack Simbach 2021-08-07 20:34:26 -04:00
parent d89ba016ef
commit 598bdbb5e1
5 changed files with 34 additions and 27 deletions

View File

@ -5,7 +5,7 @@ from homeassistant.helpers.entity import Entity
from gehomesdk.erd import ErdCode, ErdApplianceType
from .base import ApplianceApi
from ..entities import GeWacClimate, GeErdSensor, GeErdBinarySensor, GeErdOnOffSwitch
from ..entities import GeWacClimate, GeErdSensor, GeErdBinarySensor, GeErdSwitch, ErdOnOffBoolConverter
_LOGGER = logging.getLogger(__name__)
@ -23,7 +23,7 @@ class WacApi(ApplianceApi):
GeErdSensor(self, ErdCode.AC_AMBIENT_TEMPERATURE),
GeErdSensor(self, ErdCode.AC_FAN_SETTING, icon_override="mdi:fan"),
GeErdSensor(self, ErdCode.AC_OPERATION_MODE),
GeErdOnOffSwitch(self, ErdCode.AC_POWER_STATUS, icon_on_override="mdi:power-on", icon_off_override="mdi:power-off"),
GeErdSwitch(self, ErdCode.AC_POWER_STATUS, bool_converter=ErdOnOffBoolConverter(), icon_on_override="mdi:power-on", icon_off_override="mdi:power-off"),
GeErdBinarySensor(self, ErdCode.AC_FILTER_STATUS, device_class_override="problem"),
GeErdSensor(self, ErdCode.WAC_DEMAND_RESPONSE_STATE),
GeErdSensor(self, ErdCode.WAC_DEMAND_RESPONSE_POWER),

View File

@ -1,11 +1,12 @@
from .options_converter import OptionsConverter
from .bool_converter import BoolConverter, ErdOnOffBoolConverter
from .ge_entity import GeEntity
from .ge_erd_entity import GeErdEntity
from .ge_erd_binary_sensor import GeErdBinarySensor
from .ge_erd_property_binary_sensor import GeErdPropertyBinarySensor
from .ge_erd_sensor import GeErdSensor
from .ge_erd_property_sensor import GeErdPropertySensor
from .ge_erd_switch import GeErdSwitch, GeErdOnOffSwitch
from .ge_erd_switch import GeErdSwitch
from .ge_water_heater import GeWaterHeater
from .ge_erd_select import GeErdSelect
from .ge_climate import GeClimate

View File

@ -0,0 +1,19 @@
from typing import Any
from gehomesdk import ErdOnOff
class BoolConverter:
def boolify(self, value: Any) -> bool:
return bool(value)
def true_value(self) -> Any:
return True
def false_value(self) -> Any:
return False
class ErdOnOffBoolConverter(BoolConverter):
def boolify(self, value: ErdOnOff) -> bool:
return value.boolify()
def true_value(self) -> Any:
return ErdOnOff.ON
def false_value(self) -> Any:
return ErdOnOff.OFF

View File

@ -1,8 +1,7 @@
import logging
from typing import Any, List, Optional
from typing import List, Optional
from homeassistant.components.climate import ClimateEntity
from homeassistant.components.switch import is_on
from homeassistant.const import (
ATTR_TEMPERATURE,
TEMP_FAHRENHEIT,

View File

@ -1,8 +1,11 @@
import logging
from gehomesdk.erd.values import ErdOnOff
from gehomesdk import ErdCodeType
from homeassistant.components.switch import SwitchEntity
from ...devices import ApplianceApi
from .ge_erd_binary_sensor import GeErdBinarySensor
from .bool_converter import BoolConverter
_LOGGER = logging.getLogger(__name__)
@ -10,36 +13,21 @@ class GeErdSwitch(GeErdBinarySensor, SwitchEntity):
"""Switches for boolean ERD codes."""
device_class = "switch"
@property
def is_on(self) -> bool:
"""Return True if switch is on."""
return bool(self.appliance.get_erd_value(self.erd_code))
async def async_turn_on(self, **kwargs):
"""Turn the switch on."""
_LOGGER.debug(f"Turning on {self.unique_id}")
await self.appliance.async_set_erd_value(self.erd_code, True)
async def async_turn_off(self, **kwargs):
"""Turn the switch off."""
_LOGGER.debug(f"Turning on {self.unique_id}")
await self.appliance.async_set_erd_value(self.erd_code, False)
class GeErdOnOffSwitch(GeErdBinarySensor, SwitchEntity):
"""Switches for boolean ERD codes."""
device_class = "switch"
def __init__(self, api: ApplianceApi, erd_code: ErdCodeType, bool_converter: BoolConverter = BoolConverter(), erd_override: str = None, icon_on_override: str = None, icon_off_override: str = None, device_class_override: str = None):
super().__init__(api, erd_code, erd_override, icon_on_override, icon_off_override, device_class_override)
self._converter = bool_converter
@property
def is_on(self) -> bool:
"""Return True if switch is on."""
return self.appliance.get_erd_value(self.erd_code) == ErdOnOff.ON
return self._converter.boolify(self.appliance.get_erd_value(self.erd_code))
async def async_turn_on(self, **kwargs):
"""Turn the switch on."""
_LOGGER.debug(f"Turning on {self.unique_id}")
await self.appliance.async_set_erd_value(self.erd_code, ErdOnOff.ON)
await self.appliance.async_set_erd_value(self.erd_code, self._converter.true_value())
async def async_turn_off(self, **kwargs):
"""Turn the switch off."""
_LOGGER.debug(f"Turning on {self.unique_id}")
await self.appliance.async_set_erd_value(self.erd_code, ErdOnOff.OFF)
await self.appliance.async_set_erd_value(self.erd_code, self._converter.false_value())