water_heater.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. """
  2. Platform to control tuya water heater devices.
  3. """
  4. import logging
  5. from homeassistant.components.water_heater import (
  6. ATTR_CURRENT_TEMPERATURE,
  7. ATTR_OPERATION_MODE,
  8. WaterHeaterEntity,
  9. WaterHeaterEntityFeature,
  10. )
  11. from homeassistant.const import ATTR_TEMPERATURE, UnitOfTemperature
  12. from ..device import TuyaLocalDevice
  13. from ..helpers.device_config import TuyaEntityConfig
  14. from ..helpers.mixin import TuyaLocalEntity, unit_from_ascii
  15. _LOGGER = logging.getLogger(__name__)
  16. def validate_temp_unit(unit):
  17. unit = unit_from_ascii(unit)
  18. try:
  19. return UnitOfTemperature(unit)
  20. except ValueError:
  21. return None
  22. class TuyaLocalWaterHeater(TuyaLocalEntity, WaterHeaterEntity):
  23. """Representation of a Tuya water heater entity."""
  24. def __init__(self, device: TuyaLocalDevice, config: TuyaEntityConfig):
  25. """
  26. Initialise the water heater device.
  27. Args:
  28. device (TuyaLocalDevice): The device API instance.
  29. config (TuyaEntityConfig): The entity config.
  30. """
  31. dps_map = self._init_begin(device, config)
  32. self._current_temperature_dps = dps_map.pop(ATTR_CURRENT_TEMPERATURE, None)
  33. self._temperature_dps = dps_map.pop(ATTR_TEMPERATURE, None)
  34. self._unit_dps = dps_map.pop("temperature_unit", None)
  35. self._mintemp_dps = dps_map.pop("min_temperature", None)
  36. self._maxtemp_dps = dps_map.pop("max_temperature", None)
  37. self._operation_mode_dps = dps_map.pop("operation_mode", None)
  38. self._init_end(dps_map)
  39. self._support_flags = 0
  40. if self._operation_mode_dps:
  41. self._support_flags |= WaterHeaterEntityFeature.OPERATION_MODE
  42. if self._temperature_dps and not self._temperature_dps.readonly:
  43. self._support_flags |= WaterHeaterEntityFeature.TARGET_TEMPERATURE
  44. @property
  45. def supported_features(self):
  46. """Return the features supported by this climate device."""
  47. return self._support_flags
  48. @property
  49. def temperature_unit(self):
  50. """Return the unit of measurement."""
  51. # If there is a separate DPS that returns the units, use that
  52. if self._unit_dps is not None:
  53. unit = validate_temp_unit(self._unit_dps.get_value(self._device))
  54. # Only return valid units
  55. if unit is not None:
  56. return unit
  57. # If there unit attribute configured in the temperature dps, use that
  58. if self._temperature_dps:
  59. unit = validate_temp_unit(self._temperature_dps.unit)
  60. if unit is not None:
  61. return unit
  62. # Return the default unit from the device
  63. return self._device.temperature_unit
  64. @property
  65. def current_operation(self):
  66. """Return current operation ie. eco, electric, performance, ..."""
  67. return self._operation_mode_dps.get_value(self._device)
  68. @property
  69. def operation_list(self):
  70. """Return the list of available operation modes."""
  71. if self._operation_mode_dps is None:
  72. return []
  73. else:
  74. return self._operation_mode_dps.values(self._device)
  75. @property
  76. def current_temperature(self):
  77. """Return the current temperature."""
  78. if self._current_temperature_dps is None:
  79. return None
  80. return self._current_temperature_dps.get_value(self._device)
  81. @property
  82. def target_temperature(self):
  83. """Return the temperature we try to reach."""
  84. if self._temperature_dps is None:
  85. raise NotImplementedError()
  86. return self._temperature_dps.get_value(self._device)
  87. @property
  88. def target_temperature_step(self):
  89. """Return the supported step of target temperature."""
  90. dps = self._temperature_dps
  91. if dps is None:
  92. return 1
  93. return dps.step(self._device)
  94. async def async_set_temperature(self, **kwargs):
  95. """Set the target temperature of the water heater."""
  96. if kwargs.get(ATTR_OPERATION_MODE) is not None:
  97. if self._operation_mode_dps is None:
  98. raise NotImplementedError()
  99. await self.async_set_operation_mode(kwargs.get(ATTR_OPERATION_MODE))
  100. if kwargs.get(ATTR_TEMPERATURE) is not None:
  101. if self._temperature_dps is None:
  102. raise NotImplementedError()
  103. await self._temperature_dps.async_set_value(
  104. self._device, kwargs.get(ATTR_TEMPERATURE)
  105. )
  106. async def async_set_operation_mode(self, operation_mode):
  107. """Set new target operation mode."""
  108. if self._operation_mode_dps is None:
  109. raise NotImplementedError()
  110. await self._operation_mode_dps.async_set_value(self._device, operation_mode)
  111. @property
  112. def min_temp(self):
  113. """Return the minimum supported target temperature."""
  114. # if a separate min_temperature dps is specified, the device tells us.
  115. if self._mintemp_dps is not None:
  116. return self._mintemp_dps.get_value(self._device)
  117. if self._temperature_dps is None:
  118. if self._temp_low_dps is None:
  119. return None
  120. r = self._temp_low_dps.range(self._device)
  121. else:
  122. r = self._temperature_dps.range(self._device)
  123. return DEFAULT_MIN_TEMP if r is None else r["min"]
  124. @property
  125. def max_temp(self):
  126. """Return the maximum supported target temperature."""
  127. # if a separate max_temperature dps is specified, the device tells us.
  128. if self._maxtemp_dps is not None:
  129. return self._maxtemp_dps.get_value(self._device)
  130. if self._temperature_dps is None:
  131. if self._temp_high_dps is None:
  132. return None
  133. r = self._temp_high_dps.range(self._device)
  134. else:
  135. r = self._temperature_dps.range(self._device)
  136. return DEFAULT_MAX_TEMP if r is None else r["max"]