-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
/
climate.py
228 lines (188 loc) · 7.12 KB
/
climate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
"""Platform for climate integration."""
from __future__ import annotations
import logging
from typing import Any
from aiohttp import ClientSession
from whirlpool.aircon import Aircon, FanSpeed as AirconFanSpeed, Mode as AirconMode
from whirlpool.auth import Auth
from whirlpool.backendselector import BackendSelector
from homeassistant.components.climate import (
ENTITY_ID_FORMAT,
FAN_AUTO,
FAN_HIGH,
FAN_LOW,
FAN_MEDIUM,
FAN_OFF,
SWING_HORIZONTAL,
SWING_OFF,
ClimateEntity,
ClimateEntityFeature,
HVACMode,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_TEMPERATURE, UnitOfTemperature
from homeassistant.core import HomeAssistant
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity import generate_entity_id
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import WhirlpoolData
from .const import DOMAIN
_LOGGER = logging.getLogger(__name__)
AIRCON_MODE_MAP = {
AirconMode.Cool: HVACMode.COOL,
AirconMode.Heat: HVACMode.HEAT,
AirconMode.Fan: HVACMode.FAN_ONLY,
}
HVAC_MODE_TO_AIRCON_MODE = {v: k for k, v in AIRCON_MODE_MAP.items()}
AIRCON_FANSPEED_MAP = {
AirconFanSpeed.Off: FAN_OFF,
AirconFanSpeed.Auto: FAN_AUTO,
AirconFanSpeed.Low: FAN_LOW,
AirconFanSpeed.Medium: FAN_MEDIUM,
AirconFanSpeed.High: FAN_HIGH,
}
FAN_MODE_TO_AIRCON_FANSPEED = {v: k for k, v in AIRCON_FANSPEED_MAP.items()}
SUPPORTED_FAN_MODES = [FAN_AUTO, FAN_HIGH, FAN_MEDIUM, FAN_LOW, FAN_OFF]
SUPPORTED_HVAC_MODES = [
HVACMode.COOL,
HVACMode.HEAT,
HVACMode.FAN_ONLY,
HVACMode.OFF,
]
SUPPORTED_MAX_TEMP = 30
SUPPORTED_MIN_TEMP = 16
SUPPORTED_SWING_MODES = [SWING_HORIZONTAL, SWING_OFF]
SUPPORTED_TARGET_TEMPERATURE_STEP = 1
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up entry."""
whirlpool_data: WhirlpoolData = hass.data[DOMAIN][config_entry.entry_id]
aircons = [
AirConEntity(
hass,
ac_data["SAID"],
ac_data["NAME"],
whirlpool_data.backend_selector,
whirlpool_data.auth,
async_get_clientsession(hass),
)
for ac_data in whirlpool_data.appliances_manager.aircons
]
async_add_entities(aircons, True)
class AirConEntity(ClimateEntity):
"""Representation of an air conditioner."""
_attr_fan_modes = SUPPORTED_FAN_MODES
_attr_has_entity_name = True
_attr_name = None
_attr_hvac_modes = SUPPORTED_HVAC_MODES
_attr_max_temp = SUPPORTED_MAX_TEMP
_attr_min_temp = SUPPORTED_MIN_TEMP
_attr_should_poll = False
_attr_supported_features = (
ClimateEntityFeature.TARGET_TEMPERATURE
| ClimateEntityFeature.FAN_MODE
| ClimateEntityFeature.SWING_MODE
| ClimateEntityFeature.TURN_OFF
| ClimateEntityFeature.TURN_ON
)
_attr_swing_modes = SUPPORTED_SWING_MODES
_attr_target_temperature_step = SUPPORTED_TARGET_TEMPERATURE_STEP
_attr_temperature_unit = UnitOfTemperature.CELSIUS
_enable_turn_on_off_backwards_compatibility = False
def __init__(
self,
hass: HomeAssistant,
said: str,
name: str | None,
backend_selector: BackendSelector,
auth: Auth,
session: ClientSession,
) -> None:
"""Initialize the entity."""
self._aircon = Aircon(backend_selector, auth, said, session)
self.entity_id = generate_entity_id(ENTITY_ID_FORMAT, said, hass=hass)
self._attr_unique_id = said
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, said)},
name=name if name is not None else said,
manufacturer="Whirlpool",
model="Sixth Sense",
)
async def async_added_to_hass(self) -> None:
"""Connect aircon to the cloud."""
self._aircon.register_attr_callback(self.async_write_ha_state)
await self._aircon.connect()
async def async_will_remove_from_hass(self) -> None:
"""Close Whrilpool Appliance sockets before removing."""
self._aircon.unregister_attr_callback(self.async_write_ha_state)
await self._aircon.disconnect()
@property
def available(self) -> bool:
"""Return True if entity is available."""
return self._aircon.get_online()
@property
def current_temperature(self) -> float:
"""Return the current temperature."""
return self._aircon.get_current_temp()
@property
def target_temperature(self) -> float:
"""Return the temperature we try to reach."""
return self._aircon.get_temp()
async def async_set_temperature(self, **kwargs: Any) -> None:
"""Set new target temperature."""
await self._aircon.set_temp(kwargs.get(ATTR_TEMPERATURE))
@property
def current_humidity(self) -> int:
"""Return the current humidity."""
return self._aircon.get_current_humidity()
@property
def target_humidity(self) -> int:
"""Return the humidity we try to reach."""
return self._aircon.get_humidity()
async def async_set_humidity(self, humidity: int) -> None:
"""Set new target humidity."""
await self._aircon.set_humidity(humidity)
@property
def hvac_mode(self) -> HVACMode | None:
"""Return current operation ie. heat, cool, fan."""
if not self._aircon.get_power_on():
return HVACMode.OFF
mode: AirconMode = self._aircon.get_mode()
return AIRCON_MODE_MAP.get(mode)
async def async_set_hvac_mode(self, hvac_mode: HVACMode) -> None:
"""Set HVAC mode."""
if hvac_mode == HVACMode.OFF:
await self._aircon.set_power_on(False)
return
if not (mode := HVAC_MODE_TO_AIRCON_MODE.get(hvac_mode)):
raise ValueError(f"Invalid hvac mode {hvac_mode}")
await self._aircon.set_mode(mode)
if not self._aircon.get_power_on():
await self._aircon.set_power_on(True)
@property
def fan_mode(self) -> str:
"""Return the fan setting."""
fanspeed = self._aircon.get_fanspeed()
return AIRCON_FANSPEED_MAP.get(fanspeed, FAN_OFF)
async def async_set_fan_mode(self, fan_mode: str) -> None:
"""Set fan mode."""
if not (fanspeed := FAN_MODE_TO_AIRCON_FANSPEED.get(fan_mode)):
raise ValueError(f"Invalid fan mode {fan_mode}")
await self._aircon.set_fanspeed(fanspeed)
@property
def swing_mode(self) -> str:
"""Return the swing setting."""
return SWING_HORIZONTAL if self._aircon.get_h_louver_swing() else SWING_OFF
async def async_set_swing_mode(self, swing_mode: str) -> None:
"""Set new target temperature."""
await self._aircon.set_h_louver_swing(swing_mode == SWING_HORIZONTAL)
async def async_turn_on(self) -> None:
"""Turn device on."""
await self._aircon.set_power_on(True)
async def async_turn_off(self) -> None:
"""Turn device off."""
await self._aircon.set_power_on(False)