@@ -76,7 +76,7 @@ def append_if_not_empty(base, addition):
76
76
raise Exception (f"Exception while publishing to MQTT: '{ e } '" )
77
77
78
78
# Publish MQTT device discovery
79
- self .publish_homeassistant_discovery (station_dn_sanitized , device_dn_sanitized , measurement .measurement_type , measurement .data_source , topic , data_points )
79
+ self .publish_homeassistant_discovery (station_dn_sanitized , device_dn_sanitized , measurement .measurement_type , measurement .data_source , measurement . device_model , topic , data_points )
80
80
81
81
def publish_grid_data_to_mqtt (self , measurement : FusionSolarMeterMeasurement ):
82
82
"""
@@ -139,9 +139,9 @@ def append_if_not_empty(base, addition):
139
139
raise Exception (f"Exception while publishing to MQTT: '{ e } '" )
140
140
141
141
# Publish MQTT device discovery
142
- self .publish_homeassistant_discovery (station_dn_sanitized , device_dn_sanitized , measurement .measurement_type , measurement .data_source , topic , data_points )
142
+ self .publish_homeassistant_discovery (station_dn_sanitized , device_dn_sanitized , measurement .measurement_type , measurement .data_source , measurement . device_model , topic , data_points )
143
143
144
- def publish_homeassistant_discovery (self , station_dn_sanitized , device_dn_sanitized , measurement_type , data_source , state_topic , data_points ):
144
+ def publish_homeassistant_discovery (self , station_dn_sanitized , device_dn_sanitized , measurement_type , data_source , device_model , state_topic , data_points ):
145
145
"""
146
146
Publish Home Assistant discovery config for each data field.
147
147
Any field ending in '_w' is treated as a power (W) sensor,
@@ -181,12 +181,12 @@ def publish_homeassistant_discovery(self, station_dn_sanitized, device_dn_saniti
181
181
if field_name .endswith ("_w" ):
182
182
# It's a power sensor
183
183
device_class = "power"
184
- unit_of_measurement = "W "
184
+ unit_of_measurement = "kW "
185
185
state_class = "measurement"
186
186
elif field_name .endswith ("_wh" ):
187
187
# It's an energy sensor
188
188
device_class = "energy"
189
- unit_of_measurement = "Wh "
189
+ unit_of_measurement = "kWh "
190
190
# For cumulative energy sensors, setting "state_class=total_increasing"
191
191
# helps with HA’s Energy Dashboard
192
192
state_class = "total_increasing"
@@ -199,7 +199,7 @@ def publish_homeassistant_discovery(self, station_dn_sanitized, device_dn_saniti
199
199
# Topic format: homeassistant/sensor/<unique_id>/config
200
200
field_name_sanitized = re .sub (r"\W+" , "_" , field_name ).lower ()
201
201
unique_sensor_id = f"{ device_identifier } _{ field_name_sanitized } "
202
- discovery_topic = f"homeassistant/sensor/{ unique_sensor_id } /config"
202
+ discovery_topic = f"homeassistant/sensor/pyfusionsolar/ { unique_sensor_id } /config"
203
203
204
204
# Skip if device is already discovery_published
205
205
if unique_sensor_id in self .hass_discovery_published :
@@ -210,11 +210,14 @@ def publish_homeassistant_discovery(self, station_dn_sanitized, device_dn_saniti
210
210
"name" : f"{ field_name } " ,
211
211
"state_topic" : state_topic ,
212
212
"uniq_id" : unique_sensor_id ,
213
+ "suggested_display_precision" : 3 ,
213
214
"device" : {
214
215
"identifiers" : [device_identifier ],
215
- "name" : f"{ device_name } " ,
216
+ "name" : device_name ,
217
+ "model" : device_model ,
218
+ "manufacturer" : "Huawei" ,
216
219
},
217
- "value_template" : f"{{{{ value_json.{ field_name } }}}}" ,
220
+ "value_template" : f"{{{{ none if value_json.{ field_name } is none else ((value_json. { field_name } |float / 1000) if (value_json. { field_name } |float > 0) else 0) }}}}" ,
218
221
}
219
222
220
223
# Add the sensor-specific attributes
@@ -250,4 +253,4 @@ def publish_homeassistant_discovery(self, station_dn_sanitized, device_dn_saniti
250
253
self .logger .exception (f"Error publishing HA discovery to MQTT: '{ e } '" )
251
254
252
255
# Add uniqe sensor id to device discovery published history
253
- self .hass_discovery_published .append (unique_sensor_id )
256
+ self .hass_discovery_published .append (unique_sensor_id )
0 commit comments