From d345c2ba3b356f1b0759130be40e6f1a556e7c6b Mon Sep 17 00:00:00 2001 From: Patrick762 Date: Thu, 25 May 2023 17:18:00 +0200 Subject: [PATCH] fixed ssdp location field --- README.md | 6 +++--- streamdeckapi/server.py | 36 +++++++++++++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 31f96f9..bfcf8bc 100644 --- a/README.md +++ b/README.md @@ -5,9 +5,6 @@ Stream Deck API Library for Home Assistant Stream Deck Integration Only compatible with separate [Stream Deck Plugin](https://github.com/Patrick762/streamdeckapi-plugin) or the bundled server. -## Dependencies -- [websockets](https://pypi.org/project/websockets/) 11.0.2 - ## Server This library also contains a server to use the streamdeck with Linux or without the official Stream Deck Software. @@ -18,6 +15,9 @@ For this to work, the following software is required: The event `doubleTap` is not working with this server software. +### Limitations +Discovery over SSDP might not work. + ### Installation on Linux / Raspberry Pi Install requirements: diff --git a/streamdeckapi/server.py b/streamdeckapi/server.py index b39d71d..42105fc 100644 --- a/streamdeckapi/server.py +++ b/streamdeckapi/server.py @@ -6,6 +6,8 @@ import asyncio import platform import sqlite3 import base64 +import socket +from uuid import uuid4 from datetime import datetime from multiprocessing import Process import aiohttp @@ -496,15 +498,47 @@ def init_all(): deck.set_key_callback_async(on_key_change) +def get_local_ip(): + """Get local ip address.""" + connection = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + try: + connection.connect(('192.255.255.255', 1)) + address = connection.getsockname()[0] + except socket.error: + address = '127.0.0.1' + finally: + connection.close() + return address + + def start_ssdp_server(): """Start SSDP server.""" print("Starting SSDP server ...") - server = SSDPServer(SD_SSDP) + + address = get_local_ip() + broadcast = "239.255.255.250" + location = f"http://{address}:{PLUGIN_PORT}/device.xml" + usn = f"uuid:{str(uuid4())}::{SD_SSDP}" + server = "python/3 UPnP/1.1 ssdpy/0.4.1" + + print(f"IP Address for SSDP: {address}") + print(f"SSDP broadcast ip: {broadcast}") + print(f"SSDP location: {location}") + + server = SSDPServer(usn, + address=broadcast, + location=location, + max_age=1800, + extra_fields={ + "st": SD_SSDP, + "server": server + }) server.serve_forever() class Timer: """Timer class.""" + def __init__(self, interval, callback): """Init timer.""" self._interval = interval