Add Home Assistant sensor state_class support (#7677)

This commit is contained in:
Franck Nijhof 2021-06-04 11:41:42 +02:00 committed by GitHub
parent 269dac2bf8
commit 51fd598e32
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 26 deletions

View File

@ -354,35 +354,35 @@ class HomeAssistant extends Extension {
};
} else if (expose.type === 'numeric') {
const lookup = {
battery: {device_class: 'battery'},
temperature: {device_class: 'temperature'},
humidity: {device_class: 'humidity'},
illuminance_lux: {device_class: 'illuminance'},
illuminance: {device_class: 'illuminance'},
soil_moisture: {icon: 'mdi:water-percent'},
position: {icon: 'mdi:valve'},
pressure: {device_class: 'pressure'},
power: {device_class: 'power'},
linkquality: {icon: 'mdi:signal'},
current: {device_class: 'current'},
voltage: {device_class: 'voltage'},
current_phase_b: {device_class: 'current'},
voltage_phase_b: {device_class: 'voltage'},
current_phase_c: {device_class: 'current'},
voltage_phase_c: {device_class: 'voltage'},
battery: {device_class: 'battery', state_class: 'measurement'},
temperature: {device_class: 'temperature', state_class: 'measurement'},
humidity: {device_class: 'humidity', state_class: 'measurement'},
illuminance_lux: {device_class: 'illuminance', state_class: 'measurement'},
illuminance: {device_class: 'illuminance', state_class: 'measurement'},
soil_moisture: {icon: 'mdi:water-percent', state_class: 'measurement'},
position: {icon: 'mdi:valve', state_class: 'measurement'},
pressure: {device_class: 'pressure', state_class: 'measurement'},
power: {device_class: 'power', state_class: 'measurement'},
linkquality: {icon: 'mdi:signal', state_class: 'measurement'},
current: {device_class: 'current', state_class: 'measurement'},
voltage: {device_class: 'voltage', state_class: 'measurement'},
current_phase_b: {device_class: 'current', state_class: 'measurement'},
voltage_phase_b: {device_class: 'voltage', state_class: 'measurement'},
current_phase_c: {device_class: 'current', state_class: 'measurement'},
voltage_phase_c: {device_class: 'voltage', state_class: 'measurement'},
energy: {device_class: 'energy'},
smoke_density: {icon: 'mdi:google-circles-communities'},
gas_density: {icon: 'mdi:google-circles-communities'},
pm25: {icon: 'mdi:air-filter'},
pm10: {icon: 'mdi:air-filter'},
voc: {icon: 'mdi:air-filter'},
aqi: {icon: 'mdi:air-filter'},
hcho: {icon: 'mdi:air-filter'},
smoke_density: {icon: 'mdi:google-circles-communities', state_class: 'measurement'},
gas_density: {icon: 'mdi:google-circles-communities', state_class: 'measurement'},
pm25: {icon: 'mdi:air-filter', state_class: 'measurement'},
pm10: {icon: 'mdi:air-filter', state_class: 'measurement'},
voc: {icon: 'mdi:air-filter', state_class: 'measurement'},
aqi: {icon: 'mdi:air-filter', state_class: 'measurement'},
hcho: {icon: 'mdi:air-filter', state_class: 'measurement'},
requested_brightness_level: {icon: 'mdi:brightness-5'},
requested_brightness_percent: {icon: 'mdi:brightness-5'},
eco2: {icon: 'mdi:molecule-co2'},
co2: {icon: 'mdi:molecule-co2'},
local_temperature: {device_class: 'temperature'},
eco2: {icon: 'mdi:molecule-co2', state_class: 'measurement'},
co2: {icon: 'mdi:molecule-co2', state_class: 'measurement'},
local_temperature: {device_class: 'temperature', state_class: 'measurement'},
x_axis: {icon: 'mdi:axis-x-arrow'},
y_axis: {icon: 'mdi:axis-y-arrow'},
z_axis: {icon: 'mdi:axis-z-arrow'},

View File

@ -54,6 +54,7 @@ describe('HomeAssistant extension', () => {
payload = {
'unit_of_measurement': '°C',
'device_class': 'temperature',
'state_class': 'measurement',
'value_template': '{{ value_json.temperature }}',
'state_topic': 'zigbee2mqtt/weather_sensor',
'json_attributes_topic': 'zigbee2mqtt/weather_sensor',
@ -79,6 +80,7 @@ describe('HomeAssistant extension', () => {
payload = {
'unit_of_measurement': '%',
'device_class': 'humidity',
'state_class': 'measurement',
'value_template': '{{ value_json.humidity }}',
'state_topic': 'zigbee2mqtt/weather_sensor',
'json_attributes_topic': 'zigbee2mqtt/weather_sensor',
@ -104,6 +106,7 @@ describe('HomeAssistant extension', () => {
payload = {
'unit_of_measurement': 'hPa',
'device_class': 'pressure',
'state_class': 'measurement',
'value_template': '{{ value_json.pressure }}',
'state_topic': 'zigbee2mqtt/weather_sensor',
'json_attributes_topic': 'zigbee2mqtt/weather_sensor',
@ -129,6 +132,7 @@ describe('HomeAssistant extension', () => {
payload = {
'unit_of_measurement': '%',
'device_class': 'battery',
'state_class': 'measurement',
'value_template': '{{ value_json.battery }}',
'state_topic': 'zigbee2mqtt/weather_sensor',
'json_attributes_topic': 'zigbee2mqtt/weather_sensor',
@ -154,6 +158,7 @@ describe('HomeAssistant extension', () => {
payload = {
'icon': 'mdi:signal',
'unit_of_measurement': 'lqi',
'state_class': 'measurement',
'value_template': '{{ value_json.linkquality }}',
'state_topic': 'zigbee2mqtt/weather_sensor',
'json_attributes_topic': 'zigbee2mqtt/weather_sensor',
@ -304,6 +309,7 @@ describe('HomeAssistant extension', () => {
payload = {
'unit_of_measurement': '°C',
'device_class': 'temperature',
'state_class': 'measurement',
'value_template': "{{ value_json.temperature }}",
'state_topic': 'zigbee2mqtt/weather_sensor',
'json_attributes_topic': 'zigbee2mqtt/weather_sensor',
@ -329,6 +335,7 @@ describe('HomeAssistant extension', () => {
payload = {
'unit_of_measurement': '%',
'device_class': 'humidity',
'state_class': 'measurement',
'value_template': '{{ value_json.humidity }}',
'state_topic': 'zigbee2mqtt/weather_sensor',
'json_attributes_topic': 'zigbee2mqtt/weather_sensor',
@ -354,6 +361,7 @@ describe('HomeAssistant extension', () => {
payload = {
'unit_of_measurement': 'hPa',
'device_class': 'pressure',
'state_class': 'measurement',
'value_template': '{{ value_json.pressure }}',
'state_topic': 'zigbee2mqtt/weather_sensor',
'json_attributes_topic': 'zigbee2mqtt/weather_sensor',
@ -410,6 +418,7 @@ describe('HomeAssistant extension', () => {
payload = {
'unit_of_measurement': '°C',
'device_class': 'temperature',
'state_class': 'measurement',
'value_template': '{{ value_json.temperature }}',
'state_topic': 'zigbee2mqtt/weather_sensor',
'json_attributes_topic': 'zigbee2mqtt/weather_sensor',
@ -437,6 +446,7 @@ describe('HomeAssistant extension', () => {
payload = {
'unit_of_measurement': '%',
'device_class': 'humidity',
'state_class': 'measurement',
'value_template': '{{ value_json.humidity }}',
'state_topic': 'zigbee2mqtt/weather_sensor',
'json_attributes_topic': 'zigbee2mqtt/weather_sensor',
@ -698,6 +708,7 @@ describe('HomeAssistant extension', () => {
payload = {
'unit_of_measurement': '°C',
'device_class': 'temperature',
'state_class': 'measurement',
'value_template': '{{ value_json.temperature }}',
'state_topic': 'zigbee2mqtt/weather_sensor',
'json_attributes_topic': 'zigbee2mqtt/weather_sensor',
@ -840,6 +851,7 @@ describe('HomeAssistant extension', () => {
const payloadHA = {
'unit_of_measurement': '°C',
'device_class': 'temperature',
'state_class': 'measurement',
'value_template': '{{ value_json.temperature }}',
'state_topic': 'zigbee2mqtt/weather_sensor',
'json_attributes_topic': 'zigbee2mqtt/weather_sensor',
@ -966,6 +978,7 @@ describe('HomeAssistant extension', () => {
payload = {
'unit_of_measurement': '°C',
'device_class': 'temperature',
'state_class': 'measurement',
'value_template': '{{ value_json.temperature }}',
'state_topic': 'zigbee2mqtt/weather_sensor',
'json_attributes_topic': 'zigbee2mqtt/weather_sensor',
@ -1052,6 +1065,7 @@ describe('HomeAssistant extension', () => {
const payload = {
'unit_of_measurement': '°C',
'device_class': 'temperature',
'state_class': 'measurement',
'value_template': '{{ value_json.temperature }}',
'state_topic': 'zigbee2mqtt/weather_sensor_renamed',
'json_attributes_topic': 'zigbee2mqtt/weather_sensor_renamed',
@ -1122,6 +1136,7 @@ describe('HomeAssistant extension', () => {
const payload = {
'unit_of_measurement': '°C',
'device_class': 'temperature',
'state_class': 'measurement',
'value_template': '{{ value_json.temperature }}',
'state_topic': 'zigbee2mqtt/weather_sensor_renamed',
'json_attributes_topic': 'zigbee2mqtt/weather_sensor_renamed',