Advertisement
alexgill434

pixelit_liveview.py

Apr 30th, 2025
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
YAML 3.05 KB | None | 0 0
  1. import appdaemon.plugins.hass.hassapi as hass
  2. from websocket import create_connection, WebSocketConnectionClosedException
  3. from PIL import Image
  4. from io import BytesIO
  5. import threading
  6. import time
  7. import paho.mqtt.client as mqtt
  8.  
  9. class PixelItLiveView(hass.Hass):
  10.     def initialize(self):
  11.        self.log("PixelIt WebSocket Listener Starting...")
  12.  
  13.         # Setup MQTT with username and password
  14.         self.mqtt_client = mqtt.Client()
  15.         self.mqtt_client.username_pw_set("mqttuser2", password="mqttuser2")
  16.         self.mqtt_client.connect("core-mosquitto", 1883, 60)
  17.         self.mqtt_client.loop_start()
  18.  
  19.         # Start WebSocket listener in a new thread
  20.         threading.Thread(target=self.listen_to_pixelit, daemon=True).start()
  21.  
  22.     def listen_to_pixelit(self):
  23.        ws = None
  24.         while True:
  25.             try:
  26.                 if ws is None:
  27.                    ws = create_connection("ws://192.168.1.198:81")
  28.                 result = ws.recv()
  29.                 self.log(f"Received message: {result[:80]}...")  # Log first 80 chars
  30.  
  31.                if '"liveview":"' in result:
  32.                    hex_string = result.split('"liveview":"')[1].split('"')[0]
  33.                     self.create_and_send_image(hex_string)
  34.  
  35.             except (WebSocketConnectionClosedException, ConnectionRefusedError, OSError) as e:
  36.                 self.log(f"WebSocket connection error: {e}. Reconnecting in 5 seconds...")
  37.                time.sleep(5)
  38.                ws = None
  39.            except Exception as e:
  40.                self.log(f"WebSocket error: {e}. Reconnecting in 5 seconds...")
  41.                time.sleep(5)
  42.                ws = None
  43.  
  44.    def create_and_send_image(self, hex_string):
  45.        try:
  46.            width, height = 32, 8
  47.            img = Image.new("RGB", (width, height), "black")
  48.            pixels = img.load()
  49.  
  50.            for i in range(0, len(hex_string), 6):
  51.                if i + 6 <= len(hex_string):
  52.                    hex_color = hex_string[i:i+6]
  53.                    x = (i // 6) % width
  54.                    y = (i // 6) // width
  55.                    if y < height:
  56.                        r = int(hex_color[0:2], 16)
  57.                        g = int(hex_color[2:4], 16)
  58.                        b = int(hex_color[4:6], 16)
  59.                        pixels[x, y] = (r, g, b)
  60.  
  61.            # Scale up the image
  62.            scale = 10
  63.            resized = img.resize((img.width * scale, img.height * scale), Image.NEAREST)
  64.  
  65.            # Save locally
  66.            image_path = "/config/www/pixelit_liveview.png"
  67.            resized.save(image_path)
  68.            self.log(f"Saved image to {image_path}")
  69.  
  70.            # Send raw PNG over MQTT
  71.            buffered = BytesIO()
  72.            resized.save(buffered, format="PNG")
  73.            img_bytes = buffered.getvalue()
  74.  
  75.            self.mqtt_client.publish("pixelit/liveview", img_bytes, qos=1)
  76.            self.log("Published raw PNG image to MQTT topic pixelit/liveview")
  77.  
  78.        except Exception as e:
  79.            self.log(f"Error creating or sending image: {e}")
  80.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement