climate.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 temperature_unit(self):
  47. """This is not used but required by Home Assistant."""
  48. return TEMP_CELSIUS
  49. @property
  50. def hvac_mode(self):
  51. """Return current HVAC mode, ie Fan Only or Off."""
  52. dps_mode = self._device.get_property(PROPERTY_TO_DPS_ID[ATTR_HVAC_MODE])
  53. if dps_mode is not None:
  54. return GoldairTuyaDevice.get_key_for_value(HVAC_MODE_TO_DPS_MODE, dps_mode)
  55. else:
  56. return STATE_UNAVAILABLE
  57. @property
  58. def hvac_modes(self):
  59. """Return the list of available HVAC modes."""
  60. return list(HVAC_MODE_TO_DPS_MODE.keys())
  61. async def async_set_hvac_mode(self, hvac_mode):
  62. """Set new HVAC mode."""
  63. dps_mode = HVAC_MODE_TO_DPS_MODE[hvac_mode]
  64. await self._device.async_set_property(
  65. PROPERTY_TO_DPS_ID[ATTR_HVAC_MODE], dps_mode
  66. )
  67. @property
  68. def preset_mode(self):
  69. """Return current preset mode, ie Comfort, Eco, Anti-freeze."""
  70. dps_mode = self._device.get_property(PROPERTY_TO_DPS_ID[ATTR_PRESET_MODE])
  71. if dps_mode is not None:
  72. return GoldairTuyaDevice.get_key_for_value(
  73. PRESET_MODE_TO_DPS_MODE, dps_mode
  74. )
  75. else:
  76. return None
  77. @property
  78. def preset_modes(self):
  79. """Return the list of available preset modes."""
  80. return list(PRESET_MODE_TO_DPS_MODE.keys())
  81. async def async_set_preset_mode(self, preset_mode):
  82. """Set new preset mode."""
  83. dps_mode = PRESET_MODE_TO_DPS_MODE[preset_mode]
  84. await self._device.async_set_property(
  85. PROPERTY_TO_DPS_ID[ATTR_PRESET_MODE], dps_mode
  86. )
  87. @property
  88. def swing_mode(self):
  89. """Return current swing mode: horizontal or off"""
  90. dps_mode = self._device.get_property(PROPERTY_TO_DPS_ID[ATTR_SWING_MODE])
  91. if dps_mode is not None:
  92. return GoldairTuyaDevice.get_key_for_value(SWING_MODE_TO_DPS_MODE, dps_mode)
  93. else:
  94. return None
  95. @property
  96. def swing_modes(self):
  97. """Return the list of available swing modes."""
  98. return list(SWING_MODE_TO_DPS_MODE.keys())
  99. async def async_set_swing_mode(self, swing_mode):
  100. """Set new swing mode."""
  101. dps_mode = SWING_MODE_TO_DPS_MODE[swing_mode]
  102. await self._device.async_set_property(
  103. PROPERTY_TO_DPS_ID[ATTR_SWING_MODE], dps_mode
  104. )
  105. @property
  106. def fan_mode(self):
  107. """Return current fan mode: 1-12"""
  108. dps_mode = self._device.get_property(PROPERTY_TO_DPS_ID[ATTR_FAN_MODE])
  109. if (
  110. dps_mode is not None
  111. and self.preset_mode is not None
  112. and dps_mode in FAN_MODES[self.preset_mode].values()
  113. ):
  114. return GoldairTuyaDevice.get_key_for_value(
  115. FAN_MODES[self.preset_mode], dps_mode
  116. )
  117. else:
  118. return None
  119. @property
  120. def fan_modes(self):
  121. """Return the list of available fan modes."""
  122. if self.preset_mode is not None:
  123. return list(FAN_MODES[self.preset_mode].keys())
  124. else:
  125. return []
  126. async def async_set_fan_mode(self, fan_mode):
  127. """Set new fan mode."""
  128. if self.preset_mode is not None:
  129. dps_mode = FAN_MODES[self.preset_mode][int(fan_mode)]
  130. await self._device.async_set_property(
  131. PROPERTY_TO_DPS_ID[ATTR_FAN_MODE], dps_mode
  132. )
  133. async def async_update(self):
  134. await self._device.async_refresh()