climate.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. """
  2. Setup for different kinds of Tuya climate devices
  3. """
  4. from homeassistant.components.climate import (
  5. ClimateEntity,
  6. ClimateEntityFeature,
  7. HVACAction,
  8. HVACMode,
  9. )
  10. from homeassistant.components.climate.const import (
  11. ATTR_AUX_HEAT,
  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_MODE,
  20. ATTR_TARGET_TEMP_HIGH,
  21. ATTR_TARGET_TEMP_LOW,
  22. DEFAULT_MAX_HUMIDITY,
  23. DEFAULT_MAX_TEMP,
  24. DEFAULT_MIN_HUMIDITY,
  25. DEFAULT_MIN_TEMP,
  26. )
  27. from homeassistant.const import (
  28. ATTR_TEMPERATURE,
  29. PRECISION_TENTHS,
  30. PRECISION_WHOLE,
  31. UnitOfTemperature,
  32. )
  33. import logging
  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. dps_map = self._init_begin(device, config)
  64. self._aux_heat_dps = dps_map.pop(ATTR_AUX_HEAT, None)
  65. self._current_temperature_dps = dps_map.pop(ATTR_CURRENT_TEMPERATURE, None)
  66. self._current_humidity_dps = dps_map.pop(ATTR_CURRENT_HUMIDITY, None)
  67. self._fan_mode_dps = dps_map.pop(ATTR_FAN_MODE, None)
  68. self._humidity_dps = dps_map.pop(ATTR_HUMIDITY, None)
  69. self._hvac_mode_dps = dps_map.pop(ATTR_HVAC_MODE, None)
  70. self._hvac_action_dps = dps_map.pop(ATTR_HVAC_ACTION, None)
  71. self._preset_mode_dps = dps_map.pop(ATTR_PRESET_MODE, None)
  72. self._swing_mode_dps = dps_map.pop(ATTR_SWING_MODE, None)
  73. self._temperature_dps = dps_map.pop(ATTR_TEMPERATURE, None)
  74. self._temp_high_dps = dps_map.pop(ATTR_TARGET_TEMP_HIGH, None)
  75. self._temp_low_dps = dps_map.pop(ATTR_TARGET_TEMP_LOW, None)
  76. self._unit_dps = dps_map.pop("temperature_unit", None)
  77. self._mintemp_dps = dps_map.pop("min_temperature", None)
  78. self._maxtemp_dps = dps_map.pop("max_temperature", None)
  79. self._init_end(dps_map)
  80. self._support_flags = 0
  81. if self._aux_heat_dps:
  82. self._support_flags |= ClimateEntityFeature.AUX_HEAT
  83. if self._fan_mode_dps:
  84. self._support_flags |= ClimateEntityFeature.FAN_MODE
  85. if self._humidity_dps:
  86. self._support_flags |= ClimateEntityFeature.TARGET_HUMIDITY
  87. if self._preset_mode_dps:
  88. self._support_flags |= ClimateEntityFeature.PRESET_MODE
  89. if self._swing_mode_dps:
  90. self._support_flags |= ClimateEntityFeature.SWING_MODE
  91. if self._temp_high_dps and self._temp_low_dps:
  92. self._support_flags |= ClimateEntityFeature.TARGET_TEMPERATURE_RANGE
  93. elif self._temperature_dps is not None:
  94. self._support_flags |= ClimateEntityFeature.TARGET_TEMPERATURE
  95. @property
  96. def supported_features(self):
  97. """Return the features supported by this climate device."""
  98. return self._support_flags
  99. @property
  100. def temperature_unit(self):
  101. """Return the unit of measurement."""
  102. # If there is a separate DPS that returns the units, use that
  103. if self._unit_dps is not None:
  104. unit = validate_temp_unit(self._unit_dps.get_value(self._device))
  105. # Only return valid units
  106. if unit is not None:
  107. return unit
  108. # If there unit attribute configured in the temperature dps, use that
  109. if self._temperature_dps:
  110. unit = validate_temp_unit(self._temperature_dps.unit)
  111. if unit is not None:
  112. return unit
  113. # Return the default unit
  114. return UnitOfTemperature.CELSIUS
  115. @property
  116. def precision(self):
  117. """Return the precision of the temperature setting."""
  118. # unlike sensor, this is a decimal of the smallest unit that can be
  119. # represented, not a number of decimal places.
  120. dp = self._temperature_dps or self._temp_high_dps
  121. temp = dp.scale(self._device) if dp else 1
  122. current = (
  123. self._current_temperature_dps.scale(self._device)
  124. if self._current_temperature_dps
  125. else 1
  126. )
  127. if max(temp, current) > 1.0:
  128. return PRECISION_TENTHS
  129. return PRECISION_WHOLE
  130. @property
  131. def target_temperature(self):
  132. """Return the currently set target temperature."""
  133. if self._temperature_dps is None:
  134. raise NotImplementedError()
  135. return self._temperature_dps.get_value(self._device)
  136. @property
  137. def target_temperature_high(self):
  138. """Return the currently set high target temperature."""
  139. if self._temp_high_dps is None:
  140. raise NotImplementedError()
  141. return self._temp_high_dps.get_value(self._device)
  142. @property
  143. def target_temperature_low(self):
  144. """Return the currently set low target temperature."""
  145. if self._temp_low_dps is None:
  146. raise NotImplementedError()
  147. return self._temp_low_dps.get_value(self._device)
  148. @property
  149. def target_temperature_step(self):
  150. """Return the supported step of target temperature."""
  151. dps = self._temperature_dps
  152. if dps is None:
  153. dps = self._temp_high_dps
  154. if dps is None:
  155. dps = self._temp_low_dps
  156. if dps is None:
  157. return 1
  158. return dps.step(self._device)
  159. @property
  160. def min_temp(self):
  161. """Return the minimum supported target temperature."""
  162. # if a separate min_temperature dps is specified, the device tells us.
  163. if self._mintemp_dps is not None:
  164. return self._mintemp_dps.get_value(self._device)
  165. if self._temperature_dps is None:
  166. if self._temp_low_dps is None:
  167. return None
  168. r = self._temp_low_dps.range(self._device)
  169. else:
  170. r = self._temperature_dps.range(self._device)
  171. return DEFAULT_MIN_TEMP if r is None else r["min"]
  172. @property
  173. def max_temp(self):
  174. """Return the maximum supported target temperature."""
  175. # if a separate max_temperature dps is specified, the device tells us.
  176. if self._maxtemp_dps is not None:
  177. return self._maxtemp_dps.get_value(self._device)
  178. if self._temperature_dps is None:
  179. if self._temp_high_dps is None:
  180. return None
  181. r = self._temp_high_dps.range(self._device)
  182. else:
  183. r = self._temperature_dps.range(self._device)
  184. return DEFAULT_MAX_TEMP if r is None else r["max"]
  185. async def async_set_temperature(self, **kwargs):
  186. """Set new target temperature."""
  187. if kwargs.get(ATTR_PRESET_MODE) is not None:
  188. await self.async_set_preset_mode(kwargs.get(ATTR_PRESET_MODE))
  189. if kwargs.get(ATTR_TEMPERATURE) is not None:
  190. await self.async_set_target_temperature(kwargs.get(ATTR_TEMPERATURE))
  191. high = kwargs.get(ATTR_TARGET_TEMP_HIGH)
  192. low = kwargs.get(ATTR_TARGET_TEMP_LOW)
  193. if high is not None or low is not None:
  194. await self.async_set_target_temperature_range(low, high)
  195. async def async_set_target_temperature(self, target_temperature):
  196. if self._temperature_dps is None:
  197. raise NotImplementedError()
  198. await self._temperature_dps.async_set_value(self._device, target_temperature)
  199. async def async_set_target_temperature_range(self, low, high):
  200. """Set the target temperature range."""
  201. dps_map = {}
  202. if low is not None and self._temp_low_dps is not None:
  203. dps_map.update(self._temp_low_dps.get_values_to_set(self._device, low))
  204. if high is not None and self._temp_high_dps is not None:
  205. dps_map.update(self._temp_high_dps.get_values_to_set(self._device, high))
  206. if dps_map:
  207. await self._device.async_set_properties(dps_map)
  208. @property
  209. def current_temperature(self):
  210. """Return the current measured temperature."""
  211. if self._current_temperature_dps is None:
  212. return None
  213. return self._current_temperature_dps.get_value(self._device)
  214. @property
  215. def target_humidity(self):
  216. """Return the currently set target humidity."""
  217. if self._humidity_dps is None:
  218. raise NotImplementedError()
  219. return self._humidity_dps.get_value(self._device)
  220. @property
  221. def min_humidity(self):
  222. """Return the minimum supported target humidity."""
  223. if self._humidity_dps is None:
  224. return None
  225. r = self._humidity_dps.range(self._device)
  226. return DEFAULT_MIN_HUMIDITY if r is None else r["min"]
  227. @property
  228. def max_humidity(self):
  229. """Return the maximum supported target humidity."""
  230. if self._humidity_dps is None:
  231. return None
  232. r = self._humidity_dps.range(self._device)
  233. return DEFAULT_MAX_HUMIDITY if r is None else r["max"]
  234. async def async_set_humidity(self, humidity: int):
  235. if self._humidity_dps is None:
  236. raise NotImplementedError()
  237. await self._humidity_dps.async_set_value(self._device, humidity)
  238. @property
  239. def current_humidity(self):
  240. """Return the current measured humidity."""
  241. if self._current_humidity_dps is None:
  242. return None
  243. return self._current_humidity_dps.get_value(self._device)
  244. @property
  245. def hvac_action(self):
  246. """Return the current HVAC action."""
  247. if self._hvac_action_dps is None:
  248. return None
  249. action = self._hvac_action_dps.get_value(self._device)
  250. try:
  251. return HVACAction(action) if action else None
  252. except ValueError:
  253. _LOGGER.warning(f"_Unrecognised HVAC Action {action} ignored")
  254. return None
  255. @property
  256. def hvac_mode(self):
  257. """Return current HVAC mode."""
  258. if self._hvac_mode_dps is None:
  259. return HVACMode.AUTO
  260. hvac_mode = self._hvac_mode_dps.get_value(self._device)
  261. try:
  262. return HVACMode(hvac_mode) if hvac_mode else None
  263. except ValueError:
  264. _LOGGER.warning(f"Unrecognised HVAC Mode of {hvac_mode} ignored")
  265. return None
  266. @property
  267. def hvac_modes(self):
  268. """Return available HVAC modes."""
  269. if self._hvac_mode_dps is None:
  270. return []
  271. else:
  272. return self._hvac_mode_dps.values(self._device)
  273. async def async_set_hvac_mode(self, hvac_mode):
  274. """Set new HVAC mode."""
  275. if self._hvac_mode_dps is None:
  276. raise NotImplementedError()
  277. await self._hvac_mode_dps.async_set_value(self._device, hvac_mode)
  278. async def async_turn_on(self):
  279. """Turn on the climate device."""
  280. # Bypass the usual dps mapping to switch the power dp directly
  281. # this way the hvac_mode will be kept when toggling off and on.
  282. if self._hvac_mode_dps and self._hvac_mode_dps.type is bool:
  283. await self._device.async_set_property(self._hvac_mode_dps.id, True)
  284. else:
  285. await super().async_turn_on()
  286. async def async_turn_off(self):
  287. """Turn off the climate device."""
  288. # Bypass the usual dps mapping to switch the power dp directly
  289. # this way the hvac_mode will be kept when toggling off and on.
  290. if self._hvac_mode_dps and self._hvac_mode_dps.type is bool:
  291. await self._device.async_set_property(self._hvac_mode_dps.id, False)
  292. else:
  293. await super().async_turn_off()
  294. @property
  295. def is_aux_heat(self):
  296. """Return state of aux heater"""
  297. if self._aux_heat_dps is None:
  298. return None
  299. else:
  300. return self._aux_heat_dps.get_value(self._device)
  301. async def async_turn_aux_heat_on(self):
  302. """Turn on aux heater."""
  303. if self._aux_heat_dps is None:
  304. raise NotImplementedError()
  305. await self._aux_heat_dps.async_set_value(self._device, True)
  306. async def async_turn_aux_heat_off(self):
  307. """Turn off aux heater."""
  308. if self._aux_heat_dps is None:
  309. raise NotImplementedError()
  310. await self._aux_heat_dps.async_set_value(self._device, False)
  311. @property
  312. def preset_mode(self):
  313. """Return the current preset mode."""
  314. if self._preset_mode_dps is None:
  315. raise NotImplementedError()
  316. return self._preset_mode_dps.get_value(self._device)
  317. @property
  318. def preset_modes(self):
  319. """Return the list of presets that this device supports."""
  320. if self._preset_mode_dps is None:
  321. return None
  322. return self._preset_mode_dps.values(self._device)
  323. async def async_set_preset_mode(self, preset_mode):
  324. """Set the preset mode."""
  325. if self._preset_mode_dps is None:
  326. raise NotImplementedError()
  327. await self._preset_mode_dps.async_set_value(self._device, preset_mode)
  328. @property
  329. def swing_mode(self):
  330. """Return the current swing mode."""
  331. if self._swing_mode_dps is None:
  332. raise NotImplementedError()
  333. return self._swing_mode_dps.get_value(self._device)
  334. @property
  335. def swing_modes(self):
  336. """Return the list of swing modes that this device supports."""
  337. if self._swing_mode_dps is None:
  338. return None
  339. return self._swing_mode_dps.values(self._device)
  340. async def async_set_swing_mode(self, swing_mode):
  341. """Set the preset mode."""
  342. if self._swing_mode_dps is None:
  343. raise NotImplementedError()
  344. await self._swing_mode_dps.async_set_value(self._device, swing_mode)
  345. @property
  346. def fan_mode(self):
  347. """Return the current fan mode."""
  348. if self._fan_mode_dps is None:
  349. raise NotImplementedError()
  350. return self._fan_mode_dps.get_value(self._device)
  351. @property
  352. def fan_modes(self):
  353. """Return the list of fan modes that this device supports."""
  354. if self._fan_mode_dps is None:
  355. return None
  356. return self._fan_mode_dps.values(self._device)
  357. async def async_set_fan_mode(self, fan_mode):
  358. """Set the fan mode."""
  359. if self._fan_mode_dps is None:
  360. raise NotImplementedError()
  361. await self._fan_mode_dps.async_set_value(self._device, fan_mode)