Skip to content

Commit 6425441

Browse files
authored
Merge pull request #70 from KiraPC/dev
Dev
2 parents 17e9856 + bbb14ed commit 6425441

File tree

7 files changed

+63
-49
lines changed

7 files changed

+63
-49
lines changed

custom_components/switchbotremote/climate.py

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import logging
22
from homeassistant.components.climate import ClimateEntity
3-
from homeassistant.core import HomeAssistant, callback
4-
from homeassistant.helpers.event import async_track_state_change
3+
from homeassistant.core import Event, HomeAssistant, callback
4+
from homeassistant.helpers.event import async_track_state_change_event
55
from homeassistant.helpers.restore_state import RestoreEntity
66
from homeassistant.components.climate.const import (
77
HVACMode,
@@ -38,7 +38,7 @@
3838
HVACMode.OFF: 1,
3939
HVACMode.COOL: 2,
4040
HVACMode.DRY: 3,
41-
HVACMode.AUTO: 1,
41+
HVACMode.AUTO: 0,
4242
HVACMode.FAN_ONLY: 4,
4343
HVACMode.HEAT: 5,
4444
}
@@ -91,8 +91,6 @@ def __init__(self, sb: SupportedRemote, options: dict = {}) -> None:
9191
FAN_HIGH,
9292
]
9393

94-
self._supported_features = ClimateEntityFeature.TURN_OFF | ClimateEntityFeature.TURN_ON | ClimateEntityFeature.TARGET_TEMPERATURE | ClimateEntityFeature.FAN_MODE
95-
9694
self._temperature_sensor = options.get(CONF_TEMPERATURE_SENSOR, None)
9795
self._humidity_sensor = options.get(CONF_HUMIDITY_SENSOR, None)
9896
self._current_temperature = None
@@ -102,6 +100,7 @@ def __init__(self, sb: SupportedRemote, options: dict = {}) -> None:
102100
# This line will be removed after deprecation period (until 2025.1)
103101
# https://developers.home-assistant.io/blog/2024/01/24/climate-climateentityfeatures-expanded/
104102
self._enable_turn_on_off_backwards_compatibility = False
103+
self.set_supported_features()
105104

106105
@property
107106
def device_info(self):
@@ -212,6 +211,14 @@ def turn_on(self):
212211
"""Turn on."""
213212
self.set_hvac_mode(self._last_on_operation or HVACMode.COOL)
214213

214+
def set_supported_features(self):
215+
if self.hvac_mode == HVACMode.DRY or self.hvac_mode == HVACMode.FAN_ONLY:
216+
# switchbot api accept only 25 in DRY Mode
217+
self.set_temperature(temperature=25)
218+
self._supported_features = ClimateEntityFeature.TURN_OFF | ClimateEntityFeature.TURN_ON | ClimateEntityFeature.FAN_MODE
219+
else:
220+
self._supported_features = ClimateEntityFeature.TURN_OFF | ClimateEntityFeature.TURN_ON | ClimateEntityFeature.TARGET_TEMPERATURE | ClimateEntityFeature.FAN_MODE
221+
215222
def set_temperature(self, **kwargs):
216223
self._target_temperature = kwargs.get("temperature")
217224

@@ -234,6 +241,7 @@ def set_fan_mode(self, fan_mode):
234241
self._update_remote()
235242

236243
def _update_remote(self):
244+
self.set_supported_features()
237245
if (self._hvac_mode != HVACMode.OFF and self._override_off_command):
238246
self.sb.command(
239247
"setAll",
@@ -249,8 +257,9 @@ def _async_update_temp(self, state):
249257
except ValueError as ex:
250258
_LOGGER.error("Unable to update from temperature sensor: %s", ex)
251259

252-
async def _async_temp_sensor_changed(self, entity_id, old_state, new_state):
260+
async def _async_temp_sensor_changed(self, event):
253261
"""Handle temperature sensor changes."""
262+
new_state = event.data.get('new_state')
254263
if new_state is None:
255264
return
256265

@@ -266,8 +275,9 @@ def _async_update_humidity(self, state):
266275
except ValueError as ex:
267276
_LOGGER.error("Unable to update from humidity sensor: %s", ex)
268277

269-
async def _async_humidity_sensor_changed(self, entity_id, old_state, new_state):
278+
async def _async_humidity_sensor_changed(self, event):
270279
"""Handle humidity sensor changes."""
280+
new_state = event.data.get('new_state')
271281
if new_state is None:
272282
return
273283

@@ -288,8 +298,9 @@ def _async_update_power(self, state):
288298
except ValueError as ex:
289299
_LOGGER.error("Unable to update from power sensor: %s", ex)
290300

291-
async def _async_power_sensor_changed(self, entity_id, old_state, new_state):
301+
async def _async_power_sensor_changed(self, event: Event):
292302
"""Handle power sensor changes."""
303+
new_state = event.data.get('new_state')
293304
if new_state is None:
294305
return
295306

@@ -311,24 +322,24 @@ async def async_added_to_hass(self):
311322
'last_on_operation')
312323

