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"
|
2021-02-16 20:37:51 +01:00
|
|
|
|
2021-01-22 23:25:20 +01:00
|
|
|
async def on(self, id):
|
2019-01-04 09:29:09 +01:00
|
|
|
try:
|
2021-01-22 23:25:20 +01:00
|
|
|
item = self.find_by_id(id)
|
2021-02-16 20:37:51 +01:00
|
|
|
if item.instance.state is False:
|
|
|
|
await item.instance.on()
|
|
|
|
await self.push_udpate()
|
2021-03-14 11:52:46 +01:00
|
|
|
self.cbpi.push_update("cbpi/actor/"+id, item.to_dict(), True)
|
|
|
|
|
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()
|
2021-03-14 11:52:46 +01:00
|
|
|
self.cbpi.push_update("cbpi/actor/"+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()
|
2021-03-14 11:52:46 +01:00
|
|
|
self.cbpi.push_update("cbpi/actor/update", 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)
|
|
|
|
item.power = power
|
|
|
|
self.cbpi.push_update("cbpi/actor/"+id, item.to_dict())
|
|
|
|
except Exception as e:
|
|
|
|
logging.error("Failed to set power {} {}".format(id, e))
|
|
|
|
|