From 511fd3c1947110fdc4a54746d459781fbd5624d0 Mon Sep 17 00:00:00 2001 From: Bouwe Date: Sat, 23 Oct 2021 20:14:46 +0200 Subject: [PATCH 1/2] Add more state_class and device_class attributes --- custom_components/sems2mqtt/__init__.py | 29 +++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/custom_components/sems2mqtt/__init__.py b/custom_components/sems2mqtt/__init__.py index 9db3a59..85a9a95 100644 --- a/custom_components/sems2mqtt/__init__.py +++ b/custom_components/sems2mqtt/__init__.py @@ -27,6 +27,9 @@ import paho.mqtt.publish as publish import paho.mqtt.client as mqtt +from homeassistant.components.sensor import ( + +) from homeassistant.const import ( CONF_PASSWORD, CONF_USERNAME, CONF_SCAN_INTERVAL, EVENT_HOMEASSISTANT_STOP) @@ -217,6 +220,8 @@ def create_device(model): 'value_template':'{{ value_json.pgrid_w }}', 'icon':'mdi:solar-power', 'state_topic':'sems/sensors', + 'state_class': "measurement", + 'device_class': 'power', 'unique_id':'sems_solar_power_sensor', 'device': create_device(data['type']) } @@ -226,6 +231,8 @@ def create_device(model): 'value_template':'{{ value_json.temperature }}', 'icon':'mdi:thermometer', 'state_topic':'sems/sensors', + 'state_class': "measurement", + 'device_class': 'temperature', 'unique_id':'sems_inverter_temperature_sensor', 'device': create_device(data['type']) } @@ -235,6 +242,8 @@ def create_device(model): 'value_template':'{{ value_json.eday_kwh }}', 'icon':'mdi:flash', 'state_topic':'sems/sensors', + 'state_class': "total", + 'device_class': 'energy', 'unique_id':'sems_produced_today_sensor', 'device': create_device(data['type']) } @@ -255,6 +264,8 @@ def create_device(model): 'value_template':'{{ value_json.emonth_kwh }}', 'icon':'mdi:flash', 'state_topic':'sems/sensors', + 'state_class': "total", + 'device_class': 'energy', 'unique_id':'sems_produced_this_month_sensor', 'device': create_device(data['type']) } @@ -264,6 +275,8 @@ def create_device(model): 'value_template':'{{ value_json.grid_voltage }}', 'icon':'mdi:current-ac', 'state_topic':'sems/sensors', + 'state_class': "measurement", + 'device_class': 'voltage', 'unique_id':'sems_grid_voltage_sensor', 'device': create_device(data['type']) } @@ -273,6 +286,8 @@ def create_device(model): 'value_template':'{{ value_json.grid_current }}', 'icon':'mdi:current-ac', 'state_topic':'sems/sensors', + 'state_class': "measurement", + 'device_class': 'current', 'unique_id':'sems_grid_current_sensor', 'device': create_device(data['type']) } @@ -282,6 +297,8 @@ def create_device(model): 'value_template':'{{ value_json.grid_frequency }}', 'icon':'mdi:current-ac', 'state_topic':'sems/sensors', + 'state_class': "measurement", + 'device_class': None, 'unique_id':'sems_grid_frequency_sensor', 'device': create_device(data['type']) } @@ -291,6 +308,8 @@ def create_device(model): 'value_template':'{{ value_json.dc_voltage_str_1 }}', 'icon':'mdi:current-dc', 'state_topic':'sems/sensors', + 'state_class': "measurement", + 'device_class': 'voltage', 'unique_id':'sems_str_1_voltage_sensor', 'device': create_device(data['type']) } @@ -300,6 +319,8 @@ def create_device(model): 'value_template':'{{ value_json.dc_current_str_1 }}', 'icon':'mdi:current-dc', 'state_topic':'sems/sensors', + 'state_class': "measurement", + 'device_class': 'current', 'unique_id':'sems_str_1_current_sensor', 'device': create_device(data['type']) } @@ -309,6 +330,8 @@ def create_device(model): 'value_template':'{{ value_json.dc_voltage_str_2 }}', 'icon':'mdi:current-dc', 'state_topic':'sems/sensors', + 'state_class': "measurement", + 'device_class': 'voltage', 'unique_id':'sems_str_2_voltage_sensor', 'device': create_device(data['type']) } @@ -318,6 +341,8 @@ def create_device(model): 'value_template':'{{ value_json.dc_current_str_2 }}', 'icon':'mdi:current-dc', 'state_topic':'sems/sensors', + 'state_class': "measurement", + 'device_class': 'current', 'unique_id':'sems_str_2_current_sensor', 'device': create_device(data['type']) } @@ -327,6 +352,8 @@ def create_device(model): 'value_template':'{{ value_json.battery_soc }}', 'icon':'mdi:battery-charging', 'state_topic':'sems/sensors', + 'state_class': "measurement", + 'device_class': None, 'unique_id':'sems_battery_soc_sensor', 'device': create_device(data['type']) } @@ -336,6 +363,8 @@ def create_device(model): 'value_template':'{{ value_json.battery_soh }}', 'icon':'mdi:medical-bag', 'state_topic':'sems/sensors', + 'state_class': "measurement", + 'device_class': None, 'unique_id':'sems_battery_soh_sensor', 'device': create_device(data['type']) } From 8f761af7626d38b0e0e3f62a878ce56dd266eee9 Mon Sep 17 00:00:00 2001 From: Bouwe Date: Sat, 23 Oct 2021 20:22:57 +0200 Subject: [PATCH 2/2] Import from homeassistant sensorplatform and use --- custom_components/sems2mqtt/__init__.py | 62 ++++++++++++++----------- 1 file changed, 35 insertions(+), 27 deletions(-) diff --git a/custom_components/sems2mqtt/__init__.py b/custom_components/sems2mqtt/__init__.py index 85a9a95..7ed8d0d 100644 --- a/custom_components/sems2mqtt/__init__.py +++ b/custom_components/sems2mqtt/__init__.py @@ -28,7 +28,15 @@ import paho.mqtt.client as mqtt from homeassistant.components.sensor import ( - + DEVICE_CLASS_BATTERY, + DEVICE_CLASS_CURRENT, + DEVICE_CLASS_ENERGY, + DEVICE_CLASS_POWER, + DEVICE_CLASS_TEMPERATURE, + DEVICE_CLASS_VOLTAGE, + STATE_CLASS_MEASUREMENT, + STATE_CLASS_TOTAL, + STATE_CLASS_TOTAL_INCREASING, ) from homeassistant.const import ( CONF_PASSWORD, CONF_USERNAME, @@ -220,8 +228,8 @@ def create_device(model): 'value_template':'{{ value_json.pgrid_w }}', 'icon':'mdi:solar-power', 'state_topic':'sems/sensors', - 'state_class': "measurement", - 'device_class': 'power', + 'state_class': STATE_CLASS_MEASUREMENT, + 'device_class': DEVICE_CLASS_POWER, 'unique_id':'sems_solar_power_sensor', 'device': create_device(data['type']) } @@ -231,8 +239,8 @@ def create_device(model): 'value_template':'{{ value_json.temperature }}', 'icon':'mdi:thermometer', 'state_topic':'sems/sensors', - 'state_class': "measurement", - 'device_class': 'temperature', + 'state_class': STATE_CLASS_MEASUREMENT, + 'device_class': DEVICE_CLASS_TEMPERATURE, 'unique_id':'sems_inverter_temperature_sensor', 'device': create_device(data['type']) } @@ -242,8 +250,8 @@ def create_device(model): 'value_template':'{{ value_json.eday_kwh }}', 'icon':'mdi:flash', 'state_topic':'sems/sensors', - 'state_class': "total", - 'device_class': 'energy', + 'state_class': STATE_CLASS_TOTAL, + 'device_class': DEVICE_CLASS_ENERGY, 'unique_id':'sems_produced_today_sensor', 'device': create_device(data['type']) } @@ -253,8 +261,8 @@ def create_device(model): 'value_template':'{{ value_json.etotal_kwh }}', 'icon':'mdi:flash', 'state_topic':'sems/sensors', - 'state_class': "total_increasing", - 'device_class': 'energy', + 'state_class': STATE_CLASS_TOTAL_INCREASING, + 'device_class': DEVICE_CLASS_ENERGY, 'unique_id':'sems_produced_total_sensor', 'device': create_device(data['type']) } @@ -264,8 +272,8 @@ def create_device(model): 'value_template':'{{ value_json.emonth_kwh }}', 'icon':'mdi:flash', 'state_topic':'sems/sensors', - 'state_class': "total", - 'device_class': 'energy', + 'state_class': STATE_CLASS_TOTAL, + 'device_class': DEVICE_CLASS_ENERGY, 'unique_id':'sems_produced_this_month_sensor', 'device': create_device(data['type']) } @@ -275,8 +283,8 @@ def create_device(model): 'value_template':'{{ value_json.grid_voltage }}', 'icon':'mdi:current-ac', 'state_topic':'sems/sensors', - 'state_class': "measurement", - 'device_class': 'voltage', + 'state_class': STATE_CLASS_MEASUREMENT, + 'device_class': DEVICE_CLASS_VOLTAGE, 'unique_id':'sems_grid_voltage_sensor', 'device': create_device(data['type']) } @@ -286,8 +294,8 @@ def create_device(model): 'value_template':'{{ value_json.grid_current }}', 'icon':'mdi:current-ac', 'state_topic':'sems/sensors', - 'state_class': "measurement", - 'device_class': 'current', + 'state_class': STATE_CLASS_MEASUREMENT, + 'device_class': DEVICE_CLASS_CURRENT, 'unique_id':'sems_grid_current_sensor', 'device': create_device(data['type']) } @@ -297,7 +305,7 @@ def create_device(model): 'value_template':'{{ value_json.grid_frequency }}', 'icon':'mdi:current-ac', 'state_topic':'sems/sensors', - 'state_class': "measurement", + 'state_class': STATE_CLASS_MEASUREMENT, 'device_class': None, 'unique_id':'sems_grid_frequency_sensor', 'device': create_device(data['type']) @@ -308,8 +316,8 @@ def create_device(model): 'value_template':'{{ value_json.dc_voltage_str_1 }}', 'icon':'mdi:current-dc', 'state_topic':'sems/sensors', - 'state_class': "measurement", - 'device_class': 'voltage', + 'state_class': STATE_CLASS_MEASUREMENT, + 'device_class': DEVICE_CLASS_VOLTAGE, 'unique_id':'sems_str_1_voltage_sensor', 'device': create_device(data['type']) } @@ -319,8 +327,8 @@ def create_device(model): 'value_template':'{{ value_json.dc_current_str_1 }}', 'icon':'mdi:current-dc', 'state_topic':'sems/sensors', - 'state_class': "measurement", - 'device_class': 'current', + 'state_class': STATE_CLASS_MEASUREMENT, + 'device_class': DEVICE_CLASS_CURRENT, 'unique_id':'sems_str_1_current_sensor', 'device': create_device(data['type']) } @@ -330,8 +338,8 @@ def create_device(model): 'value_template':'{{ value_json.dc_voltage_str_2 }}', 'icon':'mdi:current-dc', 'state_topic':'sems/sensors', - 'state_class': "measurement", - 'device_class': 'voltage', + 'state_class': STATE_CLASS_MEASUREMENT, + 'device_class': DEVICE_CLASS_VOLTAGE, 'unique_id':'sems_str_2_voltage_sensor', 'device': create_device(data['type']) } @@ -341,8 +349,8 @@ def create_device(model): 'value_template':'{{ value_json.dc_current_str_2 }}', 'icon':'mdi:current-dc', 'state_topic':'sems/sensors', - 'state_class': "measurement", - 'device_class': 'current', + 'state_class': STATE_CLASS_MEASUREMENT, + 'device_class': DEVICE_CLASS_CURRENT, 'unique_id':'sems_str_2_current_sensor', 'device': create_device(data['type']) } @@ -352,8 +360,8 @@ def create_device(model): 'value_template':'{{ value_json.battery_soc }}', 'icon':'mdi:battery-charging', 'state_topic':'sems/sensors', - 'state_class': "measurement", - 'device_class': None, + 'state_class': STATE_CLASS_MEASUREMENT, + 'device_class': DEVICE_CLASS_BATTERY, 'unique_id':'sems_battery_soc_sensor', 'device': create_device(data['type']) } @@ -363,7 +371,7 @@ def create_device(model): 'value_template':'{{ value_json.battery_soh }}', 'icon':'mdi:medical-bag', 'state_topic':'sems/sensors', - 'state_class': "measurement", + 'state_class': STATE_CLASS_MEASUREMENT, 'device_class': None, 'unique_id':'sems_battery_soh_sensor', 'device': create_device(data['type'])