Added support for binary sensors (in zigbee.mqtt for now)

This commit is contained in:
Fabio Manganiello 2022-11-05 01:47:50 +01:00
parent 6454f9d018
commit 801ed05684
Signed by untrusted user: blacklight
GPG key ID: D90FBA7F76362774
4 changed files with 128 additions and 14 deletions

View file

@ -0,0 +1,50 @@
<template>
<div class="entity sensor-container">
<div class="head">
<div class="col-1 icon">
<EntityIcon
:icon="value.meta?.icon || {}"
:loading="loading"
:error="error" />
</div>
<div class="col-s-8 col-m-9 label">
<div class="name" v-text="value.name" />
</div>
<div class="col-s-3 col-m-2 pull-right" v-if="value.value != null">
<ToggleSwitch :value="value.value" :disabled="true" />
</div>
</div>
</div>
</template>
<script>
import EntityMixin from "./EntityMixin"
import EntityIcon from "./EntityIcon"
import ToggleSwitch from "@/components/elements/ToggleSwitch"
export default {
name: 'BinarySensor',
components: {EntityIcon, ToggleSwitch},
mixins: [EntityMixin],
}
</script>
<style lang="scss" scoped>
@import "common";
.sensor-container {
.head {
.value {
font-size: 1.1em;
font-weight: bold;
opacity: 0.7;
}
.unit {
margin-left: 0.2em;
}
}
}
</style>

View file

@ -71,14 +71,6 @@
}
},
"numeric_sensor": {
"name": "Sensor",
"name_plural": "Sensors",
"icon": {
"class": "fas fa-thermometer"
}
},
"power_sensor": {
"name": "Sensor",
"name_plural": "Sensors",
@ -109,5 +101,29 @@
"icon": {
"class": "fas fa-car-battery"
}
},
"binary_sensor": {
"name": "Sensor",
"name_plural": "Sensors",
"icon": {
"class": "fas fa-thermometer"
}
},
"numeric_sensor": {
"name": "Sensor",
"name_plural": "Sensors",
"icon": {
"class": "fas fa-thermometer"
}
},
"sensor": {
"name": "Sensor",
"name_plural": "Sensors",
"icon": {
"class": "fas fa-thermometer"
}
}
}

View file

@ -1,7 +1,11 @@
from sqlalchemy import Column, Integer, ForeignKey, Numeric, String
import logging
from sqlalchemy import Column, Integer, ForeignKey, Boolean, Numeric, String
from .devices import Device, entity_types_registry
logger = logging.getLogger(__name__)
class Sensor(Device):
__abstract__ = True
@ -46,3 +50,36 @@ if not entity_types_registry.get('NumericSensor'):
entity_types_registry['NumericSensor'] = NumericSensor
else:
NumericSensor = entity_types_registry['NumericSensor']
if not entity_types_registry.get('BinarySensor'):
class BinarySensor(Sensor):
__tablename__ = 'binary_sensor'
def __init__(self, *args, value=None, **kwargs):
if isinstance(value, str):
value = value.lower()
if value in {True, 1, '1', 't', 'true', 'on'}:
value = True
elif value in {False, 0, '0', 'f', 'false', 'off'}:
value = False
elif value is not None:
logger.warning(f'Unsupported value for BinarySensor type: {value}')
value = None
super().__init__(*args, value=value, **kwargs)
id = Column(
Integer, ForeignKey(Device.id, ondelete='CASCADE'), primary_key=True
)
value = Column(Boolean)
__mapper_args__ = {
'polymorphic_identity': __tablename__,
}
entity_types_registry['BinarySensor'] = BinarySensor
else:
BinarySensor = entity_types_registry['BinarySensor']

View file

@ -16,7 +16,7 @@ from platypush.entities.electricity import (
from platypush.entities.humidity import HumiditySensor
from platypush.entities.lights import Light
from platypush.entities.linkquality import LinkQuality
from platypush.entities.sensors import Sensor, NumericSensor
from platypush.entities.sensors import Sensor, BinarySensor, NumericSensor
from platypush.entities.switches import Switch
from platypush.entities.temperature import TemperatureSensor
from platypush.message import Mapping
@ -1495,14 +1495,20 @@ class ZigbeeMqttPlugin(MqttPlugin): # lgtm [py/missing-call-to-init]
),
'value': device_info.get('state', {}).get(exposed['property']),
'description': exposed.get('description'),
'min': exposed.get('value_min'),
'max': exposed.get('value_max'),
'unit': exposed.get('unit'),
'is_read_only': cls._is_read_only(exposed),
'is_write_only': cls._is_read_only(exposed),
'is_write_only': cls._is_write_only(exposed),
'data': device_info,
}
if exposed.get('type') == 'numeric':
sensor_args.update(
{
'min': exposed.get('value_min'),
'max': exposed.get('value_max'),
'unit': exposed.get('unit'),
}
)
if exposed.get('property') == 'battery':
entity_type = Battery
elif exposed.get('property') == 'linkquality':
@ -1519,6 +1525,11 @@ class ZigbeeMqttPlugin(MqttPlugin): # lgtm [py/missing-call-to-init]
entity_type = TemperatureSensor
elif exposed.get('property', '').endswith('humidity'):
entity_type = HumiditySensor
elif exposed.get('type') == 'binary':
entity_type = BinarySensor
sensor_args['value'] = sensor_args['value'] == exposed.get(
'value_on', True
)
elif exposed.get('type') == 'numeric':
entity_type = NumericSensor