-
Notifications
You must be signed in to change notification settings - Fork 3
/
pijuicemqtt.py
231 lines (201 loc) · 7.79 KB
/
pijuicemqtt.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
"""
Get PiJuice UPS hat information and publish to MQTT for consumption by eg Node-RED and Home Assistant
"""
import argparse
import platform
import signal
import sys
import threading
from json import dumps
import paho.mqtt.client as mqtt
import yaml
from pijuice import PiJuice
from pijuice import __version__ as library_version
SERVICE_NAME = "pijuicemqtt"
parser = argparse.ArgumentParser(description="PiJuice to MQTT")
parser.add_argument(
"-c",
"--config",
default="config.yaml",
help="Configuration yaml file, defaults to `config.yaml`",
dest="config_file",
)
args = parser.parse_args()
pijuice = PiJuice(1, 0x14) # Instantiate PiJuice interface object
timer_thread = None
def load_config(config_file):
"""Load the configuration from config yaml file and use it to override the defaults."""
with open(config_file, "r") as f:
config_override = yaml.safe_load(f)
default_config = {
"mqtt": {
"broker": "127.0.0.1",
"port": 1883,
"username": None,
"password": None,
},
"homeassistant": {
"topic": "homeassistant",
"sensor": True,
},
"publish_period": 30,
"hostname": platform.node(),
}
config = {**default_config, **config_override}
return config
def mqtt_on_connect(client, userdata, flags, rc):
"""Renew subscriptions and set Last Will message when connect to broker."""
# Set up Last Will, and then set services' status to 'online'
client.will_set(
f"{SERVICE_NAME}/{config['hostname']}/service",
payload="offline",
qos=1,
retain=True,
)
client.publish(
f"{SERVICE_NAME}/{config['hostname']}/service",
payload="online",
qos=1,
retain=True,
)
# Home Assistant MQTT autoconfig
if config["homeassistant"]["sensor"]:
print("Publishing Home Assistant MQTT autoconfig")
# Payload that is common to both autoconfig messages
battery_capacity = pijuice.config.GetBatteryProfile()["data"]["capacity"]
firmware_version = pijuice.config.GetFirmwareVersion()["data"]["version"]
base_payload = {
"availability_topic": f"{SERVICE_NAME}/{config['hostname']}/service",
"payload_available": "online",
"payload_not_available": "offline",
"state_topic": f"{SERVICE_NAME}/{config['hostname']}/status",
"json_attributes_topic": f"{SERVICE_NAME}/{config['hostname']}/status",
"device": {
"identifiers": [f"{SERVICE_NAME}-{config['hostname']}"],
"name": f"{config['hostname']} PiJuice",
"sw_version": f"Library {library_version}, Firmware {firmware_version}",
"model": f"PiJuice {battery_capacity} mAh",
"manufacturer": "PiSupply",
},
}
if "expire_after" in config["homeassistant"]:
base_payload["expire_after"] = int(config["homeassistant"]["expire_after"])
# Battery charge percentage
payload = {
"name": f"{config['hostname']} PiJuice Battery",
"unique_id": f"{SERVICE_NAME}-{config['hostname']}-batteryCharge",
"value_template": "{{ value_json.batteryCharge }}",
"device_class": "battery",
"unit_of_measurement": "%",
}
client.publish(
f"{config['homeassistant']['topic']}/sensor/{SERVICE_NAME}-{config['hostname']}/batteryCharge/config",
dumps({**base_payload, **payload}),
qos=1,
retain=True,
)
# Power/No Power binary sensor
payload = {
"name": f"{config['hostname']} PiJuice PowerInput5vIo",
"unique_id": f"{SERVICE_NAME}-{config['hostname']}-powerInput5vIo",
"value_template": "{{ value_json.powerInput5vIo }}",
"payload_off": "NOT_PRESENT",
"payload_on": "PRESENT",
"device_class": "power",
}
client.publish(
f"{config['homeassistant']['topic']}/binary_sensor/{SERVICE_NAME}-{config['hostname']}/powerInput5vIo/config",
dumps({**base_payload, **payload}),
qos=1,
retain=True,
)
# Battery Temperature sensor
payload = {
"name": f"{config['hostname']} PiJuice BatteryTemperature",
"unique_id": f"{SERVICE_NAME}-{config['hostname']}-batteryTemperature",
"value_template": "{{ value_json.batteryTemperature }}",
"device_class": "temperature",
"unit_of_measurement": "°C",
"enabled_by_default": False,
"entity_category": "diagnostic",
}
client.publish(
f"{config['homeassistant']['topic']}/sensor/{SERVICE_NAME}-{config['hostname']}/batteryTemperature/config",
dumps({**base_payload, **payload}),
qos=1,
retain=True,
)
# Battery Status sensor
payload = {
"name": f"{config['hostname']} PiJuice BatteryStatus",
"unique_id": f"{SERVICE_NAME}-{config['hostname']}-batteryStatus",
"value_template": "{{ value_json.batteryStatus }}",
"enabled_by_default": False,
"entity_category": "diagnostic",
}
client.publish(
f"{config['homeassistant']['topic']}/sensor/{SERVICE_NAME}-{config['hostname']}/batteryStatus/config",
dumps({**base_payload, **payload}),
qos=1,
retain=True,
)
def on_exit(signum, frame):
"""
Update MQTT services' status to `offline` and stop the timer thread.
Called when program exit is received.
"""
print("Exiting...")
client.publish(
f"{SERVICE_NAME}/{config['hostname']}/service",
payload="offline",
qos=1,
retain=True,
)
timer_thread.cancel()
timer_thread.join()
sys.exit(0)
def publish_pijuice():
"""
Publish PiJuice UPS Hat information every `publish_period` seconds.
See https://github.com/PiSupply/PiJuice/tree/master/Software#i2c-command-api
"""
global timer_thread
timer_thread = threading.Timer(config["publish_period"], publish_pijuice)
timer_thread.start()
try:
if "publish_online_status" in config and config["publish_online_status"]:
client.publish(
f"{SERVICE_NAME}/{config['hostname']}/service",
payload="online",
qos=1,
retain=True,
)
status = pijuice.status.GetStatus()["data"]
pijuice_status = {
"batteryCharge": pijuice.status.GetChargeLevel()["data"],
"batteryVoltage": pijuice.status.GetBatteryVoltage()["data"] / 1000,
"batteryCurrent": pijuice.status.GetBatteryCurrent()["data"] / 1000,
"batteryTemperature": pijuice.status.GetBatteryTemperature()["data"],
"batteryStatus": status["battery"],
"powerInput": status["powerInput"],
"powerInput5vIo": status["powerInput5vIo"],
"ioVoltage": pijuice.status.GetIoVoltage()["data"] / 1000,
"ioCurrent": pijuice.status.GetIoCurrent()["data"] / 1000,
}
client.publish(
f"{SERVICE_NAME}/{config['hostname']}/status",
dumps(pijuice_status),
)
except KeyError:
print("Could not read PiJuice data, skipping")
config = load_config(args.config_file)
if __name__ == "__main__":
client = mqtt.Client()
client.on_connect = mqtt_on_connect
client.username_pw_set(config["mqtt"]["username"], config["mqtt"]["password"])
client.connect(config["mqtt"]["broker"], config["mqtt"]["port"], 60)
print("PiJuice connected to MQTT broker")
signal.signal(signal.SIGINT, on_exit)
signal.signal(signal.SIGTERM, on_exit)
publish_pijuice()
client.loop_forever()