-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathleaf-python-mqtt.py
executable file
·229 lines (187 loc) · 8.62 KB
/
leaf-python-mqtt.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
#!/usr/bin/python
import pycarwings2
import time
from ConfigParser import SafeConfigParser
import logging
import sys
import pprint
import paho.mqtt.client as mqtt
import schedule
from datetime import datetime
import os
import json
config_file = 'config.ini'
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
logging.info("Startup leaf-python-MQTT: " + datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
config_file_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), config_file)
# Get login details from 'config.ini'
parser = SafeConfigParser()
if os.path.exists(config_file_path):
logging.info("Loaded config file " + config_file_path)
#candidates = [ 'config.ini', 'my_config.ini' ]
candidates = config_file_path
found = parser.read(candidates)
username = parser.get('get-leaf-info', 'username')
password = parser.get('get-leaf-info', 'password')
mqtt_host = parser.get('get-leaf-info', 'mqtt_host')
mqtt_port = parser.get('get-leaf-info', 'mqtt_port')
mqtt_username = parser.get('get-leaf-info', 'mqtt_username')
mqtt_password = parser.get('get-leaf-info', 'mqtt_password')
mqtt_control_topic = parser.get('get-leaf-info', 'mqtt_control_topic')
mqtt_status_topic = parser.get('get-leaf-info', 'mqtt_status_topic')
nissan_region_code = parser.get('get-leaf-info', 'nissan_region_code')
GET_UPDATE_INTERVAL = parser.get('get-leaf-info', 'api_update_interval_min')
logging.info("updating data from API every " + GET_UPDATE_INTERVAL +"min")
else:
logging.error("ERROR: Config file not found " + config_file_path)
quit()
# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
logging.info("Connected to MQTT host " + mqtt_host + " with result code "+str(rc))
logging.info("Suscribing to leaf control topic: " + mqtt_control_topic)
client.subscribe(mqtt_control_topic + "/#")
logging.info("Publishing to leaf status topic: " + mqtt_status_topic)
client.publish(mqtt_status_topic, "MQTT connected");
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
logging.info(msg.topic+" "+str(msg.payload))
control_subtopic = msg.topic.rsplit('/',1)[1]
control_message = msg.payload
logging.info("control sub-topic: " + control_subtopic)
logging.info("control message: " + control_message)
# If climate control messaage is received mqtt_control_topic/climate
if control_subtopic == 'climate':
logging.info('Climate control command received: ' + control_message)
if control_message == '1':
climate_control(1)
if control_message == '0':
climate_control(0)
# If climate control messaage is received on mqtt_control_topic/update
if control_subtopic == 'update':
logging.info('Update control command received: ' + control_message)
if control_message == '1':
leaf_info = get_leaf_update()
time.sleep(10)
mqtt_publish(leaf_info)
client = mqtt.Client()
# Callback when MQTT is connected
client.on_connect = on_connect
# Callback when MQTT message is received
client.on_message = on_message
# Connect to MQTT
client.username_pw_set(mqtt_username, mqtt_password);
client.connect(mqtt_host, mqtt_port, 60)
client.publish(mqtt_status_topic, "Connecting to MQTT host " + mqtt_host);
# Non-blocking MQTT subscription loop
client.loop_start()
def climate_control(climate_control_instruction):
logging.debug("login = %s , password = %s" % ( username , password) )
logging.info("Prepare Session climate control update")
s = pycarwings2.Session(username, password , nissan_region_code)
logging.info("Login...")
logging.info(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
l = s.get_leaf()
if climate_control_instruction == 1:
logging.info("Turning on climate control..wait 60s")
result_key = l.start_climate_control()
time.sleep(60)
start_cc_result = l.get_start_climate_control_result(result_key)
logging.info(start_cc_result)
if climate_control_instruction == 0:
logging.info("Turning off climate control..wait 60s")
result_key = l.stop_climate_control()
time.sleep(60)
stop_cc_result = l.get_stop_climate_control_result(result_key)
logging.info(stop_cc_result)
# Request update from car, use carefully: requires car GSM modem to powerup
def get_leaf_update():
logging.debug("login = %s , password = %s" % ( username , password) )
logging.info("Prepare Session get car update")
s = pycarwings2.Session(username, password , nissan_region_code)
logging.info("Login...")
logging.info(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
try:
l = s.get_leaf()
except:
logging.error("CarWings API error")
logging.info("Requesting update from car..wait 30s")
try:
result_key = l.request_update()
except:
logging.error("ERROR: no responce from car update")
time.sleep(30)
battery_status = l.get_status_from_update(result_key)
while battery_status is None:
logging.error("ERROR: no responce from car")
time.sleep(10)
battery_status = l.get_status_from_update(result_key)
leaf_info = l.get_latest_battery_status()
return (leaf_info)
# Get last updated data from Nissan server
def get_leaf_status():
logging.debug("login = %s , password = %s" % ( username , password) )
logging.info("Prepare Session")
s = pycarwings2.Session(username, password , nissan_region_code)
logging.info("Login...")
logging.info("Start update time: " + datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
try:
l = s.get_leaf()
except:
logging.error("CarWings API error")
return
logging.info("get_latest_battery_status")
leaf_info = l.get_latest_battery_status()
if leaf_info:
logging.info("date %s" % leaf_info.answer["BatteryStatusRecords"]["OperationDateAndTime"])
logging.info("date %s" % leaf_info.answer["BatteryStatusRecords"]["NotificationDateAndTime"])
logging.info("battery_capacity2 %s" % leaf_info.answer["BatteryStatusRecords"]["BatteryStatus"]["BatteryCapacity"])
logging.info("battery_capacity %s" % leaf_info.battery_capacity)
logging.info("charging_status %s" % leaf_info.charging_status)
logging.info("battery_capacity %s" % leaf_info.battery_capacity)
logging.info("battery_remaining_amount %s" % leaf_info.battery_remaining_amount)
logging.info("charging_status %s" % leaf_info.charging_status)
logging.info("is_charging %s" % leaf_info.is_charging)
logging.info("is_quick_charging %s" % leaf_info.is_quick_charging)
logging.info("plugin_state %s" % leaf_info.plugin_state)
logging.info("is_connected %s" % leaf_info.is_connected)
logging.info("is_connected_to_quick_charger %s" % leaf_info.is_connected_to_quick_charger)
logging.info("time_to_full_trickle %s" % leaf_info.time_to_full_trickle)
logging.info("time_to_full_l2 %s" % leaf_info.time_to_full_l2)
logging.info("time_to_full_l2_6kw %s" % leaf_info.time_to_full_l2_6kw)
logging.info("leaf_info.battery_percent %s" % leaf_info.battery_percent)
# logging.info("getting climate update")
# climate = l.get_latest_hvac_status()
# pprint.pprint(climate)
mqtt_publish(leaf_info)
logging.info("End update time: " + datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
logging.info("Schedule API update every " + GET_UPDATE_INTERVAL + "min")
return (leaf_info)
else:
logging.info("Did not get any response from the API")
return
def mqtt_publish(leaf_info):
logging.info("End update time: " + datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
logging.info("publishing to MQTT base status topic: " + mqtt_status_topic)
client.publish(mqtt_status_topic + "/last_updated", leaf_info.answer["BatteryStatusRecords"]["NotificationDateAndTime"])
time.sleep(1)
client.publish(mqtt_status_topic + "/battery_percent", leaf_info.battery_percent)
time.sleep(1)
client.publish(mqtt_status_topic + "/charging_status", leaf_info.charging_status)
time.sleep(1)
client.publish(mqtt_status_topic + "/raw", json.dumps(leaf_info.answer))
time.sleep(1)
if leaf_info.is_connected == True:
client.publish(mqtt_status_topic + "/connected", "Yes")
elif leaf_info.is_connected == False:
client.publish(mqtt_status_topic + "/connected", "No")
else:
client.publish(mqtt_status_topic + "/connected", leaf_info.is_connected)
#########################################################################################################################
# Run on first time
get_leaf_status()
# Then schedule
logging.info("Schedule API update every " + GET_UPDATE_INTERVAL + "min")
schedule.every(int(GET_UPDATE_INTERVAL)).minutes.do(get_leaf_status)
while True:
schedule.run_pending()
time.sleep(1)