climate.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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._init_end(dps_map)
  66. self._support_flags = 0
  67. if self._aux_heat_dps:
  68. self._support_flags |= SUPPORT_AUX_HEAT
  69. if self._fan_mode_dps:
  70. self._support_flags |= SUPPORT_FAN_MODE
  71. if self._humidity_dps:
  72. self._support_flags |= SUPPORT_TARGET_HUMIDITY
  73. if self._preset_mode_dps:
  74. self._support_flags |= SUPPORT_PRESET_MODE
  75. if self._swing_mode_dps:
  76. self._support_flags |= SUPPORT_SWING_MODE
  77. if self._temp_high_dps and self._temp_low_dps:
  78. self._support_flags |= SUPPORT_TARGET_TEMPERATURE_RANGE
  79. elif self._temperature_dps is not None:
  80. self._support_flags |= SUPPORT_TARGET_TEMPERATURE
  81. @property
  82. def supported_features(self):
  83. """Return the features supported by this climate device."""
  84. return self._support_flags
  85. @property
  86. def temperature_unit(self):
  87. """Return the unit of measurement."""
  88. # If there is a separate DPS that returns the units, use that
  89. if self._unit_dps is not None:
  90. unit = self._unit_dps.get_value(self._device)
  91. # Only return valid units
  92. if unit == "C":
  93. return TEMP_CELSIUS
  94. elif unit == "F":
  95. return TEMP_FAHRENHEIT
  96. elif unit == "K":
  97. return TEMP_KELVIN
  98. # If there unit attribute configured in the temperature dps, use that
  99. if self._temperature_dps:
  100. unit = self._temperature_dps.unit
  101. # Only return valid units
  102. if unit == "C":
  103. return TEMP_CELSIUS
  104. elif unit == "F":
  105. return TEMP_FAHRENHEIT
  106. elif unit == "K":
  107. return TEMP_KELVIN
  108. # Return the default unit from the device
  109. return self._device.temperature_unit
  110. @property
  111. def target_temperature(self):
  112. """Return the currently set target temperature."""
  113. if self._temperature_dps is None:
  114. raise NotImplementedError()
  115. return self._temperature_dps.get_value(self._device)
  116. @property
  117. def target_temperature_high(self):
  118. """Return the currently set high target temperature."""
  119. if self._temp_high_dps is None:
  120. raise NotImplementedError()
  121. return self._temp_high_dps.get_value(self._device)
  122. @property
  123. def target_temperature_low(self):
  124. """Return the currently set low target temperature."""
  125. if self._temp_low_dps is None:
  126. raise NotImplementedError()
  127. return self._temp_low_dps.get_value(self._device)
  128. @property
  129. def target_temperature_step(self):
  130. """Return the supported step of target temperature."""
  131. dps = self._temperature_dps
  132. if dps is None:
  133. dps = self._temp_high_dps
  134. if dps is None:
  135. dps = self._temp_low_dps
  136. if dps is None:
  137. return 1
  138. return dps.step(self._device)
  139. @property
  140. def min_temp(self):
  141. """Return the minimum supported target temperature."""
  142. if self._temperature_dps is None:
  143. if self._temp_low_dps is None:
  144. return None
  145. r = self._temp_low_dps.range(self._device)
  146. else:
  147. r = self._temperature_dps.range(self._device)
  148. return DEFAULT_MIN_TEMP if r is None else r["min"]
  149. @property
  150. def max_temp(self):
  151. """Return the maximum supported target temperature."""
  152. if self._temperature_dps is None:
  153. if self._temp_high_dps is None:
  154. return None
  155. r = self._temp_high_dps.range(self._device)
  156. else:
  157. r = self._temperature_dps.range(self._device)
  158. return DEFAULT_MAX_TEMP if r is None else r["max"]
  159. async def async_set_temperature(self, **kwargs):
  160. """Set new target temperature."""
  161. if kwargs.get(ATTR_PRESET_MODE) is not None:
  162. await self.async_set_preset_mode(kwargs.get(ATTR_PRESET_MODE))
  163. if kwargs.get(ATTR_TEMPERATURE) is not None:
  164. await self.async_set_target_temperature(kwargs.get(ATTR_TEMPERATURE))
  165. high = kwargs.get(ATTR_TARGET_TEMP_HIGH)
  166. low = kwargs.get(ATTR_TARGET_TEMP_LOW)
  167. if high is not None or low is not None:
  168. await self.async_set_target_temperature_range(low, high)
  169. async def async_set_target_temperature(self, target_temperature):
  170. if self._temperature_dps is None:
  171. raise NotImplementedError()
  172. await self._temperature_dps.async_set_value(self._device, target_temperature)
  173. async def async_set_target_temperature_range(self, low, high):
  174. """Set the target temperature range."""
  175. dps_map = {}
  176. if low is not None and self._temp_low_dps is not None:
  177. dps_map.update(self._temp_low_dps.get_values_to_set(self._device, low))
  178. if high is not None and self._temp_high_dps is not None:
  179. dps_map.update(self._temp_high_dps.get_values_to_set(self._device, high))
  180. if dps_map:
  181. await self._device.async_set_properties(dps_map)
  182. @property
  183. def current_temperature(self):
  184. """Return the current measured temperature."""
  185. if self._current_temperature_dps is None:
  186. return None
  187. return self._current_temperature_dps.get_value(self._device)
  188. @property
  189. def target_humidity(self):
  190. """Return the currently set target humidity."""
  191. if self._humidity_dps is None:
  192. raise NotImplementedError()
  193. return self._humidity_dps.get_value(self._device)
  194. @property
  195. def min_humidity(self):
  196. """Return the minimum supported target humidity."""
  197. if self._humidity_dps is None:
  198. return None
  199. r = self._humidity_dps.range(self._device)
  200. return DEFAULT_MIN_HUMIDITY if r is None else r["min"]
  201. @property
  202. def max_humidity(self):
  203. """Return the maximum supported target humidity."""
  204. if self._humidity_dps is None:
  205. return None
  206. r = self._humidity_dps.range(self._device)
  207. return DEFAULT_MAX_HUMIDITY if r is None else r["max"]
  208. async def async_set_humidity(self, humidity: int):
  209. if self._humidity_dps is None:
  210. raise NotImplementedError()
  211. await self._humidity_dps.async_set_value(self._device, humidity)
  212. @property
  213. def current_humidity(self):
  214. """Return the current measured humidity."""
  215. if self._current_humidity_dps is None:
  216. return None
  217. return self._current_humidity_dps.get_value(self._device)
  218. @property
  219. def hvac_action(self):
  220. """Return the current HVAC action."""
  221. if self._hvac_action_dps is None:
  222. return None
  223. return self._hvac_action_dps.get_value(self._device)
  224. @property
  225. def hvac_mode(self):
  226. """Return current HVAC mode."""
  227. if self._hvac_mode_dps is None:
  228. return HVAC_MODE_AUTO
  229. hvac_mode = self._hvac_mode_dps.get_value(self._device)
  230. return STATE_UNAVAILABLE if hvac_mode is None else hvac_mode
  231. @property
  232. def hvac_modes(self):
  233. """Return available HVAC modes."""
  234. if self._hvac_mode_dps is None:
  235. return []
  236. else:
  237. return self._hvac_mode_dps.values(self._device)
  238. async def async_set_hvac_mode(self, hvac_mode):
  239. """Set new HVAC mode."""
  240. if self._hvac_mode_dps is None:
  241. raise NotImplementedError()
  242. await self._hvac_mode_dps.async_set_value(self._device, hvac_mode)
  243. @property
  244. def is_aux_heat(self):
  245. """Return state of aux heater"""
  246. if self._aux_heat_dps is None:
  247. return None
  248. else:
  249. return self._aux_heat_dps.get_value(self._device)
  250. async def async_turn_aux_heat_on(self):
  251. """Turn on aux heater."""
  252. if self._aux_heat_dps is None:
  253. raise NotImplementedError()
  254. await self._aux_heat_dps.async_set_value(self._device, True)
  255. async def async_turn_aux_heat_off(self):
  256. """Turn off aux heater."""
  257. if self._aux_heat_dps is None:
  258. raise NotImplementedError()
  259. await self._aux_heat_dps.async_set_value(self._device, False)
  260. @property
  261. def preset_mode(self):
  262. """Return the current preset mode."""
  263. if self._preset_mode_dps is None:
  264. raise NotImplementedError()
  265. return self._preset_mode_dps.get_value(self._device)
  266. @property
  267. def preset_modes(self):
  268. """Return the list of presets that this device supports."""
  269. if self._preset_mode_dps is None:
  270. return None
  271. return self._preset_mode_dps.values(self._device)
  272. async def async_set_preset_mode(self, preset_mode):
  273. """Set the preset mode."""
  274. if self._preset_mode_dps is None:
  275. raise NotImplementedError()
  276. await self._preset_mode_dps.async_set_value(self._device, preset_mode)
  277. @property
  278. def swing_mode(self):
  279. """Return the current swing mode."""
  280. if self._swing_mode_dps is None:
  281. raise NotImplementedError()
  282. return self._swing_mode_dps.get_value(self._device)
  283. @property
  284. def swing_modes(self):
  285. """Return the list of swing modes that this device supports."""
  286. if self._swing_mode_dps is None:
  287. return None
  288. return self._swing_mode_dps.values(self._device)
  289. async def async_set_swing_mode(self, swing_mode):
  290. """Set the preset mode."""
  291. if self._swing_mode_dps is None:
  292. raise NotImplementedError()
  293. await self._swing_mode_dps.async_set_value(self._device, swing_mode)
  294. @property
  295. def fan_mode(self):
  296. """Return the current fan mode."""
  297. if self._fan_mode_dps is None:
  298. raise NotImplementedError()
  299. return self._fan_mode_dps.get_value(self._device)
  300. @property
  301. def fan_modes(self):
  302. """Return the list of fan modes that this device supports."""
  303. if self._fan_mode_dps is None:
  304. return None
  305. return self._fan_mode_dps.values(self._device)
  306. async def async_set_fan_mode(self, fan_mode):
  307. """Set the fan mode."""
  308. if self._fan_mode_dps is None:
  309. raise NotImplementedError()
  310. await self._fan_mode_dps.async_set_value(self._device, fan_mode)