craftbeerpi4-pione/cbpi/controller/sensor_controller.py

28 lines
1,013 B
Python
Raw Permalink 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"
2022-04-23 20:32:32 +02:00
self.sorting = True
2021-01-24 22:14:57 +01:00
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:
2022-04-02 16:27:09 +02:00
logging.error("Failed 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):
if id is None:
return None
2021-02-10 07:38:55 +01:00
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:
2022-04-02 16:27:09 +02:00
logging.error("Failed read sensor value {} {} ".format(id, e))
2021-02-10 07:38:55 +01:00
return None