diff --git a/custom_components/smartthinq_sensors/switch.py b/custom_components/smartthinq_sensors/switch.py index 91e09353..8a91c1e5 100644 --- a/custom_components/smartthinq_sensors/switch.py +++ b/custom_components/smartthinq_sensors/switch.py @@ -225,6 +225,14 @@ def _async_discover_device(lge_devices: dict) -> None: ] ) + # add AC sleep mode switch + lge_switch.extend( + [ + LGESleepModeSwitch(lge_device) + for lge_device in lge_devices.get(DeviceType.AC, []) + ] + ) + # add MicroWave switch lge_switch.extend( [ @@ -345,3 +353,28 @@ async def async_turn_on(self, **kwargs): """Turn the entity on.""" self._wrap_device.device.set_duct_zone(self._zone, True) self._api.async_set_updated() + + +class LGESleepModeSwitch(LGESwitch): + """Class to control LGE AC reservation sleep time as switch""" + + def __init__(self, api: LGEDevice): + desc = ThinQSwitchEntityDescription( + key=AirConditionerFeatures.RESERVATION_SLEEP_TIME, + name="Sleep mode", + icon="mdi:weather-night", + available_fn=lambda x: x.device.is_reservation_sleep_time_available, + turn_off_fn=lambda x: x.device.set_reservation_sleep_time(0), + turn_on_fn=lambda x: x.device.set_reservation_sleep_time(60 * 7), + ) + super().__init__(api, desc) + + @property + def is_on(self): + """Return the state of the switch.""" + return self._get_switch_state() > 0 + + # No sure if this should be a sensor + # @property + # def extra_state_attributes(self): + # return {"Remaining time (in minutes)": self._get_switch_state()} diff --git a/custom_components/smartthinq_sensors/wideq/const.py b/custom_components/smartthinq_sensors/wideq/const.py index 23c95bf6..5233659a 100644 --- a/custom_components/smartthinq_sensors/wideq/const.py +++ b/custom_components/smartthinq_sensors/wideq/const.py @@ -44,6 +44,7 @@ class AirConditionerFeatures(StrEnum): ROOM_TEMP = "room_temperature" WATER_IN_TEMP = "water_in_temperature" WATER_OUT_TEMP = "water_out_temperature" + RESERVATION_SLEEP_TIME = "reservation_sleep_time" class AirPurifierFeatures(StrEnum): diff --git a/custom_components/smartthinq_sensors/wideq/devices/ac.py b/custom_components/smartthinq_sensors/wideq/devices/ac.py index 3eab6138..4402cf5e 100644 --- a/custom_components/smartthinq_sensors/wideq/devices/ac.py +++ b/custom_components/smartthinq_sensors/wideq/devices/ac.py @@ -3,6 +3,7 @@ from enum import Enum import logging +from math import ceil from ..const import AirConditionerFeatures, TemperatureUnit from ..core_async import ClientAsync @@ -61,6 +62,7 @@ STATE_MODE_AIRCLEAN = ["AirClean", "airState.wMode.airClean"] STATE_MODE_JET = ["Jet", "airState.wMode.jet"] STATE_LIGHTING_DISPLAY = ["DisplayControl", "airState.lightingState.displayControl"] +STATE_RESERVATION_SLEEP_TIME = ["SleepTime", "airState.reservation.sleepTime"] FILTER_TYPES = [ [ @@ -87,6 +89,7 @@ CMD_STATE_MODE_AIRCLEAN = [CTRL_BASIC, "Set", STATE_MODE_AIRCLEAN] CMD_STATE_MODE_JET = [CTRL_BASIC, "Set", STATE_MODE_JET] CMD_STATE_LIGHTING_DISPLAY = [CTRL_BASIC, "Set", STATE_LIGHTING_DISPLAY] +CMD_RESERVATION_SLEEP_TIME = [CTRL_BASIC, "Set", STATE_RESERVATION_SLEEP_TIME] # AWHP Section STATE_WATER_IN_TEMP = ["WaterInTempCur", "airState.tempState.inWaterCurrent"] @@ -866,6 +869,24 @@ async def get_filter_state_v2(self): self._filter_status_supported = False return None + async def set_reservation_sleep_time(self, value: float): + keys = self._get_cmd_keys(CMD_RESERVATION_SLEEP_TIME) + await self.set( + keys[0], # "basicCtrl", + keys[1], # "Set", + key=keys[2], # key="airState.reservation.sleepTime", + value=str(ceil(value)), + ) + + @property + def is_reservation_sleep_time_available(self): + value = self._status.is_on and ( + self._status.operation_mode + in [ACMode.ACO.name, ACMode.FAN.name, ACMode.COOL.name, ACMode.DRY.name] + ) + + return value + async def set( self, ctrl_key, command, *, key=None, value=None, data=None, ctrl_path=None ): @@ -1274,6 +1295,16 @@ def hot_water_target_max_temp(self): key = self._get_state_key(STATE_HOT_WATER_MAX_TEMP) return self._str_to_temp(self._data.get(key)) + @property + def reservation_sleep_time(self): + """Return display lighting status.""" + key = self._get_state_key(STATE_RESERVATION_SLEEP_TIME) + value = float(self._data.get(key)) + + return self._update_feature( + AirConditionerFeatures.RESERVATION_SLEEP_TIME, value, False + ) + def _update_features(self): _ = [ self.current_temp, @@ -1287,4 +1318,5 @@ def _update_features(self): self.water_out_current_temp, self.mode_awhp_silent, self.hot_water_current_temp, + self.reservation_sleep_time, ]