313324
if self._temperature_sensor:
314-
async_track_state_change(
315-
self.hass, self._temperature_sensor, self._async_temp_sensor_changed)
325+
async_track_state_change_event(
326+
self.hass, [self._temperature_sensor], self._async_temp_sensor_changed)
316327

317328
temp_sensor_state = self.hass.states.get(self._temperature_sensor)
318329
if temp_sensor_state and temp_sensor_state.state != STATE_UNKNOWN:
319330
self._async_update_temp(temp_sensor_state)
320331

321332
if self._humidity_sensor:
322-
async_track_state_change(
323-
self.hass, self._humidity_sensor, self._async_humidity_sensor_changed)
333+
async_track_state_change_event(
334+
self.hass, [self._humidity_sensor], self._async_humidity_sensor_changed)
324335

325336
humidity_sensor_state = self.hass.states.get(self._humidity_sensor)
326337
if humidity_sensor_state and humidity_sensor_state.state != STATE_UNKNOWN:
327338
self._async_update_humidity(humidity_sensor_state)
328339

329340
if self._power_sensor:
330-
async_track_state_change(
331-
self.hass, self._power_sensor, self._async_power_sensor_changed)
341+
async_track_state_change_event(
342+
self.hass, [self._power_sensor], self._async_power_sensor_changed)
332343

333344
power_sensor_state = self.hass.states.get(self._power_sensor)
334345
if power_sensor_state and power_sensor_state.state != STATE_UNKNOWN:

custom_components/switchbotremote/fan.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
)
99
from homeassistant.const import STATE_UNAVAILABLE, STATE_UNKNOWN, STATE_OFF, STATE_ON
1010
from homeassistant.config_entries import ConfigEntry
11-
from homeassistant.core import HomeAssistant, callback
12-
from homeassistant.helpers.event import async_track_state_change
11+
from homeassistant.core import Event, HomeAssistant, callback
12+
from homeassistant.helpers.event import async_track_state_change_event
1313
from .client.remote import SupportedRemote
1414

