Added PercentSensor entity type.

This commit is contained in:
Fabio Manganiello 2023-04-17 02:08:15 +02:00
parent e7f64843a5
commit 65481dc6b4
Signed by untrusted user: blacklight
GPG key ID: D90FBA7F76362774
3 changed files with 82 additions and 1 deletions

View file

@ -0,0 +1,52 @@
<template>
<div class="entity sensor-container">
<div class="head">
<div class="col-1 icon">
<EntityIcon
:entity="value"
: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-text="displayValue" />
</div>
</div>
</template>
<script>
import EntityMixin from "./EntityMixin"
import EntityIcon from "./EntityIcon"
export default {
name: 'PercentSensor',
components: {EntityIcon},
mixins: [EntityMixin],
computed: {
displayValue() {
if (this.value.value == null) {
return null
}
let normValue = 100 * this.value.value
return (
normValue.toString() == normValue.toFixed(0)
? normValue.toFixed(0) : normValue.toFixed(1)
) + '%'
},
},
}
</script>
<style lang="scss" scoped>
@import "common";
.entity {
.icon {
margin-right: 1em;
}
}
</style>

View file

@ -295,6 +295,14 @@
} }
}, },
"percent_sensor": {
"name": "Sensor",
"name_plural": "Sensors",
"icon": {
"class": "fas fa-thermometer"
}
},
"enum_sensor": { "enum_sensor": {
"name": "Sensor", "name": "Sensor",
"name_plural": "Sensors", "name_plural": "Sensors",

View file

@ -91,7 +91,7 @@ if 'raw_sensor' not in Base.metadata:
} }
if 'numeric_sensor' not in Base.metadata: if 'numeric_sensor' not in Base.metadata and 'percent_sensor' not in Base.metadata:
class NumericSensor(Sensor): class NumericSensor(Sensor):
""" """
@ -113,6 +113,27 @@ if 'numeric_sensor' not in Base.metadata:
'polymorphic_identity': __tablename__, 'polymorphic_identity': __tablename__,
} }
class PercentSensor(NumericSensor):
"""
A subclass of ``NumericSensor`` that represents a percentage value.
"""
__tablename__ = 'percent_sensor'
id = Column(
Integer, ForeignKey(NumericSensor.id, ondelete='CASCADE'), primary_key=True
)
__mapper_args__ = {
'polymorphic_identity': __tablename__,
}
def __init__(self, *args, **kwargs):
self.min = 0.0
self.max = 1.0
self.unit = '%'
super().__init__(*args, **kwargs)
if 'binary_sensor' not in Base.metadata: if 'binary_sensor' not in Base.metadata: