|
| 1 | +import os |
| 2 | +import queue |
| 3 | +import time |
| 4 | + |
| 5 | +from codelab_adapter_client import AdapterNode |
| 6 | +import yeelight |
| 7 | +from yeelight import transitions |
| 8 | + |
| 9 | +def cmd_run(cmd): |
| 10 | + os.system(cmd) |
| 11 | + |
| 12 | + |
| 13 | +class YeeLightController: |
| 14 | + def __init__(self): |
| 15 | + self.bulbs = yeelight.discover_bulbs() |
| 16 | + |
| 17 | + def get_bulb(self, index): |
| 18 | + ip_addr = self.bulbs[index].get('ip') |
| 19 | + bulb = yeelight.Bulb(ip_addr) |
| 20 | + return bulb |
| 21 | + |
| 22 | + def turn_on(self, index): |
| 23 | + bulb = self.get_bulb(index) |
| 24 | + result = bulb.turn_on() |
| 25 | + return result |
| 26 | + |
| 27 | + def turn_off(self, index): |
| 28 | + bulb = self.get_bulb(index) |
| 29 | + result = bulb.turn_off() |
| 30 | + return result |
| 31 | + |
| 32 | + def set_rgb(self, index, r, g, b): |
| 33 | + bulb = self.get_bulb(index) |
| 34 | + result = bulb.set_rgb(r, g, b) |
| 35 | + return result |
| 36 | + |
| 37 | + def set_brightness(self, index, brightness): |
| 38 | + bulb = self.get_bulb(index) |
| 39 | + result = bulb.set_brightness(brightness) |
| 40 | + return result |
| 41 | + |
| 42 | + def set_temperature(self, index, temp): |
| 43 | + bulb = self.get_bulb(index) |
| 44 | + result = bulb.set_color_temp(temp) |
| 45 | + return result |
| 46 | + |
| 47 | + def set_flow(self, index, flow_preset, count=0): |
| 48 | + bulb = self.get_bulb(index) |
| 49 | + flow = yeelight.Flow( |
| 50 | + count=count, |
| 51 | + transitions=flow_preset, |
| 52 | + ) |
| 53 | + result = bulb.start_flow(flow) |
| 54 | + return result |
| 55 | + |
| 56 | + |
| 57 | +class YeelightNode(AdapterNode): |
| 58 | + def __init__(self): |
| 59 | + super().__init__() |
| 60 | + self.EXTENSION_ID = "eim/yeelight" # default: eim |
| 61 | + self.q = queue.Queue() |
| 62 | + self.bulb_ctl = YeeLightController() |
| 63 | + |
| 64 | + def extension_message_handle(self, topic, payload): |
| 65 | + self.logger.info(topic) |
| 66 | + self.logger.info(payload) |
| 67 | + self.q.put(payload) |
| 68 | + |
| 69 | + def exit_message_handle(self, topic, payload): |
| 70 | + self.terminate() |
| 71 | + |
| 72 | + def run(self): |
| 73 | + while self._running: |
| 74 | + time.sleep(0.1) |
| 75 | + if not self.q.empty(): |
| 76 | + payload = self.q.get() |
| 77 | + self.logger.info(f'python: {payload}') |
| 78 | + message_id = payload.get("message_id") |
| 79 | + python_code = payload["content"] |
| 80 | + try: |
| 81 | + output = eval(python_code, {"__builtins__": None}, { |
| 82 | + "yeelight": yeelight, |
| 83 | + "bulb_ctl": self.bulb_ctl, |
| 84 | + "transitions": transitions |
| 85 | + }) |
| 86 | + except Exception as e: |
| 87 | + output = e |
| 88 | + payload["content"] = str(output) |
| 89 | + message = {"payload": payload} |
| 90 | + self.publish(message) |
| 91 | + |
| 92 | + |
| 93 | +if __name__ == "__main__": |
| 94 | + try: |
| 95 | + node = YeelightNode() |
| 96 | + node.receive_loop_as_thread() # run extension_message_handle, noblock(threaded) |
| 97 | + node.run() |
| 98 | + except KeyboardInterrupt: |
| 99 | + node.terminate() # Clean up before exiting. |
0 commit comments