forked from molobrakos/volvooncall
-
Notifications
You must be signed in to change notification settings - Fork 0
/
voc
executable file
·141 lines (117 loc) · 3.77 KB
/
voc
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
#!/usr/bin/env python
# -*- mode: python; coding: utf-8 -*-
"""
Retrieve information from VOC
Usage:
voc (-h | --help)
voc --version
voc [-v|-vv] [options] list
voc [-v|-vv] [options] status
voc [-v|-vv] [options] print [<attribute>]
voc [-v|-vv] [options] (lock | unlock)
voc [-v|-vv] [options] heater (start | stop)
voc [-v|-vv] [options] call <method>
Options:
-u <username> VOC username
-p <password> VOC password
-i <vin> Vehicle VIN or registration number
-g Geolocate position
-h --help Show this message
-v,-vv Increase verbosity
--version Show version
"""
from sys import exit
import docopt
import logging
from volvooncall import __version__, read_credentials, Connection
_LOGGER = logging.getLogger(__name__)
def lookup_position(lat, lon):
try:
from geopy.geocoders import Nominatim
geolocator = Nominatim()
return geolocator.reverse((lat, lon))
except ImportError:
_LOGGER.info('geopy not installed. position lookup not available')
def print_vehicle(vehicle, geolocate=False):
s = '%s %dkm' % (
vehicle,
vehicle.odometer / 1000)
if vehicle.fuelAmountLevel:
s += ' (fuel %s%% %skm)' % (
vehicle.fuelAmountLevel,
vehicle.distanceToEmpty)
print(s)
try:
lat, lon = (vehicle.position['latitude'],
vehicle.position['longitude'])
pos = lookup_position(lat, lon) if geolocate else None
if pos:
print(' position: %.14f,%.14f (%s)' % (lat, lon, pos.address))
else:
print(' position: %.14f,%.14f' % (lat, lon))
except AttributeError:
pass
print(' locked: %s' % ('yes' if vehicle.is_locked else 'no'))
print(' heater: %s' % ('on' if vehicle.is_heater_on else 'off'))
def main():
"""Command line interface."""
args = docopt.docopt(__doc__,
version=__version__)
if args['-v'] == 2:
level=logging.DEBUG
elif args['-v']:
level=logging.INFO
else:
level=logging.ERROR
FORMAT = '%(asctime)s %(name)s: %(message)s'
logging.basicConfig(level=level, format=FORMAT, datefmt='%H:%M:%S')
if args['-u'] and args['-p']:
connection = Connection(args['-u'],
args['-p'])
else:
credentials = read_credentials()
if not credentials:
exit("Could not read credentials and none supplied.")
connection = Connection(**credentials)
if not connection.update():
exit('Could not connect to the server.')
if args['list']:
for vehicle in connection.vehicles:
print(vehicle)
exit()
if args['-i']:
vehicle = connection.vehicle(args['-i'])
else:
vehicle = next(connection.vehicles)
if not vehicle:
exit('Vehicle not found')
if args['status']:
print_vehicle(vehicle, args['-g'])
elif args['print']:
attr = args['<attribute>']
if attr:
if hasattr(vehicle, attr):
print(getattr(vehicle, attr))
else:
exit('unknown attribute')
else:
attrs = {k:v for k,v in vars(vehicle).items() if not k.startswith('_')}
from datetime import datetime
import json
def serialize(obj):
if isinstance(obj, datetime):
return obj.isoformat()
print(json.dumps(attrs, indent=2, default=serialize))
elif args['heater']:
if args['start']:
vehicle.start_heater()
else:
vehicle.stop_heater()
elif args['lock']:
vehicle.lock()
elif args['unlock']:
vehicle.unlock()
elif args['call']:
vehicle.call(args['<method>'])
if __name__ == '__main__':
main()