climate.py 15 KB

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