# -*- coding: utf-8 -*- import asyncio from cbpi.api import parameters, Property, CBPiSensor @parameters([Property.Text(label="Topic", configurable=True)]) class MQTTSensor(CBPiSensor): async def on_message(self, message): try: self.value = float(message) self.log_data(self.value) self.push_update(self.value) except Exception as e: print(e) def __init__(self, cbpi, id, props): super(MQTTSensor, self).__init__(cbpi, id, props) self.mqtt_task = self.cbpi.satellite.subcribe(self.props.Topic, self.on_message) self.value: int = 0 async def run(self): while self.running: await asyncio.sleep(1) def get_state(self): return dict(value=self.value) async def on_stop(self): if self.mqtt_task.done() is False:
import time from voluptuous.schema_builder import message from cbpi.api.dataclasses import NotificationAction, NotificationType from cbpi.api.dataclasses import Kettle, Props from cbpi.api import * import logging from socket import timeout from typing import KeysView from cbpi.api.config import ConfigType from cbpi.api.base import CBPiBase import numpy as np import warnings @parameters([Property.Text(label="Notification",configurable = True, description = "Text for notification"), Property.Select(label="AutoNext",options=["Yes","No"], description="Automatically move to next step (Yes) or pause after Notification (No)")]) class NotificationStep(CBPiStep): async def NextStep(self, **kwargs): await self.next() async def on_timer_done(self,timer): self.summary = self.props.get("Notification","") if self.AutoNext == True: self.cbpi.notify(self.name, self.props.get("Notification",""), NotificationType.INFO) await self.next() else: self.cbpi.notify(self.name, self.props.get("Notification",""), NotificationType.INFO, action=[NotificationAction("Next Step", self.NextStep)]) await self.push_update()
# -*- coding: utf-8 -*- import asyncio from cbpi.api import parameters, Property, CBPiSensor from cbpi.api import * import logging import json @parameters([ Property.Text(label="Topic", configurable=True, description="MQTT Topic"), Property.Text( label="PayloadDictionary", configurable=True, default_value="", description="Where to find msg in payload, leave blank for raw payload" ) ]) class MQTTSensor(CBPiSensor): def __init__(self, cbpi, id, props): super(MQTTSensor, self).__init__(cbpi, id, props) self.Topic = self.props.get("Topic", None) self.payload_text = self.props.get("PayloadDictionary", None) if self.payload_text != None: self.payload_text = self.payload_text.split('.') self.mqtt_task = self.cbpi.satellite.subcribe(self.Topic, self.on_message) self.value: float = 999 async def on_message(self, message): val = json.loads(message)
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): self.power = int(Power) 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)
from cbpi.api import parameters, Property from . import MQTTActor @parameters([ Property.Text(label="Topic", configurable=True, description="MQTT Topic"), Property.Text( label="Payload", configurable=True, description= "Payload that is sent as MQTT message. Available placeholders are {switch_onoff}: [on|off], {switch_10}: [1|0], {power}: [0-100]." ) ]) class GenericMqttActor(MQTTActor): def __init__(self, cbpi, id, props): MQTTActor.__init__(self, cbpi, id, props) self.payload = "" async def on_start(self): await MQTTActor.on_start(self) self.payload = self.props.get( "Payload", "{{\"state\": \"{switch_onoff}\", \"power\": {power}}}") def normalize_power_value(self, power): if power is not None: if power != self.power: power = min(100, power) power = max(0, power) self.power = round(power) async def publish_mqtt_message(self, topic, payload):