From 11bf0505fb1bdc276d5794b76a4394dfa2e4f928 Mon Sep 17 00:00:00 2001 From: Martin Bauer Date: Wed, 6 Mar 2024 17:15:07 +0100 Subject: [PATCH] Changes for python3.11 --- espmusicmouse/host_driver/led_cmds.py | 18 +++++++++--------- espmusicmouse/host_driver/main.py | 5 +++-- espmusicmouse/host_driver/mqtt_json.py | 8 ++++---- espmusicmouse/host_driver/requirements.txt | 4 +++- 4 files changed, 19 insertions(+), 16 deletions(-) diff --git a/espmusicmouse/host_driver/led_cmds.py b/espmusicmouse/host_driver/led_cmds.py index 2bf7daf..f0e08fe 100644 --- a/espmusicmouse/host_driver/led_cmds.py +++ b/espmusicmouse/host_driver/led_cmds.py @@ -1,4 +1,4 @@ -from dataclasses import dataclass +from dataclasses import dataclass, field import struct import colorsys @@ -98,8 +98,8 @@ class EffectAlexaSwipeConfig: bell_curve_width_in_leds: float = 3 start_position: float = 180 # in degrees forward: bool = True - primary_color: ColorRGBW = ColorRGBW(0, 0, 1, 0) - secondary_color: ColorRGBW = ColorRGBW(0, 200 / 255, 1, 0) + primary_color: ColorRGBW = field(default_factory=lambda: ColorRGBW(0, 0, 1, 0)) + secondary_color: ColorRGBW = field(default_factory=lambda: ColorRGBW(0, 200 / 255, 1, 0)) def as_bytes(self) -> bytes: return struct.pack( @@ -118,8 +118,8 @@ class EffectRandomTwoColorInterpolationConfig: num_segments: int = 3 hue1_random: bool = False hue2_random: bool = False - color1: ColorHSV = ColorHSV(240, 1, 1) - color2: ColorHSV = ColorHSV(192, 1, 1) + color1: ColorHSV = field(default_factory=lambda: ColorHSV(240, 1, 1)) + color2: ColorHSV = field(default_factory=lambda: ColorHSV(192, 1, 1)) def as_bytes(self) -> bytes: c1 = ColorHSV.fromRGB(self.color1) if isinstance(self.color1, ColorRGBW) else self.color1 @@ -136,7 +136,7 @@ class EffectRandomTwoColorInterpolationConfig: class EffectCircularConfig: speed: float = 360 # in degrees per second width: float = 180 # in degrees - color: ColorRGBW = ColorRGBW(0, 0, 1, 0) + color: ColorRGBW = field(default_factory=lambda: ColorRGBW(0, 0, 1, 0)) def as_bytes(self) -> bytes: return struct.pack(" bytes: return self.swipe.as_bytes() + self.change.as_bytes() @@ -164,4 +164,4 @@ class EffectReverseSwipe: return struct.pack(" str: - return f"Reverse swipe, speed {self.swipeSpeed}, width in leds {self.bellCurveWidthInLeds}, start position {self.startPosition}" \ No newline at end of file + return f"Reverse swipe, speed {self.swipeSpeed}, width in leds {self.bellCurveWidthInLeds}, start position {self.startPosition}" diff --git a/espmusicmouse/host_driver/main.py b/espmusicmouse/host_driver/main.py index 2b7eadb..900be4d 100755 --- a/espmusicmouse/host_driver/main.py +++ b/espmusicmouse/host_driver/main.py @@ -18,6 +18,7 @@ import warnings from pprint import pprint from typing import Optional from mqtt_json import start_mqtt +import aiohttp yaml = YAML(typ='safe') @@ -247,7 +248,7 @@ def main(config_path): cfg = load_config(config_path) loop = asyncio.get_event_loop() - hass = HomeAssistantClient(cfg["general"]["hass_url"], cfg["general"]["hass_token"], loop) + hass = HomeAssistantClient(cfg["general"]["hass_url"], cfg["general"]["hass_token"], loop=loop) coro = serial_asyncio.create_serial_connection(loop, MusicMouseProtocol, @@ -256,7 +257,7 @@ def main(config_path): transport, protocol = loop.run_until_complete(coro) controller = Controller(protocol, hass, cfg) mqtt_cfg = cfg["general"]["mqtt"] - loop.create_task(start_mqtt(protocol, mqtt_cfg["server"], mqtt_cfg["user"],mqtt_cfg["password"] )) + loop.create_task(start_mqtt(protocol, mqtt_cfg["server"], mqtt_cfg["user"], mqtt_cfg["password"] )) loop.create_task(hass.connect()) return controller, loop diff --git a/espmusicmouse/host_driver/mqtt_json.py b/espmusicmouse/host_driver/mqtt_json.py index 04a0426..1085a7e 100644 --- a/espmusicmouse/host_driver/mqtt_json.py +++ b/espmusicmouse/host_driver/mqtt_json.py @@ -1,12 +1,12 @@ from led_cmds import ColorRGBW, EffectStaticConfig, EffectStaticDetailedConfig, EffectCircularConfig, EffectRandomTwoColorInterpolationConfig, EffectAlexaSwipeConfig, EffectSwipeAndChange import asyncio -import asyncio_mqtt +import aiomqtt import json from copy import deepcopy class ShelveLightMqtt: - def __init__(self, protocol, client: asyncio_mqtt.Client): + def __init__(self, protocol, client: aiomqtt.Client): self._protocol = protocol self._mqtt_client = client @@ -176,14 +176,14 @@ async def start_mqtt(music_mouse_protocol, server, username, password): reconnect_interval = 10 # [seconds] while True: try: - async with asyncio_mqtt.Client(hostname=server, username=username, password=password) as client: + async with aiomqtt.Client(hostname=server, username=username, password=password) as client: shelve_light = ShelveLightMqtt(music_mouse_protocol, client) await shelve_light.init() async with client.filtered_messages("musicmouse_json/#") as messages: await client.subscribe("musicmouse_json/#") async for message in messages: await shelve_light.handle_light_message(message) - except asyncio_mqtt.MqttError as error: + except aiomqtt.MqttError as error: print(f'Error "{error}". Reconnecting in {reconnect_interval} seconds') finally: await asyncio.sleep(reconnect_interval) diff --git a/espmusicmouse/host_driver/requirements.txt b/espmusicmouse/host_driver/requirements.txt index adb8514..2b2cccd 100644 --- a/espmusicmouse/host_driver/requirements.txt +++ b/espmusicmouse/host_driver/requirements.txt @@ -1,3 +1,5 @@ pyserial-asyncio==0.6 -python-vlc==3.0.12118 +python-vlc==3.0.20123 hass-client==0.1.2 +ruamel.yaml==0.18.6 +aiomqtt==2.0.0 \ No newline at end of file