2018-11-04 00:47:26 +01:00
|
|
|
import logging
|
2019-01-04 09:29:09 +01:00
|
|
|
from unittest.mock import MagicMock, patch
|
2018-11-01 19:50:04 +01:00
|
|
|
|
2019-01-05 20:43:48 +01:00
|
|
|
from cbpi.api import *
|
|
|
|
|
2019-01-04 09:29:09 +01:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
try:
|
|
|
|
import RPi.GPIO as GPIO
|
|
|
|
except Exception:
|
|
|
|
logger.error("Failed to load RPi.GPIO. Using Mock")
|
|
|
|
MockRPi = MagicMock()
|
|
|
|
modules = {
|
|
|
|
"RPi": MockRPi,
|
|
|
|
"RPi.GPIO": MockRPi.GPIO
|
|
|
|
}
|
|
|
|
patcher = patch.dict("sys.modules", modules)
|
|
|
|
patcher.start()
|
|
|
|
import RPi.GPIO as GPIO
|
2018-11-01 19:50:04 +01:00
|
|
|
|
2021-01-22 23:25:20 +01:00
|
|
|
|
2021-01-23 14:41:26 +01:00
|
|
|
@parameters([Property.Number(label="Param1", configurable=True),
|
|
|
|
Property.Text(label="Param2", configurable=True, default_value="HALLO"),
|
|
|
|
Property.Select(label="Param3", options=[1,2,4]),
|
|
|
|
Property.Sensor(label="Param4"),
|
|
|
|
Property.Actor(label="Param5")])
|
|
|
|
class CustomActor(CBPiActor):
|
2021-01-22 23:25:20 +01:00
|
|
|
my_name = ""
|
2018-11-01 19:50:04 +01:00
|
|
|
|
2018-12-08 14:21:00 +01:00
|
|
|
# Custom property which can be configured by the user
|
2019-01-24 21:27:55 +01:00
|
|
|
@action("test", parameters={})
|
2021-01-22 23:25:20 +01:00
|
|
|
async def action1(self, **kwargs):
|
|
|
|
print("ACTION !", kwargs)
|
|
|
|
self.my_name = kwargs.get("name")
|
2019-01-24 21:27:55 +01:00
|
|
|
pass
|
2021-01-22 23:25:20 +01:00
|
|
|
|
2018-12-13 21:45:33 +01:00
|
|
|
def init(self):
|
2021-01-22 23:25:20 +01:00
|
|
|
print("INIT")
|
|
|
|
|
2019-01-08 23:31:39 +01:00
|
|
|
self.state = False
|
2018-12-29 00:27:19 +01:00
|
|
|
pass
|
2018-12-13 21:45:33 +01:00
|
|
|
|
2021-01-22 23:25:20 +01:00
|
|
|
async def on(self, power=0):
|
|
|
|
logger.info("ACTOR 1111 %s ON" % self.id)
|
2019-01-04 09:29:09 +01:00
|
|
|
self.state = True
|
2018-12-13 21:45:33 +01:00
|
|
|
|
2021-01-22 23:25:20 +01:00
|
|
|
async def off(self):
|
2019-01-04 09:29:09 +01:00
|
|
|
logger.info("ACTOR %s OFF " % self.id)
|
|
|
|
self.state = False
|
2018-11-01 19:50:04 +01:00
|
|
|
|
2019-01-08 23:31:39 +01:00
|
|
|
def get_state(self):
|
2021-01-22 23:25:20 +01:00
|
|
|
|
2019-01-08 23:31:39 +01:00
|
|
|
return self.state
|
2021-01-22 23:25:20 +01:00
|
|
|
|
|
|
|
async def run(self):
|
|
|
|
pass
|
2018-11-01 19:50:04 +01:00
|
|
|
|
|
|
|
def setup(cbpi):
|
|
|
|
|
2018-11-16 21:42:59 +01:00
|
|
|
'''
|
|
|
|
This method is called by the server during startup
|
|
|
|
Here you need to register your plugins at the server
|
|
|
|
|
|
|
|
:param cbpi: the cbpi core
|
|
|
|
:return:
|
|
|
|
'''
|
|
|
|
|
|
|
|
cbpi.plugin.register("CustomActor", CustomActor)
|