|
5 | 5 | from typing import Any, cast |
6 | 6 |
|
7 | 7 | from pyhilo import API |
8 | | -from pyhilo.const import LOG |
| 8 | +from pyhilo.const import LOG, STATE_UNKNOWN |
9 | 9 | from pyhilo.device import HiloDevice |
10 | 10 |
|
| 11 | +# Attributes reported only by low voltage (24 V) thermostats. Thermostat24VMode |
| 12 | +# is emitted solely by GraphqlValueMapper._build_lowvoltage_thermostat(), which |
| 13 | +# makes its presence an exact discriminator. |
| 14 | +LOW_VOLTAGE_MODE = "thermostat24_v_mode" |
| 15 | +LOW_VOLTAGE_ALLOWED_MODES = "thermostat24_v_allowed_mode" |
| 16 | +LOW_VOLTAGE_ALLOWED_FAN_MODES = "thermostat24_v_allowed_fan_mode" |
| 17 | + |
| 18 | + |
| 19 | +def as_list(value: Any) -> list[str]: |
| 20 | + """Return a list of strings from a Hilo list attribute. |
| 21 | +
|
| 22 | + Hilo sends those either as a real list or as a comma separated string |
| 23 | + depending on the transport, so accept both. |
| 24 | + """ |
| 25 | + if value is None or value in ("", STATE_UNKNOWN): |
| 26 | + return [] |
| 27 | + if isinstance(value, (list, tuple, set)): |
| 28 | + return [str(v).strip() for v in value if str(v).strip()] |
| 29 | + return [v.strip() for v in str(value).split(",") if v.strip()] |
| 30 | + |
11 | 31 |
|
12 | 32 | class Climate(HiloDevice): |
13 | 33 | """ |
@@ -95,6 +115,133 @@ async def async_set_temperature(self, temperature: float) -> None: |
95 | 115 | Args: |
96 | 116 | temperature: The desired target temperature. |
97 | 117 | """ |
| 118 | + if self.is_low_voltage: |
| 119 | + await self.async_set_low_voltage_state(target_temperature=temperature) |
| 120 | + return |
98 | 121 | if temperature != self.target_temperature: |
99 | 122 | LOG.info("%s Setting temperature to %s", self._tag, temperature) |
100 | 123 | await self.set_attribute("target_temperature", str(temperature)) |
| 124 | + |
| 125 | + def _optional_float(self, attribute: str) -> float | None: |
| 126 | + """Return a numeric reading, or None when absent or unusable.""" |
| 127 | + value = self.get_value(attribute, None) |
| 128 | + if value is None or value == STATE_UNKNOWN: |
| 129 | + return None |
| 130 | + try: |
| 131 | + return float(value) |
| 132 | + except (TypeError, ValueError): |
| 133 | + return None |
| 134 | + |
| 135 | + def _optional_str(self, attribute: str) -> str | None: |
| 136 | + """Return a textual reading, or None when absent or unusable.""" |
| 137 | + value = self.get_value(attribute, None) |
| 138 | + if value is None or value == STATE_UNKNOWN: |
| 139 | + return None |
| 140 | + return str(value) |
| 141 | + |
| 142 | + @property |
| 143 | + def is_low_voltage(self) -> bool: |
| 144 | + """Whether this is a low voltage (24 V) thermostat. |
| 145 | +
|
| 146 | + Baseboard thermostats report none of the 24 V attributes and therefore |
| 147 | + keep their heat-only behaviour. |
| 148 | + """ |
| 149 | + return self.low_voltage_mode is not None |
| 150 | + |
| 151 | + @property |
| 152 | + def low_voltage_mode(self) -> str | None: |
| 153 | + """Operating mode reported by a 24 V thermostat, in Hilo's vocabulary.""" |
| 154 | + return self._optional_str(LOW_VOLTAGE_MODE) |
| 155 | + |
| 156 | + @property |
| 157 | + def allowed_modes(self) -> list[str]: |
| 158 | + """Mode vocabulary advertised by a 24 V thermostat.""" |
| 159 | + return as_list(self.get_value(LOW_VOLTAGE_ALLOWED_MODES, None)) |
| 160 | + |
| 161 | + @property |
| 162 | + def fan_mode(self) -> str | None: |
| 163 | + """Current fan mode, when the device reports one.""" |
| 164 | + return self._optional_str("fan_mode") |
| 165 | + |
| 166 | + @property |
| 167 | + def allowed_fan_modes(self) -> list[str]: |
| 168 | + """Fan mode vocabulary advertised by a 24 V thermostat.""" |
| 169 | + return as_list(self.get_value(LOW_VOLTAGE_ALLOWED_FAN_MODES, None)) |
| 170 | + |
| 171 | + @property |
| 172 | + def current_state(self) -> str | None: |
| 173 | + """Raw running state reported by a 24 V thermostat. |
| 174 | +
|
| 175 | + Exposed for consumers, but deliberately not mapped to hvac_action yet: |
| 176 | + its vocabulary has not been observed in enough operating conditions. |
| 177 | + """ |
| 178 | + return self._optional_str("current_state") |
| 179 | + |
| 180 | + @property |
| 181 | + def cool_setpoint(self) -> float | None: |
| 182 | + """Cooling setpoint of a 24 V thermostat.""" |
| 183 | + return self._optional_float("cool_temperature_set") |
| 184 | + |
| 185 | + @property |
| 186 | + def min_cool_setpoint(self) -> float | None: |
| 187 | + """Lowest cooling setpoint allowed by a 24 V thermostat.""" |
| 188 | + return self._optional_float("min_cool_setpoint") |
| 189 | + |
| 190 | + @property |
| 191 | + def max_cool_setpoint(self) -> float | None: |
| 192 | + """Highest cooling setpoint allowed by a 24 V thermostat.""" |
| 193 | + return self._optional_float("max_cool_setpoint") |
| 194 | + |
| 195 | + @property |
| 196 | + def current_humidity(self) -> int | None: |
| 197 | + """Ambient humidity, rounded to the nearest percent. |
| 198 | +
|
| 199 | + GraphqlValueMapper reports 0 for a device without a humidity sensor, |
| 200 | + not None, so a reading of 0 is a real (if degenerate) value rather |
| 201 | + than an absent one. |
| 202 | + """ |
| 203 | + value = self._optional_float("humidity") |
| 204 | + return None if value is None else round(value) |
| 205 | + |
| 206 | + async def async_set_low_voltage_state( |
| 207 | + self, |
| 208 | + mode: str | None = None, |
| 209 | + target_temperature: float | None = None, |
| 210 | + cool_setpoint: float | None = None, |
| 211 | + fan_mode: str | None = None, |
| 212 | + ) -> None: |
| 213 | + """Write the mode and both setpoints together. |
| 214 | +
|
| 215 | + 24 V thermostats reject partial writes: the mode and both setpoints |
| 216 | + have to be sent in one request even when only one of them changed. |
| 217 | + Arguments left out keep their current value; values the device does |
| 218 | + not report at all are omitted from the payload. |
| 219 | +
|
| 220 | + Raises: |
| 221 | + ValueError: The mode isn't given and the device hasn't reported |
| 222 | + one either. Silently dropping Thermostat24VMode from the |
| 223 | + payload in that case would send setpoints without a mode, |
| 224 | + exactly the partial write this method exists to prevent. |
| 225 | + """ |
| 226 | + resolved_mode = mode if mode is not None else self.low_voltage_mode |
| 227 | + if resolved_mode is None: |
| 228 | + raise ValueError( |
| 229 | + f"{self._tag} Cannot set low voltage state: no mode was given " |
| 230 | + "and the device has not reported one. Pass mode explicitly." |
| 231 | + ) |
| 232 | + payload: dict[str, str | int | float | None] = { |
| 233 | + "Thermostat24VMode": resolved_mode, |
| 234 | + "TargetTemperature": ( |
| 235 | + target_temperature |
| 236 | + if target_temperature is not None |
| 237 | + else self._optional_float("target_temperature") |
| 238 | + ), |
| 239 | + "CoolTemperatureSet": ( |
| 240 | + cool_setpoint if cool_setpoint is not None else self.cool_setpoint |
| 241 | + ), |
| 242 | + } |
| 243 | + if fan_mode is not None: |
| 244 | + payload["FanMode"] = fan_mode |
| 245 | + payload = {k: v for k, v in payload.items() if v is not None} |
| 246 | + LOG.info("%s Setting low voltage state to %s", self._tag, payload) |
| 247 | + await self.set_attributes(payload) |
0 commit comments