forked from mrwiwi/tydom2mqtt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlight.py
118 lines (94 loc) · 4.67 KB
/
light.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
import json
import time
from datetime import datetime
from sensors import sensor
light_command_topic = "light/tydom/{id}/set_levelCmd"
light_config_topic = "homeassistant/light/tydom/{id}/config"
light_level_topic = "light/tydom/{id}/current_level"
light_set_level_topic = "light/tydom/{id}/set_level"
light_attributes_topic = "light/tydom/{id}/attributes"
class Light:
def __init__(self, tydom_attributes, set_level=None, mqtt=None):
self.attributes = tydom_attributes
self.device_id = self.attributes['device_id']
self.endpoint_id = self.attributes['endpoint_id']
self.id = self.attributes['id']
self.name = self.attributes['light_name']
try:
self.current_level = self.attributes['level']
except Exception as e:
print(e)
self.current_level = None
self.set_level = set_level
self.mqtt = mqtt
# def id(self):
# return self.id
# def name(self):
# return self.name
# def current_level(self):
# return self.current_level
# def set_level(self):
# return self.set_level
# def attributes(self):
# return self.attributes
async def setup(self):
self.device = {}
self.device['manufacturer'] = 'Delta Dore'
self.device['model'] = 'Lumiere'
self.device['name'] = self.name
self.device['identifiers'] = self.id
self.config_topic = light_config_topic.format(id=self.id)
self.config = {}
self.config['name'] = self.name
self.config['brightness_scale'] = 100
self.config['unique_id'] = self.id
self.config['optimistic'] = True
self.config['brightness_state_topic'] = light_level_topic.format(id=self.id)
self.config['brightness_command_topic'] = light_set_level_topic.format(id=self.id)
self.config['command_topic'] = light_command_topic.format(id=self.id)
# self.config['set_level_topic'] = light_set_level_topic.format(id=self.id)
self.config['state_topic'] = light_level_topic.format(id=self.id)
self.config['json_attributes_topic'] = light_attributes_topic.format(id=self.id)
self.config['payload_on'] = "ON"
self.config['payload_on'] = "ON"
self.config['on_command_type'] = "brightness"
self.config['retain'] = 'false'
self.config['device'] = self.device
# print(self.config)
if (self.mqtt != None):
self.mqtt.mqtt_client.publish(self.config_topic, json.dumps(self.config), qos=0)
# setup_pub = '(self.config_topic, json.dumps(self.config), qos=0)'
# return(setup_pub)
async def update(self):
await self.setup()
try:
await self.update_sensors()
except Exception as e:
print("light sensors Error :")
print(e)
self.level_topic = light_level_topic.format(id=self.id, current_level=self.current_level)
if (self.mqtt != None):
self.mqtt.mqtt_client.publish(self.level_topic, self.current_level, qos=0, retain=True)
# self.mqtt.mqtt_client.publish('homeassistant/sensor/tydom/last_update', str(datetime.fromtimestamp(time.time())), qos=1, retain=True)
self.mqtt.mqtt_client.publish(self.config['json_attributes_topic'], self.attributes, qos=0)
print("light created / updated : ", self.name, self.id, self.current_level)
# update_pub = '(self.level_topic, self.current_level, qos=0, retain=True)'
# return(update_pub)
async def update_sensors(self):
# print('test sensors !')
for i, j in self.attributes.items():
# sensor_name = "tydom_alarm_sensor_"+i
# print("name "+sensor_name, "elem_name "+i, "attributes_topic_from_device ",self.config['json_attributes_topic'], "mqtt",self.mqtt)
if not i == 'device_type' or not i == 'id':
new_sensor = None
new_sensor = sensor(elem_name=i, tydom_attributes_payload=self.attributes, attributes_topic_from_device=self.config['json_attributes_topic'], mqtt=self.mqtt)
await new_sensor.update()
# def __init__(self, name, elem_name, tydom_attributes_payload, attributes_topic_from_device, mqtt=None):
async def put_level(tydom_client, device_id, light_id, level):
print(light_id, 'level', level)
if not (level == ''):
await tydom_client.put_devices_data(device_id, light_id, 'level', level)
async def put_levelCmd(tydom_client, device_id, light_id, levelCmd):
print(light_id, 'levelCmd', levelCmd)
if not (levelCmd == ''):
await tydom_client.put_devices_data(device_id, light_id, 'levelCmd', levelCmd)