climate.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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. HVAC_MODE_OFF,
  24. SUPPORT_AUX_HEAT,
  25. SUPPORT_FAN_MODE,
  26. SUPPORT_PRESET_MODE,
  27. SUPPORT_SWING_MODE,
  28. SUPPORT_TARGET_HUMIDITY,
  29. SUPPORT_TARGET_TEMPERATURE,
  30. SUPPORT_TARGET_TEMPERATURE_RANGE,
  31. )
  32. from homeassistant.const import (
  33. ATTR_TEMPERATURE,
  34. STATE_UNAVAILABLE,
  35. TEMP_CELSIUS,
  36. TEMP_FAHRENHEIT,
  37. TEMP_KELVIN,
  38. )
  39. from ..device import TuyaLocalDevice
  40. from ..helpers.device_config import TuyaEntityConfig
  41. _LOGGER = logging.getLogger(__name__)
  42. class TuyaLocalClimate(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. self._device = device
  52. self._config = config
  53. self._support_flags = 0
  54. self._attr_dps = []
  55. dps_map = {c.name: c for c in config.dps()}
  56. self._aux_heat_dps = dps_map.pop(ATTR_AUX_HEAT, None)
  57. self._current_temperature_dps = dps_map.pop(ATTR_CURRENT_TEMPERATURE, None)
  58. self._current_humidity_dps = dps_map.pop(ATTR_CURRENT_HUMIDITY, None)
  59. self._fan_mode_dps = dps_map.pop(ATTR_FAN_MODE, None)
  60. self._humidity_dps = dps_map.pop(ATTR_HUMIDITY, None)
  61. self._hvac_mode_dps = dps_map.pop(ATTR_HVAC_MODE, None)
  62. self._hvac_action_dps = dps_map.pop(ATTR_HVAC_ACTION, None)
  63. self._preset_mode_dps = dps_map.pop(ATTR_PRESET_MODE, None)
  64. self._swing_mode_dps = dps_map.pop(ATTR_SWING_MODE, None)
  65. self._temperature_dps = dps_map.pop(ATTR_TEMPERATURE, None)
  66. self._temp_high_dps = dps_map.pop(ATTR_TARGET_TEMP_HIGH, None)
  67. self._temp_low_dps = dps_map.pop(ATTR_TARGET_TEMP_LOW, None)
  68. self._unit_dps = dps_map.pop("temperature_unit", None)
  69. for d in dps_map.values():
  70. if not d.hidden:
  71. self._attr_dps.append(d)
  72. if self._aux_heat_dps:
  73. self._support_flags |= SUPPORT_AUX_HEAT
  74. if self._fan_mode_dps:
  75. self._support_flags |= SUPPORT_FAN_MODE
  76. if self._humidity_dps:
  77. self._support_flags |= SUPPORT_TARGET_HUMIDITY
  78. if self._preset_mode_dps:
  79. self._support_flags |= SUPPORT_PRESET_MODE
  80. if self._swing_mode_dps:
  81. self._support_flags |= SUPPORT_SWING_MODE
  82. if self._temp_high_dps and self._temp_low_dps:
  83. self._support_flags |= SUPPORT_TARGET_TEMPERATURE_RANGE
  84. elif self._temperature_dps is not None:
  85. self._support_flags |= SUPPORT_TARGET_TEMPERATURE
  86. @property
  87. def supported_features(self):
  88. """Return the features supported by this climate device."""
  89. return self._support_flags
  90. @property
  91. def should_poll(self):
  92. """Return the polling state."""
  93. return True
  94. @property
  95. def name(self):
  96. """Return the name of the climate entity for the UI."""
  97. return self._config.name(self._device.name)
  98. @property
  99. def unique_id(self):
  100. """Return the unique id for this climate device."""
  101. return self._config.unique_id(self._device.unique_id)
  102. @property
  103. def device_info(self):
  104. """Return device information about this heater."""
  105. return self._device.device_info
  106. @property
  107. def icon(self):
  108. """Return the icon to use in the frontend for this device."""
  109. icon = self._config.icon(self._device)
  110. if icon:
  111. return icon
  112. else:
  113. return super().icon
  114. @property
  115. def temperature_unit(self):
  116. """Return the unit of measurement."""
  117. # If there is a separate DPS that returns the units, use that
  118. if self._unit_dps is not None:
  119. unit = self._unit_dps.get_value(self._device)
  120. # Only return valid units
  121. if unit == "C":
  122. return TEMP_CELSIUS
  123. elif unit == "F":
  124. return TEMP_FAHRENHEIT
  125. elif unit == "K":
  126. return TEMP_KELVIN
  127. # If there unit attribute configured in the temperature dps, use that
  128. if self._temperature_dps:
  129. unit = self._temperature_dps.unit
  130. # Only return valid units
  131. if unit == "C":
  132. return TEMP_CELSIUS
  133. elif unit == "F":
  134. return TEMP_FAHRENHEIT
  135. elif unit == "K":
  136. return TEMP_KELVIN
  137. # Return the default unit from the device
  138. return self._device.temperature_unit
  139. @property
  140. def target_temperature(self):
  141. """Return the currently set target temperature."""
  142. if self._temperature_dps is None:
  143. raise NotImplementedError()
  144. return self._temperature_dps.get_value(self._device)
  145. @property
  146. def target_temperature_high(self):
  147. """Return the currently set high target temperature."""
  148. if self._temp_high_dps is None:
  149. raise NotImplementedError()
  150. return self._temp_high_dps.get_value(self._device)
  151. @property
  152. def target_temperature_low(self):
  153. """Return the currently set low target temperature."""
  154. if self._temp_low_dps is None:
  155. raise NotImplementedError()
  156. return self._temp_low_dps.get_value(self._device)
  157. @property
  158. def target_temperature_step(self):
  159. """Return the supported step of target temperature."""
  160. dps = self._temperature_dps
  161. if dps is None:
  162. dps = self._temp_high_dps
  163. if dps is None:
  164. dps = self._temp_low_dps
  165. if dps is None:
  166. return 1
  167. return dps.step(self._device)
  168. @property
  169. def min_temp(self):
  170. """Return the minimum supported target temperature."""
  171. if self._temperature_dps is None:
  172. if self._temp_low_dps is None:
  173. return None
  174. r = self._temp_low_dps.range(self._device)
  175. else:
  176. r = self._temperature_dps.range(self._device)
  177. return DEFAULT_MIN_TEMP if r is None else r["min"]
  178. @property
  179. def max_temp(self):
  180. """Return the maximum supported target temperature."""
  181. if self._temperature_dps is None:
  182. if self._temp_high_dps is None:
  183. return None
  184. r = self._temp_high_dps.range(self._device)
  185. else:
  186. r = self._temperature_dps.range(self._device)
  187. return DEFAULT_MAX_TEMP if r is None else r["max"]
  188. async def async_set_temperature(self, **kwargs):
  189. """Set new target temperature."""
  190. if kwargs.get(ATTR_PRESET_MODE) is not None:
  191. await self.async_set_preset_mode(kwargs.get(ATTR_PRESET_MODE))
  192. if kwargs.get(ATTR_TEMPERATURE) is not None:
  193. await self.async_set_target_temperature(kwargs.get(ATTR_TEMPERATURE))
  194. high = kwargs.get(ATTR_TARGET_TEMP_HIGH)
  195. low = kwargs.get(ATTR_TARGET_TEMP_LOW)
  196. if high is not None or low is not None:
  197. await self.async_set_target_temperature_range(low, high)
  198. async def async_set_target_temperature(self, target_temperature):
  199. if self._temperature_dps is None:
  200. raise NotImplementedError()
  201. await self._temperature_dps.async_set_value(self._device, target_temperature)
  202. async def async_set_target_temperature_range(self, low, high):
  203. """Set the target temperature range."""
  204. dps_map = {}
  205. if low is not None and self._temp_low_dps is not None:
  206. dps_map.update(self._temp_low_dps.get_values_to_set(self._device, low))
  207. if high is not None and self._temp_high_dps is not None:
  208. dps_map.update(self._temp_high_dps.get_values_to_set(self._device, high))
  209. if dps_map:
  210. await self._device.async_set_properties(dps_map)
  211. @property
  212. def current_temperature(self):
  213. """Return the current measured temperature."""
  214. if self._current_temperature_dps is None:
  215. return None
  216. return self._current_temperature_dps.get_value(self._device)
  217. @property
  218. def target_humidity(self):
  219. """Return the currently set target humidity."""
  220. if self._humidity_dps is None:
  221. raise NotImplementedError()
  222. return self._humidity_dps.get_value(self._device)
  223. @property
  224. def min_humidity(self):
  225. """Return the minimum supported target humidity."""
  226. if self._humidity_dps is None:
  227. return None
  228. r = self._humidity_dps.range(self._device)
  229. return DEFAULT_MIN_HUMIDITY if r is None else r["min"]
  230. @property
  231. def max_humidity(self):
  232. """Return the maximum supported target humidity."""
  233. if self._humidity_dps is None:
  234. return None
  235. r = self._humidity_dps.range(self._device)
  236. return DEFAULT_MAX_HUMIDITY if r is None else r["max"]
  237. async def async_set_humidity(self, humidity: int):
  238. if self._humidity_dps is None:
  239. raise NotImplementedError()
  240. await self._humidity_dps.async_set_value(self._device, humidity)
  241. @property
  242. def current_humidity(self):
  243. """Return the current measured humidity."""
  244. if self._current_humidity_dps is None:
  245. return None
  246. return self._current_humidity_dps.get_value(self._device)
  247. @property
  248. def hvac_action(self):
  249. """Return the current HVAC action."""
  250. if self._hvac_action_dps is None:
  251. return None
  252. return self._hvac_action_dps.get_value(self._device)
  253. @property
  254. def hvac_mode(self):
  255. """Return current HVAC mode."""
  256. if self._hvac_mode_dps is None:
  257. return HVAC_MODE_AUTO
  258. hvac_mode = self._hvac_mode_dps.get_value(self._device)
  259. return STATE_UNAVAILABLE if hvac_mode is None else hvac_mode
  260. @property
  261. def hvac_modes(self):
  262. """Return available HVAC modes."""
  263. if self._hvac_mode_dps is None:
  264. return []
  265. else:
  266. return self._hvac_mode_dps.values(self._device)
  267. async def async_set_hvac_mode(self, hvac_mode):
  268. """Set new HVAC mode."""
  269. if self._hvac_mode_dps is None:
  270. raise NotImplementedError()
  271. await self._hvac_mode_dps.async_set_value(self._device, hvac_mode)
  272. @property
  273. def is_aux_heat(self):
  274. """Return state of aux heater"""
  275. if self._aux_heat_dps is None:
  276. return None
  277. else:
  278. return self._aux_heat_dps.get_value(self._device)
  279. async def async_turn_aux_heat_on(self):
  280. """Turn on aux heater."""
  281. if self._aux_heat_dps is None:
  282. raise NotImplementedError()
  283. await self._aux_heat_dps.async_set_value(self._device, True)
  284. async def async_turn_aux_heat_off(self):
  285. """Turn off aux heater."""
  286. if self._aux_heat_dps is None:
  287. raise NotImplementedError()
  288. await self._aux_heat_dps.async_set_value(self._device, False)
  289. @property
  290. def preset_mode(self):
  291. """Return the current preset mode."""
  292. if self._preset_mode_dps is None:
  293. raise NotImplementedError()
  294. return self._preset_mode_dps.get_value(self._device)
  295. @property
  296. def preset_modes(self):
  297. """Return the list of presets that this device supports."""
  298. if self._preset_mode_dps is None:
  299. return None
  300. return self._preset_mode_dps.values(self._device)
  301. async def async_set_preset_mode(self, preset_mode):
  302. """Set the preset mode."""
  303. if self._preset_mode_dps is None:
  304. raise NotImplementedError()
  305. await self._preset_mode_dps.async_set_value(self._device, preset_mode)
  306. @property
  307. def swing_mode(self):
  308. """Return the current swing mode."""
  309. if self._swing_mode_dps is None:
  310. raise NotImplementedError()
  311. return self._swing_mode_dps.get_value(self._device)
  312. @property
  313. def swing_modes(self):
  314. """Return the list of swing modes that this device supports."""
  315. if self._swing_mode_dps is None:
  316. return None
  317. return self._swing_mode_dps.values(self._device)
  318. async def async_set_swing_mode(self, swing_mode):
  319. """Set the preset mode."""
  320. if self._swing_mode_dps is None:
  321. raise NotImplementedError()
  322. await self._swing_mode_dps.async_set_value(self._device, swing_mode)
  323. @property
  324. def fan_mode(self):
  325. """Return the current fan mode."""
  326. if self._fan_mode_dps is None:
  327. raise NotImplementedError()
  328. return self._fan_mode_dps.get_value(self._device)
  329. @property
  330. def fan_modes(self):
  331. """Return the list of fan modes that this device supports."""
  332. if self._fan_mode_dps is None:
  333. return None
  334. return self._fan_mode_dps.values(self._device)
  335. async def async_set_fan_mode(self, fan_mode):
  336. """Set the fan mode."""
  337. if self._fan_mode_dps is None:
  338. raise NotImplementedError()
  339. await self._fan_mode_dps.async_set_value(self._device, fan_mode)
  340. @property
  341. def device_state_attributes(self):
  342. """Get additional attributes that the integration itself does not support."""
  343. attr = {}
  344. for a in self._attr_dps:
  345. attr[a.name] = a.get_value(self._device)
  346. return attr
  347. async def async_update(self):
  348. await self._device.async_refresh()