added power to actor dataclass

This commit is contained in:
avollkopf 2021-11-06 15:15:11 +01:00
parent 3bc70f8c7f
commit 4c543b46f7
4 changed files with 27 additions and 9 deletions

View file

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

View file

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

View file

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

View file

@ -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)
@ -82,8 +83,10 @@ class GPIOPWMActor(CBPiActor):
self.power = 0
if self.power > 100:
self.power = 100
if self.p and self.state == True:
self.p.ChangeDutyCycle(self.power)
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