From 4c543b46f7929ec85e40903577335ef1143b49e5 Mon Sep 17 00:00:00 2001 From: avollkopf <43980694+avollkopf@users.noreply.github.com> Date: Sat, 6 Nov 2021 15:15:11 +0100 Subject: [PATCH] added power to actor dataclass --- cbpi/api/actor.py | 1 + cbpi/api/dataclasses.py | 5 +++-- cbpi/controller/actor_controller.py | 16 ++++++++++++---- cbpi/extension/gpioactor/__init__.py | 14 +++++++++++--- 4 files changed, 27 insertions(+), 9 deletions(-) diff --git a/cbpi/api/actor.py b/cbpi/api/actor.py index 158477a..6a8d206 100644 --- a/cbpi/api/actor.py +++ b/cbpi/api/actor.py @@ -20,6 +20,7 @@ class CBPiActor(metaclass=ABCMeta): self.data_logger = None self.state = False self.running = False + self.power = 100 def init(self): pass diff --git a/cbpi/api/dataclasses.py b/cbpi/api/dataclasses.py index 7b350b4..67edfa5 100644 --- a/cbpi/api/dataclasses.py +++ b/cbpi/api/dataclasses.py @@ -55,13 +55,14 @@ class Actor: name: str = None props: Props = Props() state: bool = False + power: int = 100 type: str = None instance: str = None def __str__(self): - return "name={} props={}, state={}, type={}".format(self.name, self.props, self.state, self.type) + return "name={} props={}, state={}, type={}, power={}".format(self.name, self.props, self.state, self.type, self.power) def to_dict(self): - return dict(id=self.id, name=self.name, type=self.type, props=self.props.to_dict(), state2="HELLO WORLD", state=self.instance.get_state()) + return dict(id=self.id, name=self.name, type=self.type, props=self.props.to_dict(), state2="HELLO WORLD", state=self.instance.get_state(), power=self.power) @dataclass diff --git a/cbpi/controller/actor_controller.py b/cbpi/controller/actor_controller.py index e08b02e..0f6f8a7 100644 --- a/cbpi/controller/actor_controller.py +++ b/cbpi/controller/actor_controller.py @@ -17,7 +17,7 @@ class ActorController(BasicController): self.cbpi.push_update("cbpi/actor/"+id, item.to_dict(), True) except Exception as e: - logging.error("Faild to switch on Actor {} {}".format(id, e)) + logging.error("Failed to switch on Actor {} {}".format(id, e)) async def off(self, id): try: @@ -27,7 +27,7 @@ class ActorController(BasicController): await self.push_udpate() self.cbpi.push_update("cbpi/actor/"+id, item.to_dict()) except Exception as e: - logging.error("Faild to switch on Actor {} {}".format(id, e), True) + logging.error("Failed to switch on Actor {} {}".format(id, e), True) async def toogle(self, id): try: @@ -36,5 +36,13 @@ class ActorController(BasicController): await instance.toggle() self.cbpi.push_update("cbpi/actor/update", item.to_dict()) except Exception as e: - logging.error("Faild to switch on Actor {} {}".format(id, e)) - \ No newline at end of file + 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)) + diff --git a/cbpi/extension/gpioactor/__init__.py b/cbpi/extension/gpioactor/__init__.py index abb407b..259a7ce 100644 --- a/cbpi/extension/gpioactor/__init__.py +++ b/cbpi/extension/gpioactor/__init__.py @@ -46,6 +46,7 @@ class GPIOActor(CBPiActor): return 0 if self.inverted == False else 1 async def on_start(self): + self.power = 100 self.gpio = self.props.GPIO self.inverted = True if self.props.get("Inverted", "No") == "Yes" else False GPIO.setup(self.gpio, GPIO.OUT) @@ -81,9 +82,11 @@ class GPIOPWMActor(CBPiActor): if self.power < 0: self.power = 0 if self.power > 100: - self.power = 100 - if self.p and self.state == True: - self.p.ChangeDutyCycle(self.power) + self.power = 100 + item = self.cbpi.actor.find_by_id(self.id) + item.power = self.power + self.cbpi.push_update("cbpi/actor/"+self.id, item.to_dict()) + await self.set_power(self.power) async def on_start(self): self.gpio = self.props.get("GPIO", None) @@ -112,6 +115,11 @@ class GPIOPWMActor(CBPiActor): self.p.ChangeDutyCycle(0) self.state = False + async def set_power(self, power): + if self.p and self.state == True: + self.p.ChangeDutyCycle(power) + pass + def get_state(self): return self.state