From 1b85eefad2dc06652d0f12ed7664ed14bc689a04 Mon Sep 17 00:00:00 2001 From: avollkopf <43980694+avollkopf@users.noreply.github.com> Date: Sat, 23 Apr 2022 20:32:32 +0200 Subject: [PATCH] disable sorting of elements by default --- cbpi/__init__.py | 2 +- cbpi/controller/actor_controller.py | 9 +++++---- cbpi/controller/basic_controller2.py | 3 ++- cbpi/controller/sensor_controller.py | 1 + cbpi/websocket.py | 11 ++++++----- 5 files changed, 15 insertions(+), 11 deletions(-) diff --git a/cbpi/__init__.py b/cbpi/__init__.py index d63ee16..743997c 100644 --- a/cbpi/__init__.py +++ b/cbpi/__init__.py @@ -1,3 +1,3 @@ -__version__ = "4.0.5.a10" +__version__ = "4.0.5.a11" __codename__ = "Spring Break" diff --git a/cbpi/controller/actor_controller.py b/cbpi/controller/actor_controller.py index f31abeb..1ae2a01 100644 --- a/cbpi/controller/actor_controller.py +++ b/cbpi/controller/actor_controller.py @@ -7,6 +7,7 @@ class ActorController(BasicController): def __init__(self, cbpi): super(ActorController, self).__init__(cbpi, Actor,"actor.json") self.update_key = "actorupdate" + self.sorting=True async def on(self, id, power=None): try: @@ -20,7 +21,7 @@ class ActorController(BasicController): if item.instance.state is False: await item.instance.on(power) #await self.push_udpate() - self.cbpi.ws.send(dict(topic=self.update_key, data=list(map(lambda item: item.to_dict(), self.data)))) + self.cbpi.ws.send(dict(topic=self.update_key, data=list(map(lambda item: item.to_dict(), self.data))),self.sorting) self.cbpi.push_update("cbpi/actorupdate/{}".format(id), item.to_dict(), True) else: await self.set_power(id, power) @@ -34,7 +35,7 @@ class ActorController(BasicController): if item.instance.state is True: await item.instance.off() #await self.push_udpate() - self.cbpi.ws.send(dict(topic=self.update_key, data=list(map(lambda item: item.to_dict(), self.data)))) + self.cbpi.ws.send(dict(topic=self.update_key, data=list(map(lambda item: item.to_dict(), self.data))),self.sorting) self.cbpi.push_update("cbpi/actorupdate/{}".format(id), item.to_dict()) except Exception as e: logging.error("Failed to switch on Actor {} {}".format(id, e), True) @@ -44,7 +45,7 @@ class ActorController(BasicController): item = self.find_by_id(id) instance = item.get("instance") await instance.toggle() - self.cbpi.ws.send(dict(topic=self.update_key, data=list(map(lambda item: item.to_dict(), self.data)))) + self.cbpi.ws.send(dict(topic=self.update_key, data=list(map(lambda item: item.to_dict(), self.data))),self.sorting) self.cbpi.push_update("cbpi/actorupdate/{}".format(id), item.to_dict()) except Exception as e: logging.error("Failed to toggle Actor {} {}".format(id, e)) @@ -61,7 +62,7 @@ class ActorController(BasicController): item = self.find_by_id(id) item.power = round(power) #await self.push_udpate() - self.cbpi.ws.send(dict(topic=self.update_key, data=list(map(lambda item: item.to_dict(), self.data)))) + self.cbpi.ws.send(dict(topic=self.update_key, data=list(map(lambda item: item.to_dict(), self.data))),self.sorting) self.cbpi.push_update("cbpi/actorupdate/{}".format(id), item.to_dict()) except Exception as e: logging.error("Failed to update Actor {} {}".format(id, e)) diff --git a/cbpi/controller/basic_controller2.py b/cbpi/controller/basic_controller2.py index 1a42119..7ef8b4f 100644 --- a/cbpi/controller/basic_controller2.py +++ b/cbpi/controller/basic_controller2.py @@ -14,6 +14,7 @@ class BasicController: def __init__(self, cbpi, resource, file): self.resource = resource self.update_key = "" + self.sorting = False self.name = self.__class__.__name__ self.cbpi = cbpi self.cbpi.register(self) @@ -55,7 +56,7 @@ class BasicController: await self.push_udpate() async def push_udpate(self): - self.cbpi.ws.send(dict(topic=self.update_key, data=list(map(lambda item: item.to_dict(), self.data)))) + self.cbpi.ws.send(dict(topic=self.update_key, data=list(map(lambda item: item.to_dict(), self.data))),self.sorting) #self.cbpi.push_update("cbpi/{}".format(self.update_key), list(map(lambda item: item.to_dict(), self.data))) for item in self.data: self.cbpi.push_update("cbpi/{}/{}".format(self.update_key,item.id), item.to_dict()) diff --git a/cbpi/controller/sensor_controller.py b/cbpi/controller/sensor_controller.py index 44f0ce0..6c70c87 100644 --- a/cbpi/controller/sensor_controller.py +++ b/cbpi/controller/sensor_controller.py @@ -6,6 +6,7 @@ class SensorController(BasicController): def __init__(self, cbpi): super(SensorController, self).__init__(cbpi, Sensor, "sensor.json") self.update_key = "sensorupdate" + self.sorting = True def create_dict(self, data): try: diff --git a/cbpi/websocket.py b/cbpi/websocket.py index 842f46f..970ccc4 100644 --- a/cbpi/websocket.py +++ b/cbpi/websocket.py @@ -28,15 +28,16 @@ class CBPiWebSocket: self.send(data) - def send(self, data): + def send(self, data, sorting=False): self.logger.debug("broadcast to ws clients. Data: %s" % data) for ws in self._clients: async def send_data(ws, data): try: - try: - data['data'].sort(key=lambda x: x.get('name').upper()) - except: - pass + if sorting: + try: + data['data'].sort(key=lambda x: x.get('name').upper()) + except: + pass await ws.send_json(data=data, dumps=json_dumps) except Exception as e: self.logger.error("Error with client %s: %s" % (ws, str(e)))