This repository has been archived by the owner on Apr 23, 2023. It is now read-only.
forked from MattDog06/Nuki-MQTT-auto.-Discovery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nuki_remove_old.py
100 lines (70 loc) · 3.1 KB
/
nuki_remove_old.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
# Parameters
DEVICE_ID = "device_id"
DEVICE_NAME = "device_name"
DEVICE_MODEL = "device_model"
DISCOVERY_TOPIC = "discovery_topic"
DOOR_SENSOR_AVAILABLE = "door_sensor_available"
KEYPAD_AVAILABLE = "keypad_available"
DEFAULT_DISCOVERY_TOPIC = "homeassistant"
def get_error_message(parameter, value):
return "Parameter '" + parameter + "' is required! Current value is '" + str(value) + "'."
def get_object_id(name):
return name.replace(" ", "_").replace("-", "_").lower()
def to_json(dictonary):
return '{}'.format(dictonary).replace("\'", "\"").replace("\"\"", "\'")
def get_discovery_topic(discovery_topic, component, node_id, name):
return discovery_topic + "/" + component + "/" + node_id + "/" + get_object_id(name) + "/config"
def publish(hass, topic, payload):
data = {
"topic": topic,
"payload": payload,
"retain": 'true'
}
hass.services.call("mqtt", "publish", data)
def main(hass, data):
device_id = data.get(DEVICE_ID)
device_name = data.get(DEVICE_NAME)
discovery_topic = data.get(DISCOVERY_TOPIC) or DEFAULT_DISCOVERY_TOPIC
door_sensor_available = data.get(DOOR_SENSOR_AVAILABLE) or False
keypad_available = data.get(KEYPAD_AVAILABLE) or False
if isinstance(device_id, int): # Because of hex device id, always convert it to string
device_id = str(device_id)
if device_id == None or device_id == "":
logger.error(get_error_message(DEVICE_ID, device_id))
return
if device_name == None or device_name == "":
logger.error(get_error_message(DEVICE_NAME, device_name))
return
# Lock
name = device_name
publish(hass, get_discovery_topic(discovery_topic, "lock", device_id, name),"")
# Battery critical
name = device_name + " Battery Critical"
publish(hass, get_discovery_topic(discovery_topic, "binary_sensor", device_id, name), "")
# Battery charge state
name = device_name + " Battery"
publish(hass, get_discovery_topic(discovery_topic, "sensor", device_id, name), "")
# Battery charging
name = device_name + " Battery Charging"
publish(hass, get_discovery_topic(discovery_topic, "binary_sensor", device_id, name), "")
if door_sensor_available:
# Door sensor
name = device_name + " Door Sensor"
publish(hass, get_discovery_topic(discovery_topic, "binary_sensor", device_id, name), "")
# Door sensor battery critical
name = device_name + " Door Sensor Battery Critical"
publish(hass, get_discovery_topic(discovery_topic, "binary_sensor", device_id, name), "")
if keypad_available:
# Keypad battery critical
name = device_name + " Keypad Battery Critical"
publish(hass, get_discovery_topic(discovery_topic, "binary_sensor", device_id, name), "")
# Unlatch button
name = device_name + " Unlatch"
publish(hass, get_discovery_topic(discovery_topic, "button", device_id, name), "")
# Lock'n'Go button
name = device_name + " Lock-n-Go"
publish(hass, get_discovery_topic(discovery_topic, "button", device_id, name), "")
# Lock'n'Go with unlatch button
name = device_name + " Lock-n-Go With Unlatch"
publish(hass, get_discovery_topic(discovery_topic, "button", device_id, name), "")
main(hass, data)