climate.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. """
  2. Goldair WiFi Fan device.
  3. """
  4. from homeassistant.const import (
  5. ATTR_TEMPERATURE, TEMP_CELSIUS, STATE_UNAVAILABLE
  6. )
  7. from homeassistant.components.climate import ClimateDevice
  8. from homeassistant.components.climate.const import (
  9. ATTR_HVAC_MODE, ATTR_PRESET_MODE, ATTR_FAN_MODE, ATTR_SWING_MODE,
  10. HVAC_MODE_OFF, HVAC_MODE_FAN_ONLY,
  11. PRESET_ECO, PRESET_SLEEP,
  12. SUPPORT_FAN_MODE, SUPPORT_PRESET_MODE, SUPPORT_SWING_MODE,
  13. SWING_OFF, SWING_HORIZONTAL
  14. )
  15. from custom_components.tuya_local import TuyaLocalDevice
  16. ATTR_TARGET_TEMPERATURE = 'target_temperature'
  17. ATTR_DISPLAY_ON = 'display_on'
  18. PRESET_NORMAL = 'normal'
  19. PROPERTY_TO_DPS_ID = {
  20. ATTR_HVAC_MODE: '1',
  21. ATTR_FAN_MODE: '2',
  22. ATTR_PRESET_MODE: '3',
  23. ATTR_SWING_MODE: '8',
  24. ATTR_DISPLAY_ON: '101'
  25. }
  26. HVAC_MODE_TO_DPS_MODE = {
  27. HVAC_MODE_OFF: False,
  28. HVAC_MODE_FAN_ONLY: True
  29. }
  30. PRESET_MODE_TO_DPS_MODE = {
  31. PRESET_NORMAL: 'normal',
  32. PRESET_ECO: 'nature',
  33. PRESET_SLEEP: 'sleep'
  34. }
  35. SWING_MODE_TO_DPS_MODE = {
  36. SWING_OFF: False,
  37. SWING_HORIZONTAL: True
  38. }
  39. FAN_MODES = {
  40. PRESET_NORMAL: {1: '1', 2: '2', 3: '3', 4: '4', 5: '5', 6: '6', 7: '7', 8: '8', 9: '9', 10: '10', 11: '11',
  41. 12: '12'},
  42. PRESET_ECO: {1: '4', 2: '8', 3: '12'},
  43. PRESET_SLEEP: {1: '4', 2: '8', 3: '12'}
  44. }
  45. SUPPORT_FLAGS = SUPPORT_FAN_MODE | SUPPORT_PRESET_MODE | SUPPORT_SWING_MODE
  46. class GoldairFan(ClimateDevice):
  47. """Representation of a Goldair WiFi fan."""
  48. def __init__(self, device):
  49. """Initialize the fan.
  50. Args:
  51. name (str): The device's name.
  52. device (TuyaLocalDevice): The device API instance."""
  53. self._device = device
  54. self._support_flags = SUPPORT_FLAGS
  55. @property
  56. def supported_features(self):
  57. """Return the list of supported features."""
  58. return self._support_flags
  59. @property
  60. def should_poll(self):
  61. """Return the polling state."""
  62. return True
  63. @property
  64. def name(self):
  65. """Return the name of the climate device."""
  66. return self._device.name
  67. @property
  68. def temperature_unit(self):
  69. """This is not used but required by Home Assistant."""
  70. return TEMP_CELSIUS
  71. @property
  72. def hvac_mode(self):
  73. """Return current HVAC mode, ie Fan Only or Off."""
  74. dps_mode = self._device.get_property(PROPERTY_TO_DPS_ID[ATTR_HVAC_MODE])
  75. if dps_mode is not None:
  76. return TuyaLocalDevice.get_key_for_value(HVAC_MODE_TO_DPS_MODE, dps_mode)
  77. else:
  78. return STATE_UNAVAILABLE
  79. @property
  80. def hvac_modes(self):
  81. """Return the list of available HVAC modes."""
  82. return list(HVAC_MODE_TO_DPS_MODE.keys())
  83. def set_hvac_mode(self, hvac_mode):
  84. """Set new HVAC mode."""
  85. dps_mode = HVAC_MODE_TO_DPS_MODE[hvac_mode]
  86. self._device.set_property(PROPERTY_TO_DPS_ID[ATTR_HVAC_MODE], dps_mode)
  87. @property
  88. def preset_mode(self):
  89. """Return current preset mode, ie Comfort, Eco, Anti-freeze."""
  90. dps_mode = self._device.get_property(PROPERTY_TO_DPS_ID[ATTR_PRESET_MODE])
  91. if dps_mode is not None:
  92. return TuyaLocalDevice.get_key_for_value(PRESET_MODE_TO_DPS_MODE, dps_mode)
  93. else:
  94. return None
  95. @property
  96. def preset_modes(self):
  97. """Return the list of available preset modes."""
  98. return list(PRESET_MODE_TO_DPS_MODE.keys())
  99. def set_preset_mode(self, preset_mode):
  100. """Set new preset mode."""
  101. dps_mode = PRESET_MODE_TO_DPS_MODE[preset_mode]
  102. self._device.set_property(PROPERTY_TO_DPS_ID[ATTR_PRESET_MODE], dps_mode)
  103. @property
  104. def swing_mode(self):
  105. """Return current swing mode: horizontal or off"""
  106. dps_mode = self._device.get_property(PROPERTY_TO_DPS_ID[ATTR_SWING_MODE])
  107. if dps_mode is not None:
  108. return TuyaLocalDevice.get_key_for_value(SWING_MODE_TO_DPS_MODE, dps_mode)
  109. else:
  110. return None
  111. @property
  112. def swing_modes(self):
  113. """Return the list of available swing modes."""
  114. return list(SWING_MODE_TO_DPS_MODE.keys())
  115. def set_swing_mode(self, swing_mode):
  116. """Set new swing mode."""
  117. dps_mode = SWING_MODE_TO_DPS_MODE[swing_mode]
  118. self._device.set_property(PROPERTY_TO_DPS_ID[ATTR_SWING_MODE], dps_mode)
  119. @property
  120. def fan_mode(self):
  121. """Return current fan mode: 1-12"""
  122. dps_mode = self._device.get_property(PROPERTY_TO_DPS_ID[ATTR_FAN_MODE])
  123. if dps_mode is not None and self.preset_mode is not None:
  124. return TuyaLocalDevice.get_key_for_value(FAN_MODES[self.preset_mode], dps_mode)
  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. def 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. self._device.set_property(PROPERTY_TO_DPS_ID[ATTR_FAN_MODE], dps_mode)
  139. def update(self):
  140. self._device.refresh()