climate.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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_AUX_HEAT,
  13. ATTR_CURRENT_HUMIDITY,
  14. ATTR_CURRENT_TEMPERATURE,
  15. ATTR_FAN_MODE,
  16. ATTR_HUMIDITY,
  17. ATTR_HVAC_ACTION,
  18. ATTR_HVAC_MODE,
  19. ATTR_PRESET_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 .helpers.config import async_tuya_setup_platform
  36. from .helpers.device_config import TuyaEntityConfig
  37. from .helpers.mixin import TuyaLocalEntity, unit_from_ascii
  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. return None
  54. class TuyaLocalClimate(TuyaLocalEntity, ClimateEntity):
  55. """Representation of a Tuya Climate entity."""
  56. def __init__(self, device: TuyaLocalDevice, config: TuyaEntityConfig):
  57. """
  58. Initialise the climate device.
  59. Args:
  60. device (TuyaLocalDevice): The device API instance.
  61. config (TuyaEntityConfig): The entity config.
  62. """
  63. super().__init__()
  64. dps_map = self._init_begin(device, config)
  65. self._aux_heat_dps = dps_map.pop(ATTR_AUX_HEAT, None)
  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_mode_dps = dps_map.pop(ATTR_SWING_MODE, None)
  77. self._temperature_dps = dps_map.pop(ATTR_TEMPERATURE, None)
  78. self._temp_high_dps = dps_map.pop(ATTR_TARGET_TEMP_HIGH, None)
  79. self._temp_low_dps = dps_map.pop(ATTR_TARGET_TEMP_LOW, None)
  80. self._unit_dps = dps_map.pop("temperature_unit", None)
  81. self._mintemp_dps = dps_map.pop("min_temperature", None)
  82. self._maxtemp_dps = dps_map.pop("max_temperature", None)
  83. self._init_end(dps_map)
  84. self._support_flags = ClimateEntityFeature(0)
  85. if self._aux_heat_dps:
  86. self._support_flags |= ClimateEntityFeature.AUX_HEAT
  87. if self._fan_mode_dps:
  88. self._support_flags |= ClimateEntityFeature.FAN_MODE
  89. if self._humidity_dps:
  90. self._support_flags |= ClimateEntityFeature.TARGET_HUMIDITY
  91. if self._preset_mode_dps:
  92. self._support_flags |= ClimateEntityFeature.PRESET_MODE
  93. if self._swing_mode_dps:
  94. self._support_flags |= ClimateEntityFeature.SWING_MODE
  95. if self._temp_high_dps and self._temp_low_dps:
  96. self._support_flags |= ClimateEntityFeature.TARGET_TEMPERATURE_RANGE
  97. elif self._temperature_dps is not None:
  98. self._support_flags |= ClimateEntityFeature.TARGET_TEMPERATURE
  99. @property
  100. def supported_features(self):
  101. """Return the features supported by this climate device."""
  102. return self._support_flags
  103. @property
  104. def temperature_unit(self):
  105. """Return the unit of measurement."""
  106. # If there is a separate DPS that returns the units, use that
  107. if self._unit_dps is not None:
  108. unit = validate_temp_unit(self._unit_dps.get_value(self._device))
  109. # Only return valid units
  110. if unit is not None:
  111. return unit
  112. # If there unit attribute configured in the temperature dps, use that
  113. if self._temperature_dps:
  114. unit = validate_temp_unit(self._temperature_dps.unit)
  115. if unit is not None:
  116. return unit
  117. if self._temp_high_dps:
  118. unit = validate_temp_unit(self._temp_high_dps.unit)
  119. if unit is not None:
  120. return unit
  121. if self._temp_low_dps:
  122. unit = validate_temp_unit(self._temp_low_dps.unit)
  123. if unit is not None:
  124. return unit
  125. if self._current_temperature_dps:
  126. unit = validate_temp_unit(self._current_temperature_dps.unit)
  127. if unit is not None:
  128. return unit
  129. # Return the default unit
  130. return UnitOfTemperature.CELSIUS
  131. @property
  132. def precision(self):
  133. """Return the precision of the temperature setting."""
  134. # unlike sensor, this is a decimal of the smallest unit that can be
  135. # represented, not a number of decimal places.
  136. dp = self._temperature_dps or self._temp_high_dps
  137. temp = dp.scale(self._device) if dp else 1
  138. current = (
  139. self._current_temperature_dps.scale(self._device)
  140. if self._current_temperature_dps
  141. else 1
  142. )
  143. if max(temp, current) > 1.0:
  144. return PRECISION_TENTHS
  145. return PRECISION_WHOLE
  146. @property
  147. def target_temperature(self):
  148. """Return the currently set target temperature."""
  149. if self._temperature_dps is None:
  150. raise NotImplementedError()
  151. return self._temperature_dps.get_value(self._device)
  152. @property
  153. def target_temperature_high(self):
  154. """Return the currently set high target temperature."""
  155. if self._temp_high_dps is None:
  156. raise NotImplementedError()
  157. return self._temp_high_dps.get_value(self._device)
  158. @property
  159. def target_temperature_low(self):
  160. """Return the currently set low target temperature."""
  161. if self._temp_low_dps is None:
  162. raise NotImplementedError()
  163. return self._temp_low_dps.get_value(self._device)
  164. @property
  165. def target_temperature_step(self):
  166. """Return the supported step of target temperature."""
  167. dps = self._temperature_dps
  168. if dps is None:
  169. dps = self._temp_high_dps
  170. if dps is None:
  171. dps = self._temp_low_dps
  172. if dps is None:
  173. return 1
  174. return dps.step(self._device)
  175. @property
  176. def min_temp(self):
  177. """Return the minimum supported target temperature."""
  178. # if a separate min_temperature dps is specified, the device tells us.
  179. if self._mintemp_dps is not None:
  180. min = self._mintemp_dps.get_value(self._device)
  181. if min is not None:
  182. return min
  183. if self._temperature_dps is None:
  184. if self._temp_low_dps is None:
  185. return None
  186. r = self._temp_low_dps.range(self._device)
  187. else:
  188. r = self._temperature_dps.range(self._device)
  189. return DEFAULT_MIN_TEMP if r is None else r[0]
  190. @property
  191. def max_temp(self):
  192. """Return the maximum supported target temperature."""
  193. # if a separate max_temperature dps is specified, the device tells us.
  194. if self._maxtemp_dps is not None:
  195. max = self._maxtemp_dps.get_value(self._device)
  196. if max is not None:
  197. return max
  198. if self._temperature_dps is None:
  199. if self._temp_high_dps is None:
  200. return None
  201. r = self._temp_high_dps.range(self._device)
  202. else:
  203. r = self._temperature_dps.range(self._device)
  204. return DEFAULT_MAX_TEMP if r is None else r[1]
  205. async def async_set_temperature(self, **kwargs):
  206. """Set new target temperature."""
  207. if kwargs.get(ATTR_PRESET_MODE) is not None:
  208. await self.async_set_preset_mode(kwargs.get(ATTR_PRESET_MODE))
  209. if kwargs.get(ATTR_TEMPERATURE) is not None:
  210. await self.async_set_target_temperature(
  211. kwargs.get(ATTR_TEMPERATURE),
  212. )
  213. high = kwargs.get(ATTR_TARGET_TEMP_HIGH)
  214. low = kwargs.get(ATTR_TARGET_TEMP_LOW)
  215. if high is not None or low is not None:
  216. await self.async_set_target_temperature_range(low, high)
  217. async def async_set_target_temperature(self, target_temperature):
  218. if self._temperature_dps is None:
  219. raise NotImplementedError()
  220. await self._temperature_dps.async_set_value(
  221. self._device,
  222. target_temperature,
  223. )
  224. async def async_set_target_temperature_range(self, low, high):
  225. """Set the target temperature range."""
  226. dps_map = {}
  227. if low is not None and self._temp_low_dps is not None:
  228. dps_map.update(
  229. self._temp_low_dps.get_values_to_set(self._device, low),
  230. )
  231. if high is not None and self._temp_high_dps is not None:
  232. dps_map.update(
  233. self._temp_high_dps.get_values_to_set(self._device, high),
  234. )
  235. if dps_map:
  236. await self._device.async_set_properties(dps_map)
  237. @property
  238. def current_temperature(self):
  239. """Return the current measured temperature."""
  240. if self._current_temperature_dps is None:
  241. return None
  242. return self._current_temperature_dps.get_value(self._device)
  243. @property
  244. def target_humidity(self):
  245. """Return the currently set target humidity."""
  246. if self._humidity_dps is None:
  247. raise NotImplementedError()
  248. return self._humidity_dps.get_value(self._device)
  249. @property
  250. def min_humidity(self):
  251. """Return the minimum supported target humidity."""
  252. if self._humidity_dps is None:
  253. return None
  254. r = self._humidity_dps.range(self._device)
  255. return DEFAULT_MIN_HUMIDITY if r is None else r[0]
  256. @property
  257. def max_humidity(self):
  258. """Return the maximum supported target humidity."""
  259. if self._humidity_dps is None:
  260. return None
  261. r = self._humidity_dps.range(self._device)
  262. return DEFAULT_MAX_HUMIDITY if r is None else r[1]
  263. async def async_set_humidity(self, humidity: int):
  264. if self._humidity_dps is None:
  265. raise NotImplementedError()
  266. await self._humidity_dps.async_set_value(self._device, humidity)
  267. @property
  268. def current_humidity(self):
  269. """Return the current measured humidity."""
  270. if self._current_humidity_dps is None:
  271. return None
  272. return self._current_humidity_dps.get_value(self._device)
  273. @property
  274. def hvac_action(self):
  275. """Return the current HVAC action."""
  276. if self._hvac_action_dps is None:
  277. return None
  278. action = self._hvac_action_dps.get_value(self._device)
  279. try:
  280. return HVACAction(action) if action else None
  281. except ValueError:
  282. _LOGGER.warning(
  283. "%s/%s: Unrecognised HVAC Action %s ignored",
  284. self._config._device.config,
  285. self.name or "climate",
  286. action,
  287. )
  288. return None
  289. @property
  290. def hvac_mode(self):
  291. """Return current HVAC mode."""
  292. if self._hvac_mode_dps is None:
  293. return HVACMode.AUTO
  294. hvac_mode = self._hvac_mode_dps.get_value(self._device)
  295. try:
  296. return HVACMode(hvac_mode) if hvac_mode else None
  297. except ValueError:
  298. _LOGGER.warning(
  299. "%s/%s: Unrecognised HVAC Mode of %s ignored",
  300. self._config._device.config,
  301. self.name or "climate",
  302. hvac_mode,
  303. )
  304. return None
  305. @property
  306. def hvac_modes(self):
  307. """Return available HVAC modes."""
  308. if self._hvac_mode_dps is None:
  309. return [HVACMode.AUTO]
  310. else:
  311. return self._hvac_mode_dps.values(self._device)
  312. async def async_set_hvac_mode(self, hvac_mode):
  313. """Set new HVAC mode."""
  314. if self._hvac_mode_dps is None:
  315. raise NotImplementedError()
  316. await self._hvac_mode_dps.async_set_value(self._device, hvac_mode)
  317. async def async_turn_on(self):
  318. """Turn on the climate device."""
  319. # Bypass the usual dps mapping to switch the power dp directly
  320. # this way the hvac_mode will be kept when toggling off and on.
  321. if self._hvac_mode_dps and self._hvac_mode_dps.type is bool:
  322. await self._device.async_set_property(self._hvac_mode_dps.id, True)
  323. else:
  324. await super().async_turn_on()
  325. async def async_turn_off(self):
  326. """Turn off the climate device."""
  327. # Bypass the usual dps mapping to switch the power dp directly
  328. # this way the hvac_mode will be kept when toggling off and on.
  329. if self._hvac_mode_dps and self._hvac_mode_dps.type is bool:
  330. await self._device.async_set_property(
  331. self._hvac_mode_dps.id,
  332. False,
  333. )
  334. else:
  335. await super().async_turn_off()
  336. @property
  337. def is_aux_heat(self):
  338. """Return state of aux heater"""
  339. if self._aux_heat_dps is None:
  340. return None
  341. else:
  342. return self._aux_heat_dps.get_value(self._device)
  343. async def async_turn_aux_heat_on(self):
  344. """Turn on aux heater."""
  345. if self._aux_heat_dps is None:
  346. raise NotImplementedError()
  347. await self._aux_heat_dps.async_set_value(self._device, True)
  348. async def async_turn_aux_heat_off(self):
  349. """Turn off aux heater."""
  350. if self._aux_heat_dps is None:
  351. raise NotImplementedError()
  352. await self._aux_heat_dps.async_set_value(self._device, False)
  353. @property
  354. def preset_mode(self):
  355. """Return the current preset mode."""
  356. if self._preset_mode_dps is None:
  357. raise NotImplementedError()
  358. return self._preset_mode_dps.get_value(self._device)
  359. @property
  360. def preset_modes(self):
  361. """Return the list of presets that this device supports."""
  362. if self._preset_mode_dps is None:
  363. return None
  364. return self._preset_mode_dps.values(self._device)
  365. async def async_set_preset_mode(self, preset_mode):
  366. """Set the preset mode."""
  367. if self._preset_mode_dps is None:
  368. raise NotImplementedError()
  369. await self._preset_mode_dps.async_set_value(self._device, preset_mode)
  370. @property
  371. def swing_mode(self):
  372. """Return the current swing mode."""
  373. if self._swing_mode_dps is None:
  374. raise NotImplementedError()
  375. return self._swing_mode_dps.get_value(self._device)
  376. @property
  377. def swing_modes(self):
  378. """Return the list of swing modes that this device supports."""
  379. if self._swing_mode_dps is None:
  380. return None
  381. return self._swing_mode_dps.values(self._device)
  382. async def async_set_swing_mode(self, swing_mode):
  383. """Set the preset mode."""
  384. if self._swing_mode_dps is None:
  385. raise NotImplementedError()
  386. await self._swing_mode_dps.async_set_value(self._device, swing_mode)
  387. @property
  388. def fan_mode(self):
  389. """Return the current fan mode."""
  390. if self._fan_mode_dps is None:
  391. raise NotImplementedError()
  392. return self._fan_mode_dps.get_value(self._device)
  393. @property
  394. def fan_modes(self):
  395. """Return the list of fan modes that this device supports."""
  396. if self._fan_mode_dps is None:
  397. return None
  398. return self._fan_mode_dps.values(self._device)
  399. async def async_set_fan_mode(self, fan_mode):
  400. """Set the fan mode."""
  401. if self._fan_mode_dps is None:
  402. raise NotImplementedError()
  403. await self._fan_mode_dps.async_set_value(self._device, fan_mode)