Sensor

Sensor Controller

class core.controller.sensor_controller.SensorController(cbpi)

Bases: core.controller.crud_controller.CRUDController, core.http_endpoints.http_api.HttpCrudEndpoints

get_value(id)
setup()

This method initializes all actors during startup. It creates actor instances

Returns:
model

alias of core.database.model.SensorModel

CBPiSensor

class core.api.sensor.CBPiSensor(*args, **kwds)

Bases: core.api.extension.CBPiExtension

run(cbpi)
state()

Custom Sensor

__init__.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import asyncio
import logging
import random

from core.api import CBPiActor, Property, action, background_task
from core.api.sensor import CBPiSensor


class CustomSensor(CBPiSensor):

    # Custom Properties which will can be configured by the user

    p1 = Property.Number(label="Test")
    p2 = Property.Text(label="Test")
    interval = Property.Number(label="interval")

    # Internal runtime variable
    value = 0

    @action(key="name", parameters={})
    def myAction(self):
        '''
        Custom Action Exampel
        :return: None
        '''
        pass

    def state(self):
        super().state()

    def stop(self):
        pass

    async def run(self, cbpi):
        self.value = 0
        while True:
            await asyncio.sleep(self.interval)

            self.value = self.value + 1
            cbpi.bus.fire("sensor/%s" % self.id, value=self.value)
            print("SENSOR IS RUNNING", self.value)




def setup(cbpi):

    '''
    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("CustomSensor", CustomSensor)

config.yaml

1
2
name: DummySensor
version: 4