Skip to content

Commit

Permalink
Added controls on status avl and service call
Browse files Browse the repository at this point in the history
  • Loading branch information
ollo69 committed Aug 26, 2023
1 parent 53a7be3 commit 12738f8
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 19 deletions.
13 changes: 6 additions & 7 deletions custom_components/smartthinq_sensors/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,9 @@ def available(self) -> bool:
"""Return True if entity is available."""
return self._api.available

async def async_set_sleep_time(self, sleep_time: int | None = None):
async def async_set_sleep_time(self, sleep_time: int) -> None:
"""Call the set sleep time command for AC devices."""
if not self._api.device.is_reservation_sleep_time_available:
msg = f"{self}: reservation_sleep_time is not available"
_LOGGER.error(msg)
raise TypeError(msg)

await self._api.device.set_reservation_sleep_time(sleep_time)
raise NotImplementedError()


class LGEACClimate(LGEClimate):
Expand Down Expand Up @@ -447,6 +442,10 @@ def max_temp(self) -> float:
AWHP_MAX_TEMP if self._device.is_air_to_water else DEFAULT_MAX_TEMP
)

async def async_set_sleep_time(self, sleep_time: int) -> None:
"""Call the set sleep time command for AC devices."""
await self._api.device.set_reservation_sleep_time(sleep_time)


class LGERefrigeratorClimate(LGEClimate):
"""Refrigerator climate device."""
Expand Down
1 change: 0 additions & 1 deletion custom_components/smartthinq_sensors/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,6 @@ class ThinQSensorEntityDescription(SensorEntityDescription):
icon="mdi:weather-night",
state_class=SensorDeviceClass.DURATION,
native_unit_of_measurement=UnitOfTime.MINUTES,
# available_fn=lambda x: x.device.is_reservation_sleep_time_available,
),
)
RANGE_SENSORS: Tuple[ThinQSensorEntityDescription, ...] = (
Expand Down
2 changes: 1 addition & 1 deletion custom_components/smartthinq_sensors/wideq/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ class AirConditionerFeatures(StrEnum):
MODE_AIRCLEAN = "mode_airclean"
MODE_AWHP_SILENT = "mode_awhp_silent"
MODE_JET = "mode_jet"
RESERVATION_SLEEP_TIME = "reservation_sleep_time"
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):
Expand Down
45 changes: 35 additions & 10 deletions custom_components/smartthinq_sensors/wideq/devices/ac.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from ..core_util import TempUnitConversion
from ..device import Device, DeviceStatus
from ..device_info import DeviceInfo
from ..model_info import TYPE_RANGE

SUPPORT_OPERATION_MODE = ["SupportOpMode", "support.airState.opMode"]
SUPPORT_WIND_STRENGTH = ["SupportWindStrength", "support.airState.windStrength"]
Expand Down Expand Up @@ -283,6 +284,7 @@ def __init__(
self._supported_vertical_steps = None
self._supported_mode_jet = None
self._temperature_range = None
self._sleep_time_range = None
self._hot_water_temperature_range = None
self._temperature_step = TEMP_STEP_WHOLE
self._duct_zones = {}
Expand Down Expand Up @@ -871,20 +873,43 @@ async def get_filter_state_v2(self):
self._filter_status_supported = False
return None

async def set_reservation_sleep_time(self, value: int):
"""Set the device sleep time reservation in minutes."""
keys = self._get_cmd_keys(CMD_RESERVATION_SLEEP_TIME)
await self.set(keys[0], keys[1], key=keys[2], value=str(value))
@property
def sleep_time_range(self) -> list[int]:
"""Return valid range for sleep time."""
if self._sleep_time_range is None:
key = self._get_state_key(STATE_RESERVATION_SLEEP_TIME)
if (range_val := self.model_info.value(key, TYPE_RANGE)) is None:
self._sleep_time_range = [0, 420]
else:
self._sleep_time_range = [range_val.min, range_val.max]
return self._sleep_time_range

@property
def is_reservation_sleep_time_available(self):
def is_reservation_sleep_time_available(self) -> bool:
"""Return if reservation sleep time is available."""
value = self._status.is_on and (
self._status.operation_mode
if (status := self._status) is None:
return False
if (
status.device_features.get(AirConditionerFeatures.RESERVATION_SLEEP_TIME)
is None
):
return False
return status.is_on and (
status.operation_mode
in [ACMode.ACO.name, ACMode.FAN.name, ACMode.COOL.name, ACMode.DRY.name]
)

return value
async def set_reservation_sleep_time(self, value: int):
"""Set the device sleep time reservation in minutes."""
if not self.is_reservation_sleep_time_available:
raise ValueError("Reservation sleep time is not available")
valid_range = self.sleep_time_range
if not (valid_range[0] <= value <= valid_range[1]):
raise ValueError(
f"Invalid sleep time value. Valid range: {valid_range[0]} - {valid_range[1]}"
)
keys = self._get_cmd_keys(CMD_RESERVATION_SLEEP_TIME)
await self.set(keys[0], keys[1], key=keys[2], value=str(value))

async def set(
self, ctrl_key, command, *, key=None, value=None, data=None, ctrl_path=None
Expand Down Expand Up @@ -1298,8 +1323,8 @@ def hot_water_target_max_temp(self):
def reservation_sleep_time(self):
"""Return reservation sleep time in minutes."""
key = self._get_state_key(STATE_RESERVATION_SLEEP_TIME)
value = int(self._data.get(key))

if (value := self.to_int_or_none(self.lookup_range(key))) is None:
return None
return self._update_feature(
AirConditionerFeatures.RESERVATION_SLEEP_TIME, value, False
)
Expand Down

0 comments on commit 12738f8

Please sign in to comment.