water_heater.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 .helpers.config import async_tuya_setup_platform
  15. from .helpers.device_config import TuyaEntityConfig
  16. from .helpers.mixin import TuyaLocalEntity, unit_from_ascii
  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. unit = unit_from_ascii(unit)
  29. try:
  30. return UnitOfTemperature(unit)
  31. except ValueError:
  32. return None
  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._temperature_dps and not self._temperature_dps.readonly:
  59. self._support_flags |= WaterHeaterEntityFeature.TARGET_TEMPERATURE
  60. if self._away_mode_dps or (
  61. self._operation_mode_dps
  62. and "away" in self._operation_mode_dps.values(device)
  63. ):
  64. self._support_flags |= WaterHeaterEntityFeature.AWAY_MODE
  65. @property
  66. def supported_features(self):
  67. """Return the features supported by this climate device."""
  68. return self._support_flags
  69. @property
  70. def temperature_unit(self):
  71. """Return the unit of measurement."""
  72. # If there is a separate DPS that returns the units, use that
  73. if self._unit_dps is not None:
  74. unit = validate_temp_unit(self._unit_dps.get_value(self._device))
  75. # Only return valid units
  76. if unit is not None:
  77. return unit
  78. # If there unit attribute configured in the temperature dps, use that
  79. if self._temperature_dps:
  80. unit = validate_temp_unit(self._temperature_dps.unit)
  81. if unit is not None:
  82. return unit
  83. # Return the default unit from the device
  84. return UnitOfTemperature.CELSIUS
  85. @property
  86. def precision(self):
  87. """Return the precision of the temperature setting."""
  88. # unlike sensor, this is a decimal of the smallest unit that can be
  89. # represented, not a number of decimal places.
  90. return 1.0 / max(
  91. self._temperature_dps.scale(self._device),
  92. (
  93. self._current_temperature_dps.scale(self._device)
  94. if self._current_temperature_dps
  95. else 1.0
  96. ),
  97. )
  98. @property
  99. def current_operation(self):
  100. """Return current operation ie. eco, electric, performance, ..."""
  101. return self._operation_mode_dps.get_value(self._device)
  102. @property
  103. def operation_list(self):
  104. """Return the list of available operation modes."""
  105. if self._operation_mode_dps is None:
  106. return []
  107. else:
  108. return self._operation_mode_dps.values(self._device)
  109. @property
  110. def is_away_mode_on(self):
  111. if self._away_mode_dps:
  112. return self._away_mode_dps.get_value(self._device)
  113. elif self._operation_mode_dps and (
  114. "away" in self._operation_mode_dps.values(self._device)
  115. ):
  116. return self.current_operation == "away"
  117. @property
  118. def current_temperature(self):
  119. """Return the current temperature."""
  120. if self._current_temperature_dps is None:
  121. return None
  122. return self._current_temperature_dps.get_value(self._device)
  123. @property
  124. def target_temperature(self):
  125. """Return the temperature we try to reach."""
  126. if self._temperature_dps is None:
  127. raise NotImplementedError()
  128. return self._temperature_dps.get_value(self._device)
  129. @property
  130. def target_temperature_step(self):
  131. """Return the supported step of target temperature."""
  132. dps = self._temperature_dps
  133. if dps is None:
  134. return 1
  135. return dps.step(self._device)
  136. async def async_set_temperature(self, **kwargs):
  137. """Set the target temperature of the water heater."""
  138. if kwargs.get(ATTR_OPERATION_MODE) is not None:
  139. if self._operation_mode_dps is None:
  140. raise NotImplementedError()
  141. await self.async_set_operation_mode(
  142. kwargs.get(ATTR_OPERATION_MODE),
  143. )
  144. if kwargs.get(ATTR_TEMPERATURE) is not None:
  145. if self._temperature_dps is None:
  146. raise NotImplementedError()
  147. await self._temperature_dps.async_set_value(
  148. self._device, kwargs.get(ATTR_TEMPERATURE)
  149. )
  150. async def async_set_operation_mode(self, operation_mode):
  151. """Set new target operation mode."""
  152. if self._operation_mode_dps is None:
  153. raise NotImplementedError()
  154. await self._operation_mode_dps.async_set_value(
  155. self._device,
  156. operation_mode,
  157. )
  158. async def async_turn_away_mode_on(self):
  159. """Turn away mode on"""
  160. if self._away_mode_dps:
  161. await self._away_mode_dps.async_set_value(self._device, True)
  162. elif self._operation_mode_dps and (
  163. "away" in self._operation_mode_dps.values(self._device)
  164. ):
  165. await self.async_set_operation_mode("away")
  166. else:
  167. raise NotImplementedError()
  168. async def async_turn_away_mode_off(self):
  169. """Turn away mode off"""
  170. if self._away_mode_dps:
  171. await self._away_mode_dps.async_set_value(self._device, False)
  172. elif self._operation_mode_dps and (
  173. "away" in self._operation_mode_dps.values(self._device)
  174. ):
  175. # switch to the default mode
  176. await self.async_set_operation_mode(
  177. self._operation_mode_dps.default,
  178. )
  179. else:
  180. raise NotImplementedError()
  181. @property
  182. def min_temp(self):
  183. """Return the minimum supported target temperature."""
  184. # if a separate min_temperature dps is specified, the device tells us.
  185. if self._mintemp_dps is not None:
  186. return self._mintemp_dps.get_value(self._device)
  187. if self._temperature_dps:
  188. r = self._temperature_dps.range(self._device)
  189. return r.get("min")
  190. @property
  191. def max_temp(self):
  192. """Return the maximum supported target temperature."""
  193. # if a separate max_temperature dps is specified, the device tells us.
  194. if self._maxtemp_dps is not None:
  195. return self._maxtemp_dps.get_value(self._device)
  196. if self._temperature_dps:
  197. r = self._temperature_dps.range(self._device)
  198. return r.get("max")
  199. async def async_turn_on(self):
  200. """
  201. Turn on the water heater. Works only if operation_mode is a
  202. boolean dp.
  203. """
  204. if self._operation_mode_dps and self._operation_mode_dps.type is bool:
  205. await self._device.async_set_property(
  206. self._operation_mode_dps.id,
  207. True,
  208. )
  209. async def async_turn_off(self):
  210. """
  211. Turn off the water heater. Works only if operation_mode is a
  212. boolean dp.
  213. """
  214. if self._operation_mode_dps and self._operation_mode_dps.type is bool:
  215. await self._device.async_set_property(
  216. self._operation_mode_dps.id,
  217. False,
  218. )