climate.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. """
  2. Setup for different kinds of Tuya climate devices
  3. """
  4. import logging
  5. from homeassistant.components.climate import (
  6. ClimateEntity,
  7. ClimateEntityFeature,
  8. HVACAction,
  9. HVACMode,
  10. )
  11. from homeassistant.components.climate.const import (
  12. ATTR_CURRENT_HUMIDITY,
  13. ATTR_CURRENT_TEMPERATURE,
  14. ATTR_FAN_MODE,
  15. ATTR_HUMIDITY,
  16. ATTR_HVAC_ACTION,
  17. ATTR_HVAC_MODE,
  18. ATTR_PRESET_MODE,
  19. ATTR_SWING_HORIZONTAL_MODE,
  20. ATTR_SWING_MODE,
  21. ATTR_TARGET_TEMP_HIGH,
  22. ATTR_TARGET_TEMP_LOW,
  23. DEFAULT_MAX_HUMIDITY,
  24. DEFAULT_MAX_TEMP,
  25. DEFAULT_MIN_HUMIDITY,
  26. DEFAULT_MIN_TEMP,
  27. )
  28. from homeassistant.const import (
  29. ATTR_TEMPERATURE,
  30. PRECISION_TENTHS,
  31. PRECISION_WHOLE,
  32. UnitOfTemperature,
  33. )
  34. from .device import TuyaLocalDevice
  35. from .entity import TuyaLocalEntity, unit_from_ascii
  36. from .helpers.config import async_tuya_setup_platform
  37. from .helpers.device_config import TuyaEntityConfig
  38. _LOGGER = logging.getLogger(__name__)
  39. async def async_setup_entry(hass, config_entry, async_add_entities):
  40. config = {**config_entry.data, **config_entry.options}
  41. await async_tuya_setup_platform(
  42. hass,
  43. async_add_entities,
  44. config,
  45. "climate",
  46. TuyaLocalClimate,
  47. )
  48. def validate_temp_unit(unit):
  49. unit = unit_from_ascii(unit)
  50. try:
  51. return UnitOfTemperature(unit)
  52. except ValueError:
  53. if unit:
  54. _LOGGER.warning("%s is not a valid temperature unit", unit)
  55. class TuyaLocalClimate(TuyaLocalEntity, ClimateEntity):
  56. """Representation of a Tuya Climate entity."""
  57. def __init__(self, device: TuyaLocalDevice, config: TuyaEntityConfig):
  58. """
  59. Initialise the climate device.
  60. Args:
  61. device (TuyaLocalDevice): The device API instance.
  62. config (TuyaEntityConfig): The entity config.
  63. """
  64. super().__init__()
  65. dps_map = self._init_begin(device, config)
  66. self._current_temperature_dps = dps_map.pop(
  67. ATTR_CURRENT_TEMPERATURE,
  68. None,
  69. )
  70. self._current_humidity_dps = dps_map.pop(ATTR_CURRENT_HUMIDITY, None)
  71. self._fan_mode_dps = dps_map.pop(ATTR_FAN_MODE, None)
  72. self._humidity_dps = dps_map.pop(ATTR_HUMIDITY, None)
  73. self._hvac_mode_dps = dps_map.pop(ATTR_HVAC_MODE, None)
  74. self._hvac_action_dps = dps_map.pop(ATTR_HVAC_ACTION, None)
  75. self._preset_mode_dps = dps_map.pop(ATTR_PRESET_MODE, None)
  76. self._swing_horizontal_mode_dps = dps_map.pop(
  77. ATTR_SWING_HORIZONTAL_MODE,
  78. None,
  79. )
  80. self._swing_mode_dps = dps_map.pop(ATTR_SWING_MODE, None)
  81. self._temperature_dps = dps_map.pop(ATTR_TEMPERATURE, None)
  82. self._temp_high_dps = dps_map.pop(ATTR_TARGET_TEMP_HIGH, None)
  83. self._temp_low_dps = dps_map.pop(ATTR_TARGET_TEMP_LOW, None)
  84. self._unit_dps = dps_map.pop("temperature_unit", None)
  85. self._mintemp_dps = dps_map.pop("min_temperature", None)
  86. self._maxtemp_dps = dps_map.pop("max_temperature", None)
  87. self._init_end(dps_map)
  88. # Disable HA's backwards compatibility auto creation of turn_on/off
  89. # we explicitly define our own so this should have no effect, but
  90. # the deprecation notices in HA use this flag rather than properly
  91. # checking whether we are falling back on the auto-generation.
  92. self._enable_turn_on_off_backwards_compatibility = False
  93. if self._fan_mode_dps:
  94. self._attr_supported_features |= ClimateEntityFeature.FAN_MODE
  95. if self._humidity_dps:
  96. self._attr_supported_features |= ClimateEntityFeature.TARGET_HUMIDITY
  97. if self._preset_mode_dps:
  98. self._attr_supported_features |= ClimateEntityFeature.PRESET_MODE
  99. if self._swing_mode_dps:
  100. if self._swing_mode_dps.values(device):
  101. self._attr_supported_features |= ClimateEntityFeature.SWING_MODE
  102. if self._swing_horizontal_mode_dps:
  103. if self._swing_horizontal_mode_dps.values(device):
  104. self._attr_supported_features |= (
  105. ClimateEntityFeature.SWING_HORIZONTAL_MODE
  106. )
  107. if self._temp_high_dps and self._temp_low_dps:
  108. self._attr_supported_features |= (
  109. ClimateEntityFeature.TARGET_TEMPERATURE_RANGE
  110. )
  111. elif self._temperature_dps is not None:
  112. self._attr_supported_features |= ClimateEntityFeature.TARGET_TEMPERATURE
  113. if HVACMode.OFF in self.hvac_modes:
  114. self._attr_supported_features |= ClimateEntityFeature.TURN_OFF
  115. if self._hvac_mode_dps and self._hvac_mode_dps.type is bool:
  116. self._attr_supported_features |= ClimateEntityFeature.TURN_ON
  117. @property
  118. def temperature_unit(self):
  119. """Return the unit of measurement."""
  120. # If there is a separate DPS that returns the units, use that
  121. if self._unit_dps:
  122. unit = validate_temp_unit(self._unit_dps.get_value(self._device))
  123. # Only return valid units
  124. if unit:
  125. return unit
  126. # If there unit attribute configured in the temperature dps, use that
  127. if self._temperature_dps and self._temperature_dps.unit:
  128. unit = validate_temp_unit(self._temperature_dps.unit)
  129. if unit:
  130. return unit
  131. if self._temp_high_dps and self._temp_high_dps.unit:
  132. unit = validate_temp_unit(self._temp_high_dps.unit)
  133. if unit:
  134. return unit
  135. if self._temp_low_dps and self._temp_low_dps.unit:
  136. unit = validate_temp_unit(self._temp_low_dps.unit)
  137. if unit is not None:
  138. return unit
  139. if self._current_temperature_dps and self._current_temperature_dps.unit:
  140. unit = validate_temp_unit(self._current_temperature_dps.unit)
  141. if unit:
  142. return unit
  143. # Return the default unit
  144. return UnitOfTemperature.CELSIUS
  145. @property
  146. def precision(self):
  147. """Return the precision of the temperature setting."""
  148. # unlike sensor, this is a decimal of the smallest unit that can be
  149. # represented, not a number of decimal places.
  150. dp = self._temperature_dps or self._temp_high_dps
  151. temp = dp.scale(self._device) if dp else 1
  152. current = (
  153. self._current_temperature_dps.scale(self._device)
  154. if self._current_temperature_dps
  155. else 1
  156. )
  157. if max(temp, current) > 1.0:
  158. return PRECISION_TENTHS
  159. return PRECISION_WHOLE
  160. @property
  161. def target_temperature(self):
  162. """Return the currently set target temperature."""
  163. if self._temperature_dps is None:
  164. raise NotImplementedError()
  165. return self._temperature_dps.get_value(self._device)
  166. @property
  167. def target_temperature_high(self):
  168. """Return the currently set high target temperature."""
  169. if self._temp_high_dps is None:
  170. raise NotImplementedError()
  171. return self._temp_high_dps.get_value(self._device)
  172. @property
  173. def target_temperature_low(self):
  174. """Return the currently set low target temperature."""
  175. if self._temp_low_dps is None:
  176. raise NotImplementedError()
  177. return self._temp_low_dps.get_value(self._device)
  178. @property
  179. def target_temperature_step(self):
  180. """Return the supported step of target temperature."""
  181. dps = self._temperature_dps
  182. if dps is None:
  183. dps = self._temp_high_dps
  184. if dps is None:
  185. dps = self._temp_low_dps
  186. if dps is None:
  187. return 1
  188. return dps.step(self._device)
  189. @property
  190. def min_temp(self):
  191. """Return the minimum supported target temperature."""
  192. # if a separate min_temperature dps is specified, the device tells us.
  193. if self._mintemp_dps is not None:
  194. m = self._mintemp_dps.get_value(self._device)
  195. if m is not None:
  196. return m
  197. if self._temperature_dps is None:
  198. if self._temp_low_dps is None:
  199. return None
  200. r = self._temp_low_dps.range(self._device)
  201. else:
  202. r = self._temperature_dps.range(self._device)
  203. return DEFAULT_MIN_TEMP if r is None else r[0]
  204. @property
  205. def max_temp(self):
  206. """Return the maximum supported target temperature."""
  207. # if a separate max_temperature dps is specified, the device tells us.
  208. if self._maxtemp_dps is not None:
  209. m = self._maxtemp_dps.get_value(self._device)
  210. if m is not None:
  211. return m
  212. if self._temperature_dps is None:
  213. if self._temp_high_dps is None:
  214. return None
  215. r = self._temp_high_dps.range(self._device)
  216. else:
  217. r = self._temperature_dps.range(self._device)
  218. return DEFAULT_MAX_TEMP if r is None else r[1]
  219. async def async_set_temperature(self, **kwargs):
  220. """Set new target temperature."""
  221. async with self._device.set_lock:
  222. if kwargs.get(ATTR_PRESET_MODE) is not None:
  223. _LOGGER.info(
  224. "%s setting temperature: setting preset mode to %s",
  225. self._config.config_id,
  226. kwargs.get(ATTR_PRESET_MODE),
  227. )
  228. await self.async_set_preset_mode(kwargs.get(ATTR_PRESET_MODE))
  229. if kwargs.get(ATTR_TEMPERATURE) is not None:
  230. _LOGGER.info(
  231. "%s setting temperature to %s",
  232. self._config.config_id,
  233. kwargs.get(ATTR_TEMPERATURE),
  234. )
  235. await self.async_set_target_temperature(
  236. kwargs.get(ATTR_TEMPERATURE),
  237. )
  238. high = kwargs.get(ATTR_TARGET_TEMP_HIGH)
  239. low = kwargs.get(ATTR_TARGET_TEMP_LOW)
  240. if high is not None or low is not None:
  241. _LOGGER.info(
  242. "%s setting temperature range to %s - %s",
  243. self._config.config_id,
  244. low,
  245. high,
  246. )
  247. await self.async_set_target_temperature_range(low, high)
  248. async def async_set_target_temperature(self, target_temperature):
  249. if self._temperature_dps is None:
  250. raise NotImplementedError()
  251. async with self._device.set_lock:
  252. await self._temperature_dps.async_set_value(
  253. self._device,
  254. target_temperature,
  255. )
  256. async def async_set_target_temperature_range(self, low, high):
  257. """Set the target temperature range."""
  258. async with self._device.set_lock:
  259. dps_map = {}
  260. if low is not None and self._temp_low_dps is not None:
  261. dps_map.update(
  262. self._temp_low_dps.get_values_to_set(self._device, low, dps_map),
  263. )
  264. if high is not None and self._temp_high_dps is not None:
  265. dps_map.update(
  266. self._temp_high_dps.get_values_to_set(self._device, high, dps_map),
  267. )
  268. if dps_map:
  269. await self._device.async_set_properties(dps_map)
  270. @property
  271. def current_temperature(self):
  272. """Return the current measured temperature."""
  273. if self._current_temperature_dps:
  274. temp = self._current_temperature_dps.get_value(self._device)
  275. if self._current_temperature_dps.suggested_display_precision is not None:
  276. # Round the value to the suggested precision
  277. temp = round(
  278. temp, self._current_temperature_dps.suggested_display_precision
  279. )
  280. return temp
  281. @property
  282. def target_humidity(self):
  283. """Return the currently set target humidity."""
  284. if self._humidity_dps is None:
  285. raise NotImplementedError()
  286. return self._humidity_dps.get_value(self._device)
  287. @property
  288. def min_humidity(self):
  289. """Return the minimum supported target humidity."""
  290. if self._humidity_dps is None:
  291. return None
  292. r = self._humidity_dps.range(self._device)
  293. return DEFAULT_MIN_HUMIDITY if r is None else r[0]
  294. @property
  295. def max_humidity(self):
  296. """Return the maximum supported target humidity."""
  297. if self._humidity_dps is None:
  298. return None
  299. r = self._humidity_dps.range(self._device)
  300. return DEFAULT_MAX_HUMIDITY if r is None else r[1]
  301. async def async_set_humidity(self, humidity: int):
  302. if self._humidity_dps is None:
  303. raise NotImplementedError()
  304. async with self._device.set_lock:
  305. _LOGGER.info(
  306. "%s setting humidity to %s",
  307. self._config.config_id,
  308. humidity,
  309. )
  310. await self._humidity_dps.async_set_value(self._device, humidity)
  311. @property
  312. def current_humidity(self):
  313. """Return the current measured humidity."""
  314. if self._current_humidity_dps:
  315. return self._current_humidity_dps.get_value(self._device)
  316. @property
  317. def hvac_action(self):
  318. """Return the current HVAC action."""
  319. if self._hvac_action_dps is None:
  320. return None
  321. if self.hvac_mode is HVACMode.OFF:
  322. return HVACAction.OFF
  323. action = self._hvac_action_dps.get_value(self._device)
  324. try:
  325. return HVACAction(action) if action else None
  326. except ValueError:
  327. _LOGGER.warning(
  328. "%s/%s: Unrecognised HVAC Action %s ignored",
  329. self._config._device.config,
  330. self.name or "climate",
  331. action,
  332. )
  333. @property
  334. def hvac_mode(self):
  335. """Return current HVAC mode."""
  336. if self._hvac_mode_dps is None:
  337. return HVACMode.AUTO
  338. hvac_mode = self._hvac_mode_dps.get_value(self._device)
  339. try:
  340. return HVACMode(hvac_mode) if hvac_mode else None
  341. except ValueError:
  342. _LOGGER.warning(
  343. "%s/%s: Unrecognised HVAC Mode of %s ignored",
  344. self._config._device.config,
  345. self.name or "climate",
  346. hvac_mode,
  347. )
  348. @property
  349. def hvac_modes(self):
  350. """Return available HVAC modes."""
  351. if self._hvac_mode_dps is None:
  352. return [HVACMode.AUTO]
  353. else:
  354. return self._hvac_mode_dps.values(self._device)
  355. async def async_set_hvac_mode(self, hvac_mode):
  356. """Set new HVAC mode."""
  357. if self._hvac_mode_dps is None:
  358. raise NotImplementedError()
  359. async with self._device.set_lock:
  360. _LOGGER.info(
  361. "%s setting HVAC mode to %s",
  362. self._config.config_id,
  363. hvac_mode,
  364. )
  365. await self._hvac_mode_dps.async_set_value(self._device, hvac_mode)
  366. async def async_turn_on(self):
  367. """Turn on the climate device."""
  368. # Bypass the usual dps mapping to switch the power dp directly
  369. # this way the hvac_mode will be kept when toggling off and on.
  370. if self._hvac_mode_dps and self._hvac_mode_dps.type is bool:
  371. async with self._device.set_lock:
  372. _LOGGER.info("%s turning on", self._config.config_id)
  373. await self._device.async_set_property(self._hvac_mode_dps.id, True)
  374. else:
  375. await super().async_turn_on()
  376. async def async_turn_off(self):
  377. """Turn off the climate device."""
  378. # Bypass the usual dps mapping to switch the power dp directly
  379. # this way the hvac_mode will be kept when toggling off and on.
  380. if self._hvac_mode_dps and self._hvac_mode_dps.type is bool:
  381. async with self._device.set_lock:
  382. _LOGGER.info("%s turning off", self._config.config_id)
  383. await self._device.async_set_property(
  384. self._hvac_mode_dps.id,
  385. False,
  386. )
  387. else:
  388. await super().async_turn_off()
  389. @property
  390. def preset_mode(self):
  391. """Return the current preset mode."""
  392. if self._preset_mode_dps is None:
  393. raise NotImplementedError()
  394. return self._preset_mode_dps.get_value(self._device)
  395. @property
  396. def preset_modes(self):
  397. """Return the list of presets that this device supports."""
  398. if self._preset_mode_dps:
  399. return self._preset_mode_dps.values(self._device)
  400. async def async_set_preset_mode(self, preset_mode):
  401. """Set the preset mode."""
  402. if self._preset_mode_dps is None:
  403. raise NotImplementedError()
  404. async with self._device.set_lock:
  405. _LOGGER.info(
  406. "%s setting preset mode to %s",
  407. self._config.config_id,
  408. preset_mode,
  409. )
  410. await self._preset_mode_dps.async_set_value(self._device, preset_mode)
  411. @property
  412. def swing_mode(self):
  413. """Return the current swing mode."""
  414. if self._swing_mode_dps is None:
  415. raise NotImplementedError()
  416. return self._swing_mode_dps.get_value(self._device)
  417. @property
  418. def swing_modes(self):
  419. """Return the list of swing modes that this device supports."""
  420. if self._swing_mode_dps:
  421. return self._swing_mode_dps.values(self._device)
  422. async def async_set_swing_mode(self, swing_mode):
  423. """Set the preset mode."""
  424. if self._swing_mode_dps is None:
  425. raise NotImplementedError()
  426. async with self._device.set_lock:
  427. _LOGGER.info(
  428. "%s setting swing mode to %s",
  429. self._config.config_id,
  430. swing_mode,
  431. )
  432. await self._swing_mode_dps.async_set_value(self._device, swing_mode)
  433. @property
  434. def swing_horizontal_mode(self):
  435. """Return the current horizontal swing mode."""
  436. if self._swing_horizontal_mode_dps is None:
  437. raise NotImplementedError()
  438. return self._swing_horizontal_mode_dps.get_value(self._device)
  439. @property
  440. def swing_horizontal_modes(self):
  441. """Return the list of swing modes that this device supports."""
  442. if self._swing_horizontal_mode_dps:
  443. return self._swing_horizontal_mode_dps.values(self._device)
  444. async def async_set_swing_horizontal_mode(self, swing_mode):
  445. """Set the preset mode."""
  446. if self._swing_horizontal_mode_dps is None:
  447. raise NotImplementedError()
  448. async with self._device.set_lock:
  449. _LOGGER.info(
  450. "%s setting horizontal swing mode to %s",
  451. self._config.config_id,
  452. swing_mode,
  453. )
  454. await self._swing_horizontal_mode_dps.async_set_value(
  455. self._device,
  456. swing_mode,
  457. )
  458. @property
  459. def fan_mode(self):
  460. """Return the current fan mode."""
  461. if self._fan_mode_dps is None:
  462. raise NotImplementedError()
  463. return self._fan_mode_dps.get_value(self._device)
  464. @property
  465. def fan_modes(self):
  466. """Return the list of fan modes that this device supports."""
  467. if self._fan_mode_dps:
  468. return self._fan_mode_dps.values(self._device)
  469. async def async_set_fan_mode(self, fan_mode):
  470. """Set the fan mode."""
  471. if self._fan_mode_dps is None:
  472. raise NotImplementedError()
  473. async with self._device.set_lock:
  474. _LOGGER.info(
  475. "%s setting fan mode to %s",
  476. self._config.config_id,
  477. fan_mode,
  478. )
  479. await self._fan_mode_dps.async_set_value(self._device, fan_mode)