mirror of
https://github.com/fmtr/haco.git
synced 2026-10-25 18:40:15 -04:00
Implement Text and Sensor controls
Some checks failed
Version and Deploy / build (push) Has been cancelled
Some checks failed
Version and Deploy / build (push) Has been cancelled
This commit is contained in:
parent
5318014464
commit
911b75a295
3 changed files with 104 additions and 57 deletions
117
haco/sensor.py
117
haco/sensor.py
|
|
@ -1,44 +1,89 @@
|
|||
from haco.data.type_sensor import DeviceClassSensor
|
||||
from haco.data.uom import Uom
|
||||
from dataclasses import dataclass
|
||||
from enum import StrEnum
|
||||
|
||||
import haco.capabilities
|
||||
import haco.topics
|
||||
from haco import schema
|
||||
from haco.capabilities import Capability
|
||||
from haco.control import Control
|
||||
from haco.uom import Uom
|
||||
from haco.utils import ConvertersString
|
||||
|
||||
|
||||
class DeviceClass(StrEnum):
|
||||
"""
|
||||
|
||||
Sourced from: https://www.home-assistant.io/integrations/sensor/#device-class
|
||||
|
||||
"""
|
||||
|
||||
APPARENT_POWER = "apparent_power" # Apparent power in VA.
|
||||
AQI = "aqi" # Air Quality Index (unitless).
|
||||
ATMOSPHERIC_PRESSURE = "atmospheric_pressure" # Atmospheric pressure in cbar, bar, hPa, inHg, kPa, mbar, Pa, or psi.
|
||||
BATTERY = "battery" # Battery percentage in %.
|
||||
CARBON_DIOXIDE = "carbon_dioxide" # CO₂ in ppm.
|
||||
CARBON_MONOXIDE = "carbon_monoxide" # CO in ppm.
|
||||
CURRENT = "current" # Current in A or mA.
|
||||
DATA_RATE = "data_rate" # Data rate in bit/s, kbit/s, Mbit/s, Gbit/s, B/s, kB/s, MB/s, GB/s, KiB/s, MiB/s, or GiB/s.
|
||||
DATA_SIZE = "data_size" # Data size in bit, kbit, Mbit, Gbit, B, kB, MB, GB, TB, PB, EB, ZB, YB, KiB, MiB, GiB, TiB, PiB, EiB, ZiB, or YiB.
|
||||
DATE = "date" # ISO 8601 date string.
|
||||
DISTANCE = "distance" # Distance in km, m, cm, mm, mi, yd, or in.
|
||||
DURATION = "duration" # Duration in d, h, min, or s.
|
||||
ENERGY = "energy" # Energy in Wh, kWh, MWh, MJ, or GJ.
|
||||
ENERGY_STORAGE = "energy_storage" # Stored energy in Wh, kWh, MWh, MJ, or GJ.
|
||||
ENUM = "enum" # Limited set of non-numeric states.
|
||||
FREQUENCY = "frequency" # Frequency in Hz, kHz, MHz, or GHz.
|
||||
GAS = "gas" # Gas volume in m³, ft³, or CCF.
|
||||
HUMIDITY = "humidity" # Humidity in %.
|
||||
ILLUMINANCE = "illuminance" # Light level in lx.
|
||||
IRRADIANCE = "irradiance" # Irradiance in W/m² or BTU/(h⋅ft²).
|
||||
MOISTURE = "moisture" # Water content in %.
|
||||
MONETARY = "monetary" # Monetary value (ISO 4217).
|
||||
NITROGEN_DIOXIDE = "nitrogen_dioxide" # NO₂ in µg/m³.
|
||||
NITROGEN_MONOXIDE = "nitrogen_monoxide" # NO in µg/m³.
|
||||
NITROUS_OXIDE = "nitrous_oxide" # N₂O in µg/m³.
|
||||
OZONE = "ozone" # O₃ in µg/m³.
|
||||
PH = "ph" # pH value.
|
||||
PM1 = "pm1" # Particulate matter <1µm in µg/m³.
|
||||
PM25 = "pm25" # Particulate matter <2.5µm in µg/m³.
|
||||
PM10 = "pm10" # Particulate matter <10µm in µg/m³.
|
||||
POWER_FACTOR = "power_factor" # Power factor (unitless or %).
|
||||
POWER = "power" # Power in W or kW.
|
||||
PRECIPITATION = "precipitation" # Accumulated precipitation.
|
||||
PRECIPITATION_INTENSITY = "precipitation_intensity" # Precipitation rate.
|
||||
PRESSURE = "pressure" # Pressure in Pa, kPa, hPa, bar, cbar, mbar, mmHg, inHg, or psi.
|
||||
REACTIVE_POWER = "reactive_power" # Reactive power in var.
|
||||
SIGNAL_STRENGTH = "signal_strength" # Signal strength in dB or dBm.
|
||||
SOUND_PRESSURE = "sound_pressure" # Sound pressure in dB or dBA.
|
||||
SPEED = "speed" # Speed in ft/s, in/d, in/h, km/h, kn, m/s, mph, or mm/d.
|
||||
SULPHUR_DIOXIDE = "sulphur_dioxide" # SO₂ in µg/m³.
|
||||
TEMPERATURE = "temperature" # Temperature in °C, °F, or K.
|
||||
TIMESTAMP = "timestamp" # Datetime object or ISO 8601 string.
|
||||
VOLATILE_ORGANIC_COMPOUNDS = "volatile_organic_compounds" # VOC in µg/m³.
|
||||
VOLATILE_ORGANIC_COMPOUNDS_PARTS = "volatile_organic_compounds_parts" # VOC in ppm or ppb.
|
||||
VOLTAGE = "voltage" # Voltage in V or mV.
|
||||
VOLUME = "volume" # Volume in L, mL, gal, fl. oz., m³, ft³, or CCF.
|
||||
VOLUME_STORAGE = "volume_storage" # Stored volume.
|
||||
WATER = "water" # Water consumption in L, gal, m³, ft³, or CCF.
|
||||
WEIGHT = "weight" # Mass in kg, g, mg, µg, oz, lb, or st.
|
||||
WIND_SPEED = "wind_speed" # Wind speed in ft/s, km/h, kn, m/s, or mph.
|
||||
|
||||
|
||||
@dataclass(kw_only=True)
|
||||
class Sensor(Control):
|
||||
NAME = 'sensor'
|
||||
DATA = dict(
|
||||
platform='sensor'
|
||||
)
|
||||
converters = ConvertersString
|
||||
|
||||
UOM_TYPE = DeviceClassSensor
|
||||
UOM = Uom
|
||||
unit_of_measurement: Uom | None = None
|
||||
device_class: DeviceClass | None = None
|
||||
|
||||
def command(self, value):
|
||||
raise NotImplementedError()
|
||||
|
||||
def state(self, value):
|
||||
raise NotImplementedError()
|
||||
|
||||
@classmethod
|
||||
def get_schema(cls):
|
||||
return schema.Schema(
|
||||
capabilities=[
|
||||
haco.capabilities.Capability(
|
||||
ha=None,
|
||||
tasmota=schema.Tasmota(announce_data=haco.topics.AnnounceTopic(key='{io_ha}_topic'))
|
||||
)
|
||||
]
|
||||
)
|
||||
|
||||
def __init__(self, name, data_type=None, uom=None, icon=None, uom_type=None):
|
||||
self.data_type = data_type
|
||||
self.device_class = uom_type
|
||||
self.uom = uom
|
||||
|
||||
super().__init__(name, icon=icon)
|
||||
|
||||
@property
|
||||
def control_type(self):
|
||||
return self.data_type or (lambda v: v)
|
||||
|
||||
def get_config_ha_ex(self):
|
||||
data = {
|
||||
'unit_of_measurement': self.uom,
|
||||
'device_class': self.device_class
|
||||
}
|
||||
|
||||
return data
|
||||
def get_capabilities(cls):
|
||||
return [
|
||||
Capability(name=None, command=None)
|
||||
]
|
||||
|
|
|
|||
42
haco/text.py
42
haco/text.py
|
|
@ -1,27 +1,29 @@
|
|||
from dataclasses import dataclass
|
||||
from enum import StrEnum
|
||||
|
||||
from haco.control import Control
|
||||
|
||||
from haco.tools import get_range_pair
|
||||
from haco.utils import ConvertersString
|
||||
|
||||
|
||||
class Mode(StrEnum):
|
||||
TEXT = "text"
|
||||
PASSWORD = "password"
|
||||
|
||||
|
||||
@dataclass(kw_only=True)
|
||||
class Text(Control):
|
||||
NAME = 'text'
|
||||
DATA = dict(
|
||||
platform='text'
|
||||
)
|
||||
converters = ConvertersString
|
||||
|
||||
def __init__(self, name, icon=None, size_range: range = None, pattern: str = None):
|
||||
self.min, self.max = get_range_pair(size_range)
|
||||
self.pattern = pattern
|
||||
pattern: str | None = None
|
||||
min: int | None = None
|
||||
max: int | None = None
|
||||
mode: Mode = Mode.TEXT
|
||||
|
||||
super().__init__(name, icon=icon)
|
||||
def command(self, value):
|
||||
raise NotImplementedError()
|
||||
|
||||
def get_config_ha_ex(self):
|
||||
data = {
|
||||
'min': self.min,
|
||||
'max': self.max,
|
||||
'pattern': self.pattern,
|
||||
'mode': self.__class__.__name__.lower()
|
||||
}
|
||||
|
||||
return data
|
||||
|
||||
|
||||
class Password(Text):
|
||||
...
|
||||
def state(self, value):
|
||||
raise NotImplementedError()
|
||||
|
|
|
|||
|
|
@ -1 +1 @@
|
|||
0.1.2
|
||||
0.1.3
|
||||
Loading…
Add table
Add a link
Reference in a new issue