This repository was archived by the owner on Sep 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathswitch.py
239 lines (194 loc) · 7.81 KB
/
switch.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
229
230
231
232
233
234
235
236
237
238
239
"""Switches for the MySkoda integration."""
from asyncio import sleep
import logging
from homeassistant.components.switch import (
SwitchDeviceClass,
SwitchEntity,
SwitchEntityDescription,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import DiscoveryInfoType
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
from .const import DATA_COODINATOR, DOMAIN
from .entity import MySkodaDataEntity
from .myskoda import Vehicle
_LOGGER = logging.getLogger(__name__)
async def async_setup_entry(
hass: HomeAssistant,
config: ConfigEntry,
async_add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
"""Set up the sensor platform."""
coordinator = hass.data[DOMAIN][config.entry_id][DATA_COODINATOR]
vehicles = coordinator.data.get("vehicles")
entities = []
for vehicle in vehicles:
entities.append(WindowHeating(coordinator, vehicle))
entities.append(ReducedCurrent(coordinator, vehicle))
entities.append(BatteryCareMode(coordinator, vehicle))
entities.append(Charging(coordinator, vehicle))
async_add_entities(entities, update_before_add=True)
class MySkodaSwitch(MySkodaDataEntity, SwitchEntity):
"""Base class for all switches in the MySkoda integration."""
def __init__( # noqa: D107
self,
coordinator: DataUpdateCoordinator,
vehicle: Vehicle,
entity_description: SwitchEntityDescription,
) -> None:
super().__init__(coordinator, vehicle, entity_description)
SwitchEntity.__init__(self)
class WindowHeating(MySkodaSwitch):
"""Controls window heating."""
def __init__(self, coordinator: DataUpdateCoordinator, vehicle: Vehicle) -> None: # noqa: D107
super().__init__(
coordinator,
vehicle,
SwitchEntityDescription(
key="window_heating",
name=f"{vehicle.info.title} Window Heating",
icon="mdi:car-defrost-front",
device_class=SwitchDeviceClass.SWITCH,
),
)
self._attr_unique_id = f"{vehicle.info.vin}_window_heating"
@property
def is_on(self) -> bool | None: # noqa: D102
if not self.coordinator.data:
return None
self._update_device_from_coordinator()
return (
self.vehicle.air_conditioning.window_heating_front_on
or self.vehicle.air_conditioning.window_heating_rear_on
)
async def async_turn_off(self, **kwargs): # noqa: D102
await self.coordinator.hub.stop_window_heating(self.vehicle.info.vin)
for _ in range(10):
await sleep(15)
if not self.is_on:
break
await self.coordinator.async_refresh()
_LOGGER.debug("Window heating disabled.")
async def async_turn_on(self, **kwargs): # noqa: D102
await self.coordinator.hub.start_window_heating(self.vehicle.info.vin)
for _ in range(10):
await sleep(15)
if self.is_on:
break
await self.coordinator.async_refresh()
_LOGGER.debug("Window heating enabled.")
class BatteryCareMode(MySkodaSwitch):
"""Controls battery care mode."""
def __init__(self, coordinator: DataUpdateCoordinator, vehicle: Vehicle) -> None: # noqa: D107
super().__init__(
coordinator,
vehicle,
SwitchEntityDescription(
key="battery_care_mode",
name=f"{vehicle.info.title} Battery Care Mode",
icon="mdi:battery-heart-variant",
device_class=SwitchDeviceClass.SWITCH,
),
)
self._attr_unique_id = f"{vehicle.info.vin}_battery_care_mode"
@property
def is_on(self) -> bool | None: # noqa: D102
if not self.coordinator.data:
return None
self._update_device_from_coordinator()
return self.vehicle.charging.charging_care_mode
async def async_turn_off(self, **kwargs): # noqa: D102 # noqa: D102
await self.coordinator.hub.set_battery_care_mode(self.vehicle.info.vin, False)
for _ in range(10):
await sleep(15)
if not self.is_on:
break
await self.coordinator.async_refresh()
_LOGGER.info("Battery care mode disabled.")
async def async_turn_on(self, **kwargs): # noqa: D102
await self.coordinator.hub.set_battery_care_mode(self.vehicle.info.vin, True)
for _ in range(10):
await sleep(15)
if self.is_on:
break
await self.coordinator.async_refresh()
_LOGGER.info("Battery care mode enabled.")
class ReducedCurrent(MySkodaSwitch):
"""Control whether to charge with reduced current."""
def __init__(self, coordinator: DataUpdateCoordinator, vehicle: Vehicle) -> None: # noqa: D107
super().__init__(
coordinator,
vehicle,
SwitchEntityDescription(
key="reduced_current",
name=f"{vehicle.info.title} Reduced Current",
icon="mdi:current-ac",
device_class=SwitchDeviceClass.SWITCH,
),
)
self._attr_unique_id = f"{vehicle.info.vin}_reduced_current"
@property
def is_on(self) -> bool | None: # noqa: D102
if not self.coordinator.data:
return None
self._update_device_from_coordinator()
return self.vehicle.charging.use_reduced_current
async def async_turn_off(self, **kwargs): # noqa: D102
await self.coordinator.hub.set_reduced_current_limit(
self.vehicle.info.vin, False
)
for _ in range(10):
await sleep(15)
if not self.is_on:
break
await self.coordinator.async_refresh()
_LOGGER.info("Reduced current limit disabled.")
async def async_turn_on(self, **kwargs): # noqa: D102
await self.coordinator.hub.set_reduced_current_limit(
self.vehicle.info.vin, True
)
for _ in range(10):
await sleep(15)
if self.is_on:
break
await self.coordinator.async_refresh()
_LOGGER.info("Reduced current limit enabled.")
class Charging(MySkodaSwitch):
"""Control whether the vehicle should be charging."""
def __init__(self, coordinator: DataUpdateCoordinator, vehicle: Vehicle) -> None: # noqa: D107
super().__init__(
coordinator,
vehicle,
SwitchEntityDescription(
key="charging",
name=f"{vehicle.info.title} Charging",
icon="mdi:power-plug-battery",
device_class=SwitchDeviceClass.SWITCH,
),
)
self._attr_unique_id = f"{vehicle.info.vin}_charging"
@property
def is_on(self) -> bool | None: # noqa: D102
if not self.coordinator.data:
return None
self._update_device_from_coordinator()
return self.vehicle.charging.state == "CHARGING"
async def async_turn_off(self, **kwargs): # noqa: D102
await self.coordinator.hub.stop_charging(self.vehicle.info.vin)
for _ in range(10):
await sleep(15)
if not self.is_on:
break
await self.coordinator.async_refresh()
_LOGGER.info("Charging stopped.")
async def async_turn_on(self, **kwargs): # noqa: D102
await self.coordinator.hub.start_charging(self.vehicle.info.vin)
for _ in range(10):
await sleep(15)
if self.is_on:
break
await self.coordinator.async_refresh()
_LOGGER.info("Charging started.")