water_heater.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. """
  2. Setup for different kinds of Tuya water heater devices
  3. """
  4. import logging
  5. from homeassistant.components.water_heater import (
  6. ATTR_AWAY_MODE,
  7. ATTR_CURRENT_TEMPERATURE,
  8. ATTR_OPERATION_MODE,
  9. WaterHeaterEntity,
  10. WaterHeaterEntityFeature,
  11. )
  12. from homeassistant.const import ATTR_TEMPERATURE, UnitOfTemperature
  13. from .device import TuyaLocalDevice
  14. from .entity import TuyaLocalEntity, unit_from_ascii
  15. from .helpers.config import async_tuya_setup_platform
  16. from .helpers.device_config import TuyaEntityConfig
  17. _LOGGER = logging.getLogger(__name__)
  18. async def async_setup_entry(hass, config_entry, async_add_entities):
  19. config = {**config_entry.data, **config_entry.options}
  20. await async_tuya_setup_platform(
  21. hass,
  22. async_add_entities,
  23. config,
  24. "water_heater",
  25. TuyaLocalWaterHeater,
  26. )
  27. def validate_temp_unit(unit):
  28. translated_unit = unit_from_ascii(unit)
  29. try:
  30. return UnitOfTemperature(translated_unit)
  31. except ValueError:
  32. _LOGGER.warning("%s is not a valid temperature unit", unit)
  33. class TuyaLocalWaterHeater(TuyaLocalEntity, WaterHeaterEntity):
  34. """Representation of a Tuya water heater entity."""
  35. def __init__(self, device: TuyaLocalDevice, config: TuyaEntityConfig):
  36. """
  37. Initialise the water heater device.
  38. Args:
  39. device (TuyaLocalDevice): The device API instance.
  40. config (TuyaEntityConfig): The entity config.
  41. """
  42. super().__init__()
  43. dps_map = self._init_begin(device, config)
  44. self._current_temperature_dps = dps_map.pop(
  45. ATTR_CURRENT_TEMPERATURE,
  46. None,
  47. )
  48. self._temperature_dps = dps_map.pop(ATTR_TEMPERATURE, None)
  49. self._unit_dps = dps_map.pop("temperature_unit", None)
  50. self._mintemp_dps = dps_map.pop("min_temperature", None)
  51. self._maxtemp_dps = dps_map.pop("max_temperature", None)
  52. self._operation_mode_dps = dps_map.pop(ATTR_OPERATION_MODE, None)
  53. self._away_mode_dps = dps_map.pop(ATTR_AWAY_MODE, None)
  54. self._init_end(dps_map)
  55. self._support_flags = WaterHeaterEntityFeature(0)
  56. if self._operation_mode_dps:
  57. self._support_flags |= WaterHeaterEntityFeature.OPERATION_MODE
  58. if self._operation_mode_dps.type is bool:
  59. self._support_flags |= WaterHeaterEntityFeature.ON_OFF
  60. if "away" in self._operation_mode_dps.values(device):
  61. self._support_flags |= WaterHeaterEntityFeature.AWAY_MODE
  62. if self._temperature_dps and not self._temperature_dps.readonly:
  63. self._support_flags |= WaterHeaterEntityFeature.TARGET_TEMPERATURE
  64. if self._away_mode_dps:
  65. self._support_flags |= WaterHeaterEntityFeature.AWAY_MODE
  66. @property
  67. def supported_features(self):
  68. """Return the features supported by this climate device."""
  69. return self._support_flags
  70. @property
  71. def temperature_unit(self):
  72. """Return the unit of measurement."""
  73. # If there is a separate DPS that returns the units, use that
  74. if self._unit_dps:
  75. unit = validate_temp_unit(self._unit_dps.get_value(self._device))
  76. # Only return valid units
  77. if unit:
  78. return unit
  79. # If there unit attribute configured in the temperature dps, use that
  80. if self._temperature_dps and self._temperature_dps.unit:
  81. unit = validate_temp_unit(self._temperature_dps.unit)
  82. if unit:
  83. return unit
  84. if self._current_temperature_dps and self._current_temperature_dps.unit:
  85. unit = validate_temp_unit(self._current_temperature_dps.unit)
  86. if unit:
  87. return unit
  88. # Return the default unit
  89. return UnitOfTemperature.CELSIUS
  90. @property
  91. def precision(self):
  92. """Return the precision of the temperature setting."""
  93. # unlike sensor, this is a decimal of the smallest unit that can be
  94. # represented, not a number of decimal places.
  95. if self._temperature_dps is None:
  96. return None
  97. return 1.0 / max(
  98. self._temperature_dps.scale(self._device),
  99. (
  100. self._current_temperature_dps.scale(self._device)
  101. if self._current_temperature_dps
  102. else 1.0
  103. ),
  104. )
  105. @property
  106. def current_operation(self):
  107. """Return current operation ie. eco, electric, performance, ..."""
  108. return self._operation_mode_dps.get_value(self._device)
  109. @property
  110. def operation_list(self):
  111. """Return the list of available operation modes."""
  112. if self._operation_mode_dps is None:
  113. return []
  114. else:
  115. return self._operation_mode_dps.values(self._device)
  116. @property
  117. def is_away_mode_on(self):
  118. if self._away_mode_dps:
  119. return self._away_mode_dps.get_value(self._device)
  120. elif self._operation_mode_dps and (
  121. "away" in self._operation_mode_dps.values(self._device)
  122. ):
  123. return self.current_operation == "away"
  124. @property
  125. def current_temperature(self):
  126. """Return the current temperature."""
  127. if self._current_temperature_dps:
  128. return self._current_temperature_dps.get_value(self._device)
  129. @property
  130. def target_temperature(self):
  131. """Return the temperature we try to reach."""
  132. if self._temperature_dps is None:
  133. return None
  134. return self._temperature_dps.get_value(self._device)
  135. @property
  136. def target_temperature_step(self):
  137. """Return the supported step of target temperature."""
  138. dps = self._temperature_dps
  139. if dps is None:
  140. return 1
  141. return dps.step(self._device)
  142. async def async_set_temperature(self, **kwargs):
  143. """Set the target temperature of the water heater."""
  144. if kwargs.get(ATTR_OPERATION_MODE) is not None:
  145. if self._operation_mode_dps is None:
  146. raise NotImplementedError()
  147. await self.async_set_operation_mode(
  148. kwargs.get(ATTR_OPERATION_MODE),
  149. )
  150. if kwargs.get(ATTR_TEMPERATURE) is not None:
  151. if self._temperature_dps is None:
  152. raise NotImplementedError()
  153. await self._temperature_dps.async_set_value(
  154. self._device, kwargs.get(ATTR_TEMPERATURE)
  155. )
  156. async def async_set_operation_mode(self, operation_mode):
  157. """Set new target operation mode."""
  158. if self._operation_mode_dps is None:
  159. raise NotImplementedError()
  160. await self._operation_mode_dps.async_set_value(
  161. self._device,
  162. operation_mode,
  163. )
  164. async def async_turn_away_mode_on(self):
  165. """Turn away mode on"""
  166. if self._away_mode_dps:
  167. await self._away_mode_dps.async_set_value(self._device, True)
  168. elif self._operation_mode_dps and (
  169. "away" in self._operation_mode_dps.values(self._device)
  170. ):
  171. await self.async_set_operation_mode("away")
  172. else:
  173. raise NotImplementedError()
  174. async def async_turn_away_mode_off(self):
  175. """Turn away mode off"""
  176. if self._away_mode_dps:
  177. await self._away_mode_dps.async_set_value(self._device, False)
  178. elif self._operation_mode_dps and (
  179. "away" in self._operation_mode_dps.values(self._device)
  180. ):
  181. # switch to the default mode
  182. await self.async_set_operation_mode(
  183. self._operation_mode_dps.default,
  184. )
  185. else:
  186. raise NotImplementedError()
  187. @property
  188. def min_temp(self):
  189. """Return the minimum supported target temperature."""
  190. # if a separate min_temperature dps is specified, the device tells us.
  191. if self._mintemp_dps is not None:
  192. return self._mintemp_dps.get_value(self._device)
  193. if self._temperature_dps:
  194. r = self._temperature_dps.range(self._device)
  195. return r[0]
  196. @property
  197. def max_temp(self):
  198. """Return the maximum supported target temperature."""
  199. # if a separate max_temperature dps is specified, the device tells us.
  200. if self._maxtemp_dps is not None:
  201. return self._maxtemp_dps.get_value(self._device)
  202. if self._temperature_dps:
  203. r = self._temperature_dps.range(self._device)
  204. return r[1]
  205. async def async_turn_on(self):
  206. """
  207. Turn on the water heater. Works only if operation_mode is a
  208. boolean dp.
  209. """
  210. if self._operation_mode_dps and self._operation_mode_dps.type is bool:
  211. await self._device.async_set_property(
  212. self._operation_mode_dps.id,
  213. True,
  214. )
  215. async def async_turn_off(self):
  216. """
  217. Turn off the water heater. Works only if operation_mode is a
  218. boolean dp.
  219. """
  220. if self._operation_mode_dps and self._operation_mode_dps.type is bool:
  221. await self._device.async_set_property(
  222. self._operation_mode_dps.id,
  223. False,
  224. )