diff --git a/custom_components/ge_home/devices/wac.py b/custom_components/ge_home/devices/wac.py index 585370d..c03d51d 100644 --- a/custom_components/ge_home/devices/wac.py +++ b/custom_components/ge_home/devices/wac.py @@ -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), diff --git a/custom_components/ge_home/entities/common/__init__.py b/custom_components/ge_home/entities/common/__init__.py index 10b3c1e..691629a 100644 --- a/custom_components/ge_home/entities/common/__init__.py +++ b/custom_components/ge_home/entities/common/__init__.py @@ -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 \ No newline at end of file diff --git a/custom_components/ge_home/entities/common/bool_converter.py b/custom_components/ge_home/entities/common/bool_converter.py new file mode 100644 index 0000000..b8dcd14 --- /dev/null +++ b/custom_components/ge_home/entities/common/bool_converter.py @@ -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 \ No newline at end of file diff --git a/custom_components/ge_home/entities/common/ge_climate.py b/custom_components/ge_home/entities/common/ge_climate.py index 21e6549..5e99785 100644 --- a/custom_components/ge_home/entities/common/ge_climate.py +++ b/custom_components/ge_home/entities/common/ge_climate.py @@ -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, diff --git a/custom_components/ge_home/entities/common/ge_erd_switch.py b/custom_components/ge_home/entities/common/ge_erd_switch.py index d1dcf9e..cf39f9a 100644 --- a/custom_components/ge_home/entities/common/ge_erd_switch.py +++ b/custom_components/ge_home/entities/common/ge_erd_switch.py @@ -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) \ No newline at end of file + await self.appliance.async_set_erd_value(self.erd_code, self._converter.false_value())