craftbeerpi4-pione/cbpi/controller/sensor_controller.py

25 lines
936 B
Python
Raw Normal View History

2021-02-16 20:37:51 +01:00
from cbpi.api.dataclasses import Sensor
from cbpi.controller.basic_controller2 import BasicController
2021-01-24 22:14:57 +01:00
import logging
2019-01-01 15:35:35 +01:00
2021-01-22 23:25:20 +01:00
class SensorController(BasicController):
2018-11-01 21:25:42 +01:00
def __init__(self, cbpi):
2021-02-16 20:37:51 +01:00
super(SensorController, self).__init__(cbpi, Sensor, "sensor.json")
2021-01-24 22:14:57 +01:00
self.update_key = "sensorupdate"
def create_dict(self, data):
try:
instance = data.get("instance")
2021-02-10 07:38:55 +01:00
state =instance.get_state()
2021-01-24 22:14:57 +01:00
except Exception as e:
2021-01-30 22:29:33 +01:00
logging.error("Faild to create sensor dict {} ".format(e))
2021-01-24 22:14:57 +01:00
state = dict()
return dict(name=data.get("name"), id=data.get("id"), type=data.get("type"), state=state,props=data.get("props", []))
2021-02-10 07:38:55 +01:00
def get_sensor_value(self, id):
try:
2021-02-16 20:37:51 +01:00
return self.find_by_id(id).instance.get_state()
2021-02-10 07:38:55 +01:00
except Exception as e:
logging.error("Faild read sensor value {} {} ".format(id, e))
return None