| 1 |
"""BleBox climate entities tests.""" |
| 2 |
|
| 3 |
import json |
| 4 |
|
| 5 |
from blebox_uniapi.box_types import get_latest_api_level |
| 6 |
|
| 7 |
from .conftest import CommonEntity, DefaultBoxTest, future_date, jmerge |
| 8 |
|
| 9 |
|
| 10 |
SUPPORT_TARGET_TEMPERATURE = 1 |
| 11 |
HVAC_MODE_OFF = "hvac mode off" |
| 12 |
HVAC_MODE_HEAT = "hvac mode heat" |
| 13 |
CURRENT_HVAC_OFF = "current hvac mode off" |
| 14 |
CURRENT_HVAC_HEAT = "current hvac mode heat" |
| 15 |
CURRENT_HVAC_IDLE = "current hvac mode idle" |
| 16 |
ATTR_TEMPERATURE = "temperature" |
| 17 |
TEMP_CELSIUS = "celsius" |
| 18 |
|
| 19 |
|
| 20 |
class ClimateDevice: |
| 21 |
def __init__(self): |
| 22 |
self._state = None |
| 23 |
|
| 24 |
@property |
| 25 |
def state(self): |
| 26 |
if self._feature.is_on is None: |
| 27 |
return None |
| 28 |
|
| 29 |
if not self._feature.is_on: |
| 30 |
return HVAC_MODE_OFF |
| 31 |
return HVAC_MODE_HEAT |
| 32 |
|
| 33 |
@property |
| 34 |
def device_class(self): |
| 35 |
return None |
| 36 |
|
| 37 |
|
| 38 |
class BleBoxClimateEntity(CommonEntity, ClimateDevice): |
| 39 |
def __init__(self, feature): |
| 40 |
super().__init__(feature) |
| 41 |
ClimateDevice.__init__(self) |
| 42 |
|
| 43 |
"""Representation of a BleBox climate feature.""" |
| 44 |
|
| 45 |
@property |
| 46 |
def supported_features(self): |
| 47 |
"""Return the supported climate features.""" |
| 48 |
return SUPPORT_TARGET_TEMPERATURE |
| 49 |
|
| 50 |
@property |
| 51 |
def hvac_mode(self): |
| 52 |
"""Return the desired HVAC mode.""" |
| 53 |
return {None: None, False: HVAC_MODE_OFF, True: HVAC_MODE_HEAT}[ |
| 54 |
self._feature.is_on |
| 55 |
] |
| 56 |
|
| 57 |
@property |
| 58 |
def hvac_action(self): |
| 59 |
"""Return the actual current HVAC action.""" |
| 60 |
on = self._feature.is_on |
| 61 |
if not on: |
| 62 |
return None if on is None else CURRENT_HVAC_OFF |
| 63 |
|
| 64 |
states = {None: None, False: CURRENT_HVAC_IDLE, True: CURRENT_HVAC_HEAT} |
| 65 |
|
| 66 |
heating = self._feature.is_heating |
| 67 |
return states[heating] |
| 68 |
|
| 69 |
@property |
| 70 |
def hvac_modes(self): |
| 71 |
"""Return a list of possible HVAC modes.""" |
| 72 |
return (HVAC_MODE_OFF, HVAC_MODE_HEAT) |
| 73 |
|
| 74 |
@property |
| 75 |
def temperature_unit(self): |
| 76 |
"""Return the temperature unit.""" |
| 77 |
return TEMP_CELSIUS |
| 78 |
|
| 79 |
@property |
| 80 |
def current_temperature(self): |
| 81 |
"""Return the current temperature.""" |
| 82 |
return self._feature.current |
| 83 |
|
| 84 |
@property |
| 85 |
def max_temp(self): |
| 86 |
"""Return the maximum thermostat setting.""" |
| 87 |
return self._feature.max_temp |
| 88 |
|
| 89 |
@property |
| 90 |
def min_temp(self): |
| 91 |
"""Return the minimum thermostat setting.""" |
| 92 |
return self._feature.min_temp |
| 93 |
|
| 94 |
@property |
| 95 |
def target_temperature(self): |
| 96 |
"""Return the desired thermostat temperature.""" |
| 97 |
return self._feature.desired |
| 98 |
|
| 99 |
async def async_set_hvac_mode(self, hvac_mode): |
| 100 |
"""Set the climate entity mode.""" |
| 101 |
modemap = {HVAC_MODE_OFF: "async_off", HVAC_MODE_HEAT: "async_on"} |
| 102 |
await getattr(self._feature, modemap[hvac_mode])() |
| 103 |
|
| 104 |
async def async_set_temperature(self, **kwargs): |
| 105 |
"""Set the thermostat temperature.""" |
| 106 |
value = kwargs[ATTR_TEMPERATURE] |
| 107 |
await self._feature.async_set_temperature(value) |
| 108 |
|
| 109 |
|
| 110 |
class TestSauna(DefaultBoxTest): |
| 111 |
"""Tests for entities representing a BleBox saunaBox.""" |
| 112 |
|
| 113 |
DEVCLASS = "climates" |
| 114 |
ENTITY_CLASS = BleBoxClimateEntity |
| 115 |
|
| 116 |
DEV_INFO_PATH = "api/heat/extended/state" |
| 117 |
DEVICE_EXTENDED_INFO_PATH = "/state/extended" |
| 118 |
|
| 119 |
DEVICE_INFO = json.loads( |
| 120 |
""" |
| 121 |
{ |
| 122 |
"device": { |
| 123 |
"deviceName": "My SaunaBox", |
| 124 |
"type": "saunaBox", |
| 125 |
"fv": "0.176", |
| 126 |
"hv": "0.6", |
| 127 |
"apiLevel": "20180604", |
| 128 |
"id": "1afe34db9437", |
| 129 |
"ip": "192.168.1.11" |
| 130 |
} |
| 131 |
} |
| 132 |
""" |
| 133 |
) |
| 134 |
DEVICE_INFO_THERMO = json.loads( |
| 135 |
""" |
| 136 |
{ |
| 137 |
"device": { |
| 138 |
"deviceName": "My ThermoBox", |
| 139 |
"type": "thermoBox", |
| 140 |
"product": "thermoBox", |
| 141 |
"hv": "thB.1.0", |
| 142 |
"fv": "0.1031", |
| 143 |
"universe": 0, |
| 144 |
"apiLevel": "20200229", |
| 145 |
"iconSet": 43, |
| 146 |
"categories": [ |
| 147 |
7 |
| 148 |
], |
| 149 |
"id": "f6cfa2f11cd3", |
| 150 |
"ip": "192.168.49.183", |
| 151 |
"availableFv": null |
| 152 |
} |
| 153 |
} |
| 154 |
""" |
| 155 |
) |
| 156 |
DEVICE_EXTENDED_INFO_THERMO = json.loads( |
| 157 |
""" |
| 158 |
{ |
| 159 |
"thermo": { |
| 160 |
"state": 0, |
| 161 |
"operatingState": 3, |
| 162 |
"desiredTemp": -70, |
| 163 |
"mode": 2, |
| 164 |
"minimumTemp": -1230, |
| 165 |
"maximumTemp": 6000, |
| 166 |
"safety": { |
| 167 |
"eventReason": 0, |
| 168 |
"triggered": [ |
| 169 |
|
| 170 |
] |
| 171 |
}, |
| 172 |
"safetyTempSensor": { |
| 173 |
"sensorId": 1 |
| 174 |
} |
| 175 |
}, |
| 176 |
"sensors": [ |
| 177 |
{ |
| 178 |
"id": 0, |
| 179 |
"type": "temperature", |
| 180 |
"value": 2098, |
| 181 |
"state": 2 |
| 182 |
}, |
| 183 |
{ |
| 184 |
"id": 1, |
| 185 |
"type": "temperature", |
| 186 |
"value": 2775, |
| 187 |
"state": 2 |
| 188 |
} |
| 189 |
] |
| 190 |
} |
| 191 |
""" |
| 192 |
) |
| 193 |
|
| 194 |
def patch_version(apiLevel): |
| 195 |
"""Generate a patch for a JSON state fixture.""" |
| 196 |
return f""" |
| 197 |
{{ "device": {{ "apiLevel": {apiLevel} }} }} |
| 198 |
""" |
| 199 |
|
| 200 |
DEVICE_INFO_FUTURE = jmerge(DEVICE_INFO, patch_version(future_date())) |
| 201 |
DEVICE_INFO_LATEST = jmerge( |
| 202 |
DEVICE_INFO, patch_version(get_latest_api_level("saunaBox")) |
| 203 |
) |
| 204 |
DEVICE_INFO_UNSUPPORTED = jmerge(DEVICE_INFO, patch_version(20180603)) |
| 205 |
|
| 206 |
DEVICE_INFO_UNSPECIFIED_API = json.loads( |
| 207 |
""" |
| 208 |
{ |
| 209 |
"device": { |
| 210 |
"deviceName": "My SaunaBox", |
| 211 |
"type": "saunaBox", |
| 212 |
"fv": "0.176", |
| 213 |
"hv": "0.6", |
| 214 |
"id": "1afe34db9437", |
| 215 |
"ip": "192.168.1.11" |
| 216 |
} |
| 217 |
} |
| 218 |
""" |
| 219 |
) |
| 220 |
|
| 221 |
def patch_state(state, current, desired): |
| 222 |
"""Generate a patch for a JSON state fixture.""" |
| 223 |
return f""" |
| 224 |
{{ |
| 225 |
"heat": {{ |
| 226 |
"state": {state}, |
| 227 |
"desiredTemp": {desired}, |
| 228 |
"sensors": [ {{ "value": {current} }} ] |
| 229 |
}} |
| 230 |
}} |
| 231 |
""" |
| 232 |
|
| 233 |
STATE_DEFAULT = json.loads( |
| 234 |
""" |
| 235 |
{ |
| 236 |
"heat": { |
| 237 |
"state": 0, |
| 238 |
"desiredTemp": 6428, |
| 239 |
"maximumTemp": 12166, |
| 240 |
"minimumTemp": -5166, |
| 241 |
"sensors": [ |
| 242 |
{ |
| 243 |
"type": "temperature", |
| 244 |
"id": 0, |
| 245 |
"value": 3996, |
| 246 |
"trend": 0, |
| 247 |
"state": 2, |
| 248 |
"elapsedTimeS": 0 |
| 249 |
} |
| 250 |
] |
| 251 |
} |
| 252 |
} |
| 253 |
""" |
| 254 |
) |
| 255 |
|
| 256 |
STATE_OFF_BELOW = STATE_DEFAULT |
| 257 |
STATE_NEEDS_HEATING = jmerge(STATE_DEFAULT, patch_state(1, 2320, 3871)) |
| 258 |
|
| 259 |
STATE_OFF_ABOVE = jmerge(STATE_DEFAULT, patch_state(0, 3871, 2876)) |
| 260 |
STATE_NEEDS_COOLING = jmerge(STATE_DEFAULT, patch_state(1, 3871, 2876)) |
| 261 |
|
| 262 |
STATE_REACHED = jmerge(STATE_DEFAULT, patch_state(1, 2320, 2320)) |
| 263 |
STATE_THERMO_SET = jmerge(STATE_DEFAULT, patch_state(1, 2320, 4320)) |
| 264 |
|
| 265 |
async def test_init(self, aioclient_mock): |
| 266 |
"""Test default state.""" |
| 267 |
|
| 268 |
await self.allow_get_info(aioclient_mock) |
| 269 |
entity = (await self.async_entities(aioclient_mock))[0] |
| 270 |
|
| 271 |
assert entity.name == "My SaunaBox (saunaBox#thermostat)" |
| 272 |
assert entity.unique_id == "BleBox-saunaBox-1afe34db9437-thermostat" |
| 273 |
|
| 274 |
assert entity.device_class is None |
| 275 |
assert entity.supported_features & SUPPORT_TARGET_TEMPERATURE |
| 276 |
assert entity.hvac_modes == (HVAC_MODE_OFF, HVAC_MODE_HEAT) |
| 277 |
|
| 278 |
assert entity.hvac_mode is None |
| 279 |
assert entity.hvac_action is None |
| 280 |
assert entity.target_temperature is None |
| 281 |
assert entity.temperature_unit == TEMP_CELSIUS |
| 282 |
assert entity.state is None |
| 283 |
assert entity.max_temp is None |
| 284 |
assert entity.min_temp is None |
| 285 |
|
| 286 |
async def test_thermo_init(self, aioclient_mock): |
| 287 |
"""Test initialisation with device state""" |
| 288 |
self.DEVICE_INFO = self.DEVICE_INFO_THERMO |
| 289 |
self.DEVICE_EXTENDED_INFO = self.DEVICE_EXTENDED_INFO_THERMO |
| 290 |
await self.allow_get_info( |
| 291 |
aioclient_mock, |
| 292 |
) |
| 293 |
entity = (await self.async_entities(aioclient_mock))[0] |
| 294 |
assert entity.device_info["name"] == "My ThermoBox" |
| 295 |
|
| 296 |
async def test_thermo_sensor_error_state(self, aioclient_mock): |
| 297 |
"""A broken probe (state=3, out-of-range value) must not prevent setup.""" |
| 298 |
self.DEVICE_INFO = self.DEVICE_INFO_THERMO |
| 299 |
self.DEVICE_EXTENDED_INFO = jmerge( |
| 300 |
self.DEVICE_EXTENDED_INFO_THERMO, |
| 301 |
'{ "sensors": [ { "value": 18349, "state": 3 } ] }', |
| 302 |
) |
| 303 |
await self.allow_get_info(aioclient_mock) |
| 304 |
entity = (await self.async_entities(aioclient_mock))[0] |
| 305 |
|
| 306 |
assert entity.current_temperature is None |
| 307 |
assert entity._feature.is_error is True |
| 308 |
|
| 309 |
async def test_thermo_sensor_initializing_state(self, aioclient_mock): |
| 310 |
"""state=1 (measurement in progress) must not surface a value or an error.""" |
| 311 |
self.DEVICE_INFO = self.DEVICE_INFO_THERMO |
| 312 |
self.DEVICE_EXTENDED_INFO = jmerge( |
| 313 |
self.DEVICE_EXTENDED_INFO_THERMO, |
| 314 |
'{ "sensors": [ { "value": 18349, "state": 1 } ] }', |
| 315 |
) |
| 316 |
await self.allow_get_info(aioclient_mock) |
| 317 |
entity = (await self.async_entities(aioclient_mock))[0] |
| 318 |
|
| 319 |
assert entity.current_temperature is None |
| 320 |
assert entity._feature.is_error is False |
| 321 |
|
| 322 |
async def test_thermo_safety_off_state(self, aioclient_mock): |
| 323 |
"""state=4 (output blocked by safety system) must not prevent setup.""" |
| 324 |
self.DEVICE_INFO = self.DEVICE_INFO_THERMO |
| 325 |
self.DEVICE_EXTENDED_INFO = jmerge( |
| 326 |
self.DEVICE_EXTENDED_INFO_THERMO, |
| 327 |
'{ "thermo": { "state": 4 } }', |
| 328 |
) |
| 329 |
await self.allow_get_info(aioclient_mock) |
| 330 |
entity = (await self.async_entities(aioclient_mock))[0] |
| 331 |
|
| 332 |
assert entity._feature.is_on is False |
| 333 |
assert entity._feature.is_error is True |
| 334 |
|
| 335 |
async def test_device_info(self, aioclient_mock): |
| 336 |
await self.allow_get_info(aioclient_mock, self.DEVICE_INFO) |
| 337 |
entity = (await self.async_entities(aioclient_mock))[0] |
| 338 |
assert entity.device_info["name"] == "My SaunaBox" |
| 339 |
assert entity.device_info["mac"] == "1afe34db9437" |
| 340 |
assert entity.device_info["manufacturer"] == "BleBox" |
| 341 |
assert entity.device_info["model"] == "saunaBox" |
| 342 |
assert entity.device_info["sw_version"] == "0.176" |
| 343 |
|
| 344 |
async def test_update(self, aioclient_mock): |
| 345 |
"""Test updating.""" |
| 346 |
|
| 347 |
entity = await self.updated(aioclient_mock, self.STATE_DEFAULT) |
| 348 |
|
| 349 |
assert entity.hvac_mode == HVAC_MODE_OFF |
| 350 |
assert entity.hvac_action == CURRENT_HVAC_OFF |
| 351 |
assert entity.target_temperature == 64.3 |
| 352 |
assert entity.current_temperature == 40.0 |
| 353 |
assert entity.temperature_unit == TEMP_CELSIUS |
| 354 |
assert entity.max_temp == 121.7 |
| 355 |
assert entity.min_temp == -51.7 |
| 356 |
|
| 357 |
async def test_on_when_below_target(self, aioclient_mock): |
| 358 |
"""Test when temperature is below desired.""" |
| 359 |
|
| 360 |
entity = await self.updated(aioclient_mock, self.STATE_OFF_BELOW) |
| 361 |
assert entity.state == entity.hvac_mode == HVAC_MODE_OFF |
| 362 |
assert entity.hvac_action == CURRENT_HVAC_OFF |
| 363 |
|
| 364 |
self.allow_get(aioclient_mock, "/s/1", self.STATE_NEEDS_HEATING) |
| 365 |
await entity.async_set_hvac_mode(HVAC_MODE_HEAT) |
| 366 |
|
| 367 |
assert entity.target_temperature == 38.7 |
| 368 |
assert entity.current_temperature == 23.2 |
| 369 |
assert entity.state == entity.hvac_mode == HVAC_MODE_HEAT |
| 370 |
assert entity.hvac_action == CURRENT_HVAC_HEAT |
| 371 |
|
| 372 |
async def test_on_when_above_target(self, aioclient_mock): |
| 373 |
"""Test when temperature is below desired.""" |
| 374 |
|
| 375 |
entity = await self.updated(aioclient_mock, self.STATE_OFF_ABOVE) |
| 376 |
assert entity.state == entity.hvac_mode == HVAC_MODE_OFF |
| 377 |
assert entity.hvac_action == CURRENT_HVAC_OFF |
| 378 |
|
| 379 |
self.allow_get(aioclient_mock, "/s/1", self.STATE_NEEDS_COOLING) |
| 380 |
await entity.async_set_hvac_mode(HVAC_MODE_HEAT) |
| 381 |
|
| 382 |
assert entity.target_temperature == 28.8 |
| 383 |
assert entity.current_temperature == 38.7 |
| 384 |
assert entity.state == entity.hvac_mode == HVAC_MODE_HEAT |
| 385 |
assert entity.hvac_action == CURRENT_HVAC_IDLE |
| 386 |
|
| 387 |
async def test_on_when_at_target(self, aioclient_mock): |
| 388 |
"""Test when temperature is below desired.""" |
| 389 |
|
| 390 |
entity = await self.updated(aioclient_mock, self.STATE_OFF_ABOVE) |
| 391 |
assert entity.state == entity.hvac_mode == HVAC_MODE_OFF |
| 392 |
assert entity.hvac_action == CURRENT_HVAC_OFF |
| 393 |
|
| 394 |
self.allow_get(aioclient_mock, "/s/1", self.STATE_REACHED) |
| 395 |
await entity.async_set_hvac_mode(HVAC_MODE_HEAT) |
| 396 |
|
| 397 |
assert entity.target_temperature == 23.2 |
| 398 |
assert entity.current_temperature == 23.2 |
| 399 |
assert entity.state == entity.hvac_mode == HVAC_MODE_HEAT |
| 400 |
assert entity.hvac_action == CURRENT_HVAC_IDLE |
| 401 |
|
| 402 |
async def test_off(self, aioclient_mock): |
| 403 |
"""Test turning off.""" |
| 404 |
|
| 405 |
entity = await self.updated(aioclient_mock, self.STATE_REACHED) |
| 406 |
|
| 407 |
self.allow_get(aioclient_mock, "/s/0", self.STATE_OFF_BELOW) |
| 408 |
await entity.async_set_hvac_mode(HVAC_MODE_OFF) |
| 409 |
|
| 410 |
assert entity.target_temperature == 64.3 |
| 411 |
assert entity.current_temperature == 40.0 |
| 412 |
assert entity.state == entity.hvac_mode == HVAC_MODE_OFF |
| 413 |
assert entity.hvac_action == CURRENT_HVAC_OFF |
| 414 |
|
| 415 |
async def test_set_thermo(self, aioclient_mock): |
| 416 |
"""Test setting thermostat.""" |
| 417 |
|
| 418 |
entity = await self.updated(aioclient_mock, self.STATE_REACHED) |
| 419 |
self.allow_get(aioclient_mock, "/s/t/4321", self.STATE_THERMO_SET) |
| 420 |
await entity.async_set_temperature(**{ATTR_TEMPERATURE: 43.21}) |
| 421 |
assert entity.current_temperature == 23.2 |
| 422 |
assert entity.target_temperature == 43.2 |
| 423 |
assert entity.state == entity.hvac_mode == HVAC_MODE_HEAT |
| 424 |
assert entity.hvac_action == CURRENT_HVAC_HEAT |