| 1 |
from enum import IntEnum |
| 2 |
from .sensor import Temperature |
| 3 |
from typing import Optional, Any, Union |
| 4 |
from .feature import Feature |
| 5 |
from blebox_uniapi.jfollow import follow |
| 6 |
|
| 7 |
|
| 8 |
class BleboxThermoState(IntEnum): |
| 9 |
"""Possible states of a thermo regulator, as reported by the device.""" |
| 10 |
|
| 11 |
OFF = 0 |
| 12 |
ON = 1 |
| 13 |
BOOST = 3 |
| 14 |
SAFETY_OFF = 4 |
| 15 |
|
| 16 |
|
| 17 |
class Climate(Temperature): |
| 18 |
_is_on: Optional[bool] |
| 19 |
_desired: Union[float, int, None] |
| 20 |
_is_heating: Optional[bool] |
| 21 |
_is_cooling: Optional[bool] |
| 22 |
_min_temp: Union[float, int, None] |
| 23 |
_max_temp: Union[float, int, None] |
| 24 |
_mode: Optional[int] |
| 25 |
_havc_action: Optional[int] |
| 26 |
|
| 27 |
def __init__(self, product, alias, methods, mode): |
| 28 |
super().__init__(product, alias, methods) |
| 29 |
self._mode = mode |
| 30 |
|
| 31 |
@property |
| 32 |
def mode(self) -> Optional[int]: |
| 33 |
return self._mode |
| 34 |
|
| 35 |
@property |
| 36 |
def is_on(self) -> Optional[bool]: |
| 37 |
return self._is_on |
| 38 |
|
| 39 |
@property |
| 40 |
def desired(self) -> Any: |
| 41 |
return self._desired |
| 42 |
|
| 43 |
@property |
| 44 |
def current(self) -> Any: |
| 45 |
return self._current |
| 46 |
|
| 47 |
@property |
| 48 |
def max_temp(self) -> Union[float, int, None]: |
| 49 |
return self._max_temp |
| 50 |
|
| 51 |
@property |
| 52 |
def min_temp(self) -> Union[float, int, None]: |
| 53 |
return self._min_temp |
| 54 |
|
| 55 |
@property |
| 56 |
def is_heating(self) -> Optional[bool]: |
| 57 |
return self._is_heating |
| 58 |
|
| 59 |
@property |
| 60 |
def is_cooling(self) -> Optional[bool]: |
| 61 |
return self._is_cooling |
| 62 |
|
| 63 |
@property |
| 64 |
def hvac_action(self) -> Optional[int]: |
| 65 |
return self._havc_action |
| 66 |
|
| 67 |
@classmethod |
| 68 |
def many_from_config( |
| 69 |
cls, product, box_type_config, extended_state |
| 70 |
) -> list["Feature"]: |
| 71 |
|
| 72 |
|
| 73 |
alias, methods = box_type_config[0] |
| 74 |
if extended_state is not None: |
| 75 |
safetyIdPath = box_type_config[0][1].get("safetySensorId") |
| 76 |
if safetyIdPath: |
| 77 |
safety_sensor_id = follow(extended_state, safetyIdPath) |
| 78 |
temp_sensor_id = cls.get_temp_sensor_id( |
| 79 |
safety_sensor_id, extended_state["sensors"] |
| 80 |
) |
| 81 |
methods = Feature.resolve_access_method_paths(methods, temp_sensor_id) |
| 82 |
|
| 83 |
args = [alias, methods] |
| 84 |
mode = extended_state.get("thermo", {}).get("mode", 1) |
| 85 |
return [cls(product, *args, mode)] |
| 86 |
return [cls(product, *args, 1) for args in box_type_config] |
| 87 |
|
| 88 |
async def async_on(self) -> None: |
| 89 |
await self.async_api_command("on") |
| 90 |
|
| 91 |
async def async_off(self) -> None: |
| 92 |
await self.async_api_command("off") |
| 93 |
|
| 94 |
async def async_set_temperature(self, value: Any) -> None: |
| 95 |
await self.async_api_command("set", int(round(value * 100.0))) |
| 96 |
|
| 97 |
def _read_is_on(self) -> Optional[bool]: |
| 98 |
product = self._product |
| 99 |
if product.last_data is not None: |
| 100 |
raw = self.raw_value("state") |
| 101 |
if raw is not None: |
| 102 |
alias = self._alias |
| 103 |
return product.expect_int(alias, raw, max(BleboxThermoState), 0) in ( |
| 104 |
BleboxThermoState.ON, |
| 105 |
BleboxThermoState.BOOST, |
| 106 |
) |
| 107 |
return None |
| 108 |
|
| 109 |
def _read_safety_off(self) -> bool: |
| 110 |
"""Return True when the safety system forced the output off.""" |
| 111 |
if self._product.last_data is None: |
| 112 |
return False |
| 113 |
return self.raw_value("state") == BleboxThermoState.SAFETY_OFF |
| 114 |
|
| 115 |
def _read_operating_state(self) -> Optional[int]: |
| 116 |
"""Return current operating state""" |
| 117 |
if self._product.last_data is not None: |
| 118 |
try: |
| 119 |
raw = self.raw_value("operatingState") |
| 120 |
if raw in range(0, 4) and isinstance(raw, int): |
| 121 |
return raw |
| 122 |
except KeyError: |
| 123 |
return None |
| 124 |
|
| 125 |
def _read_is_heating(self) -> Optional[bool]: |
| 126 |
if not self._product.last_data or self.current is None or self.desired is None: |
| 127 |
return None |
| 128 |
|
| 129 |
return self.is_on and (self.current < self.desired) |
| 130 |
|
| 131 |
def _read_is_cooling(self) -> Optional[bool]: |
| 132 |
if not self._product.last_data or self.current is None or self.desired is None: |
| 133 |
return None |
| 134 |
|
| 135 |
return self.is_on and (self.current > self.desired) |
| 136 |
|
| 137 |
def _read_mode(self) -> Optional[int]: |
| 138 |
if self._product.last_data is not None: |
| 139 |
try: |
| 140 |
raw = self.raw_value("mode") |
| 141 |
if raw in range(0, 3) and isinstance(raw, int): |
| 142 |
return raw |
| 143 |
except KeyError: |
| 144 |
return 1 |
| 145 |
return None |
| 146 |
|
| 147 |
def after_update(self) -> None: |
| 148 |
self._is_on = self._read_is_on() |
| 149 |
self._desired = self._read_temperature("desired") |
| 150 |
|
| 151 |
state = self._read_state("temperature") |
| 152 |
if self._state_is_error(state): |
| 153 |
self._error = True |
| 154 |
self._current = None |
| 155 |
else: |
| 156 |
self._error = False |
| 157 |
if self._state_is_initializing(state): |
| 158 |
self._current = None |
| 159 |
else: |
| 160 |
self._current = self._read_temperature("temperature") |
| 161 |
|
| 162 |
if self._read_safety_off(): |
| 163 |
self._error = True |
| 164 |
|
| 165 |
self._native_value = self._current |
| 166 |
self._is_heating = self._read_is_heating() |
| 167 |
self._is_cooling = self._read_is_cooling() |
| 168 |
self._havc_action = self._read_operating_state() |
| 169 |
if self._product.last_data is None: |
| 170 |
self._min_temp = None |
| 171 |
self._max_temp = None |
| 172 |
return |
| 173 |
|
| 174 |
raw_min = self.raw_value("minimum") |
| 175 |
if raw_min is None: |
| 176 |
return |
| 177 |
|
| 178 |
self._min_temp = self._read_temperature("minimum") |
| 179 |
self._max_temp = self._read_temperature("maximum") |
| 180 |
|
| 181 |
@staticmethod |
| 182 |
def get_temp_sensor_id(safety_sensor_id: int, sensor_list) -> Optional[int]: |
| 183 |
"""Return ID of the first sensor which is not a safety sensor.""" |
| 184 |
li_sensor_id = [ |
| 185 |
sensor.get("id") |
| 186 |
for sensor in sensor_list |
| 187 |
if sensor.get("id") is not None and sensor.get("id") != safety_sensor_id |
| 188 |
] |
| 189 |
li_sensor_id.sort() |
| 190 |
|
| 191 |
if not li_sensor_id: |
| 192 |
return None |
| 193 |
|
| 194 |
return li_sensor_id[0] |