Fix[extension]: mqtt_sensor did not work with Mash_profile

Error NoneType object as no attribute 'get'
This commit is contained in:
Martin 2021-03-18 19:27:03 +01:00
parent 919d6c8e93
commit 6dc1c22e20
2 changed files with 25 additions and 28 deletions

View file

@ -1,10 +1,8 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import asyncio import asyncio
import random import random
import re
import random from cbpi.api import parameters, CBPiSensor
from aiohttp import web
from cbpi.api import *
@parameters([]) @parameters([])
@ -13,20 +11,20 @@ class CustomSensor(CBPiSensor):
def __init__(self, cbpi, id, props): def __init__(self, cbpi, id, props):
super(CustomSensor, self).__init__(cbpi, id, props) super(CustomSensor, self).__init__(cbpi, id, props)
self.value = 0 self.value = 0
async def run(self):
while self.running == True: async def run(self):
while self.running:
self.value = random.randint(10, 100) self.value = random.randint(10, 100)
self.log_data(self.value) self.log_data(self.value)
self.push_update(self.value) self.push_update(self.value)
await asyncio.sleep(1) await asyncio.sleep(1)
def get_state(self): def get_state(self):
return dict(value=self.value) return dict(value=self.value)
def setup(cbpi): def setup(cbpi):
''' '''
This method is called by the server during startup This method is called by the server during startup
Here you need to register your plugins at the server Here you need to register your plugins at the server

View file

@ -1,9 +1,7 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import asyncio import asyncio
import random
import re from cbpi.api import parameters, Property, CBPiSensor
from aiohttp import web
from cbpi.api import *
@parameters([Property.Text(label="Topic", configurable=True)]) @parameters([Property.Text(label="Topic", configurable=True)])
@ -11,18 +9,19 @@ class MQTTSensor(CBPiSensor):
async def on_message(self, message): async def on_message(self, message):
try: try:
self.value = message self.value = float(message)
self.log_data(self.value) self.log_data(self.value)
self.push_update(message) self.push_update(self.value)
except Exception as e: except Exception as e:
print(e) print(e)
def __init__(self, cbpi, id, props): def __init__(self, cbpi, id, props):
super(MQTTSensor, self).__init__(cbpi, id, props) super(MQTTSensor, self).__init__(cbpi, id, props)
self.mqtt_task = self.cbpi.satellite.subcribe(self.props.Topic, self.on_message) self.mqtt_task = self.cbpi.satellite.subcribe(self.props.Topic, self.on_message)
self.value: int = 0
async def run(self): async def run(self):
while self.running == True: while self.running:
await asyncio.sleep(1) await asyncio.sleep(1)
def get_state(self): def get_state(self):
@ -36,8 +35,8 @@ class MQTTSensor(CBPiSensor):
except asyncio.CancelledError: except asyncio.CancelledError:
pass pass
def setup(cbpi):
def setup(cbpi):
''' '''
This method is called by the server during startup This method is called by the server during startup
Here you need to register your plugins at the server Here you need to register your plugins at the server