1
0

smart_thermostat_radiator.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. """Smart Thermostat Radiator Device."""
  2. import logging
  3. from typing import Any
  4. from ..const import SwitchbotModel
  5. from ..const.climate import ClimateAction, ClimateMode
  6. from ..const.climate import SmartThermostatRadiatorMode as STRMode
  7. from .device import (
  8. SwitchbotEncryptedDevice,
  9. SwitchbotOperationError,
  10. SwitchbotSequenceDevice,
  11. update_after_operation,
  12. )
  13. _LOGGER = logging.getLogger(__name__)
  14. DEVICE_GET_BASIC_SETTINGS_KEY = "5702"
  15. _modes = STRMode.get_valid_modes()
  16. SMART_THERMOSTAT_TO_HA_HVAC_MODE = {
  17. "off": ClimateMode.OFF,
  18. **dict.fromkeys(_modes, ClimateMode.HEAT),
  19. }
  20. COMMAND_SET_MODE = {
  21. mode.lname: f"570F7800{index:02X}" for index, mode in enumerate(STRMode)
  22. }
  23. # fast heating default use max temperature
  24. COMMAND_SET_TEMP = {
  25. STRMode.MANUAL.lname: "570F7801{temp:04X}",
  26. STRMode.ECO.lname: "570F7802{temp:02X}",
  27. STRMode.COMFORT.lname: "570F7803{temp:02X}",
  28. STRMode.SCHEDULE.lname: "570F7806{temp:04X}",
  29. }
  30. MODE_TEMP_RANGE = {
  31. STRMode.ECO.lname: (10.0, 20.0),
  32. STRMode.COMFORT.lname: (10.0, 25.0),
  33. }
  34. DEFAULT_TEMP_RANGE = (5.0, 35.0)
  35. class SwitchbotSmartThermostatRadiator(
  36. SwitchbotSequenceDevice, SwitchbotEncryptedDevice
  37. ):
  38. """Representation of a Switchbot Smart Thermostat Radiator."""
  39. _model = SwitchbotModel.SMART_THERMOSTAT_RADIATOR
  40. _turn_off_command = "570100"
  41. _turn_on_command = "570101"
  42. @property
  43. def min_temperature(self) -> float:
  44. """Return the minimum target temperature."""
  45. return MODE_TEMP_RANGE.get(self.preset_mode, DEFAULT_TEMP_RANGE)[0]
  46. @property
  47. def max_temperature(self) -> float:
  48. """Return the maximum target temperature."""
  49. return MODE_TEMP_RANGE.get(self.preset_mode, DEFAULT_TEMP_RANGE)[1]
  50. @property
  51. def preset_modes(self) -> list[str]:
  52. """Return the supported preset modes."""
  53. return STRMode.get_modes()
  54. @property
  55. def preset_mode(self) -> str | None:
  56. """Return the current preset mode."""
  57. return self.get_current_mode()
  58. @property
  59. def hvac_modes(self) -> set[ClimateMode]:
  60. """Return the supported hvac modes."""
  61. return {ClimateMode.HEAT, ClimateMode.OFF}
  62. @property
  63. def hvac_mode(self) -> ClimateMode | None:
  64. """Return the current hvac mode."""
  65. return SMART_THERMOSTAT_TO_HA_HVAC_MODE.get(self.preset_mode, ClimateMode.OFF)
  66. @property
  67. def hvac_action(self) -> ClimateAction | None:
  68. """Return current action."""
  69. return self.get_action()
  70. @property
  71. def current_temperature(self) -> float | None:
  72. """Return the current temperature."""
  73. return self.get_current_temperature()
  74. @property
  75. def target_temperature(self) -> float | None:
  76. """Return the target temperature."""
  77. return self.get_target_temperature()
  78. @update_after_operation
  79. async def set_hvac_mode(self, hvac_mode: ClimateMode) -> None:
  80. """Set the hvac mode."""
  81. if hvac_mode == ClimateMode.OFF:
  82. return await self.turn_off()
  83. return await self.set_preset_mode("comfort")
  84. @update_after_operation
  85. async def set_preset_mode(self, preset_mode: str) -> bool:
  86. """Send command to set thermostat preset_mode."""
  87. return await self._send_command(COMMAND_SET_MODE[preset_mode])
  88. @update_after_operation
  89. async def set_target_temperature(self, temperature: float) -> bool:
  90. """Send command to set target temperature."""
  91. if self.preset_mode == STRMode.OFF.lname:
  92. raise SwitchbotOperationError("Cannot set temperature when mode is OFF.")
  93. if self.preset_mode == STRMode.BOOST.lname:
  94. raise SwitchbotOperationError("Boost mode defaults to max temperature.")
  95. temp_value = int(temperature * 10)
  96. cmd = COMMAND_SET_TEMP[self.preset_mode].format(temp=temp_value)
  97. _LOGGER.debug(
  98. "Setting temperature %.1f°C in mode %s → cmd=%s",
  99. temperature,
  100. self.preset_mode,
  101. cmd,
  102. )
  103. return await self._send_command(cmd)
  104. async def get_basic_info(self) -> dict[str, Any] | None:
  105. """Get device basic settings."""
  106. if not (_data := await self._get_basic_info()):
  107. return None
  108. _LOGGER.debug("data: %s", _data)
  109. battery = _data[1]
  110. firmware = _data[2] / 10.0
  111. hardware = _data[3]
  112. last_mode = STRMode.get_mode_name((_data[4] >> 3) & 0x07)
  113. mode = STRMode.get_mode_name(_data[4] & 0x07)
  114. temp_raw_value = _data[5] << 8 | _data[6]
  115. temp_sign = 1 if temp_raw_value >> 15 else -1
  116. temperature = temp_sign * (temp_raw_value & 0x7FFF) / 10.0
  117. manual_target_temp = (_data[7] << 8 | _data[8]) / 10.0
  118. comfort_target_temp = _data[9] / 10.0
  119. economic_target_temp = _data[10] / 10.0
  120. fast_heat_time = _data[11]
  121. child_lock = bool(_data[12] & 0x03)
  122. target_temp = (_data[13] << 8 | _data[14]) / 10.0
  123. door_open = bool(_data[14] & 0x01)
  124. result = {
  125. "battery": battery,
  126. "firmware": firmware,
  127. "hardware": hardware,
  128. "last_mode": last_mode,
  129. "mode": mode,
  130. "temperature": temperature,
  131. "manual_target_temp": manual_target_temp,
  132. "comfort_target_temp": comfort_target_temp,
  133. "economic_target_temp": economic_target_temp,
  134. "fast_heat_time": fast_heat_time,
  135. "child_lock": child_lock,
  136. "target_temp": target_temp,
  137. "door_open": door_open,
  138. }
  139. _LOGGER.debug("Smart Thermostat Radiator basic info: %s", result)
  140. return result
  141. def is_on(self) -> bool | None:
  142. """Return true if the thermostat is on."""
  143. return self._get_adv_value("isOn")
  144. def get_current_mode(self) -> str | None:
  145. """Return the current mode of the thermostat."""
  146. return self._get_adv_value("mode")
  147. def door_open(self) -> bool | None:
  148. """Return true if the door is open."""
  149. return self._get_adv_value("door_open")
  150. def get_current_temperature(self) -> float | None:
  151. """Return the current temperature."""
  152. return self._get_adv_value("temperature")
  153. def get_target_temperature(self) -> float | None:
  154. """Return the target temperature."""
  155. return self._get_adv_value("target_temperature")
  156. def get_action(self) -> ClimateAction:
  157. """Return current action from cache."""
  158. if not self.is_on():
  159. return ClimateAction.OFF
  160. return ClimateAction.HEATING