climate.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. """
  2. Goldair WiFi Fan 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_FAN_MODE,
  10. ATTR_HVAC_MODE,
  11. ATTR_PRESET_MODE,
  12. ATTR_SWING_MODE,
  13. SUPPORT_FAN_MODE,
  14. SUPPORT_PRESET_MODE,
  15. SUPPORT_SWING_MODE,
  16. )
  17. from homeassistant.const import ATTR_TEMPERATURE, STATE_UNAVAILABLE, TEMP_CELSIUS
  18. from ..device import GoldairTuyaDevice
  19. from .const import (
  20. FAN_MODES,
  21. HVAC_MODE_TO_DPS_MODE,
  22. PRESET_MODE_TO_DPS_MODE,
  23. PROPERTY_TO_DPS_ID,
  24. SWING_MODE_TO_DPS_MODE,
  25. )
  26. SUPPORT_FLAGS = SUPPORT_FAN_MODE | SUPPORT_PRESET_MODE | SUPPORT_SWING_MODE
  27. class GoldairFan(ClimateEntity):
  28. """Representation of a Goldair WiFi fan."""
  29. def __init__(self, device):
  30. """Initialize the fan.
  31. Args:
  32. name (str): The device's name.
  33. device (GoldairTuyaDevice): The device API instance."""
  34. self._device = device
  35. self._support_flags = SUPPORT_FLAGS
  36. @property
  37. def supported_features(self):
  38. """Return the list of supported features."""
  39. return self._support_flags
  40. @property
  41. def should_poll(self):
  42. """Return the polling state."""
  43. return True
  44. @property
  45. def name(self):
  46. """Return the name of the climate device."""
  47. return self._device.name
  48. @property
  49. def unique_id(self):
  50. """Return the unique id for this fan."""
  51. return self._device.unique_id
  52. @property
  53. def device_info(self):
  54. """Return device information about this fan."""
  55. return self._device.device_info
  56. @property
  57. def icon(self):
  58. """Return the icon to use in the frontend for this device."""
  59. return "mdi:fan"
  60. @property
  61. def temperature_unit(self):
  62. """This is not used but required by Home Assistant."""
  63. return TEMP_CELSIUS
  64. @property
  65. def hvac_mode(self):
  66. """Return current HVAC mode, ie Fan Only or Off."""
  67. dps_mode = self._device.get_property(PROPERTY_TO_DPS_ID[ATTR_HVAC_MODE])
  68. if dps_mode is not None:
  69. return GoldairTuyaDevice.get_key_for_value(HVAC_MODE_TO_DPS_MODE, dps_mode)
  70. else:
  71. return STATE_UNAVAILABLE
  72. @property
  73. def hvac_modes(self):
  74. """Return the list of available HVAC modes."""
  75. return list(HVAC_MODE_TO_DPS_MODE.keys())
  76. async def async_set_hvac_mode(self, hvac_mode):
  77. """Set new HVAC mode."""
  78. dps_mode = HVAC_MODE_TO_DPS_MODE[hvac_mode]
  79. await self._device.async_set_property(
  80. PROPERTY_TO_DPS_ID[ATTR_HVAC_MODE], dps_mode
  81. )
  82. @property
  83. def preset_mode(self):
  84. """Return current preset mode, ie Comfort, Eco, Anti-freeze."""
  85. dps_mode = self._device.get_property(PROPERTY_TO_DPS_ID[ATTR_PRESET_MODE])
  86. if dps_mode is not None:
  87. return GoldairTuyaDevice.get_key_for_value(
  88. PRESET_MODE_TO_DPS_MODE, dps_mode
  89. )
  90. else:
  91. return None
  92. @property
  93. def preset_modes(self):
  94. """Return the list of available preset modes."""
  95. return list(PRESET_MODE_TO_DPS_MODE.keys())
  96. async def async_set_preset_mode(self, preset_mode):
  97. """Set new preset mode."""
  98. dps_mode = PRESET_MODE_TO_DPS_MODE[preset_mode]
  99. await self._device.async_set_property(
  100. PROPERTY_TO_DPS_ID[ATTR_PRESET_MODE], dps_mode
  101. )
  102. @property
  103. def swing_mode(self):
  104. """Return current swing mode: horizontal or off"""
  105. dps_mode = self._device.get_property(PROPERTY_TO_DPS_ID[ATTR_SWING_MODE])
  106. if dps_mode is not None:
  107. return GoldairTuyaDevice.get_key_for_value(SWING_MODE_TO_DPS_MODE, dps_mode)
  108. else:
  109. return None
  110. @property
  111. def swing_modes(self):
  112. """Return the list of available swing modes."""
  113. return list(SWING_MODE_TO_DPS_MODE.keys())
  114. async def async_set_swing_mode(self, swing_mode):
  115. """Set new swing mode."""
  116. dps_mode = SWING_MODE_TO_DPS_MODE[swing_mode]
  117. await self._device.async_set_property(
  118. PROPERTY_TO_DPS_ID[ATTR_SWING_MODE], dps_mode
  119. )
  120. @property
  121. def fan_mode(self):
  122. """Return current fan mode: 1-12 or 1-3 depending on the preset"""
  123. dps_mode = self._device.get_property(PROPERTY_TO_DPS_ID[ATTR_FAN_MODE])
  124. if (
  125. dps_mode is not None
  126. and self.preset_mode is not None
  127. and dps_mode in FAN_MODES[self.preset_mode].values()
  128. ):
  129. return GoldairTuyaDevice.get_key_for_value(
  130. FAN_MODES[self.preset_mode], dps_mode
  131. )
  132. else:
  133. return None
  134. @property
  135. def fan_modes(self):
  136. """Return the list of available fan modes."""
  137. if self.preset_mode is not None:
  138. return list(FAN_MODES[self.preset_mode].keys())
  139. else:
  140. return []
  141. async def async_set_fan_mode(self, fan_mode):
  142. """Set new fan mode."""
  143. if self.preset_mode is not None:
  144. dps_mode = FAN_MODES[self.preset_mode][int(fan_mode)]
  145. await self._device.async_set_property(
  146. PROPERTY_TO_DPS_ID[ATTR_FAN_MODE], dps_mode
  147. )
  148. else:
  149. raise ValueError("Fan mode can only be set when a preset mode is set")
  150. async def async_update(self):
  151. await self._device.async_refresh()