1 """Support for SwitchBee climate."""
3 from __future__
import annotations
7 from switchbee.api.central_unit
import SwitchBeeDeviceOfflineError, SwitchBeeError
8 from switchbee.const
import (
12 ThermostatTemperatureUnit,
14 from switchbee.device
import ApiStateCommand, SwitchBeeThermostat
32 from .const
import DOMAIN
33 from .coordinator
import SwitchBeeCoordinator
34 from .entity
import SwitchBeeDeviceEntity
37 ThermostatFanSpeed.AUTO: FAN_AUTO,
38 ThermostatFanSpeed.LOW: FAN_LOW,
39 ThermostatFanSpeed.MEDIUM: FAN_MEDIUM,
40 ThermostatFanSpeed.HIGH: FAN_HIGH,
43 FAN_HASS_TO_SB: dict[str |
None, str] = {
44 FAN_AUTO: ThermostatFanSpeed.AUTO,
45 FAN_LOW: ThermostatFanSpeed.LOW,
46 FAN_MEDIUM: ThermostatFanSpeed.MEDIUM,
47 FAN_HIGH: ThermostatFanSpeed.HIGH,
50 HVAC_MODE_SB_TO_HASS = {
51 ThermostatMode.COOL: HVACMode.COOL,
52 ThermostatMode.HEAT: HVACMode.HEAT,
53 ThermostatMode.FAN: HVACMode.FAN_ONLY,
56 HVAC_MODE_HASS_TO_SB: dict[HVACMode | str |
None, str] = {
57 HVACMode.COOL: ThermostatMode.COOL,
58 HVACMode.HEAT: ThermostatMode.HEAT,
59 HVACMode.FAN_ONLY: ThermostatMode.FAN,
62 HVAC_ACTION_SB_TO_HASS = {
63 ThermostatMode.COOL: HVACAction.COOLING,
64 ThermostatMode.HEAT: HVACAction.HEATING,
65 ThermostatMode.FAN: HVACAction.FAN,
68 HVAC_UNIT_SB_TO_HASS = {
69 ThermostatTemperatureUnit.CELSIUS: UnitOfTemperature.CELSIUS,
70 ThermostatTemperatureUnit.FAHRENHEIT: UnitOfTemperature.FAHRENHEIT,
73 SUPPORTED_FAN_MODES = [FAN_AUTO, FAN_HIGH, FAN_MEDIUM, FAN_LOW]
77 hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
79 """Set up SwitchBee climate."""
80 coordinator: SwitchBeeCoordinator = hass.data[DOMAIN][entry.entry_id]
83 for switchbee_device
in coordinator.data.values()
84 if isinstance(switchbee_device, SwitchBeeThermostat)
89 """Representation of a SwitchBee climate."""
91 _attr_fan_modes = SUPPORTED_FAN_MODES
92 _attr_target_temperature_step = 1
93 _enable_turn_on_off_backwards_compatibility =
False
97 device: SwitchBeeThermostat,
98 coordinator: SwitchBeeCoordinator,
100 """Initialize the Switchbee switch."""
101 super().
__init__(device, coordinator)
106 self.
_attr_hvac_modes_attr_hvac_modes = [HVAC_MODE_SB_TO_HASS[mode]
for mode
in device.modes]
109 ClimateEntityFeature.TARGET_TEMPERATURE | ClimateEntityFeature.FAN_MODE
113 ClimateEntityFeature.TURN_OFF | ClimateEntityFeature.TURN_ON
119 """Handle updated data from the coordinator."""
124 coordinator_device = self._get_coordinator_device()
126 self._attr_hvac_mode: HVACMode = (
128 if coordinator_device.state == ApiStateCommand.OFF
129 else HVAC_MODE_SB_TO_HASS[coordinator_device.mode]
138 if hvac_mode == HVACMode.OFF:
139 await self.
_operate_operate(power=ApiStateCommand.OFF)
142 power=ApiStateCommand.ON, mode=HVAC_MODE_HASS_TO_SB[hvac_mode]
146 """Set new target temperature."""
147 await self.
_operate_operate(target_temperature=kwargs[ATTR_TEMPERATURE])
150 """Set AC fan mode."""
151 await self.
_operate_operate(fan=FAN_HASS_TO_SB[fan_mode])
155 power: str |
None =
None,
156 mode: str |
None =
None,
157 fan: str |
None =
None,
158 target_temperature: int |
None =
None,
160 """Send request to central unit."""
163 power = ApiStateCommand.ON
165 power = ApiStateCommand.OFF
169 fan = FAN_HASS_TO_SB[self.
fan_modefan_mode]
170 if target_temperature
is None:
173 state: dict[str, int | str] = {
174 ApiAttribute.POWER: power,
175 ApiAttribute.MODE: mode,
176 ApiAttribute.FAN: fan,
177 ApiAttribute.CONFIGURED_TEMPERATURE: target_temperature,
181 await self.coordinator.api.set_state(self._device.id, state)
182 except (SwitchBeeError, SwitchBeeDeviceOfflineError)
as exp:
184 f
"Failed to set {self.name} state {state}, error: {exp!s}"
187 await self.coordinator.async_refresh()
float|None target_temperature(self)
HVACMode|None hvac_mode(self)
list[HVACMode] hvac_modes(self)
_attr_current_temperature
None async_set_temperature(self, **Any kwargs)
None async_set_fan_mode(self, str fan_mode)
None _update_attrs_from_coordinator(self)
None _operate(self, str|None power=None, str|None mode=None, str|None fan=None, int|None target_temperature=None)
None __init__(self, SwitchBeeThermostat device, SwitchBeeCoordinator coordinator)
None async_set_hvac_mode(self, HVACMode hvac_mode)
None _handle_coordinator_update(self)
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)