4
0

climate.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. """
  2. Goldair GECO WiFi Heater device.
  3. """
  4. try:
  5. from homeassistant.components.climate import ClimateEntity
  6. except ImportError:
  7. from homeassistant.components.climate import ClimateDevice as ClimateEntity
  8. from homeassistant.components.climate.const import (
  9. ATTR_HVAC_MODE,
  10. HVAC_MODE_HEAT,
  11. SUPPORT_TARGET_TEMPERATURE,
  12. )
  13. from homeassistant.const import ATTR_TEMPERATURE, STATE_UNAVAILABLE
  14. from ..device import TuyaLocalDevice
  15. from .const import (
  16. ATTR_ERROR,
  17. ATTR_TARGET_TEMPERATURE,
  18. HVAC_MODE_TO_DPS_MODE,
  19. PROPERTY_TO_DPS_ID,
  20. )
  21. SUPPORT_FLAGS = SUPPORT_TARGET_TEMPERATURE
  22. class GoldairGECOHeater(ClimateEntity):
  23. """Representation of a Goldair GECO WiFi heater."""
  24. def __init__(self, device):
  25. """Initialize the heater.
  26. Args:
  27. device (TuyaLocalDevice): The device API instance."""
  28. self._device = device
  29. self._support_flags = SUPPORT_FLAGS
  30. self._TEMPERATURE_STEP = 1
  31. self._TEMPERATURE_LIMITS = {"min": 15, "max": 35}
  32. @property
  33. def supported_features(self):
  34. """Return the list of supported features."""
  35. return self._support_flags
  36. @property
  37. def should_poll(self):
  38. """Return the polling state."""
  39. return True
  40. @property
  41. def name(self):
  42. """Return the name of the climate device."""
  43. return self._device.name
  44. @property
  45. def unique_id(self):
  46. """Return the unique id for this heater."""
  47. return self._device.unique_id
  48. @property
  49. def device_info(self):
  50. """Return device information about this heater."""
  51. return self._device.device_info
  52. @property
  53. def icon(self):
  54. """Return the icon to use in the frontend for this device."""
  55. hvac_mode = self.hvac_mode
  56. if hvac_mode == HVAC_MODE_HEAT:
  57. return "mdi:radiator"
  58. else:
  59. return "mdi:radiator-disabled"
  60. @property
  61. def temperature_unit(self):
  62. """Return the unit of measurement."""
  63. return self._device.temperature_unit
  64. @property
  65. def target_temperature(self):
  66. """Return the temperature we try to reach."""
  67. return self._device.get_property(PROPERTY_TO_DPS_ID[ATTR_TARGET_TEMPERATURE])
  68. @property
  69. def target_temperature_step(self):
  70. """Return the supported step of target temperature."""
  71. return self._TEMPERATURE_STEP
  72. @property
  73. def min_temp(self):
  74. """Return the minimum temperature."""
  75. return self._TEMPERATURE_LIMITS["min"]
  76. @property
  77. def max_temp(self):
  78. """Return the maximum temperature."""
  79. return self._TEMPERATURE_LIMITS["max"]
  80. async def async_set_temperature(self, **kwargs):
  81. """Set new target temperatures."""
  82. if kwargs.get(ATTR_TEMPERATURE) is not None:
  83. await self.async_set_target_temperature(kwargs.get(ATTR_TEMPERATURE))
  84. async def async_set_target_temperature(self, target_temperature):
  85. target_temperature = int(round(target_temperature))
  86. limits = self._TEMPERATURE_LIMITS
  87. if not limits["min"] <= target_temperature <= limits["max"]:
  88. raise ValueError(
  89. f"Target temperature ({target_temperature}) must be between "
  90. f'{limits["min"]} and {limits["max"]}.'
  91. )
  92. await self._device.async_set_property(
  93. PROPERTY_TO_DPS_ID[ATTR_TARGET_TEMPERATURE], target_temperature
  94. )
  95. @property
  96. def current_temperature(self):
  97. """Return the current temperature."""
  98. return self._device.get_property(PROPERTY_TO_DPS_ID[ATTR_TEMPERATURE])
  99. @property
  100. def hvac_mode(self):
  101. """Return current HVAC mode, ie Heat or Off."""
  102. dps_mode = self._device.get_property(PROPERTY_TO_DPS_ID[ATTR_HVAC_MODE])
  103. if dps_mode is not None:
  104. return TuyaLocalDevice.get_key_for_value(HVAC_MODE_TO_DPS_MODE, dps_mode)
  105. else:
  106. return STATE_UNAVAILABLE
  107. @property
  108. def hvac_modes(self):
  109. """Return the list of available HVAC modes."""
  110. return list(HVAC_MODE_TO_DPS_MODE.keys())
  111. async def async_set_hvac_mode(self, hvac_mode):
  112. """Set new HVAC mode."""
  113. dps_mode = HVAC_MODE_TO_DPS_MODE[hvac_mode]
  114. await self._device.async_set_property(
  115. PROPERTY_TO_DPS_ID[ATTR_HVAC_MODE], dps_mode
  116. )
  117. @property
  118. def device_state_attributes(self):
  119. """Get additional attributes that HA doesn't naturally support."""
  120. error = self._device.get_property(PROPERTY_TO_DPS_ID[ATTR_ERROR])
  121. return {ATTR_ERROR: error or None}
  122. async def async_update(self):
  123. await self._device.async_refresh()