craftbeerpi4-pione/cbpi/controller/actor_controller.py

86 lines
3.7 KiB
Python
Raw Normal View History

2021-02-16 20:37:51 +01:00
from cbpi.api.dataclasses import Actor
from cbpi.controller.basic_controller2 import BasicController
2018-12-29 00:27:19 +01:00
import logging
2021-01-22 23:25:20 +01:00
from tabulate import tabulate
class ActorController(BasicController):
2018-11-01 21:25:42 +01:00
2018-11-01 19:50:04 +01:00
def __init__(self, cbpi):
2021-02-16 20:37:51 +01:00
super(ActorController, self).__init__(cbpi, Actor,"actor.json")
2021-01-24 22:14:57 +01:00
self.update_key = "actorupdate"
2022-04-23 20:32:32 +02:00
self.sorting=True
2021-02-16 20:37:51 +01:00
2021-11-15 20:18:33 +01:00
async def on(self, id, power=None):
2019-01-04 09:29:09 +01:00
try:
2021-01-22 23:25:20 +01:00
item = self.find_by_id(id)
if power is None:
logging.info("Power is none")
if item.power:
power = item.power
else:
power = 100
2021-02-16 20:37:51 +01:00
if item.instance.state is False:
2021-11-11 11:22:13 +01:00
await item.instance.on(power)
#await self.push_udpate()
2022-04-23 20:32:32 +02:00
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)
2021-11-11 11:22:13 +01:00
else:
await self.set_power(id, power)
2021-03-14 11:52:46 +01:00
2019-01-04 09:29:09 +01:00
except Exception as e:
2021-11-06 15:15:11 +01:00
logging.error("Failed to switch on Actor {} {}".format(id, e))
2019-01-28 22:21:31 +01:00
2021-01-22 23:25:20 +01:00
async def off(self, id):
try:
item = self.find_by_id(id)
2021-02-16 20:37:51 +01:00
if item.instance.state is True:
await item.instance.off()
#await self.push_udpate()
2022-04-23 20:32:32 +02:00
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())
2021-01-22 23:25:20 +01:00
except Exception as e:
2021-11-06 15:15:11 +01:00
logging.error("Failed to switch on Actor {} {}".format(id, e), True)
2018-12-13 21:45:33 +01:00
2021-01-22 23:25:20 +01:00
async def toogle(self, id):
try:
item = self.find_by_id(id)
instance = item.get("instance")
await instance.toggle()
2022-04-23 20:32:32 +02:00
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())
2021-01-22 23:25:20 +01:00
except Exception as e:
2021-11-06 15:15:11 +01:00
logging.error("Failed to toggle Actor {} {}".format(id, e))
async def set_power(self, id, power):
try:
item = self.find_by_id(id)
2021-11-15 20:18:33 +01:00
await item.instance.set_power(power)
except Exception as e:
logging.error("Failed to set power {} {}".format(id, e))
async def actor_update(self, id, power):
try:
item = self.find_by_id(id)
item.power = round(power)
#await self.push_udpate()
2022-04-23 20:32:32 +02:00
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())
2021-11-06 15:15:11 +01:00
except Exception as e:
2021-11-15 20:18:33 +01:00
logging.error("Failed to update Actor {} {}".format(id, e))
2022-08-31 20:46:27 +02:00
async def timeractor_update(self, id, timer):
try:
item = self.find_by_id(id)
item.timer = round(timer)
#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.push_update("cbpi/actorupdate/{}".format(id), item.to_dict())
except Exception as e:
logging.error("Failed to update Actor {} {}".format(id, e))
2022-08-31 20:46:27 +02:00
async def ws_actor_update(self):
try:
#await self.push_udpate()
self.cbpi.ws.send(dict(topic=self.update_key, data=list(map(lambda x: x.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 Actors {}".format(e))