2021-12-03 00:34:08 +01:00
|
|
|
import asyncio
|
|
|
|
import json
|
|
|
|
from cbpi.api import parameters, Property, CBPiActor
|
|
|
|
from cbpi.api import *
|
|
|
|
|
|
|
|
@parameters([Property.Text(label="Topic", configurable=True, description = "MQTT Topic")])
|
|
|
|
class MQTTActor(CBPiActor):
|
|
|
|
|
|
|
|
# Custom property which can be configured by the user
|
|
|
|
@action("Set Power", parameters=[Property.Number(label="Power", configurable=True, description="Power Setting [0-100]")])
|
|
|
|
async def setpower(self,Power = 100 ,**kwargs):
|
2022-01-05 06:46:35 +01:00
|
|
|
self.power=int(Power)
|
2021-12-03 00:34:08 +01:00
|
|
|
if self.power < 0:
|
|
|
|
self.power = 0
|
|
|
|
if self.power > 100:
|
|
|
|
self.power = 100
|
|
|
|
await self.set_power(self.power)
|
|
|
|
|
|
|
|
def __init__(self, cbpi, id, props):
|
|
|
|
super(MQTTActor, self).__init__(cbpi, id, props)
|
|
|
|
|
|
|
|
async def on_start(self):
|
|
|
|
self.topic = self.props.get("Topic", None)
|
|
|
|
self.power = 100
|
2022-03-09 08:25:17 +01:00
|
|
|
await self.off()
|
|
|
|
self.state = False
|
2021-12-03 00:34:08 +01:00
|
|
|
|
|
|
|
async def on(self, power=None):
|
|
|
|
if power is not None:
|
|
|
|
if power != self.power:
|
|
|
|
power = min(100, power)
|
|
|
|
power = max(0, power)
|
|
|
|
self.power = round(power)
|
|
|
|
await self.cbpi.satellite.publish(self.topic, json.dumps(
|
|
|
|
{"state": "on", "power": self.power}), True)
|
|
|
|
self.state = True
|
|
|
|
pass
|
|
|
|
|
|
|
|
async def off(self):
|
|
|
|
self.state = False
|
|
|
|
await self.cbpi.satellite.publish(self.topic, json.dumps(
|
|
|
|
{"state": "off", "power": self.power}), True)
|
|
|
|
pass
|
|
|
|
|
|
|
|
async def run(self):
|
|
|
|
while self.running:
|
|
|
|
await asyncio.sleep(1)
|
|
|
|
|
|
|
|
def get_state(self):
|
|
|
|
return self.state
|
|
|
|
|
|
|
|
async def set_power(self, power):
|
|
|
|
self.power = round(power)
|
|
|
|
if self.state == True:
|
|
|
|
await self.on(power)
|
|
|
|
else:
|
|
|
|
await self.off()
|
|
|
|
await self.cbpi.actor.actor_update(self.id,power)
|
|
|
|
pass
|