annotate protocols.py @ 20:24a574108365

more protocols; bugs in setColor
author drewp@bigasterisk.com
date Mon, 29 Jan 2024 11:52:43 -0800
parents 61d4ccecfed8
children b8201490c731
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
20
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 15
diff changeset
1 import logging
15
61d4ccecfed8 rough refactor
drewp@bigasterisk.com
parents:
diff changeset
2 import json
20
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 15
diff changeset
3 import aiohttp
15
61d4ccecfed8 rough refactor
drewp@bigasterisk.com
parents:
diff changeset
4 from color_convert import DeviceColor
61d4ccecfed8 rough refactor
drewp@bigasterisk.com
parents:
diff changeset
5 from mqtt_io import MqttIo
61d4ccecfed8 rough refactor
drewp@bigasterisk.com
parents:
diff changeset
6
20
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 15
diff changeset
7 log = logging.getLogger('prot')
15
61d4ccecfed8 rough refactor
drewp@bigasterisk.com
parents:
diff changeset
8
61d4ccecfed8 rough refactor
drewp@bigasterisk.com
parents:
diff changeset
9 class Transport:
61d4ccecfed8 rough refactor
drewp@bigasterisk.com
parents:
diff changeset
10
61d4ccecfed8 rough refactor
drewp@bigasterisk.com
parents:
diff changeset
11 def linked(self):
61d4ccecfed8 rough refactor
drewp@bigasterisk.com
parents:
diff changeset
12 return {'label': str(self)}
61d4ccecfed8 rough refactor
drewp@bigasterisk.com
parents:
diff changeset
13
61d4ccecfed8 rough refactor
drewp@bigasterisk.com
parents:
diff changeset
14 async def send(self, dc: DeviceColor):
61d4ccecfed8 rough refactor
drewp@bigasterisk.com
parents:
diff changeset
15 raise TypeError
61d4ccecfed8 rough refactor
drewp@bigasterisk.com
parents:
diff changeset
16
61d4ccecfed8 rough refactor
drewp@bigasterisk.com
parents:
diff changeset
17
61d4ccecfed8 rough refactor
drewp@bigasterisk.com
parents:
diff changeset
18 def zigbeeHexMessage(dc: DeviceColor) -> dict:
61d4ccecfed8 rough refactor
drewp@bigasterisk.com
parents:
diff changeset
19 msg: dict = {"transition": 0, "brightness": int(255 * dc.brightness)}
61d4ccecfed8 rough refactor
drewp@bigasterisk.com
parents:
diff changeset
20 c = "#%02x%02x%02x" % (int(dc.r * 255), int(dc.g * 255), int(dc.b * 255))
61d4ccecfed8 rough refactor
drewp@bigasterisk.com
parents:
diff changeset
21 msg["color"] = {"hex": c}
61d4ccecfed8 rough refactor
drewp@bigasterisk.com
parents:
diff changeset
22 return msg
61d4ccecfed8 rough refactor
drewp@bigasterisk.com
parents:
diff changeset
23
61d4ccecfed8 rough refactor
drewp@bigasterisk.com
parents:
diff changeset
24
61d4ccecfed8 rough refactor
drewp@bigasterisk.com
parents:
diff changeset
25 class ZigbeeTransport(Transport):
61d4ccecfed8 rough refactor
drewp@bigasterisk.com
parents:
diff changeset
26
61d4ccecfed8 rough refactor
drewp@bigasterisk.com
parents:
diff changeset
27 def __init__(self, mqtt: MqttIo, name: str, ieee: str):
61d4ccecfed8 rough refactor
drewp@bigasterisk.com
parents:
diff changeset
28 self.mqtt = mqtt
61d4ccecfed8 rough refactor
drewp@bigasterisk.com
parents:
diff changeset
29 self.name = name
61d4ccecfed8 rough refactor
drewp@bigasterisk.com
parents:
diff changeset
30 self.ieee = ieee
61d4ccecfed8 rough refactor
drewp@bigasterisk.com
parents:
diff changeset
31
61d4ccecfed8 rough refactor
drewp@bigasterisk.com
parents:
diff changeset
32 def linked(self):
20
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 15
diff changeset
33 return {'url': f'https://bigasterisk.com/zigbee/console/#/device/{self.ieee}/info', 'label': self.name}
15
61d4ccecfed8 rough refactor
drewp@bigasterisk.com
parents:
diff changeset
34
61d4ccecfed8 rough refactor
drewp@bigasterisk.com
parents:
diff changeset
35 async def send(self, dc: DeviceColor):
61d4ccecfed8 rough refactor
drewp@bigasterisk.com
parents:
diff changeset
36 await self.mqtt.publish(f'zigbee/{self.name}/set', json.dumps(zigbeeHexMessage(dc)))
20
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 15
diff changeset
37
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 15
diff changeset
38
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 15
diff changeset
39 class SonoffRelayTransport(Transport):
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 15
diff changeset
40
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 15
diff changeset
41 def __init__(self, mqtt: MqttIo, name: str):
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 15
diff changeset
42 self.mqtt = mqtt
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 15
diff changeset
43 self.name = name
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 15
diff changeset
44
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 15
diff changeset
45 def linked(self):
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 15
diff changeset
46 return {'label': self.name}
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 15
diff changeset
47
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 15
diff changeset
48 async def send(self, dc: DeviceColor):
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 15
diff changeset
49 topic = f'{self.name}/switch/sonoff_basic_relay/command'
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 15
diff changeset
50 msg = 'ON' if dc.brightness else 'OFF'
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 15
diff changeset
51 log.info(f'sonoff {topic=} {msg=}')
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 15
diff changeset
52 await self.mqtt.publish(topic, msg)
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 15
diff changeset
53
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 15
diff changeset
54
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 15
diff changeset
55 class _WebTransport(Transport):
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 15
diff changeset
56
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 15
diff changeset
57 def __init__(self, hostname: str):
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 15
diff changeset
58 self.hostname = hostname
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 15
diff changeset
59 self._session = aiohttp.ClientSession()
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 15
diff changeset
60
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 15
diff changeset
61 def linked(self):
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 15
diff changeset
62 return {'url': f'http://{self.hostname}/', 'label': self.hostname}
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 15
diff changeset
63
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 15
diff changeset
64
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 15
diff changeset
65 class TasmotaWebTransport(_WebTransport):
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 15
diff changeset
66
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 15
diff changeset
67 async def send(self, dc: DeviceColor):
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 15
diff changeset
68 cmnd = 'Color ' + ','.join(str(int(x * 255)) for x in (dc.r, dc.g, dc.b, dc.cw, dc.ww))
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 15
diff changeset
69 async with self._session.get(f'http://{self.hostname}/cm', params={'cmnd': cmnd}) as resp:
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 15
diff changeset
70 await resp.text()
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 15
diff changeset
71 # {"POWER":"ON","Dimmer":21,"Color":"3636363600","HSBColor":"0,0,21","White":21,"CT":153,"Channel":[21,21,21,21,0]}
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 15
diff changeset
72
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 15
diff changeset
73
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 15
diff changeset
74 class ShellyGen1WebTransport(_WebTransport):
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 15
diff changeset
75
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 15
diff changeset
76 async def send(self, dc: DeviceColor):
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 15
diff changeset
77 # also see https://shelly-api-docs.shelly.cloud/gen1/#shelly-rgbw2-color-status for metrics
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 15
diff changeset
78 async with self._session.get(f'http://{self.hostname}/light/0',
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 15
diff changeset
79 params={
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 15
diff changeset
80 'red': int(dc.r * 255),
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 15
diff changeset
81 'green': int(dc.g * 255),
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 15
diff changeset
82 'blue': int(dc.b * 255),
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 15
diff changeset
83 'white': int(dc.w * 255),
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 15
diff changeset
84 }) as resp:
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 15
diff changeset
85 await resp.text()
24a574108365 more protocols; bugs in setColor
drewp@bigasterisk.com
parents: 15
diff changeset
86 # {..."mode":"color","red":255,"green":242,"blue":0,"white":255,"gain":59,"effect":0,"transition":0,"power":18.00,"overpower":false}