-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
92 lines (78 loc) · 3.61 KB
/
__init__.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
from __future__ import annotations
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, ServiceCall
from homeassistant.exceptions import ServiceValidationError
from .const import DOMAIN
def setup(hass: HomeAssistant, config: ConfigEntry) -> bool:
if DOMAIN not in hass.data:
hass.data[DOMAIN] = {}
async def handle_update_sensors(call: ServiceCall) -> bool:
success = False
for entry_id in hass.data[DOMAIN]:
this_config = hass.data[DOMAIN][entry_id]
if "vin" in call.data:
found = False
vehicles = this_config['vehicles']
for vin in vehicles:
if vin == call.data['vin']:
found = True
if not found:
continue
this_bluelink = this_config['bluelink']
await this_bluelink.setup(force=True)
success = True
return success
async def handle_lock_vehicle(call: ServiceCall) -> bool:
if "vin" not in call.data:
raise ServiceValidationError("Missing VIN")
for entry_id in hass.data[DOMAIN]:
this_config = hass.data[DOMAIN][entry_id]
vehicles = this_config['vehicles']
if call.data['vin'] in vehicles:
print(f"Locking vehicle {call.data['vin']}")
this_bluelink = this_config['bluelink']
success = await this_bluelink.lock_vehicle(vin=call.data['vin'])
print(f"Success: {success}")
return success
return False
async def handle_unlock_vehicle(call) -> bool:
if "vin" not in call.data:
raise ServiceValidationError("Missing VIN")
for entry_id in hass.data[DOMAIN]:
this_config = hass.data[DOMAIN][entry_id]
vehicles = this_config['vehicles']
if call.data['vin'] in vehicles:
print(f"Unlocking vehicle {call.data['vin']}")
this_bluelink = this_config['bluelink']
success = await this_bluelink.unlock_vehicle(vin=call.data['vin'])
print(f"Success: {success}")
return success
return False
async def handle_start_vehicle(call) -> bool:
if "vin" not in call.data:
raise ServiceValidationError("Missing VIN")
for entry_id in hass.data[DOMAIN]:
this_config = hass.data[DOMAIN][entry_id]
vehicles = this_config['vehicles']
if call.data['vin'] in vehicles:
print(f"Starting vehicle {call.data['vin']}")
this_bluelink = this_config['bluelink']
success = await this_bluelink.start_vehicle(vin=call.data['vin'])
print(f"Success: {success}")
return success
return False
hass.services.register(DOMAIN, "update_sensors", handle_update_sensors)
hass.services.register(DOMAIN, "lock_vehicle", handle_lock_vehicle)
hass.services.register(DOMAIN, "unlock_vehicle", handle_unlock_vehicle)
hass.services.register(DOMAIN, "start_vehicle", handle_start_vehicle)
return True
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
success = await hass.config_entries.async_forward_entry_setups(entry, ["sensor"])
if success:
hass.data[DOMAIN].push(entry.entry_id)
return True
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
success = await hass.config_entries.async_unload_platforms(entry, ["sensor"])
if success:
hass.data[DOMAIN].pop(entry.entry_id)
return success