climate.py 16 KB

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