climate.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. """
  2. Platform to control tuya climate devices.
  3. """
  4. import logging
  5. from homeassistant.components.climate import ClimateEntity
  6. from homeassistant.components.climate.const import (
  7. ATTR_AUX_HEAT,
  8. ATTR_CURRENT_HUMIDITY,
  9. ATTR_CURRENT_TEMPERATURE,
  10. ATTR_FAN_MODE,
  11. ATTR_HUMIDITY,
  12. ATTR_HVAC_ACTION,
  13. ATTR_HVAC_MODE,
  14. ATTR_PRESET_MODE,
  15. ATTR_SWING_MODE,
  16. ATTR_TARGET_TEMP_HIGH,
  17. ATTR_TARGET_TEMP_LOW,
  18. DEFAULT_MAX_HUMIDITY,
  19. DEFAULT_MAX_TEMP,
  20. DEFAULT_MIN_HUMIDITY,
  21. DEFAULT_MIN_TEMP,
  22. HVAC_MODE_AUTO,
  23. SUPPORT_AUX_HEAT,
  24. SUPPORT_FAN_MODE,
  25. SUPPORT_PRESET_MODE,
  26. SUPPORT_SWING_MODE,
  27. SUPPORT_TARGET_HUMIDITY,
  28. SUPPORT_TARGET_TEMPERATURE,
  29. SUPPORT_TARGET_TEMPERATURE_RANGE,
  30. )
  31. from homeassistant.const import (
  32. ATTR_TEMPERATURE,
  33. STATE_UNAVAILABLE,
  34. TEMP_CELSIUS,
  35. TEMP_FAHRENHEIT,
  36. TEMP_KELVIN,
  37. )
  38. from ..device import TuyaLocalDevice
  39. from ..helpers.device_config import TuyaEntityConfig
  40. from ..helpers.mixin import TuyaLocalEntity
  41. _LOGGER = logging.getLogger(__name__)
  42. class TuyaLocalClimate(TuyaLocalEntity, ClimateEntity):
  43. """Representation of a Tuya Climate entity."""
  44. def __init__(self, device: TuyaLocalDevice, config: TuyaEntityConfig):
  45. """
  46. Initialise the climate device.
  47. Args:
  48. device (TuyaLocalDevice): The device API instance.
  49. config (TuyaEntityConfig): The entity config.
  50. """
  51. dps_map = self._init_begin(device, config)
  52. self._aux_heat_dps = dps_map.pop(ATTR_AUX_HEAT, None)
  53. self._current_temperature_dps = dps_map.pop(ATTR_CURRENT_TEMPERATURE, None)
  54. self._current_humidity_dps = dps_map.pop(ATTR_CURRENT_HUMIDITY, None)
  55. self._fan_mode_dps = dps_map.pop(ATTR_FAN_MODE, None)
  56. self._humidity_dps = dps_map.pop(ATTR_HUMIDITY, None)
  57. self._hvac_mode_dps = dps_map.pop(ATTR_HVAC_MODE, None)
  58. self._hvac_action_dps = dps_map.pop(ATTR_HVAC_ACTION, None)
  59. self._preset_mode_dps = dps_map.pop(ATTR_PRESET_MODE, None)
  60. self._swing_mode_dps = dps_map.pop(ATTR_SWING_MODE, None)
  61. self._temperature_dps = dps_map.pop(ATTR_TEMPERATURE, None)
  62. self._temp_high_dps = dps_map.pop(ATTR_TARGET_TEMP_HIGH, None)
  63. self._temp_low_dps = dps_map.pop(ATTR_TARGET_TEMP_LOW, None)
  64. self._unit_dps = dps_map.pop("temperature_unit", None)
  65. self._mintemp_dps = dps_map.pop("min_temperature", None)
  66. self._maxtemp_dps = dps_map.pop("max_temperature", None)
  67. self._init_end(dps_map)
  68. self._support_flags = 0
  69. if self._aux_heat_dps:
  70. self._support_flags |= SUPPORT_AUX_HEAT
  71. if self._fan_mode_dps:
  72. self._support_flags |= SUPPORT_FAN_MODE
  73. if self._humidity_dps:
  74. self._support_flags |= SUPPORT_TARGET_HUMIDITY
  75. if self._preset_mode_dps:
  76. self._support_flags |= SUPPORT_PRESET_MODE
  77. if self._swing_mode_dps:
  78. self._support_flags |= SUPPORT_SWING_MODE
  79. if self._temp_high_dps and self._temp_low_dps:
  80. self._support_flags |= SUPPORT_TARGET_TEMPERATURE_RANGE
  81. elif self._temperature_dps is not None:
  82. self._support_flags |= SUPPORT_TARGET_TEMPERATURE
  83. @property
  84. def supported_features(self):
  85. """Return the features supported by this climate device."""
  86. return self._support_flags
  87. @property
  88. def temperature_unit(self):
  89. """Return the unit of measurement."""
  90. # If there is a separate DPS that returns the units, use that
  91. if self._unit_dps is not None:
  92. unit = self._unit_dps.get_value(self._device)
  93. # Only return valid units
  94. if unit == "C":
  95. return TEMP_CELSIUS
  96. elif unit == "F":
  97. return TEMP_FAHRENHEIT
  98. elif unit == "K":
  99. return TEMP_KELVIN
  100. # If there unit attribute configured in the temperature dps, use that
  101. if self._temperature_dps:
  102. unit = self._temperature_dps.unit
  103. # Only return valid units
  104. if unit == "C":
  105. return TEMP_CELSIUS
  106. elif unit == "F":
  107. return TEMP_FAHRENHEIT
  108. elif unit == "K":
  109. return TEMP_KELVIN
  110. # Return the default unit from the device
  111. return self._device.temperature_unit
  112. @property
  113. def target_temperature(self):
  114. """Return the currently set target temperature."""
  115. if self._temperature_dps is None:
  116. raise NotImplementedError()
  117. return self._temperature_dps.get_value(self._device)
  118. @property
  119. def target_temperature_high(self):
  120. """Return the currently set high target temperature."""
  121. if self._temp_high_dps is None:
  122. raise NotImplementedError()
  123. return self._temp_high_dps.get_value(self._device)
  124. @property
  125. def target_temperature_low(self):
  126. """Return the currently set low target temperature."""
  127. if self._temp_low_dps is None:
  128. raise NotImplementedError()
  129. return self._temp_low_dps.get_value(self._device)
  130. @property
  131. def target_temperature_step(self):
  132. """Return the supported step of target temperature."""
  133. dps = self._temperature_dps
  134. if dps is None:
  135. dps = self._temp_high_dps
  136. if dps is None:
  137. dps = self._temp_low_dps
  138. if dps is None:
  139. return 1
  140. return dps.step(self._device)
  141. @property
  142. def min_temp(self):
  143. """Return the minimum supported target temperature."""
  144. # if a separate min_temperature dps is specified, the device tells us.
  145. if self._mintemp_dps is not None:
  146. return self._mintemp_dps.get_value(self._device)
  147. if self._temperature_dps is None:
  148. if self._temp_low_dps is None:
  149. return None
  150. r = self._temp_low_dps.range(self._device)
  151. else:
  152. r = self._temperature_dps.range(self._device)
  153. return DEFAULT_MIN_TEMP if r is None else r["min"]
  154. @property
  155. def max_temp(self):
  156. """Return the maximum supported target temperature."""
  157. # if a separate max_temperature dps is specified, the device tells us.
  158. if self._maxtemp_dps is not None:
  159. return self._maxtemp_dps.get_value(self._device)
  160. if self._temperature_dps is None:
  161. if self._temp_high_dps is None:
  162. return None
  163. r = self._temp_high_dps.range(self._device)
  164. else:
  165. r = self._temperature_dps.range(self._device)
  166. return DEFAULT_MAX_TEMP if r is None else r["max"]
  167. async def async_set_temperature(self, **kwargs):
  168. """Set new target temperature."""
  169. if kwargs.get(ATTR_PRESET_MODE) is not None:
  170. await self.async_set_preset_mode(kwargs.get(ATTR_PRESET_MODE))
  171. if kwargs.get(ATTR_TEMPERATURE) is not None:
  172. await self.async_set_target_temperature(kwargs.get(ATTR_TEMPERATURE))
  173. high = kwargs.get(ATTR_TARGET_TEMP_HIGH)
  174. low = kwargs.get(ATTR_TARGET_TEMP_LOW)
  175. if high is not None or low is not None:
  176. await self.async_set_target_temperature_range(low, high)
  177. async def async_set_target_temperature(self, target_temperature):
  178. if self._temperature_dps is None:
  179. raise NotImplementedError()
  180. await self._temperature_dps.async_set_value(self._device, target_temperature)
  181. async def async_set_target_temperature_range(self, low, high):
  182. """Set the target temperature range."""
  183. dps_map = {}
  184. if low is not None and self._temp_low_dps is not None:
  185. dps_map.update(self._temp_low_dps.get_values_to_set(self._device, low))
  186. if high is not None and self._temp_high_dps is not None:
  187. dps_map.update(self._temp_high_dps.get_values_to_set(self._device, high))
  188. if dps_map:
  189. await self._device.async_set_properties(dps_map)
  190. @property
  191. def current_temperature(self):
  192. """Return the current measured temperature."""
  193. if self._current_temperature_dps is None:
  194. return None
  195. return self._current_temperature_dps.get_value(self._device)
  196. @property
  197. def target_humidity(self):
  198. """Return the currently set target humidity."""
  199. if self._humidity_dps is None:
  200. raise NotImplementedError()
  201. return self._humidity_dps.get_value(self._device)
  202. @property
  203. def min_humidity(self):
  204. """Return the minimum supported target humidity."""
  205. if self._humidity_dps is None:
  206. return None
  207. r = self._humidity_dps.range(self._device)
  208. return DEFAULT_MIN_HUMIDITY if r is None else r["min"]
  209. @property
  210. def max_humidity(self):
  211. """Return the maximum supported target humidity."""
  212. if self._humidity_dps is None:
  213. return None
  214. r = self._humidity_dps.range(self._device)
  215. return DEFAULT_MAX_HUMIDITY if r is None else r["max"]
  216. async def async_set_humidity(self, humidity: int):
  217. if self._humidity_dps is None:
  218. raise NotImplementedError()
  219. await self._humidity_dps.async_set_value(self._device, humidity)
  220. @property
  221. def current_humidity(self):
  222. """Return the current measured humidity."""
  223. if self._current_humidity_dps is None:
  224. return None
  225. return self._current_humidity_dps.get_value(self._device)
  226. @property
  227. def hvac_action(self):
  228. """Return the current HVAC action."""
  229. if self._hvac_action_dps is None:
  230. return None
  231. return self._hvac_action_dps.get_value(self._device)
  232. @property
  233. def hvac_mode(self):
  234. """Return current HVAC mode."""
  235. if self._hvac_mode_dps is None:
  236. return HVAC_MODE_AUTO
  237. hvac_mode = self._hvac_mode_dps.get_value(self._device)
  238. return STATE_UNAVAILABLE if hvac_mode is None else hvac_mode
  239. @property
  240. def hvac_modes(self):
  241. """Return available HVAC modes."""
  242. if self._hvac_mode_dps is None:
  243. return []
  244. else:
  245. return self._hvac_mode_dps.values(self._device)
  246. async def async_set_hvac_mode(self, hvac_mode):
  247. """Set new HVAC mode."""
  248. if self._hvac_mode_dps is None:
  249. raise NotImplementedError()
  250. await self._hvac_mode_dps.async_set_value(self._device, hvac_mode)
  251. @property
  252. def is_aux_heat(self):
  253. """Return state of aux heater"""
  254. if self._aux_heat_dps is None:
  255. return None
  256. else:
  257. return self._aux_heat_dps.get_value(self._device)
  258. async def async_turn_aux_heat_on(self):
  259. """Turn on aux heater."""
  260. if self._aux_heat_dps is None:
  261. raise NotImplementedError()
  262. await self._aux_heat_dps.async_set_value(self._device, True)
  263. async def async_turn_aux_heat_off(self):
  264. """Turn off aux heater."""
  265. if self._aux_heat_dps is None:
  266. raise NotImplementedError()
  267. await self._aux_heat_dps.async_set_value(self._device, False)
  268. @property
  269. def preset_mode(self):
  270. """Return the current preset mode."""
  271. if self._preset_mode_dps is None:
  272. raise NotImplementedError()
  273. return self._preset_mode_dps.get_value(self._device)
  274. @property
  275. def preset_modes(self):
  276. """Return the list of presets that this device supports."""
  277. if self._preset_mode_dps is None:
  278. return None
  279. return self._preset_mode_dps.values(self._device)
  280. async def async_set_preset_mode(self, preset_mode):
  281. """Set the preset mode."""
  282. if self._preset_mode_dps is None:
  283. raise NotImplementedError()
  284. await self._preset_mode_dps.async_set_value(self._device, preset_mode)
  285. @property
  286. def swing_mode(self):
  287. """Return the current swing mode."""
  288. if self._swing_mode_dps is None:
  289. raise NotImplementedError()
  290. return self._swing_mode_dps.get_value(self._device)
  291. @property
  292. def swing_modes(self):
  293. """Return the list of swing modes that this device supports."""
  294. if self._swing_mode_dps is None:
  295. return None
  296. return self._swing_mode_dps.values(self._device)
  297. async def async_set_swing_mode(self, swing_mode):
  298. """Set the preset mode."""
  299. if self._swing_mode_dps is None:
  300. raise NotImplementedError()
  301. await self._swing_mode_dps.async_set_value(self._device, swing_mode)
  302. @property
  303. def fan_mode(self):
  304. """Return the current fan mode."""
  305. if self._fan_mode_dps is None:
  306. raise NotImplementedError()
  307. return self._fan_mode_dps.get_value(self._device)
  308. @property
  309. def fan_modes(self):
  310. """Return the list of fan modes that this device supports."""
  311. if self._fan_mode_dps is None:
  312. return None
  313. return self._fan_mode_dps.values(self._device)
  314. async def async_set_fan_mode(self, fan_mode):
  315. """Set the fan mode."""
  316. if self._fan_mode_dps is None:
  317. raise NotImplementedError()
  318. await self._fan_mode_dps.async_set_value(self._device, fan_mode)