1515
from .const import (
@@ -170,8 +170,9 @@ def _async_update_power(self, state):
170170
except ValueError as ex:
171171
_LOGGER.error("Unable to update from power sensor: %s", ex)
172172

173-
async def _async_power_sensor_changed(self, entity_id, old_state, new_state):
173+
async def _async_power_sensor_changed(self, event: Event):
174174
"""Handle power sensor changes."""
175+
new_state = event.data.get('new_state')
175176
if new_state is None:
176177
return
177178

@@ -182,8 +183,8 @@ async def async_added_to_hass(self):
182183
await super().async_added_to_hass()
183184

184185
if self._power_sensor:
185-
async_track_state_change(
186-
self.hass, self._power_sensor, self._async_power_sensor_changed
186+
async_track_state_change_event(
187+
self.hass, [self._power_sensor], self._async_power_sensor_changed
187188
)
188189

189190
power_sensor_state = self.hass.states.get(self._power_sensor)

custom_components/switchbotremote/light.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
from typing import List
33
from homeassistant.components.light import LightEntity
44
from homeassistant.helpers.restore_state import RestoreEntity
5-
from homeassistant.core import HomeAssistant, callback
6-
from homeassistant.helpers.event import async_track_state_change
5+
from homeassistant.core import Event, HomeAssistant, callback
6+
from homeassistant.helpers.event import async_track_state_change_event
77
from homeassistant.helpers.entity import DeviceInfo
88
from homeassistant.config_entries import ConfigEntry
99
from homeassistant.const import STATE_UNAVAILABLE, STATE_UNKNOWN, STATE_OFF, STATE_ON
@@ -100,8 +100,9 @@ def _async_update_power(self, state):
100100
except ValueError as ex:
101101
_LOGGER.error("Unable to update from power sensor: %s", ex)
102102

103-
async def _async_power_sensor_changed(self, entity_id, old_state, new_state):
103+
async def _async_power_sensor_changed(self, event: Event):
104104
"""Handle power sensor changes."""
105+
new_state = event.data.get('new_state')
105106
if new_state is None:
106107
return
107108

@@ -112,8 +113,8 @@ async def async_added_to_hass(self):
112113
await super().async_added_to_hass()
113114

114115
if self._power_sensor:
115-
async_track_state_change(
116-
self.hass, self._power_sensor, self._async_power_sensor_changed)
116+
async_track_state_change_event(
117+
self.hass, [self._power_sensor], self._async_power_sensor_changed)
117118

118119
power_sensor_state = self.hass.states.get(self._power_sensor)
119120
if power_sensor_state and power_sensor_state.state != STATE_UNKNOWN:

custom_components/switchbotremote/media_player.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
STATE_UNKNOWN,
1313
)
1414
from homeassistant.config_entries import ConfigEntry
15-
from homeassistant.core import HomeAssistant, callback
16-
from homeassistant.helpers.event import async_track_state_change
15+
from homeassistant.core import Event, HomeAssistant, callback
16+
from homeassistant.helpers.event import async_track_state_change_event
1717
from .client.remote import SupportedRemote
1818

1919
from .const import DOMAIN, MEDIA_CLASS, IR_MEDIA_TYPES, DIY_PROJECTOR_TYPE, PROJECTOR_TYPE, CONF_POWER_SENSOR
@@ -226,8 +226,9 @@ def _async_update_power(self, state):
226226
except ValueError as ex:
227227
_LOGGER.error("Unable to update from power sensor: %s", ex)
228228

229-
async def _async_power_sensor_changed(self, entity_id, old_state, new_state):
229+
async def _async_power_sensor_changed(self, event: Event):
230230
"""Handle power sensor changes."""
231+
new_state = event.data.get('new_state')
231232
if new_state is None:
232233
return
233234

@@ -239,8 +240,8 @@ async def async_added_to_hass(self):
239240
await super().async_added_to_hass()
240241

241242
if self._power_sensor:
242-
async_track_state_change(
243-
self.hass, self._power_sensor, self._async_power_sensor_changed)
243+
async_track_state_change_event(
244+
self.hass, [self._power_sensor], self._async_power_sensor_changed)
244245

245246
power_sensor_state = self.hass.states.get(self._power_sensor)
246247
if power_sensor_state and power_sensor_state.state != STATE_UNKNOWN:

custom_components/switchbotremote/remote.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
from typing import List
33
from homeassistant.components.remote import RemoteEntity
44
from homeassistant.helpers.restore_state import RestoreEntity
5-
from homeassistant.core import HomeAssistant, callback
6-
from homeassistant.helpers.event import async_track_state_change
5+
from homeassistant.core import Event, HomeAssistant, callback
6+
from homeassistant.helpers.event import async_track_state_change_event
77
from homeassistant.helpers.entity import DeviceInfo
88
from homeassistant.config_entries import ConfigEntry
99
from homeassistant.const import STATE_UNAVAILABLE, STATE_UNKNOWN, STATE_OFF, STATE_ON
@@ -68,8 +68,9 @@ def _async_update_power(self, state):
6868
except ValueError as ex:
6969
_LOGGER.error("Unable to update from power sensor: %s", ex)
7070

71-
async def _async_power_sensor_changed(self, entity_id, old_state, new_state):
71+
async def _async_power_sensor_changed(self, event: Event):
7272
"""Handle power sensor changes."""
73+
new_state = event.data.get('new_state')
7374
if new_state is None:
7475
return
7576

@@ -80,8 +81,8 @@ async def async_added_to_hass(self):
8081
await super().async_added_to_hass()
8182

8283
if self._power_sensor:
83-
async_track_state_change(
84-
self.hass, self._power_sensor, self._async_power_sensor_changed)
84+
async_track_state_change_event(
85+
self.hass, [self._power_sensor], self._async_power_sensor_changed)
8586

8687
power_sensor_state = self.hass.states.get(self._power_sensor)
8788
if power_sensor_state and power_sensor_state.state != STATE_UNKNOWN:

custom_components/switchbotremote/vacuum.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
from typing import List
22
from homeassistant.components.vacuum import (
33
StateVacuumEntity,
4-
VacuumEntityFeature, # v2022.5
5-
STATE_DOCKED,
6-
STATE_CLEANING,
7-
STATE_IDLE,
8-
STATE_IDLE,
9-
STATE_RETURNING
4+
VacuumEntityFeature
105
)
6+
from homeassistant.components.vacuum.const import VacuumActivity
117
from homeassistant.helpers.restore_state import RestoreEntity
128
from homeassistant.core import HomeAssistant
139
from homeassistant.helpers.entity import DeviceInfo
@@ -26,7 +22,7 @@ def __init__(self, hass: HomeAssistant, sb: SupportedRemote, options: dict = {})
2622
self._hass = hass
2723
self._unique_id = sb.id
2824
self._device_name = sb.name
29-
self._state = STATE_IDLE
25+
self._state = VacuumActivity.IDLE
3026

3127
self._supported_features = VacuumEntityFeature.STATE | VacuumEntityFeature.START | VacuumEntityFeature.STOP | VacuumEntityFeature.RETURN_HOME
3228

@@ -73,7 +69,7 @@ async def async_added_to_hass(self):
7369
async def async_start(self):
7470
"""Send the power on command."""
7571
await self.send_command("turnOn")
76-
self._state = STATE_CLEANING
72+
self._state = VacuumActivity.CLEANING
7773

7874
async def async_stop(self):
7975
"""Send the power off command."""

custom_components/switchbotremote/water_heater.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
from homeassistant.components.water_heater import WaterHeaterEntity, WaterHeaterEntityFeature, STATE_HEAT_PUMP
55
from homeassistant.helpers.restore_state import RestoreEntity
66
from homeassistant.core import HomeAssistant, callback
7-
from homeassistant.helpers.event import async_track_state_change
7+
from homeassistant.core import Event
8+
from homeassistant.helpers.event import async_track_state_change_event
89
from homeassistant.helpers.entity import DeviceInfo
910
from homeassistant.config_entries import ConfigEntry
1011
from homeassistant.const import STATE_UNAVAILABLE, STATE_UNKNOWN, STATE_OFF, STATE_ON
@@ -116,8 +117,9 @@ def _async_update_temp(self, state):
116117
except ValueError as ex:
117118
_LOGGER.error("Unable to update from temperature sensor: %s", ex)
118119

119-
async def _async_temp_sensor_changed(self, entity_id, old_state, new_state):
120+
async def _async_temp_sensor_changed(self, event: Event):
120121
"""Handle temperature sensor changes."""
122+
new_state = event.data.get('new_state')
121123
if new_state is None:
122124
return
123125

@@ -138,8 +140,9 @@ def _async_update_power(self, state):
138140
except ValueError as ex:
139141
_LOGGER.error("Unable to update from power sensor: %s", ex)
140142

141-
async def _async_power_sensor_changed(self, entity_id, old_state, new_state):
143+
async def _async_power_sensor_changed(self, event: Event):
142144
"""Handle power sensor changes."""
145+
new_state = event.data.get('new_state')
143146
if new_state is None:
144147
return
145148

@@ -150,16 +153,16 @@ async def async_added_to_hass(self):
150153
await super().async_added_to_hass()
151154

152155
if self._temperature_sensor:
153-
async_track_state_change(
154-
self.hass, self._temperature_sensor, self._async_temp_sensor_changed)
156+
async_track_state_change_event(
157+
self.hass, [self._temperature_sensor], self._async_temp_sensor_changed)
155158

156159
temp_sensor_state = self.hass.states.get(self._temperature_sensor)
157160
if temp_sensor_state and temp_sensor_state.state != STATE_UNKNOWN:
158161
self._async_update_temp(temp_sensor_state)
159162

160163
if self._power_sensor:
161-
async_track_state_change(
162-
self.hass, self._power_sensor, self._async_power_sensor_changed)
164+
async_track_state_change_event(
165+
self.hass, [self._power_sensor], self._async_power_sensor_changed)
163166

164167
power_sensor_state = self.hass.states.get(self._power_sensor)
165168
if power_sensor_state and power_sensor_state.state != STATE_UNKNOWN:

0 commit comments

Comments
 (0)