diff options
| author | diogo464 <[email protected]> | 2025-12-05 12:17:01 +0000 |
|---|---|---|
| committer | diogo464 <[email protected]> | 2025-12-05 12:17:01 +0000 |
| commit | 0c86da392af50c7588b087c3f72602e8368af65e (patch) | |
| tree | 894cd2f353298b83a56cde06eafd7b1e366aa6b3 /src | |
| parent | 1d2ee64d0ec917a2c2b66f8d58e1f37dd174a89d (diff) | |
reworked entity creation
Diffstat (limited to 'src')
| -rw-r--r-- | src/binary_state.rs | 56 | ||||
| -rw-r--r-- | src/constants.rs | 147 | ||||
| -rw-r--r-- | src/entity.rs | 36 | ||||
| -rw-r--r-- | src/entity_binary_sensor.rs | 83 | ||||
| -rw-r--r-- | src/entity_button.rs | 41 | ||||
| -rw-r--r-- | src/entity_category.rs | 17 | ||||
| -rw-r--r-- | src/entity_number.rs | 193 | ||||
| -rw-r--r-- | src/entity_sensor.rs | 30 | ||||
| -rw-r--r-- | src/entity_switch.rs | 59 | ||||
| -rw-r--r-- | src/lib.rs | 344 | ||||
| -rw-r--r-- | src/unit.rs | 1114 |
11 files changed, 1846 insertions, 274 deletions
diff --git a/src/binary_state.rs b/src/binary_state.rs new file mode 100644 index 0000000..d512856 --- /dev/null +++ b/src/binary_state.rs | |||
| @@ -0,0 +1,56 @@ | |||
| 1 | use core::str::FromStr; | ||
| 2 | |||
| 3 | use crate::constants; | ||
| 4 | |||
| 5 | pub struct InvalidBinaryState; | ||
| 6 | |||
| 7 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
| 8 | pub enum BinaryState { | ||
| 9 | On, | ||
| 10 | Off, | ||
| 11 | } | ||
| 12 | |||
| 13 | impl BinaryState { | ||
| 14 | pub fn as_str(&self) -> &'static str { | ||
| 15 | match self { | ||
| 16 | Self::On => constants::HA_SWITCH_STATE_ON, | ||
| 17 | Self::Off => constants::HA_SWITCH_STATE_OFF, | ||
| 18 | } | ||
| 19 | } | ||
| 20 | |||
| 21 | pub fn flip(self) -> Self { | ||
| 22 | match self { | ||
| 23 | Self::On => Self::Off, | ||
| 24 | Self::Off => Self::On, | ||
| 25 | } | ||
| 26 | } | ||
| 27 | } | ||
| 28 | |||
| 29 | impl core::fmt::Display for BinaryState { | ||
| 30 | fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { | ||
| 31 | f.write_str(self.as_str()) | ||
| 32 | } | ||
| 33 | } | ||
| 34 | |||
| 35 | impl FromStr for BinaryState { | ||
| 36 | type Err = InvalidBinaryState; | ||
| 37 | |||
| 38 | fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
| 39 | if s.eq_ignore_ascii_case(constants::HA_SWITCH_STATE_ON) { | ||
| 40 | return Ok(Self::On); | ||
| 41 | } | ||
| 42 | if s.eq_ignore_ascii_case(constants::HA_SWITCH_STATE_OFF) { | ||
| 43 | return Ok(Self::Off); | ||
| 44 | } | ||
| 45 | Err(InvalidBinaryState) | ||
| 46 | } | ||
| 47 | } | ||
| 48 | |||
| 49 | impl TryFrom<&[u8]> for BinaryState { | ||
| 50 | type Error = InvalidBinaryState; | ||
| 51 | |||
| 52 | fn try_from(value: &[u8]) -> Result<Self, Self::Error> { | ||
| 53 | let string = str::from_utf8(value).map_err(|_| InvalidBinaryState)?; | ||
| 54 | string.parse() | ||
| 55 | } | ||
| 56 | } | ||
diff --git a/src/constants.rs b/src/constants.rs index 0803bdb..8c48bed 100644 --- a/src/constants.rs +++ b/src/constants.rs | |||
| @@ -101,6 +101,53 @@ pub const HA_DEVICE_CLASS_BUTTON_UPDATE: &str = "update"; | |||
| 101 | pub const HA_DEVICE_CLASS_SWITCH_OUTLET: &str = "outlet"; | 101 | pub const HA_DEVICE_CLASS_SWITCH_OUTLET: &str = "outlet"; |
| 102 | pub const HA_DEVICE_CLASS_SWITCH_SWITCH: &str = "switch"; | 102 | pub const HA_DEVICE_CLASS_SWITCH_SWITCH: &str = "switch"; |
| 103 | 103 | ||
| 104 | pub const HA_DEVICE_CLASS_NUMBER_APPARENT_POWER: &str = "apparent_power"; | ||
| 105 | pub const HA_DEVICE_CLASS_NUMBER_AQI: &str = "aqi"; | ||
| 106 | pub const HA_DEVICE_CLASS_NUMBER_ATMOSPHERIC_PRESSURE: &str = "atmospheric_pressure"; | ||
| 107 | pub const HA_DEVICE_CLASS_NUMBER_BATTERY: &str = "battery"; | ||
| 108 | pub const HA_DEVICE_CLASS_NUMBER_CARBON_DIOXIDE: &str = "carbon_dioxide"; | ||
| 109 | pub const HA_DEVICE_CLASS_NUMBER_CARBON_MONOXIDE: &str = "carbon_monoxide"; | ||
| 110 | pub const HA_DEVICE_CLASS_NUMBER_CURRENT: &str = "current"; | ||
| 111 | pub const HA_DEVICE_CLASS_NUMBER_DATA_RATE: &str = "data_rate"; | ||
| 112 | pub const HA_DEVICE_CLASS_NUMBER_DATA_SIZE: &str = "data_size"; | ||
| 113 | pub const HA_DEVICE_CLASS_NUMBER_DISTANCE: &str = "distance"; | ||
| 114 | pub const HA_DEVICE_CLASS_NUMBER_DURATION: &str = "duration"; | ||
| 115 | pub const HA_DEVICE_CLASS_NUMBER_ENERGY: &str = "energy"; | ||
| 116 | pub const HA_DEVICE_CLASS_NUMBER_FREQUENCY: &str = "frequency"; | ||
| 117 | pub const HA_DEVICE_CLASS_NUMBER_GAS: &str = "gas"; | ||
| 118 | pub const HA_DEVICE_CLASS_NUMBER_HUMIDITY: &str = "humidity"; | ||
| 119 | pub const HA_DEVICE_CLASS_NUMBER_ILLUMINANCE: &str = "illuminance"; | ||
| 120 | pub const HA_DEVICE_CLASS_NUMBER_IRRADIANCE: &str = "irradiance"; | ||
| 121 | pub const HA_DEVICE_CLASS_NUMBER_MOISTURE: &str = "moisture"; | ||
| 122 | pub const HA_DEVICE_CLASS_NUMBER_MONETARY: &str = "monetary"; | ||
| 123 | pub const HA_DEVICE_CLASS_NUMBER_NITROGEN_DIOXIDE: &str = "nitrogen_dioxide"; | ||
| 124 | pub const HA_DEVICE_CLASS_NUMBER_NITROGEN_MONOXIDE: &str = "nitrogen_monoxide"; | ||
| 125 | pub const HA_DEVICE_CLASS_NUMBER_NITROUS_OXIDE: &str = "nitrous_oxide"; | ||
| 126 | pub const HA_DEVICE_CLASS_NUMBER_OZONE: &str = "ozone"; | ||
| 127 | pub const HA_DEVICE_CLASS_NUMBER_PH: &str = "ph"; | ||
| 128 | pub const HA_DEVICE_CLASS_NUMBER_PM1: &str = "pm1"; | ||
| 129 | pub const HA_DEVICE_CLASS_NUMBER_PM25: &str = "pm25"; | ||
| 130 | pub const HA_DEVICE_CLASS_NUMBER_PM10: &str = "pm10"; | ||
| 131 | pub const HA_DEVICE_CLASS_NUMBER_POWER_FACTOR: &str = "power_factor"; | ||
| 132 | pub const HA_DEVICE_CLASS_NUMBER_POWER: &str = "power"; | ||
| 133 | pub const HA_DEVICE_CLASS_NUMBER_PRECIPITATION: &str = "precipitation"; | ||
| 134 | pub const HA_DEVICE_CLASS_NUMBER_PRECIPITATION_INTENSITY: &str = "precipitation_intensity"; | ||
| 135 | pub const HA_DEVICE_CLASS_NUMBER_PRESSURE: &str = "pressure"; | ||
| 136 | pub const HA_DEVICE_CLASS_NUMBER_REACTIVE_POWER: &str = "reactive_power"; | ||
| 137 | pub const HA_DEVICE_CLASS_NUMBER_SIGNAL_STRENGTH: &str = "signal_strength"; | ||
| 138 | pub const HA_DEVICE_CLASS_NUMBER_SOUND_PRESSURE: &str = "sound_pressure"; | ||
| 139 | pub const HA_DEVICE_CLASS_NUMBER_SPEED: &str = "speed"; | ||
| 140 | pub const HA_DEVICE_CLASS_NUMBER_SULPHUR_DIOXIDE: &str = "sulphur_dioxide"; | ||
| 141 | pub const HA_DEVICE_CLASS_NUMBER_TEMPERATURE: &str = "temperature"; | ||
| 142 | pub const HA_DEVICE_CLASS_NUMBER_VOLATILE_ORGANIC_COMPOUNDS: &str = "volatile_organic_compounds"; | ||
| 143 | pub const HA_DEVICE_CLASS_NUMBER_VOLATILE_ORGANIC_COMPOUNDS_PARTS: &str = | ||
| 144 | "volatile_organic_compounds_parts"; | ||
| 145 | pub const HA_DEVICE_CLASS_NUMBER_VOLTAGE: &str = "voltage"; | ||
| 146 | pub const HA_DEVICE_CLASS_NUMBER_VOLUME: &str = "volume"; | ||
| 147 | pub const HA_DEVICE_CLASS_NUMBER_WATER: &str = "water"; | ||
| 148 | pub const HA_DEVICE_CLASS_NUMBER_WEIGHT: &str = "weight"; | ||
| 149 | pub const HA_DEVICE_CLASS_NUMBER_WIND_SPEED: &str = "wind_speed"; | ||
| 150 | |||
| 104 | pub const HA_UNIT_TEMPERATURE_CELSIUS: &str = "°C"; | 151 | pub const HA_UNIT_TEMPERATURE_CELSIUS: &str = "°C"; |
| 105 | pub const HA_UNIT_TEMPERATURE_KELVIN: &str = "K"; | 152 | pub const HA_UNIT_TEMPERATURE_KELVIN: &str = "K"; |
| 106 | pub const HA_UNIT_TEMPERATURE_FAHRENHEIT: &str = "°F"; | 153 | pub const HA_UNIT_TEMPERATURE_FAHRENHEIT: &str = "°F"; |
| @@ -144,8 +191,108 @@ pub const HA_UNIT_CURRENCY_CENT: &str = "¢"; | |||
| 144 | pub const HA_ENTITY_CATEGORY_CONFIG: &str = "config"; | 191 | pub const HA_ENTITY_CATEGORY_CONFIG: &str = "config"; |
| 145 | pub const HA_ENTITY_CATEGORY_DIAGNOSTIC: &str = "diagnostic"; | 192 | pub const HA_ENTITY_CATEGORY_DIAGNOSTIC: &str = "diagnostic"; |
| 146 | 193 | ||
| 194 | pub const HA_BINARY_STATE_ON: &str = "ON"; | ||
| 195 | pub const HA_BINARY_STATE_OFF: &str = "OFF"; | ||
| 196 | |||
| 147 | pub const HA_SWITCH_STATE_ON: &str = "ON"; | 197 | pub const HA_SWITCH_STATE_ON: &str = "ON"; |
| 148 | pub const HA_SWITCH_STATE_OFF: &str = "OFF"; | 198 | pub const HA_SWITCH_STATE_OFF: &str = "OFF"; |
| 149 | 199 | ||
| 150 | pub const HA_BINARY_SENSOR_STATE_ON: &str = "ON"; | 200 | pub const HA_BINARY_SENSOR_STATE_ON: &str = "ON"; |
| 151 | pub const HA_BINARY_SENSOR_STATE_OFF: &str = "OFF"; | 201 | pub const HA_BINARY_SENSOR_STATE_OFF: &str = "OFF"; |
| 202 | |||
| 203 | // Number units - Energy | ||
| 204 | pub const HA_UNIT_ENERGY_JOULE: &str = "J"; | ||
| 205 | pub const HA_UNIT_ENERGY_KILOJOULE: &str = "kJ"; | ||
| 206 | pub const HA_UNIT_ENERGY_MEGAJOULE: &str = "MJ"; | ||
| 207 | pub const HA_UNIT_ENERGY_GIGAJOULE: &str = "GJ"; | ||
| 208 | pub const HA_UNIT_ENERGY_MILLIWATTHOUR: &str = "mWh"; | ||
| 209 | pub const HA_UNIT_ENERGY_WATTHOUR: &str = "Wh"; | ||
| 210 | pub const HA_UNIT_ENERGY_KWH: &str = "kWh"; | ||
| 211 | pub const HA_UNIT_ENERGY_MEGAWATTHOUR: &str = "MWh"; | ||
| 212 | pub const HA_UNIT_ENERGY_GIGAWATTHOUR: &str = "GWh"; | ||
| 213 | pub const HA_UNIT_ENERGY_TERAWATTHOUR: &str = "TWh"; | ||
| 214 | pub const HA_UNIT_ENERGY_CALORIE: &str = "cal"; | ||
| 215 | pub const HA_UNIT_ENERGY_KILOCALORIE: &str = "kcal"; | ||
| 216 | pub const HA_UNIT_ENERGY_MEGACALORIE: &str = "Mcal"; | ||
| 217 | pub const HA_UNIT_ENERGY_GIGACALORIE: &str = "Gcal"; | ||
| 218 | |||
| 219 | // Number units - Pressure | ||
| 220 | pub const HA_UNIT_PRESSURE_MILLIPASCAL: &str = "mPa"; | ||
| 221 | pub const HA_UNIT_PRESSURE_PASCAL: &str = "Pa"; | ||
| 222 | pub const HA_UNIT_PRESSURE_HPA: &str = "hPa"; | ||
| 223 | pub const HA_UNIT_PRESSURE_KILOPASCAL: &str = "kPa"; | ||
| 224 | pub const HA_UNIT_PRESSURE_BAR: &str = "bar"; | ||
| 225 | pub const HA_UNIT_PRESSURE_CENTIBAR: &str = "cbar"; | ||
| 226 | pub const HA_UNIT_PRESSURE_MILLIBAR: &str = "mbar"; | ||
| 227 | pub const HA_UNIT_PRESSURE_MILLIMETER_MERCURY: &str = "mmHg"; | ||
| 228 | pub const HA_UNIT_PRESSURE_INCH_MERCURY: &str = "inHg"; | ||
| 229 | pub const HA_UNIT_PRESSURE_INCH_WATER: &str = "inH₂O"; | ||
| 230 | pub const HA_UNIT_PRESSURE_PSI: &str = "psi"; | ||
| 231 | |||
| 232 | // Number units - Volume | ||
| 233 | pub const HA_UNIT_VOLUME_LITER: &str = "L"; | ||
| 234 | pub const HA_UNIT_VOLUME_MILLILITER: &str = "mL"; | ||
| 235 | pub const HA_UNIT_VOLUME_GALLON: &str = "gal"; | ||
| 236 | pub const HA_UNIT_VOLUME_FLUID_OUNCE: &str = "fl. oz."; | ||
| 237 | pub const HA_UNIT_VOLUME_CUBIC_METER: &str = "m³"; | ||
| 238 | pub const HA_UNIT_VOLUME_CUBIC_FOOT: &str = "ft³"; | ||
| 239 | pub const HA_UNIT_VOLUME_CCF: &str = "CCF"; | ||
| 240 | pub const HA_UNIT_VOLUME_MCF: &str = "MCF"; | ||
| 241 | |||
| 242 | // Number units - Speed | ||
| 243 | pub const HA_UNIT_SPEED_FEET_PER_SECOND: &str = "ft/s"; | ||
| 244 | pub const HA_UNIT_SPEED_INCH_PER_DAY: &str = "in/d"; | ||
| 245 | pub const HA_UNIT_SPEED_INCH_PER_HOUR: &str = "in/h"; | ||
| 246 | pub const HA_UNIT_SPEED_INCH_PER_SECOND: &str = "in/s"; | ||
| 247 | pub const HA_UNIT_SPEED_KILOMETER_PER_HOUR: &str = "km/h"; | ||
| 248 | pub const HA_UNIT_SPEED_KNOT: &str = "kn"; | ||
| 249 | pub const HA_UNIT_SPEED_METER_PER_SECOND: &str = "m/s"; | ||
| 250 | pub const HA_UNIT_SPEED_MILE_PER_HOUR: &str = "mph"; | ||
| 251 | pub const HA_UNIT_SPEED_MILLIMETER_PER_DAY: &str = "mm/d"; | ||
| 252 | pub const HA_UNIT_SPEED_MILLIMETER_PER_SECOND: &str = "mm/s"; | ||
| 253 | |||
| 254 | // Number units - Distance (additional to existing) | ||
| 255 | pub const HA_UNIT_DISTANCE_MILE: &str = "mi"; | ||
| 256 | pub const HA_UNIT_DISTANCE_NAUTICAL_MILE: &str = "nmi"; | ||
| 257 | pub const HA_UNIT_DISTANCE_YARD: &str = "yd"; | ||
| 258 | pub const HA_UNIT_DISTANCE_INCH: &str = "in"; | ||
| 259 | |||
| 260 | // Number units - Power (additional) | ||
| 261 | pub const HA_UNIT_POWER_MILLIWATT: &str = "mW"; | ||
| 262 | pub const HA_UNIT_POWER_MEGAWATT: &str = "MW"; | ||
| 263 | pub const HA_UNIT_POWER_GIGAWATT: &str = "GW"; | ||
| 264 | pub const HA_UNIT_POWER_TERAWATT: &str = "TW"; | ||
| 265 | |||
| 266 | // Number units - Current (additional) | ||
| 267 | pub const HA_UNIT_CURRENT_MILLIAMPERE: &str = "mA"; | ||
| 268 | |||
| 269 | // Number units - Voltage (additional) | ||
| 270 | pub const HA_UNIT_VOLTAGE_MILLIVOLT: &str = "mV"; | ||
| 271 | pub const HA_UNIT_VOLTAGE_MICROVOLT: &str = "µV"; | ||
| 272 | pub const HA_UNIT_VOLTAGE_KILOVOLT: &str = "kV"; | ||
| 273 | pub const HA_UNIT_VOLTAGE_MEGAVOLT: &str = "MV"; | ||
| 274 | |||
| 275 | // Number units - Data Rate | ||
| 276 | pub const HA_UNIT_DATA_RATE_BIT_PER_SECOND: &str = "bit/s"; | ||
| 277 | pub const HA_UNIT_DATA_RATE_KILOBIT_PER_SECOND: &str = "kbit/s"; | ||
| 278 | pub const HA_UNIT_DATA_RATE_MEGABIT_PER_SECOND: &str = "Mbit/s"; | ||
| 279 | pub const HA_UNIT_DATA_RATE_GIGABIT_PER_SECOND: &str = "Gbit/s"; | ||
| 280 | pub const HA_UNIT_DATA_RATE_BYTE_PER_SECOND: &str = "B/s"; | ||
| 281 | pub const HA_UNIT_DATA_RATE_KILOBYTE_PER_SECOND: &str = "kB/s"; | ||
| 282 | pub const HA_UNIT_DATA_RATE_MEGABYTE_PER_SECOND: &str = "MB/s"; | ||
| 283 | pub const HA_UNIT_DATA_RATE_GIGABYTE_PER_SECOND: &str = "GB/s"; | ||
| 284 | pub const HA_UNIT_DATA_RATE_KIBIBYTE_PER_SECOND: &str = "KiB/s"; | ||
| 285 | pub const HA_UNIT_DATA_RATE_MEBIBYTE_PER_SECOND: &str = "MiB/s"; | ||
| 286 | pub const HA_UNIT_DATA_RATE_GIBIBYTE_PER_SECOND: &str = "GiB/s"; | ||
| 287 | |||
| 288 | // Number units - Weight | ||
| 289 | pub const HA_UNIT_WEIGHT_KILOGRAM: &str = "kg"; | ||
| 290 | pub const HA_UNIT_WEIGHT_GRAM: &str = "g"; | ||
| 291 | pub const HA_UNIT_WEIGHT_MILLIGRAM: &str = "mg"; | ||
| 292 | pub const HA_UNIT_WEIGHT_MICROGRAM: &str = "µg"; | ||
| 293 | pub const HA_UNIT_WEIGHT_OUNCE: &str = "oz"; | ||
| 294 | pub const HA_UNIT_WEIGHT_POUND: &str = "lb"; | ||
| 295 | pub const HA_UNIT_WEIGHT_STONE: &str = "st"; | ||
| 296 | |||
| 297 | // Light | ||
| 298 | pub const HA_UNIT_LIGHT_LUX: &str = "lx"; | ||
diff --git a/src/entity.rs b/src/entity.rs new file mode 100644 index 0000000..ac15921 --- /dev/null +++ b/src/entity.rs | |||
| @@ -0,0 +1,36 @@ | |||
| 1 | use crate::EntityCategory; | ||
| 2 | |||
| 3 | #[derive(Debug, Default)] | ||
| 4 | pub struct EntityCommonConfig { | ||
| 5 | pub name: Option<&'static str>, | ||
| 6 | pub icon: Option<&'static str>, | ||
| 7 | pub category: Option<EntityCategory>, | ||
| 8 | pub picture: Option<&'static str>, | ||
| 9 | } | ||
| 10 | |||
| 11 | impl EntityCommonConfig { | ||
| 12 | pub(crate) fn populate(&self, config: &mut EntityConfig) { | ||
| 13 | config.name = self.name; | ||
| 14 | config.icon = self.icon; | ||
| 15 | config.category = self.category.map(|c| c.as_str()); | ||
| 16 | config.picture = self.picture; | ||
| 17 | } | ||
| 18 | } | ||
| 19 | |||
| 20 | #[derive(Default)] | ||
| 21 | pub struct EntityConfig { | ||
| 22 | pub id: &'static str, | ||
| 23 | pub name: Option<&'static str>, | ||
| 24 | pub domain: &'static str, | ||
| 25 | pub device_class: Option<&'static str>, | ||
| 26 | pub measurement_unit: Option<&'static str>, | ||
| 27 | pub icon: Option<&'static str>, | ||
| 28 | pub picture: Option<&'static str>, | ||
| 29 | pub category: Option<&'static str>, | ||
| 30 | pub state_class: Option<&'static str>, | ||
| 31 | pub schema: Option<&'static str>, | ||
| 32 | pub min: Option<f32>, | ||
| 33 | pub max: Option<f32>, | ||
| 34 | pub step: Option<f32>, | ||
| 35 | pub mode: Option<&'static str>, | ||
| 36 | } | ||
diff --git a/src/entity_binary_sensor.rs b/src/entity_binary_sensor.rs new file mode 100644 index 0000000..b80f718 --- /dev/null +++ b/src/entity_binary_sensor.rs | |||
| @@ -0,0 +1,83 @@ | |||
| 1 | use crate::{BinaryState, Entity, EntityCommonConfig, EntityConfig, constants}; | ||
| 2 | |||
| 3 | #[derive(Debug, Default, Clone, Copy, PartialEq, Eq)] | ||
| 4 | pub enum BinarySensorClass { | ||
| 5 | #[default] | ||
| 6 | Generic, | ||
| 7 | Battery, | ||
| 8 | BatteryCharging, | ||
| 9 | Connectivity, | ||
| 10 | Door, | ||
| 11 | GarageDoor, | ||
| 12 | Motion, | ||
| 13 | Occupancy, | ||
| 14 | Opening, | ||
| 15 | Plug, | ||
| 16 | Power, | ||
| 17 | Presence, | ||
| 18 | Problem, | ||
| 19 | Smoke, | ||
| 20 | Window, | ||
| 21 | } | ||
| 22 | |||
| 23 | #[derive(Debug, Default)] | ||
| 24 | pub struct BinarySensorConfig { | ||
| 25 | pub common: EntityCommonConfig, | ||
| 26 | pub class: BinarySensorClass, | ||
| 27 | } | ||
| 28 | |||
| 29 | impl BinarySensorConfig { | ||
| 30 | pub(crate) fn populate(&self, config: &mut EntityConfig) { | ||
| 31 | self.common.populate(config); | ||
| 32 | config.domain = constants::HA_DOMAIN_BINARY_SENSOR; | ||
| 33 | config.device_class = match self.class { | ||
| 34 | BinarySensorClass::Generic => None, | ||
| 35 | BinarySensorClass::Battery => Some(constants::HA_DEVICE_CLASS_BINARY_SENSOR_BATTERY), | ||
| 36 | BinarySensorClass::BatteryCharging => { | ||
| 37 | Some(constants::HA_DEVICE_CLASS_BINARY_SENSOR_BATTERY_CHARGING) | ||
| 38 | } | ||
| 39 | BinarySensorClass::Connectivity => { | ||
| 40 | Some(constants::HA_DEVICE_CLASS_BINARY_SENSOR_CONNECTIVITY) | ||
| 41 | } | ||
| 42 | BinarySensorClass::Door => Some(constants::HA_DEVICE_CLASS_BINARY_SENSOR_DOOR), | ||
| 43 | BinarySensorClass::GarageDoor => { | ||
| 44 | Some(constants::HA_DEVICE_CLASS_BINARY_SENSOR_GARAGE_DOOR) | ||
| 45 | } | ||
| 46 | BinarySensorClass::Motion => Some(constants::HA_DEVICE_CLASS_BINARY_SENSOR_MOTION), | ||
| 47 | BinarySensorClass::Occupancy => { | ||
| 48 | Some(constants::HA_DEVICE_CLASS_BINARY_SENSOR_OCCUPANCY) | ||
| 49 | } | ||
| 50 | BinarySensorClass::Opening => Some(constants::HA_DEVICE_CLASS_BINARY_SENSOR_OPENING), | ||
| 51 | BinarySensorClass::Plug => Some(constants::HA_DEVICE_CLASS_BINARY_SENSOR_PLUG), | ||
| 52 | BinarySensorClass::Power => Some(constants::HA_DEVICE_CLASS_BINARY_SENSOR_POWER), | ||
| 53 | BinarySensorClass::Presence => Some(constants::HA_DEVICE_CLASS_BINARY_SENSOR_PRESENCE), | ||
| 54 | BinarySensorClass::Problem => Some(constants::HA_DEVICE_CLASS_BINARY_SENSOR_PROBLEM), | ||
| 55 | BinarySensorClass::Smoke => Some(constants::HA_DEVICE_CLASS_BINARY_SENSOR_SMOKE), | ||
| 56 | BinarySensorClass::Window => Some(constants::HA_DEVICE_CLASS_BINARY_SENSOR_WINDOW), | ||
| 57 | }; | ||
| 58 | } | ||
| 59 | } | ||
| 60 | |||
| 61 | pub struct BinarySensor<'a>(Entity<'a>); | ||
| 62 | |||
| 63 | impl<'a> BinarySensor<'a> { | ||
| 64 | pub(crate) fn new(entity: Entity<'a>) -> Self { | ||
| 65 | Self(entity) | ||
| 66 | } | ||
| 67 | |||
| 68 | pub fn set(&mut self, state: BinaryState) { | ||
| 69 | self.0.publish(state.as_str().as_bytes()); | ||
| 70 | } | ||
| 71 | |||
| 72 | pub fn value(&self) -> Option<BinaryState> { | ||
| 73 | self.0 | ||
| 74 | .with_data(|data| BinaryState::try_from(data.publish_value.as_slice())) | ||
| 75 | .ok() | ||
| 76 | } | ||
| 77 | |||
| 78 | pub fn toggle(&mut self) -> BinaryState { | ||
| 79 | let new_state = self.value().unwrap_or(BinaryState::Off).flip(); | ||
| 80 | self.set(new_state); | ||
| 81 | new_state | ||
| 82 | } | ||
| 83 | } | ||
diff --git a/src/entity_button.rs b/src/entity_button.rs new file mode 100644 index 0000000..baa89a4 --- /dev/null +++ b/src/entity_button.rs | |||
| @@ -0,0 +1,41 @@ | |||
| 1 | use crate::{Entity, EntityCommonConfig, EntityConfig, constants}; | ||
| 2 | |||
| 3 | #[derive(Debug, Default, Clone, Copy, PartialEq, Eq)] | ||
| 4 | pub enum ButtonClass { | ||
| 5 | #[default] | ||
| 6 | Generic, | ||
| 7 | Identify, | ||
| 8 | Restart, | ||
| 9 | Update, | ||
| 10 | } | ||
| 11 | |||
| 12 | #[derive(Debug, Default)] | ||
| 13 | pub struct ButtonConfig { | ||
| 14 | pub common: EntityCommonConfig, | ||
| 15 | pub class: ButtonClass, | ||
| 16 | } | ||
| 17 | |||
| 18 | impl ButtonConfig { | ||
| 19 | pub(crate) fn populate(&self, config: &mut EntityConfig) { | ||
| 20 | self.common.populate(config); | ||
| 21 | config.domain = constants::HA_DOMAIN_BUTTON; | ||
| 22 | config.device_class = match self.class { | ||
| 23 | ButtonClass::Generic => None, | ||
| 24 | ButtonClass::Identify => Some(constants::HA_DEVICE_CLASS_BUTTON_IDENTIFY), | ||
| 25 | ButtonClass::Restart => Some(constants::HA_DEVICE_CLASS_BUTTON_RESTART), | ||
| 26 | ButtonClass::Update => Some(constants::HA_DEVICE_CLASS_BUTTON_UPDATE), | ||
| 27 | }; | ||
| 28 | } | ||
| 29 | } | ||
| 30 | |||
| 31 | pub struct Button<'a>(Entity<'a>); | ||
| 32 | |||
| 33 | impl<'a> Button<'a> { | ||
| 34 | pub(crate) fn new(entity: Entity<'a>) -> Self { | ||
| 35 | Self(entity) | ||
| 36 | } | ||
| 37 | |||
| 38 | pub async fn pressed(&mut self) { | ||
| 39 | self.0.wait_command().await; | ||
| 40 | } | ||
| 41 | } | ||
diff --git a/src/entity_category.rs b/src/entity_category.rs new file mode 100644 index 0000000..741e7dd --- /dev/null +++ b/src/entity_category.rs | |||
| @@ -0,0 +1,17 @@ | |||
| 1 | use crate::constants; | ||
| 2 | |||
| 3 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
| 4 | pub enum EntityCategory { | ||
| 5 | Config, | ||
| 6 | Diagnostic, | ||
| 7 | } | ||
| 8 | |||
| 9 | impl EntityCategory { | ||
| 10 | pub(crate) fn as_str(&self) -> &'static str { | ||
| 11 | match self { | ||
| 12 | EntityCategory::Config => constants::HA_ENTITY_CATEGORY_CONFIG, | ||
| 13 | EntityCategory::Diagnostic => constants::HA_ENTITY_CATEGORY_DIAGNOSTIC, | ||
| 14 | } | ||
| 15 | } | ||
| 16 | } | ||
| 17 | |||
diff --git a/src/entity_number.rs b/src/entity_number.rs new file mode 100644 index 0000000..90d849c --- /dev/null +++ b/src/entity_number.rs | |||
| @@ -0,0 +1,193 @@ | |||
| 1 | use crate::{Entity, EntityCommonConfig, EntityConfig, NumberUnit, constants}; | ||
| 2 | |||
| 3 | #[derive(Debug, Default, Clone, Copy, PartialEq, Eq)] | ||
| 4 | pub enum NumberMode { | ||
| 5 | #[default] | ||
| 6 | Auto, | ||
| 7 | Box, | ||
| 8 | Slider, | ||
| 9 | } | ||
| 10 | |||
| 11 | #[derive(Debug, Default, Clone, Copy, PartialEq, Eq)] | ||
| 12 | pub enum NumberClass { | ||
| 13 | #[default] | ||
| 14 | Generic, | ||
| 15 | ApparentPower, | ||
| 16 | Aqi, | ||
| 17 | AtmosphericPressure, | ||
| 18 | Battery, | ||
| 19 | CarbonDioxide, | ||
| 20 | CarbonMonoxide, | ||
| 21 | Current, | ||
| 22 | DataRate, | ||
| 23 | DataSize, | ||
| 24 | Distance, | ||
| 25 | Duration, | ||
| 26 | Energy, | ||
| 27 | Frequency, | ||
| 28 | Gas, | ||
| 29 | Humidity, | ||
| 30 | Illuminance, | ||
| 31 | Irradiance, | ||
| 32 | Moisture, | ||
| 33 | Monetary, | ||
| 34 | NitrogenDioxide, | ||
| 35 | NitrogenMonoxide, | ||
| 36 | NitrousOxide, | ||
| 37 | Ozone, | ||
| 38 | Ph, | ||
| 39 | Pm1, | ||
| 40 | Pm25, | ||
| 41 | Pm10, | ||
| 42 | PowerFactor, | ||
| 43 | Power, | ||
| 44 | Precipitation, | ||
| 45 | PrecipitationIntensity, | ||
| 46 | Pressure, | ||
| 47 | ReactivePower, | ||
| 48 | SignalStrength, | ||
| 49 | SoundPressure, | ||
| 50 | Speed, | ||
| 51 | SulphurDioxide, | ||
| 52 | Temperature, | ||
| 53 | VolatileOrganicCompounds, | ||
| 54 | VolatileOrganicCompoundsParts, | ||
| 55 | Voltage, | ||
| 56 | Volume, | ||
| 57 | Water, | ||
| 58 | Weight, | ||
| 59 | WindSpeed, | ||
| 60 | } | ||
| 61 | |||
| 62 | #[derive(Debug)] | ||
| 63 | pub struct NumberConfig { | ||
| 64 | pub common: EntityCommonConfig, | ||
| 65 | pub unit: Option<NumberUnit>, | ||
| 66 | pub min: Option<f32>, | ||
| 67 | pub max: Option<f32>, | ||
| 68 | pub step: Option<f32>, | ||
| 69 | pub mode: NumberMode, | ||
| 70 | pub class: NumberClass, | ||
| 71 | } | ||
| 72 | |||
| 73 | impl Default for NumberConfig { | ||
| 74 | fn default() -> Self { | ||
| 75 | Self { | ||
| 76 | common: EntityCommonConfig::default(), | ||
| 77 | unit: None, | ||
| 78 | min: None, | ||
| 79 | max: None, | ||
| 80 | step: None, | ||
| 81 | mode: NumberMode::Auto, | ||
| 82 | class: NumberClass::Generic, | ||
| 83 | } | ||
| 84 | } | ||
| 85 | } | ||
| 86 | |||
| 87 | impl NumberConfig { | ||
| 88 | pub(crate) fn populate(&self, config: &mut EntityConfig) { | ||
| 89 | self.common.populate(config); | ||
| 90 | config.domain = constants::HA_DOMAIN_NUMBER; | ||
| 91 | config.mode = Some(match self.mode { | ||
| 92 | NumberMode::Auto => constants::HA_NUMBER_MODE_AUTO, | ||
| 93 | NumberMode::Box => constants::HA_NUMBER_MODE_BOX, | ||
| 94 | NumberMode::Slider => constants::HA_NUMBER_MODE_SLIDER, | ||
| 95 | }); | ||
| 96 | config.device_class = match self.class { | ||
| 97 | NumberClass::Generic => None, | ||
| 98 | NumberClass::ApparentPower => Some(constants::HA_DEVICE_CLASS_NUMBER_APPARENT_POWER), | ||
| 99 | NumberClass::Aqi => Some(constants::HA_DEVICE_CLASS_NUMBER_AQI), | ||
| 100 | NumberClass::AtmosphericPressure => { | ||
| 101 | Some(constants::HA_DEVICE_CLASS_NUMBER_ATMOSPHERIC_PRESSURE) | ||
| 102 | } | ||
| 103 | NumberClass::Battery => Some(constants::HA_DEVICE_CLASS_NUMBER_BATTERY), | ||
| 104 | NumberClass::CarbonDioxide => Some(constants::HA_DEVICE_CLASS_NUMBER_CARBON_DIOXIDE), | ||
| 105 | NumberClass::CarbonMonoxide => Some(constants::HA_DEVICE_CLASS_NUMBER_CARBON_MONOXIDE), | ||
| 106 | NumberClass::Current => Some(constants::HA_DEVICE_CLASS_NUMBER_CURRENT), | ||
| 107 | NumberClass::DataRate => Some(constants::HA_DEVICE_CLASS_NUMBER_DATA_RATE), | ||
| 108 | NumberClass::DataSize => Some(constants::HA_DEVICE_CLASS_NUMBER_DATA_SIZE), | ||
| 109 | NumberClass::Distance => Some(constants::HA_DEVICE_CLASS_NUMBER_DISTANCE), | ||
| 110 | NumberClass::Duration => Some(constants::HA_DEVICE_CLASS_NUMBER_DURATION), | ||
| 111 | NumberClass::Energy => Some(constants::HA_DEVICE_CLASS_NUMBER_ENERGY), | ||
| 112 | NumberClass::Frequency => Some(constants::HA_DEVICE_CLASS_NUMBER_FREQUENCY), | ||
| 113 | NumberClass::Gas => Some(constants::HA_DEVICE_CLASS_NUMBER_GAS), | ||
| 114 | NumberClass::Humidity => Some(constants::HA_DEVICE_CLASS_NUMBER_HUMIDITY), | ||
| 115 | NumberClass::Illuminance => Some(constants::HA_DEVICE_CLASS_NUMBER_ILLUMINANCE), | ||
| 116 | NumberClass::Irradiance => Some(constants::HA_DEVICE_CLASS_NUMBER_IRRADIANCE), | ||
| 117 | NumberClass::Moisture => Some(constants::HA_DEVICE_CLASS_NUMBER_MOISTURE), | ||
| 118 | NumberClass::Monetary => Some(constants::HA_DEVICE_CLASS_NUMBER_MONETARY), | ||
| 119 | NumberClass::NitrogenDioxide => { | ||
| 120 | Some(constants::HA_DEVICE_CLASS_NUMBER_NITROGEN_DIOXIDE) | ||
| 121 | } | ||
| 122 | NumberClass::NitrogenMonoxide => { | ||
| 123 | Some(constants::HA_DEVICE_CLASS_NUMBER_NITROGEN_MONOXIDE) | ||
| 124 | } | ||
| 125 | NumberClass::NitrousOxide => Some(constants::HA_DEVICE_CLASS_NUMBER_NITROUS_OXIDE), | ||
| 126 | NumberClass::Ozone => Some(constants::HA_DEVICE_CLASS_NUMBER_OZONE), | ||
| 127 | NumberClass::Ph => Some(constants::HA_DEVICE_CLASS_NUMBER_PH), | ||
| 128 | NumberClass::Pm1 => Some(constants::HA_DEVICE_CLASS_NUMBER_PM1), | ||
| 129 | NumberClass::Pm25 => Some(constants::HA_DEVICE_CLASS_NUMBER_PM25), | ||
| 130 | NumberClass::Pm10 => Some(constants::HA_DEVICE_CLASS_NUMBER_PM10), | ||
| 131 | NumberClass::PowerFactor => Some(constants::HA_DEVICE_CLASS_NUMBER_POWER_FACTOR), | ||
| 132 | NumberClass::Power => Some(constants::HA_DEVICE_CLASS_NUMBER_POWER), | ||
| 133 | NumberClass::Precipitation => Some(constants::HA_DEVICE_CLASS_NUMBER_PRECIPITATION), | ||
| 134 | NumberClass::PrecipitationIntensity => { | ||
| 135 | Some(constants::HA_DEVICE_CLASS_NUMBER_PRECIPITATION_INTENSITY) | ||
| 136 | } | ||
| 137 | NumberClass::Pressure => Some(constants::HA_DEVICE_CLASS_NUMBER_PRESSURE), | ||
| 138 | NumberClass::ReactivePower => Some(constants::HA_DEVICE_CLASS_NUMBER_REACTIVE_POWER), | ||
| 139 | NumberClass::SignalStrength => Some(constants::HA_DEVICE_CLASS_NUMBER_SIGNAL_STRENGTH), | ||
| 140 | NumberClass::SoundPressure => Some(constants::HA_DEVICE_CLASS_NUMBER_SOUND_PRESSURE), | ||
| 141 | NumberClass::Speed => Some(constants::HA_DEVICE_CLASS_NUMBER_SPEED), | ||
| 142 | NumberClass::SulphurDioxide => Some(constants::HA_DEVICE_CLASS_NUMBER_SULPHUR_DIOXIDE), | ||
| 143 | NumberClass::Temperature => Some(constants::HA_DEVICE_CLASS_NUMBER_TEMPERATURE), | ||
| 144 | NumberClass::VolatileOrganicCompounds => { | ||
| 145 | Some(constants::HA_DEVICE_CLASS_NUMBER_VOLATILE_ORGANIC_COMPOUNDS) | ||
| 146 | } | ||
| 147 | NumberClass::VolatileOrganicCompoundsParts => { | ||
| 148 | Some(constants::HA_DEVICE_CLASS_NUMBER_VOLATILE_ORGANIC_COMPOUNDS_PARTS) | ||
| 149 | } | ||
| 150 | NumberClass::Voltage => Some(constants::HA_DEVICE_CLASS_NUMBER_VOLTAGE), | ||
| 151 | NumberClass::Volume => Some(constants::HA_DEVICE_CLASS_NUMBER_VOLUME), | ||
| 152 | NumberClass::Water => Some(constants::HA_DEVICE_CLASS_NUMBER_WATER), | ||
| 153 | NumberClass::Weight => Some(constants::HA_DEVICE_CLASS_NUMBER_WEIGHT), | ||
| 154 | NumberClass::WindSpeed => Some(constants::HA_DEVICE_CLASS_NUMBER_WIND_SPEED), | ||
| 155 | }; | ||
| 156 | config.measurement_unit = self.unit.as_ref().map(|u| u.as_str()); | ||
| 157 | config.min = self.min; | ||
| 158 | config.max = self.max; | ||
| 159 | config.step = self.step; | ||
| 160 | } | ||
| 161 | } | ||
| 162 | |||
| 163 | pub struct Number<'a>(Entity<'a>); | ||
| 164 | |||
| 165 | impl<'a> Number<'a> { | ||
| 166 | pub(crate) fn new(entity: Entity<'a>) -> Self { | ||
| 167 | Self(entity) | ||
| 168 | } | ||
| 169 | |||
| 170 | pub fn value(&mut self) -> Option<f32> { | ||
| 171 | self.0.with_data(|data| { | ||
| 172 | str::from_utf8(&data.command_value) | ||
| 173 | .ok() | ||
| 174 | .and_then(|v| v.parse::<f32>().ok()) | ||
| 175 | }) | ||
| 176 | } | ||
| 177 | |||
| 178 | pub async fn value_wait(&mut self) -> f32 { | ||
| 179 | loop { | ||
| 180 | self.0.wait_command().await; | ||
| 181 | match self.value() { | ||
| 182 | Some(value) => return value, | ||
| 183 | None => continue, | ||
| 184 | } | ||
| 185 | } | ||
| 186 | } | ||
| 187 | |||
| 188 | pub fn value_set(&mut self, value: f32) { | ||
| 189 | use core::fmt::Write; | ||
| 190 | self.0 | ||
| 191 | .publish_with(|view| write!(view, "{}", value).unwrap()); | ||
| 192 | } | ||
| 193 | } | ||
diff --git a/src/entity_sensor.rs b/src/entity_sensor.rs new file mode 100644 index 0000000..5d7794f --- /dev/null +++ b/src/entity_sensor.rs | |||
| @@ -0,0 +1,30 @@ | |||
| 1 | use crate::{Entity, EntityCommonConfig, EntityConfig, TemperatureUnit, constants}; | ||
| 2 | |||
| 3 | #[derive(Debug, Default)] | ||
| 4 | pub struct TemperatureSensorConfig { | ||
| 5 | pub common: EntityCommonConfig, | ||
| 6 | pub unit: TemperatureUnit, | ||
| 7 | } | ||
| 8 | |||
| 9 | impl TemperatureSensorConfig { | ||
| 10 | pub(crate) fn populate(&self, config: &mut EntityConfig) { | ||
| 11 | self.common.populate(config); | ||
| 12 | config.domain = constants::HA_DOMAIN_SENSOR; | ||
| 13 | config.device_class = Some(constants::HA_DEVICE_CLASS_SENSOR_TEMPERATURE); | ||
| 14 | config.measurement_unit = Some(self.unit.as_str()); | ||
| 15 | } | ||
| 16 | } | ||
| 17 | |||
| 18 | pub struct TemperatureSensor<'a>(Entity<'a>); | ||
| 19 | |||
| 20 | impl<'a> TemperatureSensor<'a> { | ||
| 21 | pub(crate) fn new(entity: Entity<'a>) -> Self { | ||
| 22 | Self(entity) | ||
| 23 | } | ||
| 24 | |||
| 25 | pub fn publish(&mut self, temperature: f32) { | ||
| 26 | use core::fmt::Write; | ||
| 27 | self.0 | ||
| 28 | .publish_with(|view| write!(view, "{}", temperature).unwrap()); | ||
| 29 | } | ||
| 30 | } | ||
diff --git a/src/entity_switch.rs b/src/entity_switch.rs new file mode 100644 index 0000000..4d2efdb --- /dev/null +++ b/src/entity_switch.rs | |||
| @@ -0,0 +1,59 @@ | |||
| 1 | use crate::{BinaryState, Entity, EntityCommonConfig, EntityConfig, constants}; | ||
| 2 | |||
| 3 | #[derive(Debug, Default, Clone, Copy, PartialEq, Eq)] | ||
| 4 | pub enum SwitchClass { | ||
| 5 | #[default] | ||
| 6 | Generic, | ||
| 7 | Outlet, | ||
| 8 | Switch, | ||
| 9 | } | ||
| 10 | |||
| 11 | #[derive(Debug, Default)] | ||
| 12 | pub struct SwitchConfig { | ||
| 13 | pub common: EntityCommonConfig, | ||
| 14 | pub class: SwitchClass, | ||
| 15 | } | ||
| 16 | |||
| 17 | impl SwitchConfig { | ||
| 18 | pub(crate) fn populate(&self, config: &mut EntityConfig) { | ||
| 19 | self.common.populate(config); | ||
| 20 | config.domain = constants::HA_DOMAIN_SWITCH; | ||
| 21 | config.device_class = match self.class { | ||
| 22 | SwitchClass::Generic => None, | ||
| 23 | SwitchClass::Outlet => Some(constants::HA_DEVICE_CLASS_SWITCH_OUTLET), | ||
| 24 | SwitchClass::Switch => Some(constants::HA_DEVICE_CLASS_SWITCH_SWITCH), | ||
| 25 | }; | ||
| 26 | } | ||
| 27 | } | ||
| 28 | |||
| 29 | pub struct Switch<'a>(Entity<'a>); | ||
| 30 | |||
| 31 | impl<'a> Switch<'a> { | ||
| 32 | pub(crate) fn new(entity: Entity<'a>) -> Self { | ||
| 33 | Self(entity) | ||
| 34 | } | ||
| 35 | |||
| 36 | pub fn state(&self) -> Option<BinaryState> { | ||
| 37 | self.0 | ||
| 38 | .with_data(|data| BinaryState::try_from(data.command_value.as_slice()).ok()) | ||
| 39 | } | ||
| 40 | |||
| 41 | pub fn toggle(&mut self) -> BinaryState { | ||
| 42 | let new_state = self.state().unwrap_or(BinaryState::Off).flip(); | ||
| 43 | self.set(new_state); | ||
| 44 | new_state | ||
| 45 | } | ||
| 46 | |||
| 47 | pub fn set(&mut self, state: BinaryState) { | ||
| 48 | self.0.publish(state.as_str().as_bytes()); | ||
| 49 | } | ||
| 50 | |||
| 51 | pub async fn wait(&mut self) -> BinaryState { | ||
| 52 | loop { | ||
| 53 | self.0.wait_command().await; | ||
| 54 | if let Some(state) = self.state() { | ||
| 55 | return state; | ||
| 56 | } | ||
| 57 | } | ||
| 58 | } | ||
| 59 | } | ||
| @@ -1,6 +1,6 @@ | |||
| 1 | #![no_std] | 1 | #![no_std] |
| 2 | 2 | ||
| 3 | use core::{cell::RefCell, str::FromStr, task::Waker}; | 3 | use core::{cell::RefCell, task::Waker}; |
| 4 | 4 | ||
| 5 | use defmt::Format; | 5 | use defmt::Format; |
| 6 | use embassy_sync::waitqueue::AtomicWaker; | 6 | use embassy_sync::waitqueue::AtomicWaker; |
| @@ -12,10 +12,35 @@ use heapless::{ | |||
| 12 | use serde::Serialize; | 12 | use serde::Serialize; |
| 13 | 13 | ||
| 14 | pub mod constants; | 14 | pub mod constants; |
| 15 | mod transport; | ||
| 16 | mod unit; | ||
| 17 | 15 | ||
| 16 | mod binary_state; | ||
| 17 | pub use binary_state::*; | ||
| 18 | |||
| 19 | mod entity; | ||
| 20 | pub use entity::*; | ||
| 21 | |||
| 22 | mod entity_binary_sensor; | ||
| 23 | pub use entity_binary_sensor::*; | ||
| 24 | |||
| 25 | mod entity_button; | ||
| 26 | pub use entity_button::*; | ||
| 27 | |||
| 28 | mod entity_category; | ||
| 29 | pub use entity_category::*; | ||
| 30 | |||
| 31 | mod entity_number; | ||
| 32 | pub use entity_number::*; | ||
| 33 | |||
| 34 | mod entity_sensor; | ||
| 35 | pub use entity_sensor::*; | ||
| 36 | |||
| 37 | mod entity_switch; | ||
| 38 | pub use entity_switch::*; | ||
| 39 | |||
| 40 | mod transport; | ||
| 18 | pub use transport::Transport; | 41 | pub use transport::Transport; |
| 42 | |||
| 43 | mod unit; | ||
| 19 | pub use unit::*; | 44 | pub use unit::*; |
| 20 | 45 | ||
| 21 | #[derive(Debug, Format, Clone, Copy, Serialize)] | 46 | #[derive(Debug, Format, Clone, Copy, Serialize)] |
| @@ -31,7 +56,8 @@ struct EntityDiscovery<'a> { | |||
| 31 | #[serde(rename = "unique_id")] | 56 | #[serde(rename = "unique_id")] |
| 32 | id: &'a str, | 57 | id: &'a str, |
| 33 | 58 | ||
| 34 | name: &'a str, | 59 | #[serde(skip_serializing_if = "Option::is_none")] |
| 60 | name: Option<&'a str>, | ||
| 35 | 61 | ||
| 36 | #[serde(skip_serializing_if = "Option::is_none")] | 62 | #[serde(skip_serializing_if = "Option::is_none")] |
| 37 | device_class: Option<&'a str>, | 63 | device_class: Option<&'a str>, |
| @@ -55,6 +81,9 @@ struct EntityDiscovery<'a> { | |||
| 55 | icon: Option<&'a str>, | 81 | icon: Option<&'a str>, |
| 56 | 82 | ||
| 57 | #[serde(skip_serializing_if = "Option::is_none")] | 83 | #[serde(skip_serializing_if = "Option::is_none")] |
| 84 | entity_picture: Option<&'a str>, | ||
| 85 | |||
| 86 | #[serde(skip_serializing_if = "Option::is_none")] | ||
| 58 | min: Option<f32>, | 87 | min: Option<f32>, |
| 59 | 88 | ||
| 60 | #[serde(skip_serializing_if = "Option::is_none")] | 89 | #[serde(skip_serializing_if = "Option::is_none")] |
| @@ -152,223 +181,6 @@ impl Default for DeviceResources { | |||
| 152 | } | 181 | } |
| 153 | } | 182 | } |
| 154 | 183 | ||
| 155 | pub struct TemperatureSensor<'a>(Entity<'a>); | ||
| 156 | |||
| 157 | impl<'a> TemperatureSensor<'a> { | ||
| 158 | pub fn publish(&mut self, temperature: f32) { | ||
| 159 | use core::fmt::Write; | ||
| 160 | self.0 | ||
| 161 | .publish_with(|view| write!(view, "{}", temperature).unwrap()); | ||
| 162 | } | ||
| 163 | } | ||
| 164 | |||
| 165 | pub struct Button<'a>(Entity<'a>); | ||
| 166 | |||
| 167 | impl<'a> Button<'a> { | ||
| 168 | pub async fn pressed(&mut self) { | ||
| 169 | self.0.wait_command().await; | ||
| 170 | } | ||
| 171 | } | ||
| 172 | |||
| 173 | pub struct Number<'a>(Entity<'a>); | ||
| 174 | |||
| 175 | impl<'a> Number<'a> { | ||
| 176 | pub fn value(&mut self) -> Option<f32> { | ||
| 177 | self.0.with_data(|data| { | ||
| 178 | str::from_utf8(&data.command_value) | ||
| 179 | .ok() | ||
| 180 | .and_then(|v| v.parse::<f32>().ok()) | ||
| 181 | }) | ||
| 182 | } | ||
| 183 | |||
| 184 | pub async fn value_wait(&mut self) -> f32 { | ||
| 185 | loop { | ||
| 186 | self.0.wait_command().await; | ||
| 187 | match self.value() { | ||
| 188 | Some(value) => return value, | ||
| 189 | None => continue, | ||
| 190 | } | ||
| 191 | } | ||
| 192 | } | ||
| 193 | |||
| 194 | pub fn value_set(&mut self, value: f32) { | ||
| 195 | use core::fmt::Write; | ||
| 196 | self.0 | ||
| 197 | .publish_with(|view| write!(view, "{}", value).unwrap()); | ||
| 198 | } | ||
| 199 | } | ||
| 200 | |||
| 201 | pub struct InvalidSwitchState; | ||
| 202 | |||
| 203 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
| 204 | pub enum SwitchState { | ||
| 205 | On, | ||
| 206 | Off, | ||
| 207 | } | ||
| 208 | |||
| 209 | impl SwitchState { | ||
| 210 | pub fn as_str(&self) -> &'static str { | ||
| 211 | match self { | ||
| 212 | Self::On => constants::HA_SWITCH_STATE_ON, | ||
| 213 | Self::Off => constants::HA_SWITCH_STATE_OFF, | ||
| 214 | } | ||
| 215 | } | ||
| 216 | |||
| 217 | pub fn flip(self) -> Self { | ||
| 218 | match self { | ||
| 219 | Self::On => Self::Off, | ||
| 220 | Self::Off => Self::On, | ||
| 221 | } | ||
| 222 | } | ||
| 223 | } | ||
| 224 | |||
| 225 | impl core::fmt::Display for SwitchState { | ||
| 226 | fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { | ||
| 227 | f.write_str(self.as_str()) | ||
| 228 | } | ||
| 229 | } | ||
| 230 | |||
| 231 | impl FromStr for SwitchState { | ||
| 232 | type Err = InvalidSwitchState; | ||
| 233 | |||
| 234 | fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
| 235 | if s.eq_ignore_ascii_case(constants::HA_SWITCH_STATE_ON) { | ||
| 236 | return Ok(Self::On); | ||
| 237 | } | ||
| 238 | if s.eq_ignore_ascii_case(constants::HA_SWITCH_STATE_OFF) { | ||
| 239 | return Ok(Self::Off); | ||
| 240 | } | ||
| 241 | Err(InvalidSwitchState) | ||
| 242 | } | ||
| 243 | } | ||
| 244 | |||
| 245 | impl TryFrom<&[u8]> for SwitchState { | ||
| 246 | type Error = InvalidSwitchState; | ||
| 247 | |||
| 248 | fn try_from(value: &[u8]) -> Result<Self, Self::Error> { | ||
| 249 | let string = str::from_utf8(value).map_err(|_| InvalidSwitchState)?; | ||
| 250 | string.parse() | ||
| 251 | } | ||
| 252 | } | ||
| 253 | |||
| 254 | pub struct Switch<'a>(Entity<'a>); | ||
| 255 | |||
| 256 | impl<'a> Switch<'a> { | ||
| 257 | pub fn state(&self) -> Option<SwitchState> { | ||
| 258 | self.0 | ||
| 259 | .with_data(|data| SwitchState::try_from(data.command_value.as_slice()).ok()) | ||
| 260 | } | ||
| 261 | |||
| 262 | pub fn toggle(&mut self) -> SwitchState { | ||
| 263 | let new_state = self.state().unwrap_or(SwitchState::Off).flip(); | ||
| 264 | self.set(new_state); | ||
| 265 | new_state | ||
| 266 | } | ||
| 267 | |||
| 268 | pub fn set(&mut self, state: SwitchState) { | ||
| 269 | self.0.publish(state.as_str().as_bytes()); | ||
| 270 | } | ||
| 271 | |||
| 272 | pub async fn wait(&mut self) -> SwitchState { | ||
| 273 | loop { | ||
| 274 | self.0.wait_command().await; | ||
| 275 | if let Some(state) = self.state() { | ||
| 276 | return state; | ||
| 277 | } | ||
| 278 | } | ||
| 279 | } | ||
| 280 | } | ||
| 281 | |||
| 282 | pub struct InvalidBinarySensorState; | ||
| 283 | |||
| 284 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
| 285 | pub enum BinarySensorState { | ||
| 286 | On, | ||
| 287 | Off, | ||
| 288 | } | ||
| 289 | |||
| 290 | impl BinarySensorState { | ||
| 291 | pub fn as_str(&self) -> &'static str { | ||
| 292 | match self { | ||
| 293 | Self::On => constants::HA_BINARY_SENSOR_STATE_ON, | ||
| 294 | Self::Off => constants::HA_BINARY_SENSOR_STATE_OFF, | ||
| 295 | } | ||
| 296 | } | ||
| 297 | |||
| 298 | pub fn flip(self) -> Self { | ||
| 299 | match self { | ||
| 300 | Self::On => Self::Off, | ||
| 301 | Self::Off => Self::On, | ||
| 302 | } | ||
| 303 | } | ||
| 304 | } | ||
| 305 | |||
| 306 | impl core::fmt::Display for BinarySensorState { | ||
| 307 | fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { | ||
| 308 | f.write_str(self.as_str()) | ||
| 309 | } | ||
| 310 | } | ||
| 311 | |||
| 312 | impl FromStr for BinarySensorState { | ||
| 313 | type Err = InvalidBinarySensorState; | ||
| 314 | |||
| 315 | fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
| 316 | if s.eq_ignore_ascii_case(constants::HA_BINARY_SENSOR_STATE_ON) { | ||
| 317 | return Ok(Self::On); | ||
| 318 | } | ||
| 319 | if s.eq_ignore_ascii_case(constants::HA_BINARY_SENSOR_STATE_OFF) { | ||
| 320 | return Ok(Self::Off); | ||
| 321 | } | ||
| 322 | Err(InvalidBinarySensorState) | ||
| 323 | } | ||
| 324 | } | ||
| 325 | |||
| 326 | impl TryFrom<&[u8]> for BinarySensorState { | ||
| 327 | type Error = InvalidBinarySensorState; | ||
| 328 | |||
| 329 | fn try_from(value: &[u8]) -> Result<Self, Self::Error> { | ||
| 330 | let string = str::from_utf8(value).map_err(|_| InvalidBinarySensorState)?; | ||
| 331 | string.parse() | ||
| 332 | } | ||
| 333 | } | ||
| 334 | |||
| 335 | pub struct BinarySensor<'a>(Entity<'a>); | ||
| 336 | |||
| 337 | impl<'a> BinarySensor<'a> { | ||
| 338 | pub fn set(&mut self, state: BinarySensorState) { | ||
| 339 | self.0.publish(state.as_str().as_bytes()); | ||
| 340 | } | ||
| 341 | |||
| 342 | pub fn value(&self) -> Option<BinarySensorState> { | ||
| 343 | self.0 | ||
| 344 | .with_data(|data| BinarySensorState::try_from(data.publish_value.as_slice())) | ||
| 345 | .ok() | ||
| 346 | } | ||
| 347 | |||
| 348 | pub fn toggle(&mut self) -> BinarySensorState { | ||
| 349 | let new_state = self.value().unwrap_or(BinarySensorState::Off).flip(); | ||
| 350 | self.set(new_state); | ||
| 351 | new_state | ||
| 352 | } | ||
| 353 | } | ||
| 354 | |||
| 355 | #[derive(Default)] | ||
| 356 | pub struct EntityConfig { | ||
| 357 | pub id: &'static str, | ||
| 358 | pub name: &'static str, | ||
| 359 | pub domain: &'static str, | ||
| 360 | pub device_class: Option<&'static str>, | ||
| 361 | pub measurement_unit: Option<&'static str>, | ||
| 362 | pub icon: Option<&'static str>, | ||
| 363 | pub category: Option<&'static str>, | ||
| 364 | pub state_class: Option<&'static str>, | ||
| 365 | pub schema: Option<&'static str>, | ||
| 366 | pub min: Option<f32>, | ||
| 367 | pub max: Option<f32>, | ||
| 368 | pub step: Option<f32>, | ||
| 369 | pub mode: Option<&'static str>, | ||
| 370 | } | ||
| 371 | |||
| 372 | struct EntityData { | 184 | struct EntityData { |
| 373 | config: EntityConfig, | 185 | config: EntityConfig, |
| 374 | publish_dirty: bool, | 186 | publish_dirty: bool, |
| @@ -380,8 +192,8 @@ struct EntityData { | |||
| 380 | } | 192 | } |
| 381 | 193 | ||
| 382 | pub struct Entity<'a> { | 194 | pub struct Entity<'a> { |
| 383 | data: &'a RefCell<Option<EntityData>>, | 195 | pub(crate) data: &'a RefCell<Option<EntityData>>, |
| 384 | waker: &'a AtomicWaker, | 196 | pub(crate) waker: &'a AtomicWaker, |
| 385 | } | 197 | } |
| 386 | 198 | ||
| 387 | impl<'a> Entity<'a> { | 199 | impl<'a> Entity<'a> { |
| @@ -521,69 +333,54 @@ impl<'a> Device<'a> { | |||
| 521 | pub fn create_temperature_sensor( | 333 | pub fn create_temperature_sensor( |
| 522 | &self, | 334 | &self, |
| 523 | id: &'static str, | 335 | id: &'static str, |
| 524 | name: &'static str, | 336 | config: TemperatureSensorConfig, |
| 525 | unit: TemperatureUnit, | ||
| 526 | ) -> TemperatureSensor<'a> { | 337 | ) -> TemperatureSensor<'a> { |
| 527 | let entity = self.create_entity(EntityConfig { | 338 | let mut entity_config = EntityConfig::default(); |
| 528 | id, | 339 | entity_config.id = id; |
| 529 | name, | 340 | config.populate(&mut entity_config); |
| 530 | domain: constants::HA_DOMAIN_SENSOR, | 341 | |
| 531 | device_class: Some(constants::HA_DEVICE_CLASS_SENSOR_TEMPERATURE), | 342 | let entity = self.create_entity(entity_config); |
| 532 | measurement_unit: Some(unit.as_str()), | 343 | TemperatureSensor::new(entity) |
| 533 | ..Default::default() | ||
| 534 | }); | ||
| 535 | TemperatureSensor(entity) | ||
| 536 | } | 344 | } |
| 537 | 345 | ||
| 538 | pub fn create_button(&self, id: &'static str, name: &'static str) -> Button<'a> { | 346 | pub fn create_button(&self, id: &'static str, config: ButtonConfig) -> Button<'a> { |
| 539 | let entity = self.create_entity(EntityConfig { | 347 | let mut entity_config = EntityConfig::default(); |
| 540 | id, | 348 | entity_config.id = id; |
| 541 | name, | 349 | config.populate(&mut entity_config); |
| 542 | domain: constants::HA_DOMAIN_BUTTON, | 350 | |
| 543 | ..Default::default() | 351 | let entity = self.create_entity(entity_config); |
| 544 | }); | 352 | Button::new(entity) |
| 545 | Button(entity) | ||
| 546 | } | 353 | } |
| 547 | 354 | ||
| 548 | pub fn create_number(&self, id: &'static str, name: &'static str) -> Number<'a> { | 355 | pub fn create_number(&self, id: &'static str, config: NumberConfig) -> Number<'a> { |
| 549 | let entity = self.create_entity(EntityConfig { | 356 | let mut entity_config = EntityConfig::default(); |
| 550 | id, | 357 | entity_config.id = id; |
| 551 | name, | 358 | config.populate(&mut entity_config); |
| 552 | domain: constants::HA_DOMAIN_NUMBER, | 359 | |
| 553 | measurement_unit: Some("s"), | 360 | let entity = self.create_entity(entity_config); |
| 554 | min: Some(0.0), | 361 | Number::new(entity) |
| 555 | max: Some(200.0), | ||
| 556 | step: Some(2.0), | ||
| 557 | mode: Some(constants::HA_NUMBER_MODE_AUTO), | ||
| 558 | ..Default::default() | ||
| 559 | }); | ||
| 560 | Number(entity) | ||
| 561 | } | 362 | } |
| 562 | 363 | ||
| 563 | pub fn create_switch(&self, id: &'static str, name: &'static str) -> Switch<'a> { | 364 | pub fn create_switch(&self, id: &'static str, config: SwitchConfig) -> Switch<'a> { |
| 564 | let entity = self.create_entity(EntityConfig { | 365 | let mut entity_config = EntityConfig::default(); |
| 565 | id, | 366 | entity_config.id = id; |
| 566 | name, | 367 | config.populate(&mut entity_config); |
| 567 | domain: constants::HA_DOMAIN_SWITCH, | 368 | |
| 568 | ..Default::default() | 369 | let entity = self.create_entity(entity_config); |
| 569 | }); | 370 | Switch::new(entity) |
| 570 | Switch(entity) | ||
| 571 | } | 371 | } |
| 572 | 372 | ||
| 573 | pub fn create_binary_sensor( | 373 | pub fn create_binary_sensor( |
| 574 | &self, | 374 | &self, |
| 575 | id: &'static str, | 375 | id: &'static str, |
| 576 | name: &'static str, | 376 | config: BinarySensorConfig, |
| 577 | class: &'static str, | ||
| 578 | ) -> BinarySensor<'a> { | 377 | ) -> BinarySensor<'a> { |
| 579 | let entity = self.create_entity(EntityConfig { | 378 | let mut entity_config = EntityConfig::default(); |
| 580 | id, | 379 | entity_config.id = id; |
| 581 | name, | 380 | config.populate(&mut entity_config); |
| 582 | domain: constants::HA_DOMAIN_BINARY_SENSOR, | 381 | |
| 583 | device_class: Some(class), | 382 | let entity = self.create_entity(entity_config); |
| 584 | ..Default::default() | 383 | BinarySensor::new(entity) |
| 585 | }); | ||
| 586 | BinarySensor(entity) | ||
| 587 | } | 384 | } |
| 588 | 385 | ||
| 589 | pub async fn run<T: Transport>(&mut self, transport: &mut T) -> ! { | 386 | pub async fn run<T: Transport>(&mut self, transport: &mut T) -> ! { |
| @@ -667,6 +464,7 @@ impl<'a> Device<'a> { | |||
| 667 | schema: entity_config.schema, | 464 | schema: entity_config.schema, |
| 668 | state_class: entity_config.state_class, | 465 | state_class: entity_config.state_class, |
| 669 | icon: entity_config.icon, | 466 | icon: entity_config.icon, |
| 467 | entity_picture: entity_config.picture, | ||
| 670 | min: entity_config.min, | 468 | min: entity_config.min, |
| 671 | max: entity_config.max, | 469 | max: entity_config.max, |
| 672 | step: entity_config.step, | 470 | step: entity_config.step, |
diff --git a/src/unit.rs b/src/unit.rs index f0557da..e61c867 100644 --- a/src/unit.rs +++ b/src/unit.rs | |||
| @@ -1,5 +1,6 @@ | |||
| 1 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] | 1 | #[derive(Debug, Default, Clone, Copy, PartialEq, Eq)] |
| 2 | pub enum TemperatureUnit { | 2 | pub enum TemperatureUnit { |
| 3 | #[default] | ||
| 3 | Celcius, | 4 | Celcius, |
| 4 | Kelvin, | 5 | Kelvin, |
| 5 | Fahrenheit, | 6 | Fahrenheit, |
| @@ -23,22 +24,1133 @@ pub enum HumidityUnit { | |||
| 23 | Other(&'static str), | 24 | Other(&'static str), |
| 24 | } | 25 | } |
| 25 | 26 | ||
| 27 | impl HumidityUnit { | ||
| 28 | pub fn as_str(&self) -> &'static str { | ||
| 29 | match self { | ||
| 30 | HumidityUnit::Percentage => crate::constants::HA_UNIT_PERCENTAGE, | ||
| 31 | HumidityUnit::Other(other) => other, | ||
| 32 | } | ||
| 33 | } | ||
| 34 | } | ||
| 35 | |||
| 26 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] | 36 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 27 | pub enum BatteryUnit { | 37 | pub enum BatteryUnit { |
| 28 | Percentage, | 38 | Percentage, |
| 29 | } | 39 | } |
| 30 | 40 | ||
| 41 | impl BatteryUnit { | ||
| 42 | pub fn as_str(&self) -> &'static str { | ||
| 43 | match self { | ||
| 44 | BatteryUnit::Percentage => crate::constants::HA_UNIT_PERCENTAGE, | ||
| 45 | } | ||
| 46 | } | ||
| 47 | } | ||
| 48 | |||
| 31 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] | 49 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 32 | pub enum LightUnit { | 50 | pub enum LightUnit { |
| 33 | Lux, | 51 | Lux, |
| 34 | } | 52 | } |
| 35 | 53 | ||
| 54 | impl LightUnit { | ||
| 55 | pub fn as_str(&self) -> &'static str { | ||
| 56 | match self { | ||
| 57 | LightUnit::Lux => crate::constants::HA_UNIT_LIGHT_LUX, | ||
| 58 | } | ||
| 59 | } | ||
| 60 | } | ||
| 61 | |||
| 36 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] | 62 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 37 | pub enum PressureUnit { | 63 | pub enum PressureUnit { |
| 38 | HectoPascal, | 64 | HectoPascal, |
| 39 | } | 65 | } |
| 40 | 66 | ||
| 67 | impl PressureUnit { | ||
| 68 | pub fn as_str(&self) -> &'static str { | ||
| 69 | match self { | ||
| 70 | PressureUnit::HectoPascal => crate::constants::HA_UNIT_PRESSURE_HPA, | ||
| 71 | } | ||
| 72 | } | ||
| 73 | } | ||
| 74 | |||
| 41 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] | 75 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 42 | pub enum EnergyUnit { | 76 | pub enum EnergyUnit { |
| 43 | KiloWattHour, | 77 | KiloWattHour, |
| 44 | } | 78 | } |
| 79 | |||
| 80 | impl EnergyUnit { | ||
| 81 | pub fn as_str(&self) -> &'static str { | ||
| 82 | match self { | ||
| 83 | EnergyUnit::KiloWattHour => crate::constants::HA_UNIT_ENERGY_KWH, | ||
| 84 | } | ||
| 85 | } | ||
| 86 | } | ||
| 87 | |||
| 88 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
| 89 | pub enum TimeUnit { | ||
| 90 | Milliseconds, | ||
| 91 | Seconds, | ||
| 92 | Minutes, | ||
| 93 | Hours, | ||
| 94 | Days, | ||
| 95 | } | ||
| 96 | |||
| 97 | impl TimeUnit { | ||
| 98 | pub fn as_str(&self) -> &'static str { | ||
| 99 | match self { | ||
| 100 | TimeUnit::Milliseconds => crate::constants::HA_UNIT_TIME_MILLISECONDS, | ||
| 101 | TimeUnit::Seconds => crate::constants::HA_UNIT_TIME_SECONDS, | ||
| 102 | TimeUnit::Minutes => crate::constants::HA_UNIT_TIME_MINUTES, | ||
| 103 | TimeUnit::Hours => crate::constants::HA_UNIT_TIME_HOURS, | ||
| 104 | TimeUnit::Days => crate::constants::HA_UNIT_TIME_DAYS, | ||
| 105 | } | ||
| 106 | } | ||
| 107 | } | ||
| 108 | |||
| 109 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
| 110 | pub enum PowerUnit { | ||
| 111 | Watt, | ||
| 112 | KiloWatt, | ||
| 113 | } | ||
| 114 | |||
| 115 | impl PowerUnit { | ||
| 116 | pub fn as_str(&self) -> &'static str { | ||
| 117 | match self { | ||
| 118 | PowerUnit::Watt => crate::constants::HA_UNIT_POWER_WATT, | ||
| 119 | PowerUnit::KiloWatt => crate::constants::HA_UNIT_POWER_KILOWATT, | ||
| 120 | } | ||
| 121 | } | ||
| 122 | } | ||
| 123 | |||
| 124 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
| 125 | pub enum VoltageUnit { | ||
| 126 | Volt, | ||
| 127 | } | ||
| 128 | |||
| 129 | impl VoltageUnit { | ||
| 130 | pub fn as_str(&self) -> &'static str { | ||
| 131 | match self { | ||
| 132 | VoltageUnit::Volt => crate::constants::HA_UNIT_VOLTAGE_VOLT, | ||
| 133 | } | ||
| 134 | } | ||
| 135 | } | ||
| 136 | |||
| 137 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
| 138 | pub enum CurrentUnit { | ||
| 139 | Ampere, | ||
| 140 | } | ||
| 141 | |||
| 142 | impl CurrentUnit { | ||
| 143 | pub fn as_str(&self) -> &'static str { | ||
| 144 | match self { | ||
| 145 | CurrentUnit::Ampere => crate::constants::HA_UNIT_CURRENT_AMPERE, | ||
| 146 | } | ||
| 147 | } | ||
| 148 | } | ||
| 149 | |||
| 150 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
| 151 | pub enum DistanceUnit { | ||
| 152 | Millimeter, | ||
| 153 | Centimeter, | ||
| 154 | Meter, | ||
| 155 | Kilometer, | ||
| 156 | } | ||
| 157 | |||
| 158 | impl DistanceUnit { | ||
| 159 | pub fn as_str(&self) -> &'static str { | ||
| 160 | match self { | ||
| 161 | DistanceUnit::Millimeter => crate::constants::HA_UNIT_DISTANCE_MILLIMETER, | ||
| 162 | DistanceUnit::Centimeter => crate::constants::HA_UNIT_DISTANCE_CENTIMETER, | ||
| 163 | DistanceUnit::Meter => crate::constants::HA_UNIT_DISTANCE_METER, | ||
| 164 | DistanceUnit::Kilometer => crate::constants::HA_UNIT_DISTANCE_KILOMETER, | ||
| 165 | } | ||
| 166 | } | ||
| 167 | } | ||
| 168 | |||
| 169 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
| 170 | pub enum CurrencyUnit { | ||
| 171 | USD, | ||
| 172 | EUR, | ||
| 173 | GBP, | ||
| 174 | JPY, | ||
| 175 | CNY, | ||
| 176 | CAD, | ||
| 177 | AUD, | ||
| 178 | CHF, | ||
| 179 | INR, | ||
| 180 | BRL, | ||
| 181 | Dollar, | ||
| 182 | Euro, | ||
| 183 | Pound, | ||
| 184 | Yen, | ||
| 185 | Cent, | ||
| 186 | Other(&'static str), | ||
| 187 | } | ||
| 188 | |||
| 189 | impl CurrencyUnit { | ||
| 190 | pub fn as_str(&self) -> &'static str { | ||
| 191 | match self { | ||
| 192 | CurrencyUnit::USD => crate::constants::HA_UNIT_CURRENCY_USD, | ||
| 193 | CurrencyUnit::EUR => crate::constants::HA_UNIT_CURRENCY_EUR, | ||
| 194 | CurrencyUnit::GBP => crate::constants::HA_UNIT_CURRENCY_GBP, | ||
| 195 | CurrencyUnit::JPY => crate::constants::HA_UNIT_CURRENCY_JPY, | ||
| 196 | CurrencyUnit::CNY => crate::constants::HA_UNIT_CURRENCY_CNY, | ||
| 197 | CurrencyUnit::CAD => crate::constants::HA_UNIT_CURRENCY_CAD, | ||
| 198 | CurrencyUnit::AUD => crate::constants::HA_UNIT_CURRENCY_AUD, | ||
| 199 | CurrencyUnit::CHF => crate::constants::HA_UNIT_CURRENCY_CHF, | ||
| 200 | CurrencyUnit::INR => crate::constants::HA_UNIT_CURRENCY_INR, | ||
| 201 | CurrencyUnit::BRL => crate::constants::HA_UNIT_CURRENCY_BRL, | ||
| 202 | CurrencyUnit::Dollar => crate::constants::HA_UNIT_CURRENCY_DOLLAR, | ||
| 203 | CurrencyUnit::Euro => crate::constants::HA_UNIT_CURRENCY_EURO, | ||
| 204 | CurrencyUnit::Pound => crate::constants::HA_UNIT_CURRENCY_POUND, | ||
| 205 | CurrencyUnit::Yen => crate::constants::HA_UNIT_CURRENCY_YEN, | ||
| 206 | CurrencyUnit::Cent => crate::constants::HA_UNIT_CURRENCY_CENT, | ||
| 207 | CurrencyUnit::Other(other) => other, | ||
| 208 | } | ||
| 209 | } | ||
| 210 | } | ||
| 211 | |||
| 212 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
| 213 | pub enum NumberUnit { | ||
| 214 | // Energy | ||
| 215 | Joule, | ||
| 216 | KiloJoule, | ||
| 217 | MegaJoule, | ||
| 218 | GigaJoule, | ||
| 219 | MilliWattHour, | ||
| 220 | WattHour, | ||
| 221 | KiloWattHour, | ||
| 222 | MegaWattHour, | ||
| 223 | GigaWattHour, | ||
| 224 | TeraWattHour, | ||
| 225 | Calorie, | ||
| 226 | KiloCalorie, | ||
| 227 | MegaCalorie, | ||
| 228 | GigaCalorie, | ||
| 229 | // Temperature (°C, °F, K) | ||
| 230 | Celsius, | ||
| 231 | Fahrenheit, | ||
| 232 | Kelvin, | ||
| 233 | // Pressure | ||
| 234 | MilliPascal, | ||
| 235 | Pascal, | ||
| 236 | HectoPascal, | ||
| 237 | KiloPascal, | ||
| 238 | Bar, | ||
| 239 | CentiBar, | ||
| 240 | MilliBar, | ||
| 241 | MillimeterMercury, | ||
| 242 | InchMercury, | ||
| 243 | InchWater, | ||
| 244 | Psi, | ||
| 245 | // Volume | ||
| 246 | Liter, | ||
| 247 | MilliLiter, | ||
| 248 | Gallon, | ||
| 249 | FluidOunce, | ||
| 250 | CubicMeter, | ||
| 251 | CubicFoot, | ||
| 252 | CCF, | ||
| 253 | MCF, | ||
| 254 | // Speed | ||
| 255 | FeetPerSecond, | ||
| 256 | InchPerDay, | ||
| 257 | InchPerHour, | ||
| 258 | InchPerSecond, | ||
| 259 | KilometerPerHour, | ||
| 260 | Knot, | ||
| 261 | MeterPerSecond, | ||
| 262 | MilePerHour, | ||
| 263 | MillimeterPerDay, | ||
| 264 | MillimeterPerSecond, | ||
| 265 | // Distance | ||
| 266 | Kilometer, | ||
| 267 | Meter, | ||
| 268 | Centimeter, | ||
| 269 | Millimeter, | ||
| 270 | Mile, | ||
| 271 | NauticalMile, | ||
| 272 | Yard, | ||
| 273 | Inch, | ||
| 274 | // Power | ||
| 275 | MilliWatt, | ||
| 276 | Watt, | ||
| 277 | KiloWatt, | ||
| 278 | MegaWatt, | ||
| 279 | GigaWatt, | ||
| 280 | TeraWatt, | ||
| 281 | // Current | ||
| 282 | Ampere, | ||
| 283 | MilliAmpere, | ||
| 284 | // Voltage | ||
| 285 | Volt, | ||
| 286 | MilliVolt, | ||
| 287 | MicroVolt, | ||
| 288 | KiloVolt, | ||
| 289 | MegaVolt, | ||
| 290 | // Data Rate | ||
| 291 | BitPerSecond, | ||
| 292 | KiloBitPerSecond, | ||
| 293 | MegaBitPerSecond, | ||
| 294 | GigaBitPerSecond, | ||
| 295 | BytePerSecond, | ||
| 296 | KiloBytePerSecond, | ||
| 297 | MegaBytePerSecond, | ||
| 298 | GigaBytePerSecond, | ||
| 299 | KibiBytePerSecond, | ||
| 300 | MebiBytePerSecond, | ||
| 301 | GibiBytePerSecond, | ||
| 302 | // Weight | ||
| 303 | Kilogram, | ||
| 304 | Gram, | ||
| 305 | Milligram, | ||
| 306 | Microgram, | ||
| 307 | Ounce, | ||
| 308 | Pound, | ||
| 309 | Stone, | ||
| 310 | // Other | ||
| 311 | Percentage, | ||
| 312 | Other(&'static str), | ||
| 313 | } | ||
| 314 | |||
| 315 | impl NumberUnit { | ||
| 316 | pub fn as_str(&self) -> &'static str { | ||
| 317 | match self { | ||
| 318 | // Energy | ||
| 319 | NumberUnit::Joule => crate::constants::HA_UNIT_ENERGY_JOULE, | ||
| 320 | NumberUnit::KiloJoule => crate::constants::HA_UNIT_ENERGY_KILOJOULE, | ||
| 321 | NumberUnit::MegaJoule => crate::constants::HA_UNIT_ENERGY_MEGAJOULE, | ||
| 322 | NumberUnit::GigaJoule => crate::constants::HA_UNIT_ENERGY_GIGAJOULE, | ||
| 323 | NumberUnit::MilliWattHour => crate::constants::HA_UNIT_ENERGY_MILLIWATTHOUR, | ||
| 324 | NumberUnit::WattHour => crate::constants::HA_UNIT_ENERGY_WATTHOUR, | ||
| 325 | NumberUnit::KiloWattHour => crate::constants::HA_UNIT_ENERGY_KWH, | ||
| 326 | NumberUnit::MegaWattHour => crate::constants::HA_UNIT_ENERGY_MEGAWATTHOUR, | ||
| 327 | NumberUnit::GigaWattHour => crate::constants::HA_UNIT_ENERGY_GIGAWATTHOUR, | ||
| 328 | NumberUnit::TeraWattHour => crate::constants::HA_UNIT_ENERGY_TERAWATTHOUR, | ||
| 329 | NumberUnit::Calorie => crate::constants::HA_UNIT_ENERGY_CALORIE, | ||
| 330 | NumberUnit::KiloCalorie => crate::constants::HA_UNIT_ENERGY_KILOCALORIE, | ||
| 331 | NumberUnit::MegaCalorie => crate::constants::HA_UNIT_ENERGY_MEGACALORIE, | ||
| 332 | NumberUnit::GigaCalorie => crate::constants::HA_UNIT_ENERGY_GIGACALORIE, | ||
| 333 | // Temperature | ||
| 334 | NumberUnit::Celsius => crate::constants::HA_UNIT_TEMPERATURE_CELSIUS, | ||
| 335 | NumberUnit::Fahrenheit => crate::constants::HA_UNIT_TEMPERATURE_FAHRENHEIT, | ||
| 336 | NumberUnit::Kelvin => crate::constants::HA_UNIT_TEMPERATURE_KELVIN, | ||
| 337 | // Pressure | ||
| 338 | NumberUnit::MilliPascal => crate::constants::HA_UNIT_PRESSURE_MILLIPASCAL, | ||
| 339 | NumberUnit::Pascal => crate::constants::HA_UNIT_PRESSURE_PASCAL, | ||
| 340 | NumberUnit::HectoPascal => crate::constants::HA_UNIT_PRESSURE_HPA, | ||
| 341 | NumberUnit::KiloPascal => crate::constants::HA_UNIT_PRESSURE_KILOPASCAL, | ||
| 342 | NumberUnit::Bar => crate::constants::HA_UNIT_PRESSURE_BAR, | ||
| 343 | NumberUnit::CentiBar => crate::constants::HA_UNIT_PRESSURE_CENTIBAR, | ||
| 344 | NumberUnit::MilliBar => crate::constants::HA_UNIT_PRESSURE_MILLIBAR, | ||
| 345 | NumberUnit::MillimeterMercury => crate::constants::HA_UNIT_PRESSURE_MILLIMETER_MERCURY, | ||
| 346 | NumberUnit::InchMercury => crate::constants::HA_UNIT_PRESSURE_INCH_MERCURY, | ||
| 347 | NumberUnit::InchWater => crate::constants::HA_UNIT_PRESSURE_INCH_WATER, | ||
| 348 | NumberUnit::Psi => crate::constants::HA_UNIT_PRESSURE_PSI, | ||
| 349 | // Volume | ||
| 350 | NumberUnit::Liter => crate::constants::HA_UNIT_VOLUME_LITER, | ||
| 351 | NumberUnit::MilliLiter => crate::constants::HA_UNIT_VOLUME_MILLILITER, | ||
| 352 | NumberUnit::Gallon => crate::constants::HA_UNIT_VOLUME_GALLON, | ||
| 353 | NumberUnit::FluidOunce => crate::constants::HA_UNIT_VOLUME_FLUID_OUNCE, | ||
| 354 | NumberUnit::CubicMeter => crate::constants::HA_UNIT_VOLUME_CUBIC_METER, | ||
| 355 | NumberUnit::CubicFoot => crate::constants::HA_UNIT_VOLUME_CUBIC_FOOT, | ||
| 356 | NumberUnit::CCF => crate::constants::HA_UNIT_VOLUME_CCF, | ||
| 357 | NumberUnit::MCF => crate::constants::HA_UNIT_VOLUME_MCF, | ||
| 358 | // Speed | ||
| 359 | NumberUnit::FeetPerSecond => crate::constants::HA_UNIT_SPEED_FEET_PER_SECOND, | ||
| 360 | NumberUnit::InchPerDay => crate::constants::HA_UNIT_SPEED_INCH_PER_DAY, | ||
| 361 | NumberUnit::InchPerHour => crate::constants::HA_UNIT_SPEED_INCH_PER_HOUR, | ||
| 362 | NumberUnit::InchPerSecond => crate::constants::HA_UNIT_SPEED_INCH_PER_SECOND, | ||
| 363 | NumberUnit::KilometerPerHour => crate::constants::HA_UNIT_SPEED_KILOMETER_PER_HOUR, | ||
| 364 | NumberUnit::Knot => crate::constants::HA_UNIT_SPEED_KNOT, | ||
| 365 | NumberUnit::MeterPerSecond => crate::constants::HA_UNIT_SPEED_METER_PER_SECOND, | ||
| 366 | NumberUnit::MilePerHour => crate::constants::HA_UNIT_SPEED_MILE_PER_HOUR, | ||
| 367 | NumberUnit::MillimeterPerDay => crate::constants::HA_UNIT_SPEED_MILLIMETER_PER_DAY, | ||
| 368 | NumberUnit::MillimeterPerSecond => crate::constants::HA_UNIT_SPEED_MILLIMETER_PER_SECOND, | ||
| 369 | // Distance | ||
| 370 | NumberUnit::Kilometer => crate::constants::HA_UNIT_DISTANCE_KILOMETER, | ||
| 371 | NumberUnit::Meter => crate::constants::HA_UNIT_DISTANCE_METER, | ||
| 372 | NumberUnit::Centimeter => crate::constants::HA_UNIT_DISTANCE_CENTIMETER, | ||
| 373 | NumberUnit::Millimeter => crate::constants::HA_UNIT_DISTANCE_MILLIMETER, | ||
| 374 | NumberUnit::Mile => crate::constants::HA_UNIT_DISTANCE_MILE, | ||
| 375 | NumberUnit::NauticalMile => crate::constants::HA_UNIT_DISTANCE_NAUTICAL_MILE, | ||
| 376 | NumberUnit::Yard => crate::constants::HA_UNIT_DISTANCE_YARD, | ||
| 377 | NumberUnit::Inch => crate::constants::HA_UNIT_DISTANCE_INCH, | ||
| 378 | // Power | ||
| 379 | NumberUnit::MilliWatt => crate::constants::HA_UNIT_POWER_MILLIWATT, | ||
| 380 | NumberUnit::Watt => crate::constants::HA_UNIT_POWER_WATT, | ||
| 381 | NumberUnit::KiloWatt => crate::constants::HA_UNIT_POWER_KILOWATT, | ||
| 382 | NumberUnit::MegaWatt => crate::constants::HA_UNIT_POWER_MEGAWATT, | ||
| 383 | NumberUnit::GigaWatt => crate::constants::HA_UNIT_POWER_GIGAWATT, | ||
| 384 | NumberUnit::TeraWatt => crate::constants::HA_UNIT_POWER_TERAWATT, | ||
| 385 | // Current | ||
| 386 | NumberUnit::Ampere => crate::constants::HA_UNIT_CURRENT_AMPERE, | ||
| 387 | NumberUnit::MilliAmpere => crate::constants::HA_UNIT_CURRENT_MILLIAMPERE, | ||
| 388 | // Voltage | ||
| 389 | NumberUnit::Volt => crate::constants::HA_UNIT_VOLTAGE_VOLT, | ||
| 390 | NumberUnit::MilliVolt => crate::constants::HA_UNIT_VOLTAGE_MILLIVOLT, | ||
| 391 | NumberUnit::MicroVolt => crate::constants::HA_UNIT_VOLTAGE_MICROVOLT, | ||
| 392 | NumberUnit::KiloVolt => crate::constants::HA_UNIT_VOLTAGE_KILOVOLT, | ||
| 393 | NumberUnit::MegaVolt => crate::constants::HA_UNIT_VOLTAGE_MEGAVOLT, | ||
| 394 | // Data Rate | ||
| 395 | NumberUnit::BitPerSecond => crate::constants::HA_UNIT_DATA_RATE_BIT_PER_SECOND, | ||
| 396 | NumberUnit::KiloBitPerSecond => crate::constants::HA_UNIT_DATA_RATE_KILOBIT_PER_SECOND, | ||
| 397 | NumberUnit::MegaBitPerSecond => crate::constants::HA_UNIT_DATA_RATE_MEGABIT_PER_SECOND, | ||
| 398 | NumberUnit::GigaBitPerSecond => crate::constants::HA_UNIT_DATA_RATE_GIGABIT_PER_SECOND, | ||
| 399 | NumberUnit::BytePerSecond => crate::constants::HA_UNIT_DATA_RATE_BYTE_PER_SECOND, | ||
| 400 | NumberUnit::KiloBytePerSecond => crate::constants::HA_UNIT_DATA_RATE_KILOBYTE_PER_SECOND, | ||
| 401 | NumberUnit::MegaBytePerSecond => crate::constants::HA_UNIT_DATA_RATE_MEGABYTE_PER_SECOND, | ||
| 402 | NumberUnit::GigaBytePerSecond => crate::constants::HA_UNIT_DATA_RATE_GIGABYTE_PER_SECOND, | ||
| 403 | NumberUnit::KibiBytePerSecond => crate::constants::HA_UNIT_DATA_RATE_KIBIBYTE_PER_SECOND, | ||
| 404 | NumberUnit::MebiBytePerSecond => crate::constants::HA_UNIT_DATA_RATE_MEBIBYTE_PER_SECOND, | ||
| 405 | NumberUnit::GibiBytePerSecond => crate::constants::HA_UNIT_DATA_RATE_GIBIBYTE_PER_SECOND, | ||
| 406 | // Weight | ||
| 407 | NumberUnit::Kilogram => crate::constants::HA_UNIT_WEIGHT_KILOGRAM, | ||
| 408 | NumberUnit::Gram => crate::constants::HA_UNIT_WEIGHT_GRAM, | ||
| 409 | NumberUnit::Milligram => crate::constants::HA_UNIT_WEIGHT_MILLIGRAM, | ||
| 410 | NumberUnit::Microgram => crate::constants::HA_UNIT_WEIGHT_MICROGRAM, | ||
| 411 | NumberUnit::Ounce => crate::constants::HA_UNIT_WEIGHT_OUNCE, | ||
| 412 | NumberUnit::Pound => crate::constants::HA_UNIT_WEIGHT_POUND, | ||
| 413 | NumberUnit::Stone => crate::constants::HA_UNIT_WEIGHT_STONE, | ||
| 414 | // Other | ||
| 415 | NumberUnit::Percentage => crate::constants::HA_UNIT_PERCENTAGE, | ||
| 416 | NumberUnit::Other(other) => other, | ||
| 417 | } | ||
| 418 | } | ||
| 419 | } | ||
| 420 | |||
| 421 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
| 422 | pub enum Unit { | ||
| 423 | TemperatureCelcius, | ||
| 424 | TemperatureKelvin, | ||
| 425 | TemperatureFahrenheit, | ||
| 426 | HumidityPercentage, | ||
| 427 | BatteryPercentage, | ||
| 428 | LightLux, | ||
| 429 | PressureHectoPascal, | ||
| 430 | EnergyKiloWattHour, | ||
| 431 | TimeMilliseconds, | ||
| 432 | TimeSeconds, | ||
| 433 | TimeMinutes, | ||
| 434 | TimeHours, | ||
| 435 | TimeDays, | ||
| 436 | PowerWatt, | ||
| 437 | PowerKiloWatt, | ||
| 438 | VoltageVolt, | ||
| 439 | CurrentAmpere, | ||
| 440 | DistanceMillimeter, | ||
| 441 | DistanceCentimeter, | ||
| 442 | DistanceMeter, | ||
| 443 | DistanceKilometer, | ||
| 444 | CurrencyUSD, | ||
| 445 | CurrencyEUR, | ||
| 446 | CurrencyGBP, | ||
| 447 | CurrencyJPY, | ||
| 448 | CurrencyCNY, | ||
| 449 | CurrencyCAD, | ||
| 450 | CurrencyAUD, | ||
| 451 | CurrencyCHF, | ||
| 452 | CurrencyINR, | ||
| 453 | CurrencyBRL, | ||
| 454 | CurrencyDollar, | ||
| 455 | CurrencyEuro, | ||
| 456 | CurrencyPound, | ||
| 457 | CurrencyYen, | ||
| 458 | CurrencyCent, | ||
| 459 | // Number units | ||
| 460 | NumberJoule, | ||
| 461 | NumberKiloJoule, | ||
| 462 | NumberMegaJoule, | ||
| 463 | NumberGigaJoule, | ||
| 464 | NumberMilliWattHour, | ||
| 465 | NumberWattHour, | ||
| 466 | NumberKiloWattHour, | ||
| 467 | NumberMegaWattHour, | ||
| 468 | NumberGigaWattHour, | ||
| 469 | NumberTeraWattHour, | ||
| 470 | NumberCalorie, | ||
| 471 | NumberKiloCalorie, | ||
| 472 | NumberMegaCalorie, | ||
| 473 | NumberGigaCalorie, | ||
| 474 | NumberCelsius, | ||
| 475 | NumberFahrenheit, | ||
| 476 | NumberKelvin, | ||
| 477 | NumberMilliPascal, | ||
| 478 | NumberPascal, | ||
| 479 | NumberHectoPascal, | ||
| 480 | NumberKiloPascal, | ||
| 481 | NumberBar, | ||
| 482 | NumberCentiBar, | ||
| 483 | NumberMilliBar, | ||
| 484 | NumberMillimeterMercury, | ||
| 485 | NumberInchMercury, | ||
| 486 | NumberInchWater, | ||
| 487 | NumberPsi, | ||
| 488 | NumberLiter, | ||
| 489 | NumberMilliLiter, | ||
| 490 | NumberGallon, | ||
| 491 | NumberFluidOunce, | ||
| 492 | NumberCubicMeter, | ||
| 493 | NumberCubicFoot, | ||
| 494 | NumberCCF, | ||
| 495 | NumberMCF, | ||
| 496 | NumberFeetPerSecond, | ||
| 497 | NumberInchPerDay, | ||
| 498 | NumberInchPerHour, | ||
| 499 | NumberInchPerSecond, | ||
| 500 | NumberKilometerPerHour, | ||
| 501 | NumberKnot, | ||
| 502 | NumberMeterPerSecond, | ||
| 503 | NumberMilePerHour, | ||
| 504 | NumberMillimeterPerDay, | ||
| 505 | NumberMillimeterPerSecond, | ||
| 506 | NumberKilometer, | ||
| 507 | NumberMeter, | ||
| 508 | NumberCentimeter, | ||
| 509 | NumberMillimeter, | ||
| 510 | NumberMile, | ||
| 511 | NumberNauticalMile, | ||
| 512 | NumberYard, | ||
| 513 | NumberInch, | ||
| 514 | NumberMilliWatt, | ||
| 515 | NumberWatt, | ||
| 516 | NumberKiloWatt, | ||
| 517 | NumberMegaWatt, | ||
| 518 | NumberGigaWatt, | ||
| 519 | NumberTeraWatt, | ||
| 520 | NumberAmpere, | ||
| 521 | NumberMilliAmpere, | ||
| 522 | NumberVolt, | ||
| 523 | NumberMilliVolt, | ||
| 524 | NumberMicroVolt, | ||
| 525 | NumberKiloVolt, | ||
| 526 | NumberMegaVolt, | ||
| 527 | NumberBitPerSecond, | ||
| 528 | NumberKiloBitPerSecond, | ||
| 529 | NumberMegaBitPerSecond, | ||
| 530 | NumberGigaBitPerSecond, | ||
| 531 | NumberBytePerSecond, | ||
| 532 | NumberKiloBytePerSecond, | ||
| 533 | NumberMegaBytePerSecond, | ||
| 534 | NumberGigaBytePerSecond, | ||
| 535 | NumberKibiBytePerSecond, | ||
| 536 | NumberMebiBytePerSecond, | ||
| 537 | NumberGibiBytePerSecond, | ||
| 538 | NumberKilogram, | ||
| 539 | NumberGram, | ||
| 540 | NumberMilligram, | ||
| 541 | NumberMicrogram, | ||
| 542 | NumberOunce, | ||
| 543 | NumberPound, | ||
| 544 | NumberStone, | ||
| 545 | NumberPercentage, | ||
| 546 | Other(&'static str), | ||
| 547 | } | ||
| 548 | |||
| 549 | impl Unit { | ||
| 550 | pub fn as_str(&self) -> &'static str { | ||
| 551 | match self { | ||
| 552 | Unit::TemperatureCelcius => crate::constants::HA_UNIT_TEMPERATURE_CELSIUS, | ||
| 553 | Unit::TemperatureKelvin => crate::constants::HA_UNIT_TEMPERATURE_KELVIN, | ||
| 554 | Unit::TemperatureFahrenheit => crate::constants::HA_UNIT_TEMPERATURE_FAHRENHEIT, | ||
| 555 | Unit::HumidityPercentage => crate::constants::HA_UNIT_PERCENTAGE, | ||
| 556 | Unit::BatteryPercentage => crate::constants::HA_UNIT_PERCENTAGE, | ||
| 557 | Unit::LightLux => crate::constants::HA_UNIT_LIGHT_LUX, | ||
| 558 | Unit::PressureHectoPascal => crate::constants::HA_UNIT_PRESSURE_HPA, | ||
| 559 | Unit::EnergyKiloWattHour => crate::constants::HA_UNIT_ENERGY_KWH, | ||
| 560 | Unit::TimeMilliseconds => crate::constants::HA_UNIT_TIME_MILLISECONDS, | ||
| 561 | Unit::TimeSeconds => crate::constants::HA_UNIT_TIME_SECONDS, | ||
| 562 | Unit::TimeMinutes => crate::constants::HA_UNIT_TIME_MINUTES, | ||
| 563 | Unit::TimeHours => crate::constants::HA_UNIT_TIME_HOURS, | ||
| 564 | Unit::TimeDays => crate::constants::HA_UNIT_TIME_DAYS, | ||
| 565 | Unit::PowerWatt => crate::constants::HA_UNIT_POWER_WATT, | ||
| 566 | Unit::PowerKiloWatt => crate::constants::HA_UNIT_POWER_KILOWATT, | ||
| 567 | Unit::VoltageVolt => crate::constants::HA_UNIT_VOLTAGE_VOLT, | ||
| 568 | Unit::CurrentAmpere => crate::constants::HA_UNIT_CURRENT_AMPERE, | ||
| 569 | Unit::DistanceMillimeter => crate::constants::HA_UNIT_DISTANCE_MILLIMETER, | ||
| 570 | Unit::DistanceCentimeter => crate::constants::HA_UNIT_DISTANCE_CENTIMETER, | ||
| 571 | Unit::DistanceMeter => crate::constants::HA_UNIT_DISTANCE_METER, | ||
| 572 | Unit::DistanceKilometer => crate::constants::HA_UNIT_DISTANCE_KILOMETER, | ||
| 573 | Unit::CurrencyUSD => crate::constants::HA_UNIT_CURRENCY_USD, | ||
| 574 | Unit::CurrencyEUR => crate::constants::HA_UNIT_CURRENCY_EUR, | ||
| 575 | Unit::CurrencyGBP => crate::constants::HA_UNIT_CURRENCY_GBP, | ||
| 576 | Unit::CurrencyJPY => crate::constants::HA_UNIT_CURRENCY_JPY, | ||
| 577 | Unit::CurrencyCNY => crate::constants::HA_UNIT_CURRENCY_CNY, | ||
| 578 | Unit::CurrencyCAD => crate::constants::HA_UNIT_CURRENCY_CAD, | ||
| 579 | Unit::CurrencyAUD => crate::constants::HA_UNIT_CURRENCY_AUD, | ||
| 580 | Unit::CurrencyCHF => crate::constants::HA_UNIT_CURRENCY_CHF, | ||
| 581 | Unit::CurrencyINR => crate::constants::HA_UNIT_CURRENCY_INR, | ||
| 582 | Unit::CurrencyBRL => crate::constants::HA_UNIT_CURRENCY_BRL, | ||
| 583 | Unit::CurrencyDollar => crate::constants::HA_UNIT_CURRENCY_DOLLAR, | ||
| 584 | Unit::CurrencyEuro => crate::constants::HA_UNIT_CURRENCY_EURO, | ||
| 585 | Unit::CurrencyPound => crate::constants::HA_UNIT_CURRENCY_POUND, | ||
| 586 | Unit::CurrencyYen => crate::constants::HA_UNIT_CURRENCY_YEN, | ||
| 587 | Unit::CurrencyCent => crate::constants::HA_UNIT_CURRENCY_CENT, | ||
| 588 | // Number units | ||
| 589 | Unit::NumberJoule => crate::constants::HA_UNIT_ENERGY_JOULE, | ||
| 590 | Unit::NumberKiloJoule => crate::constants::HA_UNIT_ENERGY_KILOJOULE, | ||
| 591 | Unit::NumberMegaJoule => crate::constants::HA_UNIT_ENERGY_MEGAJOULE, | ||
| 592 | Unit::NumberGigaJoule => crate::constants::HA_UNIT_ENERGY_GIGAJOULE, | ||
| 593 | Unit::NumberMilliWattHour => crate::constants::HA_UNIT_ENERGY_MILLIWATTHOUR, | ||
| 594 | Unit::NumberWattHour => crate::constants::HA_UNIT_ENERGY_WATTHOUR, | ||
| 595 | Unit::NumberKiloWattHour => crate::constants::HA_UNIT_ENERGY_KWH, | ||
| 596 | Unit::NumberMegaWattHour => crate::constants::HA_UNIT_ENERGY_MEGAWATTHOUR, | ||
| 597 | Unit::NumberGigaWattHour => crate::constants::HA_UNIT_ENERGY_GIGAWATTHOUR, | ||
| 598 | Unit::NumberTeraWattHour => crate::constants::HA_UNIT_ENERGY_TERAWATTHOUR, | ||
| 599 | Unit::NumberCalorie => crate::constants::HA_UNIT_ENERGY_CALORIE, | ||
| 600 | Unit::NumberKiloCalorie => crate::constants::HA_UNIT_ENERGY_KILOCALORIE, | ||
| 601 | Unit::NumberMegaCalorie => crate::constants::HA_UNIT_ENERGY_MEGACALORIE, | ||
| 602 | Unit::NumberGigaCalorie => crate::constants::HA_UNIT_ENERGY_GIGACALORIE, | ||
| 603 | Unit::NumberCelsius => crate::constants::HA_UNIT_TEMPERATURE_CELSIUS, | ||
| 604 | Unit::NumberFahrenheit => crate::constants::HA_UNIT_TEMPERATURE_FAHRENHEIT, | ||
| 605 | Unit::NumberKelvin => crate::constants::HA_UNIT_TEMPERATURE_KELVIN, | ||
| 606 | Unit::NumberMilliPascal => crate::constants::HA_UNIT_PRESSURE_MILLIPASCAL, | ||
| 607 | Unit::NumberPascal => crate::constants::HA_UNIT_PRESSURE_PASCAL, | ||
| 608 | Unit::NumberHectoPascal => crate::constants::HA_UNIT_PRESSURE_HPA, | ||
| 609 | Unit::NumberKiloPascal => crate::constants::HA_UNIT_PRESSURE_KILOPASCAL, | ||
| 610 | Unit::NumberBar => crate::constants::HA_UNIT_PRESSURE_BAR, | ||
| 611 | Unit::NumberCentiBar => crate::constants::HA_UNIT_PRESSURE_CENTIBAR, | ||
| 612 | Unit::NumberMilliBar => crate::constants::HA_UNIT_PRESSURE_MILLIBAR, | ||
| 613 | Unit::NumberMillimeterMercury => crate::constants::HA_UNIT_PRESSURE_MILLIMETER_MERCURY, | ||
| 614 | Unit::NumberInchMercury => crate::constants::HA_UNIT_PRESSURE_INCH_MERCURY, | ||
| 615 | Unit::NumberInchWater => crate::constants::HA_UNIT_PRESSURE_INCH_WATER, | ||
| 616 | Unit::NumberPsi => crate::constants::HA_UNIT_PRESSURE_PSI, | ||
| 617 | Unit::NumberLiter => crate::constants::HA_UNIT_VOLUME_LITER, | ||
| 618 | Unit::NumberMilliLiter => crate::constants::HA_UNIT_VOLUME_MILLILITER, | ||
| 619 | Unit::NumberGallon => crate::constants::HA_UNIT_VOLUME_GALLON, | ||
| 620 | Unit::NumberFluidOunce => crate::constants::HA_UNIT_VOLUME_FLUID_OUNCE, | ||
| 621 | Unit::NumberCubicMeter => crate::constants::HA_UNIT_VOLUME_CUBIC_METER, | ||
| 622 | Unit::NumberCubicFoot => crate::constants::HA_UNIT_VOLUME_CUBIC_FOOT, | ||
| 623 | Unit::NumberCCF => crate::constants::HA_UNIT_VOLUME_CCF, | ||
| 624 | Unit::NumberMCF => crate::constants::HA_UNIT_VOLUME_MCF, | ||
| 625 | Unit::NumberFeetPerSecond => crate::constants::HA_UNIT_SPEED_FEET_PER_SECOND, | ||
| 626 | Unit::NumberInchPerDay => crate::constants::HA_UNIT_SPEED_INCH_PER_DAY, | ||
| 627 | Unit::NumberInchPerHour => crate::constants::HA_UNIT_SPEED_INCH_PER_HOUR, | ||
| 628 | Unit::NumberInchPerSecond => crate::constants::HA_UNIT_SPEED_INCH_PER_SECOND, | ||
| 629 | Unit::NumberKilometerPerHour => crate::constants::HA_UNIT_SPEED_KILOMETER_PER_HOUR, | ||
| 630 | Unit::NumberKnot => crate::constants::HA_UNIT_SPEED_KNOT, | ||
| 631 | Unit::NumberMeterPerSecond => crate::constants::HA_UNIT_SPEED_METER_PER_SECOND, | ||
| 632 | Unit::NumberMilePerHour => crate::constants::HA_UNIT_SPEED_MILE_PER_HOUR, | ||
| 633 | Unit::NumberMillimeterPerDay => crate::constants::HA_UNIT_SPEED_MILLIMETER_PER_DAY, | ||
| 634 | Unit::NumberMillimeterPerSecond => crate::constants::HA_UNIT_SPEED_MILLIMETER_PER_SECOND, | ||
| 635 | Unit::NumberKilometer => crate::constants::HA_UNIT_DISTANCE_KILOMETER, | ||
| 636 | Unit::NumberMeter => crate::constants::HA_UNIT_DISTANCE_METER, | ||
| 637 | Unit::NumberCentimeter => crate::constants::HA_UNIT_DISTANCE_CENTIMETER, | ||
| 638 | Unit::NumberMillimeter => crate::constants::HA_UNIT_DISTANCE_MILLIMETER, | ||
| 639 | Unit::NumberMile => crate::constants::HA_UNIT_DISTANCE_MILE, | ||
| 640 | Unit::NumberNauticalMile => crate::constants::HA_UNIT_DISTANCE_NAUTICAL_MILE, | ||
| 641 | Unit::NumberYard => crate::constants::HA_UNIT_DISTANCE_YARD, | ||
| 642 | Unit::NumberInch => crate::constants::HA_UNIT_DISTANCE_INCH, | ||
| 643 | Unit::NumberMilliWatt => crate::constants::HA_UNIT_POWER_MILLIWATT, | ||
| 644 | Unit::NumberWatt => crate::constants::HA_UNIT_POWER_WATT, | ||
| 645 | Unit::NumberKiloWatt => crate::constants::HA_UNIT_POWER_KILOWATT, | ||
| 646 | Unit::NumberMegaWatt => crate::constants::HA_UNIT_POWER_MEGAWATT, | ||
| 647 | Unit::NumberGigaWatt => crate::constants::HA_UNIT_POWER_GIGAWATT, | ||
| 648 | Unit::NumberTeraWatt => crate::constants::HA_UNIT_POWER_TERAWATT, | ||
| 649 | Unit::NumberAmpere => crate::constants::HA_UNIT_CURRENT_AMPERE, | ||
| 650 | Unit::NumberMilliAmpere => crate::constants::HA_UNIT_CURRENT_MILLIAMPERE, | ||
| 651 | Unit::NumberVolt => crate::constants::HA_UNIT_VOLTAGE_VOLT, | ||
| 652 | Unit::NumberMilliVolt => crate::constants::HA_UNIT_VOLTAGE_MILLIVOLT, | ||
| 653 | Unit::NumberMicroVolt => crate::constants::HA_UNIT_VOLTAGE_MICROVOLT, | ||
| 654 | Unit::NumberKiloVolt => crate::constants::HA_UNIT_VOLTAGE_KILOVOLT, | ||
| 655 | Unit::NumberMegaVolt => crate::constants::HA_UNIT_VOLTAGE_MEGAVOLT, | ||
| 656 | Unit::NumberBitPerSecond => crate::constants::HA_UNIT_DATA_RATE_BIT_PER_SECOND, | ||
| 657 | Unit::NumberKiloBitPerSecond => crate::constants::HA_UNIT_DATA_RATE_KILOBIT_PER_SECOND, | ||
| 658 | Unit::NumberMegaBitPerSecond => crate::constants::HA_UNIT_DATA_RATE_MEGABIT_PER_SECOND, | ||
| 659 | Unit::NumberGigaBitPerSecond => crate::constants::HA_UNIT_DATA_RATE_GIGABIT_PER_SECOND, | ||
| 660 | Unit::NumberBytePerSecond => crate::constants::HA_UNIT_DATA_RATE_BYTE_PER_SECOND, | ||
| 661 | Unit::NumberKiloBytePerSecond => crate::constants::HA_UNIT_DATA_RATE_KILOBYTE_PER_SECOND, | ||
| 662 | Unit::NumberMegaBytePerSecond => crate::constants::HA_UNIT_DATA_RATE_MEGABYTE_PER_SECOND, | ||
| 663 | Unit::NumberGigaBytePerSecond => crate::constants::HA_UNIT_DATA_RATE_GIGABYTE_PER_SECOND, | ||
| 664 | Unit::NumberKibiBytePerSecond => crate::constants::HA_UNIT_DATA_RATE_KIBIBYTE_PER_SECOND, | ||
| 665 | Unit::NumberMebiBytePerSecond => crate::constants::HA_UNIT_DATA_RATE_MEBIBYTE_PER_SECOND, | ||
| 666 | Unit::NumberGibiBytePerSecond => crate::constants::HA_UNIT_DATA_RATE_GIBIBYTE_PER_SECOND, | ||
| 667 | Unit::NumberKilogram => crate::constants::HA_UNIT_WEIGHT_KILOGRAM, | ||
| 668 | Unit::NumberGram => crate::constants::HA_UNIT_WEIGHT_GRAM, | ||
| 669 | Unit::NumberMilligram => crate::constants::HA_UNIT_WEIGHT_MILLIGRAM, | ||
| 670 | Unit::NumberMicrogram => crate::constants::HA_UNIT_WEIGHT_MICROGRAM, | ||
| 671 | Unit::NumberOunce => crate::constants::HA_UNIT_WEIGHT_OUNCE, | ||
| 672 | Unit::NumberPound => crate::constants::HA_UNIT_WEIGHT_POUND, | ||
| 673 | Unit::NumberStone => crate::constants::HA_UNIT_WEIGHT_STONE, | ||
| 674 | Unit::NumberPercentage => crate::constants::HA_UNIT_PERCENTAGE, | ||
| 675 | Unit::Other(other) => other, | ||
| 676 | } | ||
| 677 | } | ||
| 678 | } | ||
| 679 | |||
| 680 | // From conversions | ||
| 681 | impl From<TemperatureUnit> for Unit { | ||
| 682 | fn from(unit: TemperatureUnit) -> Self { | ||
| 683 | match unit { | ||
| 684 | TemperatureUnit::Celcius => Unit::TemperatureCelcius, | ||
| 685 | TemperatureUnit::Kelvin => Unit::TemperatureKelvin, | ||
| 686 | TemperatureUnit::Fahrenheit => Unit::TemperatureFahrenheit, | ||
| 687 | TemperatureUnit::Other(other) => Unit::Other(other), | ||
| 688 | } | ||
| 689 | } | ||
| 690 | } | ||
| 691 | |||
| 692 | impl From<HumidityUnit> for Unit { | ||
| 693 | fn from(unit: HumidityUnit) -> Self { | ||
| 694 | match unit { | ||
| 695 | HumidityUnit::Percentage => Unit::HumidityPercentage, | ||
| 696 | HumidityUnit::Other(other) => Unit::Other(other), | ||
| 697 | } | ||
| 698 | } | ||
| 699 | } | ||
| 700 | |||
| 701 | impl From<BatteryUnit> for Unit { | ||
| 702 | fn from(unit: BatteryUnit) -> Self { | ||
| 703 | match unit { | ||
| 704 | BatteryUnit::Percentage => Unit::BatteryPercentage, | ||
| 705 | } | ||
| 706 | } | ||
| 707 | } | ||
| 708 | |||
| 709 | impl From<LightUnit> for Unit { | ||
| 710 | fn from(unit: LightUnit) -> Self { | ||
| 711 | match unit { | ||
| 712 | LightUnit::Lux => Unit::LightLux, | ||
| 713 | } | ||
| 714 | } | ||
| 715 | } | ||
| 716 | |||
| 717 | impl From<PressureUnit> for Unit { | ||
| 718 | fn from(unit: PressureUnit) -> Self { | ||
| 719 | match unit { | ||
| 720 | PressureUnit::HectoPascal => Unit::PressureHectoPascal, | ||
| 721 | } | ||
| 722 | } | ||
| 723 | } | ||
| 724 | |||
| 725 | impl From<EnergyUnit> for Unit { | ||
| 726 | fn from(unit: EnergyUnit) -> Self { | ||
| 727 | match unit { | ||
| 728 | EnergyUnit::KiloWattHour => Unit::EnergyKiloWattHour, | ||
| 729 | } | ||
| 730 | } | ||
| 731 | } | ||
| 732 | |||
| 733 | impl From<TimeUnit> for Unit { | ||
| 734 | fn from(unit: TimeUnit) -> Self { | ||
| 735 | match unit { | ||
| 736 | TimeUnit::Milliseconds => Unit::TimeMilliseconds, | ||
| 737 | TimeUnit::Seconds => Unit::TimeSeconds, | ||
| 738 | TimeUnit::Minutes => Unit::TimeMinutes, | ||
| 739 | TimeUnit::Hours => Unit::TimeHours, | ||
| 740 | TimeUnit::Days => Unit::TimeDays, | ||
| 741 | } | ||
| 742 | } | ||
| 743 | } | ||
| 744 | |||
| 745 | impl From<PowerUnit> for Unit { | ||
| 746 | fn from(unit: PowerUnit) -> Self { | ||
| 747 | match unit { | ||
| 748 | PowerUnit::Watt => Unit::PowerWatt, | ||
| 749 | PowerUnit::KiloWatt => Unit::PowerKiloWatt, | ||
| 750 | } | ||
| 751 | } | ||
| 752 | } | ||
| 753 | |||
| 754 | impl From<VoltageUnit> for Unit { | ||
| 755 | fn from(unit: VoltageUnit) -> Self { | ||
| 756 | match unit { | ||
| 757 | VoltageUnit::Volt => Unit::VoltageVolt, | ||
| 758 | } | ||
| 759 | } | ||
| 760 | } | ||
| 761 | |||
| 762 | impl From<CurrentUnit> for Unit { | ||
| 763 | fn from(unit: CurrentUnit) -> Self { | ||
| 764 | match unit { | ||
| 765 | CurrentUnit::Ampere => Unit::CurrentAmpere, | ||
| 766 | } | ||
| 767 | } | ||
| 768 | } | ||
| 769 | |||
| 770 | impl From<DistanceUnit> for Unit { | ||
| 771 | fn from(unit: DistanceUnit) -> Self { | ||
| 772 | match unit { | ||
| 773 | DistanceUnit::Millimeter => Unit::DistanceMillimeter, | ||
| 774 | DistanceUnit::Centimeter => Unit::DistanceCentimeter, | ||
| 775 | DistanceUnit::Meter => Unit::DistanceMeter, | ||
| 776 | DistanceUnit::Kilometer => Unit::DistanceKilometer, | ||
| 777 | } | ||
| 778 | } | ||
| 779 | } | ||
| 780 | |||
| 781 | impl From<CurrencyUnit> for Unit { | ||
| 782 | fn from(unit: CurrencyUnit) -> Self { | ||
| 783 | match unit { | ||
| 784 | CurrencyUnit::USD => Unit::CurrencyUSD, | ||
| 785 | CurrencyUnit::EUR => Unit::CurrencyEUR, | ||
| 786 | CurrencyUnit::GBP => Unit::CurrencyGBP, | ||
| 787 | CurrencyUnit::JPY => Unit::CurrencyJPY, | ||
| 788 | CurrencyUnit::CNY => Unit::CurrencyCNY, | ||
| 789 | CurrencyUnit::CAD => Unit::CurrencyCAD, | ||
| 790 | CurrencyUnit::AUD => Unit::CurrencyAUD, | ||
| 791 | CurrencyUnit::CHF => Unit::CurrencyCHF, | ||
| 792 | CurrencyUnit::INR => Unit::CurrencyINR, | ||
| 793 | CurrencyUnit::BRL => Unit::CurrencyBRL, | ||
| 794 | CurrencyUnit::Dollar => Unit::CurrencyDollar, | ||
| 795 | CurrencyUnit::Euro => Unit::CurrencyEuro, | ||
| 796 | CurrencyUnit::Pound => Unit::CurrencyPound, | ||
| 797 | CurrencyUnit::Yen => Unit::CurrencyYen, | ||
| 798 | CurrencyUnit::Cent => Unit::CurrencyCent, | ||
| 799 | CurrencyUnit::Other(other) => Unit::Other(other), | ||
| 800 | } | ||
| 801 | } | ||
| 802 | } | ||
| 803 | |||
| 804 | impl From<NumberUnit> for Unit { | ||
| 805 | fn from(unit: NumberUnit) -> Self { | ||
| 806 | match unit { | ||
| 807 | NumberUnit::Joule => Unit::NumberJoule, | ||
| 808 | NumberUnit::KiloJoule => Unit::NumberKiloJoule, | ||
| 809 | NumberUnit::MegaJoule => Unit::NumberMegaJoule, | ||
| 810 | NumberUnit::GigaJoule => Unit::NumberGigaJoule, | ||
| 811 | NumberUnit::MilliWattHour => Unit::NumberMilliWattHour, | ||
| 812 | NumberUnit::WattHour => Unit::NumberWattHour, | ||
| 813 | NumberUnit::KiloWattHour => Unit::NumberKiloWattHour, | ||
| 814 | NumberUnit::MegaWattHour => Unit::NumberMegaWattHour, | ||
| 815 | NumberUnit::GigaWattHour => Unit::NumberGigaWattHour, | ||
| 816 | NumberUnit::TeraWattHour => Unit::NumberTeraWattHour, | ||
| 817 | NumberUnit::Calorie => Unit::NumberCalorie, | ||
| 818 | NumberUnit::KiloCalorie => Unit::NumberKiloCalorie, | ||
| 819 | NumberUnit::MegaCalorie => Unit::NumberMegaCalorie, | ||
| 820 | NumberUnit::GigaCalorie => Unit::NumberGigaCalorie, | ||
| 821 | NumberUnit::Celsius => Unit::NumberCelsius, | ||
| 822 | NumberUnit::Fahrenheit => Unit::NumberFahrenheit, | ||
| 823 | NumberUnit::Kelvin => Unit::NumberKelvin, | ||
| 824 | NumberUnit::MilliPascal => Unit::NumberMilliPascal, | ||
| 825 | NumberUnit::Pascal => Unit::NumberPascal, | ||
| 826 | NumberUnit::HectoPascal => Unit::NumberHectoPascal, | ||
| 827 | NumberUnit::KiloPascal => Unit::NumberKiloPascal, | ||
| 828 | NumberUnit::Bar => Unit::NumberBar, | ||
| 829 | NumberUnit::CentiBar => Unit::NumberCentiBar, | ||
| 830 | NumberUnit::MilliBar => Unit::NumberMilliBar, | ||
| 831 | NumberUnit::MillimeterMercury => Unit::NumberMillimeterMercury, | ||
| 832 | NumberUnit::InchMercury => Unit::NumberInchMercury, | ||
| 833 | NumberUnit::InchWater => Unit::NumberInchWater, | ||
| 834 | NumberUnit::Psi => Unit::NumberPsi, | ||
| 835 | NumberUnit::Liter => Unit::NumberLiter, | ||
| 836 | NumberUnit::MilliLiter => Unit::NumberMilliLiter, | ||
| 837 | NumberUnit::Gallon => Unit::NumberGallon, | ||
| 838 | NumberUnit::FluidOunce => Unit::NumberFluidOunce, | ||
| 839 | NumberUnit::CubicMeter => Unit::NumberCubicMeter, | ||
| 840 | NumberUnit::CubicFoot => Unit::NumberCubicFoot, | ||
| 841 | NumberUnit::CCF => Unit::NumberCCF, | ||
| 842 | NumberUnit::MCF => Unit::NumberMCF, | ||
| 843 | NumberUnit::FeetPerSecond => Unit::NumberFeetPerSecond, | ||
| 844 | NumberUnit::InchPerDay => Unit::NumberInchPerDay, | ||
| 845 | NumberUnit::InchPerHour => Unit::NumberInchPerHour, | ||
| 846 | NumberUnit::InchPerSecond => Unit::NumberInchPerSecond, | ||
| 847 | NumberUnit::KilometerPerHour => Unit::NumberKilometerPerHour, | ||
| 848 | NumberUnit::Knot => Unit::NumberKnot, | ||
| 849 | NumberUnit::MeterPerSecond => Unit::NumberMeterPerSecond, | ||
| 850 | NumberUnit::MilePerHour => Unit::NumberMilePerHour, | ||
| 851 | NumberUnit::MillimeterPerDay => Unit::NumberMillimeterPerDay, | ||
| 852 | NumberUnit::MillimeterPerSecond => Unit::NumberMillimeterPerSecond, | ||
| 853 | NumberUnit::Kilometer => Unit::NumberKilometer, | ||
| 854 | NumberUnit::Meter => Unit::NumberMeter, | ||
| 855 | NumberUnit::Centimeter => Unit::NumberCentimeter, | ||
| 856 | NumberUnit::Millimeter => Unit::NumberMillimeter, | ||
| 857 | NumberUnit::Mile => Unit::NumberMile, | ||
| 858 | NumberUnit::NauticalMile => Unit::NumberNauticalMile, | ||
| 859 | NumberUnit::Yard => Unit::NumberYard, | ||
| 860 | NumberUnit::Inch => Unit::NumberInch, | ||
| 861 | NumberUnit::MilliWatt => Unit::NumberMilliWatt, | ||
| 862 | NumberUnit::Watt => Unit::NumberWatt, | ||
| 863 | NumberUnit::KiloWatt => Unit::NumberKiloWatt, | ||
| 864 | NumberUnit::MegaWatt => Unit::NumberMegaWatt, | ||
| 865 | NumberUnit::GigaWatt => Unit::NumberGigaWatt, | ||
| 866 | NumberUnit::TeraWatt => Unit::NumberTeraWatt, | ||
| 867 | NumberUnit::Ampere => Unit::NumberAmpere, | ||
| 868 | NumberUnit::MilliAmpere => Unit::NumberMilliAmpere, | ||
| 869 | NumberUnit::Volt => Unit::NumberVolt, | ||
| 870 | NumberUnit::MilliVolt => Unit::NumberMilliVolt, | ||
| 871 | NumberUnit::MicroVolt => Unit::NumberMicroVolt, | ||
| 872 | NumberUnit::KiloVolt => Unit::NumberKiloVolt, | ||
| 873 | NumberUnit::MegaVolt => Unit::NumberMegaVolt, | ||
| 874 | NumberUnit::BitPerSecond => Unit::NumberBitPerSecond, | ||
| 875 | NumberUnit::KiloBitPerSecond => Unit::NumberKiloBitPerSecond, | ||
| 876 | NumberUnit::MegaBitPerSecond => Unit::NumberMegaBitPerSecond, | ||
| 877 | NumberUnit::GigaBitPerSecond => Unit::NumberGigaBitPerSecond, | ||
| 878 | NumberUnit::BytePerSecond => Unit::NumberBytePerSecond, | ||
| 879 | NumberUnit::KiloBytePerSecond => Unit::NumberKiloBytePerSecond, | ||
| 880 | NumberUnit::MegaBytePerSecond => Unit::NumberMegaBytePerSecond, | ||
| 881 | NumberUnit::GigaBytePerSecond => Unit::NumberGigaBytePerSecond, | ||
| 882 | NumberUnit::KibiBytePerSecond => Unit::NumberKibiBytePerSecond, | ||
| 883 | NumberUnit::MebiBytePerSecond => Unit::NumberMebiBytePerSecond, | ||
| 884 | NumberUnit::GibiBytePerSecond => Unit::NumberGibiBytePerSecond, | ||
| 885 | NumberUnit::Kilogram => Unit::NumberKilogram, | ||
| 886 | NumberUnit::Gram => Unit::NumberGram, | ||
| 887 | NumberUnit::Milligram => Unit::NumberMilligram, | ||
| 888 | NumberUnit::Microgram => Unit::NumberMicrogram, | ||
| 889 | NumberUnit::Ounce => Unit::NumberOunce, | ||
| 890 | NumberUnit::Pound => Unit::NumberPound, | ||
| 891 | NumberUnit::Stone => Unit::NumberStone, | ||
| 892 | NumberUnit::Percentage => Unit::NumberPercentage, | ||
| 893 | NumberUnit::Other(other) => Unit::Other(other), | ||
| 894 | } | ||
| 895 | } | ||
| 896 | } | ||
| 897 | |||
| 898 | // TryFrom conversions | ||
| 899 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
| 900 | pub struct UnitConversionError; | ||
| 901 | |||
| 902 | impl TryFrom<Unit> for TemperatureUnit { | ||
| 903 | type Error = UnitConversionError; | ||
| 904 | |||
| 905 | fn try_from(unit: Unit) -> Result<Self, Self::Error> { | ||
| 906 | match unit { | ||
| 907 | Unit::TemperatureCelcius => Ok(TemperatureUnit::Celcius), | ||
| 908 | Unit::TemperatureKelvin => Ok(TemperatureUnit::Kelvin), | ||
| 909 | Unit::TemperatureFahrenheit => Ok(TemperatureUnit::Fahrenheit), | ||
| 910 | Unit::Other(other) => Ok(TemperatureUnit::Other(other)), | ||
| 911 | _ => Err(UnitConversionError), | ||
| 912 | } | ||
| 913 | } | ||
| 914 | } | ||
| 915 | |||
| 916 | impl TryFrom<Unit> for HumidityUnit { | ||
| 917 | type Error = UnitConversionError; | ||
| 918 | |||
| 919 | fn try_from(unit: Unit) -> Result<Self, Self::Error> { | ||
| 920 | match unit { | ||
| 921 | Unit::HumidityPercentage => Ok(HumidityUnit::Percentage), | ||
| 922 | Unit::Other(other) => Ok(HumidityUnit::Other(other)), | ||
| 923 | _ => Err(UnitConversionError), | ||
| 924 | } | ||
| 925 | } | ||
| 926 | } | ||
| 927 | |||
| 928 | impl TryFrom<Unit> for BatteryUnit { | ||
| 929 | type Error = UnitConversionError; | ||
| 930 | |||
| 931 | fn try_from(unit: Unit) -> Result<Self, Self::Error> { | ||
| 932 | match unit { | ||
| 933 | Unit::BatteryPercentage => Ok(BatteryUnit::Percentage), | ||
| 934 | _ => Err(UnitConversionError), | ||
| 935 | } | ||
| 936 | } | ||
| 937 | } | ||
| 938 | |||
| 939 | impl TryFrom<Unit> for LightUnit { | ||
| 940 | type Error = UnitConversionError; | ||
| 941 | |||
| 942 | fn try_from(unit: Unit) -> Result<Self, Self::Error> { | ||
| 943 | match unit { | ||
| 944 | Unit::LightLux => Ok(LightUnit::Lux), | ||
| 945 | _ => Err(UnitConversionError), | ||
| 946 | } | ||
| 947 | } | ||
| 948 | } | ||
| 949 | |||
| 950 | impl TryFrom<Unit> for PressureUnit { | ||
| 951 | type Error = UnitConversionError; | ||
| 952 | |||
| 953 | fn try_from(unit: Unit) -> Result<Self, Self::Error> { | ||
| 954 | match unit { | ||
| 955 | Unit::PressureHectoPascal => Ok(PressureUnit::HectoPascal), | ||
| 956 | _ => Err(UnitConversionError), | ||
| 957 | } | ||
| 958 | } | ||
| 959 | } | ||
| 960 | |||
| 961 | impl TryFrom<Unit> for EnergyUnit { | ||
| 962 | type Error = UnitConversionError; | ||
| 963 | |||
| 964 | fn try_from(unit: Unit) -> Result<Self, Self::Error> { | ||
| 965 | match unit { | ||
| 966 | Unit::EnergyKiloWattHour => Ok(EnergyUnit::KiloWattHour), | ||
| 967 | _ => Err(UnitConversionError), | ||
| 968 | } | ||
| 969 | } | ||
| 970 | } | ||
| 971 | |||
| 972 | impl TryFrom<Unit> for TimeUnit { | ||
| 973 | type Error = UnitConversionError; | ||
| 974 | |||
| 975 | fn try_from(unit: Unit) -> Result<Self, Self::Error> { | ||
| 976 | match unit { | ||
| 977 | Unit::TimeMilliseconds => Ok(TimeUnit::Milliseconds), | ||
| 978 | Unit::TimeSeconds => Ok(TimeUnit::Seconds), | ||
| 979 | Unit::TimeMinutes => Ok(TimeUnit::Minutes), | ||
| 980 | Unit::TimeHours => Ok(TimeUnit::Hours), | ||
| 981 | Unit::TimeDays => Ok(TimeUnit::Days), | ||
| 982 | _ => Err(UnitConversionError), | ||
| 983 | } | ||
| 984 | } | ||
| 985 | } | ||
| 986 | |||
| 987 | impl TryFrom<Unit> for PowerUnit { | ||
| 988 | type Error = UnitConversionError; | ||
| 989 | |||
| 990 | fn try_from(unit: Unit) -> Result<Self, Self::Error> { | ||
| 991 | match unit { | ||
| 992 | Unit::PowerWatt => Ok(PowerUnit::Watt), | ||
| 993 | Unit::PowerKiloWatt => Ok(PowerUnit::KiloWatt), | ||
| 994 | _ => Err(UnitConversionError), | ||
| 995 | } | ||
| 996 | } | ||
| 997 | } | ||
| 998 | |||
| 999 | impl TryFrom<Unit> for VoltageUnit { | ||
| 1000 | type Error = UnitConversionError; | ||
| 1001 | |||
| 1002 | fn try_from(unit: Unit) -> Result<Self, Self::Error> { | ||
| 1003 | match unit { | ||
| 1004 | Unit::VoltageVolt => Ok(VoltageUnit::Volt), | ||
| 1005 | _ => Err(UnitConversionError), | ||
| 1006 | } | ||
| 1007 | } | ||
| 1008 | } | ||
| 1009 | |||
| 1010 | impl TryFrom<Unit> for CurrentUnit { | ||
| 1011 | type Error = UnitConversionError; | ||
| 1012 | |||
| 1013 | fn try_from(unit: Unit) -> Result<Self, Self::Error> { | ||
| 1014 | match unit { | ||
| 1015 | Unit::CurrentAmpere => Ok(CurrentUnit::Ampere), | ||
| 1016 | _ => Err(UnitConversionError), | ||
| 1017 | } | ||
| 1018 | } | ||
| 1019 | } | ||
| 1020 | |||
| 1021 | impl TryFrom<Unit> for DistanceUnit { | ||
| 1022 | type Error = UnitConversionError; | ||
| 1023 | |||
| 1024 | fn try_from(unit: Unit) -> Result<Self, Self::Error> { | ||
| 1025 | match unit { | ||
| 1026 | Unit::DistanceMillimeter => Ok(DistanceUnit::Millimeter), | ||
| 1027 | Unit::DistanceCentimeter => Ok(DistanceUnit::Centimeter), | ||
| 1028 | Unit::DistanceMeter => Ok(DistanceUnit::Meter), | ||
| 1029 | Unit::DistanceKilometer => Ok(DistanceUnit::Kilometer), | ||
| 1030 | _ => Err(UnitConversionError), | ||
| 1031 | } | ||
| 1032 | } | ||
| 1033 | } | ||
| 1034 | |||
| 1035 | impl TryFrom<Unit> for CurrencyUnit { | ||
| 1036 | type Error = UnitConversionError; | ||
| 1037 | |||
| 1038 | fn try_from(unit: Unit) -> Result<Self, Self::Error> { | ||
| 1039 | match unit { | ||
| 1040 | Unit::CurrencyUSD => Ok(CurrencyUnit::USD), | ||
| 1041 | Unit::CurrencyEUR => Ok(CurrencyUnit::EUR), | ||
| 1042 | Unit::CurrencyGBP => Ok(CurrencyUnit::GBP), | ||
| 1043 | Unit::CurrencyJPY => Ok(CurrencyUnit::JPY), | ||
| 1044 | Unit::CurrencyCNY => Ok(CurrencyUnit::CNY), | ||
| 1045 | Unit::CurrencyCAD => Ok(CurrencyUnit::CAD), | ||
| 1046 | Unit::CurrencyAUD => Ok(CurrencyUnit::AUD), | ||
| 1047 | Unit::CurrencyCHF => Ok(CurrencyUnit::CHF), | ||
| 1048 | Unit::CurrencyINR => Ok(CurrencyUnit::INR), | ||
| 1049 | Unit::CurrencyBRL => Ok(CurrencyUnit::BRL), | ||
| 1050 | Unit::CurrencyDollar => Ok(CurrencyUnit::Dollar), | ||
| 1051 | Unit::CurrencyEuro => Ok(CurrencyUnit::Euro), | ||
| 1052 | Unit::CurrencyPound => Ok(CurrencyUnit::Pound), | ||
| 1053 | Unit::CurrencyYen => Ok(CurrencyUnit::Yen), | ||
| 1054 | Unit::CurrencyCent => Ok(CurrencyUnit::Cent), | ||
| 1055 | Unit::Other(other) => Ok(CurrencyUnit::Other(other)), | ||
| 1056 | _ => Err(UnitConversionError), | ||
| 1057 | } | ||
| 1058 | } | ||
| 1059 | } | ||
| 1060 | |||
| 1061 | impl TryFrom<Unit> for NumberUnit { | ||
| 1062 | type Error = UnitConversionError; | ||
| 1063 | |||
| 1064 | fn try_from(unit: Unit) -> Result<Self, Self::Error> { | ||
| 1065 | match unit { | ||
| 1066 | Unit::NumberJoule => Ok(NumberUnit::Joule), | ||
| 1067 | Unit::NumberKiloJoule => Ok(NumberUnit::KiloJoule), | ||
| 1068 | Unit::NumberMegaJoule => Ok(NumberUnit::MegaJoule), | ||
| 1069 | Unit::NumberGigaJoule => Ok(NumberUnit::GigaJoule), | ||
| 1070 | Unit::NumberMilliWattHour => Ok(NumberUnit::MilliWattHour), | ||
| 1071 | Unit::NumberWattHour => Ok(NumberUnit::WattHour), | ||
| 1072 | Unit::NumberKiloWattHour => Ok(NumberUnit::KiloWattHour), | ||
| 1073 | Unit::NumberMegaWattHour => Ok(NumberUnit::MegaWattHour), | ||
| 1074 | Unit::NumberGigaWattHour => Ok(NumberUnit::GigaWattHour), | ||
| 1075 | Unit::NumberTeraWattHour => Ok(NumberUnit::TeraWattHour), | ||
| 1076 | Unit::NumberCalorie => Ok(NumberUnit::Calorie), | ||
| 1077 | Unit::NumberKiloCalorie => Ok(NumberUnit::KiloCalorie), | ||
| 1078 | Unit::NumberMegaCalorie => Ok(NumberUnit::MegaCalorie), | ||
| 1079 | Unit::NumberGigaCalorie => Ok(NumberUnit::GigaCalorie), | ||
| 1080 | Unit::NumberCelsius => Ok(NumberUnit::Celsius), | ||
| 1081 | Unit::NumberFahrenheit => Ok(NumberUnit::Fahrenheit), | ||
| 1082 | Unit::NumberKelvin => Ok(NumberUnit::Kelvin), | ||
| 1083 | Unit::NumberMilliPascal => Ok(NumberUnit::MilliPascal), | ||
| 1084 | Unit::NumberPascal => Ok(NumberUnit::Pascal), | ||
| 1085 | Unit::NumberHectoPascal => Ok(NumberUnit::HectoPascal), | ||
| 1086 | Unit::NumberKiloPascal => Ok(NumberUnit::KiloPascal), | ||
| 1087 | Unit::NumberBar => Ok(NumberUnit::Bar), | ||
| 1088 | Unit::NumberCentiBar => Ok(NumberUnit::CentiBar), | ||
| 1089 | Unit::NumberMilliBar => Ok(NumberUnit::MilliBar), | ||
| 1090 | Unit::NumberMillimeterMercury => Ok(NumberUnit::MillimeterMercury), | ||
| 1091 | Unit::NumberInchMercury => Ok(NumberUnit::InchMercury), | ||
| 1092 | Unit::NumberInchWater => Ok(NumberUnit::InchWater), | ||
| 1093 | Unit::NumberPsi => Ok(NumberUnit::Psi), | ||
| 1094 | Unit::NumberLiter => Ok(NumberUnit::Liter), | ||
| 1095 | Unit::NumberMilliLiter => Ok(NumberUnit::MilliLiter), | ||
| 1096 | Unit::NumberGallon => Ok(NumberUnit::Gallon), | ||
| 1097 | Unit::NumberFluidOunce => Ok(NumberUnit::FluidOunce), | ||
| 1098 | Unit::NumberCubicMeter => Ok(NumberUnit::CubicMeter), | ||
| 1099 | Unit::NumberCubicFoot => Ok(NumberUnit::CubicFoot), | ||
| 1100 | Unit::NumberCCF => Ok(NumberUnit::CCF), | ||
| 1101 | Unit::NumberMCF => Ok(NumberUnit::MCF), | ||
| 1102 | Unit::NumberFeetPerSecond => Ok(NumberUnit::FeetPerSecond), | ||
| 1103 | Unit::NumberInchPerDay => Ok(NumberUnit::InchPerDay), | ||
| 1104 | Unit::NumberInchPerHour => Ok(NumberUnit::InchPerHour), | ||
| 1105 | Unit::NumberInchPerSecond => Ok(NumberUnit::InchPerSecond), | ||
| 1106 | Unit::NumberKilometerPerHour => Ok(NumberUnit::KilometerPerHour), | ||
| 1107 | Unit::NumberKnot => Ok(NumberUnit::Knot), | ||
| 1108 | Unit::NumberMeterPerSecond => Ok(NumberUnit::MeterPerSecond), | ||
| 1109 | Unit::NumberMilePerHour => Ok(NumberUnit::MilePerHour), | ||
| 1110 | Unit::NumberMillimeterPerDay => Ok(NumberUnit::MillimeterPerDay), | ||
| 1111 | Unit::NumberMillimeterPerSecond => Ok(NumberUnit::MillimeterPerSecond), | ||
| 1112 | Unit::NumberKilometer => Ok(NumberUnit::Kilometer), | ||
| 1113 | Unit::NumberMeter => Ok(NumberUnit::Meter), | ||
| 1114 | Unit::NumberCentimeter => Ok(NumberUnit::Centimeter), | ||
| 1115 | Unit::NumberMillimeter => Ok(NumberUnit::Millimeter), | ||
| 1116 | Unit::NumberMile => Ok(NumberUnit::Mile), | ||
| 1117 | Unit::NumberNauticalMile => Ok(NumberUnit::NauticalMile), | ||
| 1118 | Unit::NumberYard => Ok(NumberUnit::Yard), | ||
| 1119 | Unit::NumberInch => Ok(NumberUnit::Inch), | ||
| 1120 | Unit::NumberMilliWatt => Ok(NumberUnit::MilliWatt), | ||
| 1121 | Unit::NumberWatt => Ok(NumberUnit::Watt), | ||
| 1122 | Unit::NumberKiloWatt => Ok(NumberUnit::KiloWatt), | ||
| 1123 | Unit::NumberMegaWatt => Ok(NumberUnit::MegaWatt), | ||
| 1124 | Unit::NumberGigaWatt => Ok(NumberUnit::GigaWatt), | ||
| 1125 | Unit::NumberTeraWatt => Ok(NumberUnit::TeraWatt), | ||
| 1126 | Unit::NumberAmpere => Ok(NumberUnit::Ampere), | ||
| 1127 | Unit::NumberMilliAmpere => Ok(NumberUnit::MilliAmpere), | ||
| 1128 | Unit::NumberVolt => Ok(NumberUnit::Volt), | ||
| 1129 | Unit::NumberMilliVolt => Ok(NumberUnit::MilliVolt), | ||
| 1130 | Unit::NumberMicroVolt => Ok(NumberUnit::MicroVolt), | ||
| 1131 | Unit::NumberKiloVolt => Ok(NumberUnit::KiloVolt), | ||
| 1132 | Unit::NumberMegaVolt => Ok(NumberUnit::MegaVolt), | ||
| 1133 | Unit::NumberBitPerSecond => Ok(NumberUnit::BitPerSecond), | ||
| 1134 | Unit::NumberKiloBitPerSecond => Ok(NumberUnit::KiloBitPerSecond), | ||
| 1135 | Unit::NumberMegaBitPerSecond => Ok(NumberUnit::MegaBitPerSecond), | ||
| 1136 | Unit::NumberGigaBitPerSecond => Ok(NumberUnit::GigaBitPerSecond), | ||
| 1137 | Unit::NumberBytePerSecond => Ok(NumberUnit::BytePerSecond), | ||
| 1138 | Unit::NumberKiloBytePerSecond => Ok(NumberUnit::KiloBytePerSecond), | ||
| 1139 | Unit::NumberMegaBytePerSecond => Ok(NumberUnit::MegaBytePerSecond), | ||
| 1140 | Unit::NumberGigaBytePerSecond => Ok(NumberUnit::GigaBytePerSecond), | ||
| 1141 | Unit::NumberKibiBytePerSecond => Ok(NumberUnit::KibiBytePerSecond), | ||
| 1142 | Unit::NumberMebiBytePerSecond => Ok(NumberUnit::MebiBytePerSecond), | ||
| 1143 | Unit::NumberGibiBytePerSecond => Ok(NumberUnit::GibiBytePerSecond), | ||
| 1144 | Unit::NumberKilogram => Ok(NumberUnit::Kilogram), | ||
| 1145 | Unit::NumberGram => Ok(NumberUnit::Gram), | ||
| 1146 | Unit::NumberMilligram => Ok(NumberUnit::Milligram), | ||
| 1147 | Unit::NumberMicrogram => Ok(NumberUnit::Microgram), | ||
| 1148 | Unit::NumberOunce => Ok(NumberUnit::Ounce), | ||
| 1149 | Unit::NumberPound => Ok(NumberUnit::Pound), | ||
| 1150 | Unit::NumberStone => Ok(NumberUnit::Stone), | ||
| 1151 | Unit::NumberPercentage => Ok(NumberUnit::Percentage), | ||
| 1152 | Unit::Other(other) => Ok(NumberUnit::Other(other)), | ||
| 1153 | _ => Err(UnitConversionError), | ||
| 1154 | } | ||
| 1155 | } | ||
| 1156 | } | ||
