-
-
Notifications
You must be signed in to change notification settings - Fork 109
Expand file tree
/
Copy path__init__.py
More file actions
292 lines (241 loc) · 9.9 KB
/
Copy path__init__.py
File metadata and controls
292 lines (241 loc) · 9.9 KB
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
"""The FordPass integration."""
import asyncio
import logging
from datetime import timedelta
import async_timeout
import voluptuous as vol
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers.update_coordinator import (
CoordinatorEntity,
DataUpdateCoordinator,
UpdateFailed,
)
from homeassistant.helpers.storage import Store
from .const import (
CONF_DISTANCE_UNIT,
CONF_PRESSURE_UNIT,
DEFAULT_DISTANCE_UNIT,
DEFAULT_PRESSURE_UNIT,
DEFAULT_REGION,
DOMAIN,
MANUFACTURER,
REGION,
VEHICLE,
VIN,
UPDATE_INTERVAL,
UPDATE_INTERVAL_DEFAULT,
COORDINATOR,
STORAGE_VERSION,
STORAGE_KEY_PREFIX,
)
from .fordpass_new import Vehicle
CONFIG_SCHEMA = vol.Schema({DOMAIN: vol.Schema({})}, extra=vol.ALLOW_EXTRA)
PLATFORMS = ["lock", "sensor", "switch", "device_tracker"]
_LOGGER = logging.getLogger(__name__)
async def async_setup(hass: HomeAssistant, config: dict):
"""Set up the FordPass component."""
hass.data.setdefault(DOMAIN, {})
return True
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
"""Set up FordPass from a config entry."""
user = entry.data[CONF_USERNAME]
password = entry.data[CONF_PASSWORD]
vin = entry.data[VIN]
if UPDATE_INTERVAL in entry.options:
update_interval = entry.options[UPDATE_INTERVAL]
else:
update_interval = UPDATE_INTERVAL_DEFAULT
_LOGGER.debug(update_interval)
for ar_entry in entry.data:
_LOGGER.debug(ar_entry)
if REGION in entry.data.keys():
_LOGGER.debug(entry.data[REGION])
region = entry.data[REGION]
else:
_LOGGER.debug("CANT GET REGION")
region = DEFAULT_REGION
# Create token store for this user
token_store = Store(hass, STORAGE_VERSION, f"{STORAGE_KEY_PREFIX}_{user}")
coordinator = FordPassDataUpdateCoordinator(
hass, user, password, vin, region, update_interval, token_store
)
await coordinator.async_refresh() # Get initial data
fordpass_options_listener = entry.add_update_listener(options_update_listener)
if not entry.options:
await async_update_options(hass, entry)
if not coordinator.last_update_success:
raise ConfigEntryNotReady
hass.data[DOMAIN][entry.entry_id] = {
COORDINATOR: coordinator,
"fordpass_options_listener": fordpass_options_listener
}
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
async def async_refresh_status_service(service_call):
"""Handle refresh status for specific VIN or all vehicles"""
target_vin = service_call.data.get("vin", "")
if target_vin:
# Refresh specific vehicle
_LOGGER.debug(f"Refreshing status for VIN: {target_vin}")
try:
# Use the async request_update method directly
result = await coordinator.vehicle.request_update(target_vin)
if result:
_LOGGER.debug("Refresh command succeeded, updating coordinator data")
# Wait a moment for the vehicle to process the command
await asyncio.sleep(2)
# Force refresh the coordinator data
await coordinator.async_request_refresh()
else:
_LOGGER.warning("Refresh command failed")
except Exception as e:
_LOGGER.error(f"Error during refresh: {e}")
else:
# Refresh all vehicles for this account
all_entries = hass.config_entries.async_entries(DOMAIN)
current_username = entry.data[CONF_USERNAME]
for config_entry in all_entries:
if config_entry.data.get(CONF_USERNAME) == current_username:
entry_coordinator = hass.data[DOMAIN][config_entry.entry_id][COORDINATOR]
_LOGGER.debug(f"Refreshing status for VIN: {entry_coordinator.vin}")
try:
# Use the async request_update method directly
result = await entry_coordinator.vehicle.request_update()
if result:
_LOGGER.debug("Refresh command succeeded, updating coordinator data")
# Wait a moment for the vehicle to process the command
await asyncio.sleep(2)
# Force refresh the coordinator data
await entry_coordinator.async_request_refresh()
else:
_LOGGER.warning(f"Refresh command failed for VIN: {entry_coordinator.vin}")
except Exception as e:
_LOGGER.error(f"Error during refresh for VIN {entry_coordinator.vin}: {e}")
async def async_clear_tokens_service(service_call):
"""Clear tokens for this user account"""
await coordinator.vehicle.clear_token()
async def poll_api_service(service_call):
"""Poll API for this vehicle"""
await coordinator.async_request_refresh()
async def handle_reload(service):
"""Handle reload service call."""
_LOGGER.debug("Reloading Integration")
current_entries = hass.config_entries.async_entries(DOMAIN)
reload_tasks = [
hass.config_entries.async_reload(entry.entry_id)
for entry in current_entries
]
await asyncio.gather(*reload_tasks)
hass.services.async_register(
DOMAIN,
"refresh_status",
async_refresh_status_service,
)
hass.services.async_register(
DOMAIN,
"clear_tokens",
async_clear_tokens_service,
)
hass.services.async_register(
DOMAIN,
"reload",
handle_reload
)
hass.services.async_register(
DOMAIN,
"poll_api",
poll_api_service
)
return True
async def async_update_options(hass, config_entry):
"""Update options entries on change"""
options = {
CONF_PRESSURE_UNIT: config_entry.data.get(
CONF_PRESSURE_UNIT, DEFAULT_PRESSURE_UNIT
)
}
options[CONF_DISTANCE_UNIT] = config_entry.data.get(
CONF_DISTANCE_UNIT, DEFAULT_DISTANCE_UNIT
)
hass.config_entries.async_update_entry(config_entry, options=options)
async def options_update_listener(hass: HomeAssistant, entry: ConfigEntry):
"""Options listener to refresh config entries on option change"""
_LOGGER.debug("OPTIONS CHANGE")
await hass.config_entries.async_reload(entry.entry_id)
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload a config entry."""
if await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
hass.data[DOMAIN].pop(entry.entry_id)
return True
return False
class FordPassDataUpdateCoordinator(DataUpdateCoordinator):
"""DataUpdateCoordinator to handle fetching new data about the vehicle."""
def __init__(self, hass, user, password, vin, region, update_interval, token_store):
"""Initialize the coordinator and set up the Vehicle object."""
self._hass = hass
self.vin = vin
self.vehicle = Vehicle(user, password, vin, region, token_store, hass)
self._available = True
super().__init__(
hass,
_LOGGER,
name=DOMAIN,
update_interval=timedelta(seconds=update_interval),
)
async def _async_update_data(self):
"""Fetch data from FordPass."""
try:
async with async_timeout.timeout(30):
data = await self.vehicle.status()
# Temporarily removed due to Ford backend API changes
# data["guardstatus"] = await self.vehicle.guardStatus()
#data["messages"] = await self.vehicle.messages()
data["vehicles"] = await self.vehicle.vehicles()
_LOGGER.debug(data)
# If data has now been fetched but was previously unavailable, log and reset
if not self._available:
_LOGGER.info("Restored connection to FordPass for %s", self.vin)
self._available = True
return data
except Exception as ex:
self._available = False # Mark as unavailable
_LOGGER.warning(str(ex))
_LOGGER.warning("Error communicating with FordPass for %s", self.vin)
raise UpdateFailed(
f"Error communicating with FordPass for {self.vin}"
) from ex
class FordPassEntity(CoordinatorEntity):
"""Defines a base FordPass entity."""
def __init__(
self, *, device_id: str, name: str, coordinator: FordPassDataUpdateCoordinator
):
"""Initialize the entity."""
super().__init__(coordinator)
self._device_id = device_id
self._name = name
@property
def name(self):
"""Return the name of the entity."""
return self._name
@property
def unique_id(self):
"""Return the unique ID of the entity."""
return f"{self.coordinator.vin}-{self._device_id}"
@property
def device_info(self):
"""Return device information about this device."""
if self._device_id is None:
return None
model = "unknown"
if self.coordinator.data["vehicles"] is not None:
for vehicle in self.coordinator.data["vehicles"]["vehicleProfile"]:
if vehicle["VIN"] == self.coordinator.vin:
model = f"{vehicle['year']} {vehicle['model']}"
return {
"identifiers": {(DOMAIN, self.coordinator.vin)},
"name": f"{VEHICLE} ({self.coordinator.vin})",
"model": f"{model}",
"manufacturer": MANUFACTURER,
}