mirror of
https://github.com/PiBrewing/craftbeerpi4.git
synced 2024-11-10 01:17:42 +01:00
Added MQTT Actor and power setting
This commit is contained in:
parent
5f63d4e5aa
commit
75ab9b96fc
5 changed files with 93 additions and 5 deletions
|
@ -1 +1 @@
|
||||||
__version__ = "4.0.0.47"
|
__version__ = "4.0.0.49"
|
||||||
|
|
|
@ -11,6 +11,8 @@ class ActorController(BasicController):
|
||||||
async def on(self, id, power=None):
|
async def on(self, id, power=None):
|
||||||
try:
|
try:
|
||||||
item = self.find_by_id(id)
|
item = self.find_by_id(id)
|
||||||
|
if item.power:
|
||||||
|
power = item.power
|
||||||
if item.instance.state is False:
|
if item.instance.state is False:
|
||||||
await item.instance.on(power)
|
await item.instance.on(power)
|
||||||
await self.push_udpate()
|
await self.push_udpate()
|
||||||
|
|
|
@ -21,7 +21,8 @@ class SatelliteController:
|
||||||
self.client = None
|
self.client = None
|
||||||
self.topic_filters = [
|
self.topic_filters = [
|
||||||
("cbpi/actor/+/on", self._actor_on),
|
("cbpi/actor/+/on", self._actor_on),
|
||||||
("cbpi/actor/+/off", self._actor_off)
|
("cbpi/actor/+/off", self._actor_off),
|
||||||
|
("cbpi/actor/+/power", self._actor_power),
|
||||||
]
|
]
|
||||||
self.tasks = set()
|
self.tasks = set()
|
||||||
|
|
||||||
|
@ -33,7 +34,7 @@ class SatelliteController:
|
||||||
try:
|
try:
|
||||||
await self.client.publish(topic, message, qos=1, retain=retain)
|
await self.client.publish(topic, message, qos=1, retain=retain)
|
||||||
except:
|
except:
|
||||||
self.logger.warning("Faild to push data via mqtt")
|
self.logger.warning("Failed to push data via mqtt")
|
||||||
|
|
||||||
async def _actor_on(self, messages):
|
async def _actor_on(self, messages):
|
||||||
async for message in messages:
|
async for message in messages:
|
||||||
|
@ -41,7 +42,7 @@ class SatelliteController:
|
||||||
topic_key = message.topic.split("/")
|
topic_key = message.topic.split("/")
|
||||||
await self.cbpi.actor.on(topic_key[2])
|
await self.cbpi.actor.on(topic_key[2])
|
||||||
except:
|
except:
|
||||||
self.logger.warning("Faild to process actor on via mqtt")
|
self.logger.warning("Failed to process actor on via mqtt")
|
||||||
|
|
||||||
async def _actor_off(self, messages):
|
async def _actor_off(self, messages):
|
||||||
async for message in messages:
|
async for message in messages:
|
||||||
|
@ -49,7 +50,24 @@ class SatelliteController:
|
||||||
topic_key = message.topic.split("/")
|
topic_key = message.topic.split("/")
|
||||||
await self.cbpi.actor.off(topic_key[2])
|
await self.cbpi.actor.off(topic_key[2])
|
||||||
except:
|
except:
|
||||||
self.logger.warning("Faild to process actor off via mqtt")
|
self.logger.warning("Failed to process actor off via mqtt")
|
||||||
|
|
||||||
|
async def _actor_power(self, messages):
|
||||||
|
async for message in messages:
|
||||||
|
try:
|
||||||
|
topic_key = message.topic.split("/")
|
||||||
|
try:
|
||||||
|
power=int(message.payload.decode())
|
||||||
|
if power > 100:
|
||||||
|
power = 100
|
||||||
|
if power < 0:
|
||||||
|
power = 0
|
||||||
|
await self.cbpi.actor.set_power(topic_key[2],power)
|
||||||
|
await self.cbpi.actor.actor_update(topic_key[2],power)
|
||||||
|
except:
|
||||||
|
self.logger.warning("Failed to set actor power via mqtt. No valid power in message")
|
||||||
|
except:
|
||||||
|
self.logger.warning("Failed to set actor power via mqtt")
|
||||||
|
|
||||||
def subcribe(self, topic, method):
|
def subcribe(self, topic, method):
|
||||||
task = asyncio.create_task(self._subcribe(topic, method))
|
task = asyncio.create_task(self._subcribe(topic, method))
|
||||||
|
|
65
cbpi/extension/mqtt_actor/__init__.py
Normal file
65
cbpi/extension/mqtt_actor/__init__.py
Normal file
|
@ -0,0 +1,65 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
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)
|
||||||
|
self.power = 100
|
||||||
|
|
||||||
|
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 = int(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"}), 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):
|
||||||
|
await self.on(power)
|
||||||
|
await self.cbpi.actor.actor_update(self.id,power)
|
||||||
|
pass
|
||||||
|
|
||||||
|
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:
|
||||||
|
'''
|
||||||
|
if cbpi.static_config.get("mqtt", False) is True:
|
||||||
|
cbpi.plugin.register("MQTTActor", MQTTActor)
|
3
cbpi/extension/mqtt_actor/config.yaml
Normal file
3
cbpi/extension/mqtt_actor/config.yaml
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
name: DummySensor
|
||||||
|
version: 4
|
||||||
|
active: true
|
Loading…
Reference in a new issue