smart_thermostat_radiator.py 7.2 KB

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