flickerstrip-py/flickerstrip_py/discovery.py

33 lines
1.0 KiB
Python

from ssdpy import SSDPClient
import re
import json
from flickerstrip import Flickerstrip
class FlickerstripDiscoveryClient:
def __init__(self):
self.client = SSDPClient(timeout=15)
def __discovered(self, device):
loc = device["location"]
result = re.search("http://(.*):80/description.xml", loc)
ip_address = result.group(1)
return Flickerstrip(ip_address)
def discover(self):
print("Discovering devices...")
devices = self.client.m_search("ssdp:all")
print(f"Discovered {len(devices)} device(s).")
filtered = [d for d in devices if "Flickerstrip" in d["server"]]
print(f"Discovered {len(filtered)} flickerstrip(s).")
mapped = [self.__discovered(d) for d in filtered]
return mapped
if __name__ == "__main__":
client = FlickerstripDiscoveryClient()
flickerstrips = client.discover()
if len(flickerstrips) > 0:
flickerstrips[0].force_update()
print(json.dumps(flickerstrips[0].status.to_json()))