disable sorting of elements by default

This commit is contained in:
avollkopf 2022-04-23 20:32:32 +02:00
parent da975f92b2
commit 1b85eefad2
5 changed files with 15 additions and 11 deletions

View file

@ -1,3 +1,3 @@
__version__ = "4.0.5.a10"
__version__ = "4.0.5.a11"
__codename__ = "Spring Break"

View file

@ -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))

View file

@ -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())

View file

@ -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:

View file

@ -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)))