climate.py 5.1 KB

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