Skip to content

Commit

Permalink
feat(mqtt): Add device_class attribute to battery and WiFi sensors fo…
Browse files Browse the repository at this point in the history
…r Home Assistant (#1910)

* Add DeviceClass enum

* Add device_class to Wifi sensor

* Add device class to battery sensor
  • Loading branch information
mill1000 authored Oct 26, 2023
1 parent 744fe20 commit aac0f51
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const CapabilityMqttHandle = require("./CapabilityMqttHandle");
const ComponentType = require("../homeassistant/ComponentType");
const DataType = require("../homie/DataType");
const DeviceClass = require("../homeassistant/DeviceClass");
const EntityCategory = require("../homeassistant/EntityCategory");
const HassAnchor = require("../homeassistant/HassAnchor");
const InLineHassComponent = require("../homeassistant/components/InLineHassComponent");
Expand Down Expand Up @@ -96,7 +97,8 @@ class WifiConfigurationCapabilityMqttHandle extends CapabilityMqttHandle {
HassAnchor.REFERENCE.HASS_WIFI_CONFIG_ATTRS
),
json_attributes_template: "{{ value_json.attributes | to_json }}",
entity_category: EntityCategory.DIAGNOSTIC
entity_category: EntityCategory.DIAGNOSTIC,
device_class: DeviceClass.SIGNAL_STRENGTH
},
topics: {
"": {
Expand Down
61 changes: 61 additions & 0 deletions backend/lib/mqtt/homeassistant/DeviceClass.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* Retrieved from https://github.com/home-assistant/core/blob/8b1cfbc46cc79e676f75dfa4da097a2e47375b6f/homeassistant/components/sensor/const.py#L64-L416 on 2023-10-25.
*
* See also https://developers.home-assistant.io/docs/core/entity/#generic-properties
*
* @enum {string}
*/
const DeviceClass = Object.freeze({
DATE: "date",
ENUM: "enum",
TIMESTAMP: "timestamp",
APPARENT_POWER: "apparent_power",
AQI: "aqi",
ATMOSPHERIC_PRESSURE: "atmospheric_pressure",
BATTERY: "battery",
CO: "carbon_monoxide",
CO2: "carbon_dioxide",
CURRENT: "current",
DATA_RATE: "data_rate",
DATA_SIZE: "data_size",
DISTANCE: "distance",
DURATION: "duration",
ENERGY: "energy",
ENERGY_STORAGE: "energy_storage",
FREQUENCY: "frequency",
GAS: "gas",
HUMIDITY: "humidity",
ILLUMINANCE: "illuminance",
IRRADIANCE: "irradiance",
MOISTURE: "moisture",
MONETARY: "monetary",
NITROGEN_DIOXIDE: "nitrogen_dioxide",
NITROGEN_MONOXIDE: "nitrogen_monoxide",
NITROUS_OXIDE: "nitrous_oxide",
OZONE: "ozone",
PH: "ph",
PM1: "pm1",
PM10: "pm10",
PM25: "pm25",
POWER_FACTOR: "power_factor",
POWER: "power",
PRECIPITATION: "precipitation",
PRECIPITATION_INTENSITY: "precipitation_intensity",
PRESSURE: "pressure",
REACTIVE_POWER: "reactive_power",
SIGNAL_STRENGTH: "signal_strength",
SOUND_PRESSURE: "sound_pressure",
SPEED: "speed",
SULPHUR_DIOXIDE: "sulphur_dioxide",
TEMPERATURE: "temperature",
VOLATILE_ORGANIC_COMPOUNDS: "volatile_organic_compounds",
VOLATILE_ORGANIC_COMPOUNDS_PARTS: "volatile_organic_compounds_parts",
VOLTAGE: "voltage",
VOLUME: "volume",
VOLUME_STORAGE: "volume_storage",
WATER: "water",
WEIGHT: "weight",
WIND_SPEED: "wind_speed"
});

module.exports = DeviceClass;
4 changes: 3 additions & 1 deletion backend/lib/mqtt/status/BatteryStateMqttHandle.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const ComponentType = require("../homeassistant/ComponentType");
const DataType = require("../homie/DataType");
const DeviceClass = require("../homeassistant/DeviceClass");
const EntityCategory = require("../homeassistant/EntityCategory");
const HassAnchor = require("../homeassistant/HassAnchor");
const InLineHassComponent = require("../homeassistant/components/InLineHassComponent");
Expand Down Expand Up @@ -57,7 +58,8 @@ class BatteryStateMqttHandle extends RobotStateNodeMqttHandle {
state_topic: prop.getBaseTopic(),
icon: "mdi:battery",
entity_category: EntityCategory.DIAGNOSTIC,
unit_of_measurement: Unit.PERCENT
unit_of_measurement: Unit.PERCENT,
device_class: DeviceClass.BATTERY
}
})
);
Expand Down

0 comments on commit aac0f51

Please sign in to comment.