Skip to content

Commit 1c33a3c

Browse files
authored
Merge pull request #27 from JasperE84/v2.0.2-dev
V2.0.2 - Change Hass MQTT discovery template to kW instead of W
2 parents 693c018 + aeec0ff commit 1c33a3c

File tree

3 files changed

+14
-10
lines changed

3 files changed

+14
-10
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ Kenter provides measurement services for **commercially rented** grid transforme
126126
# Changelog
127127
| Version | Description |
128128
| --- | --- |
129+
| 2.0.2 | Default to kW in home assistant with 3 digit precision suggestion |
129130
| 2.0.1 | Now sending MQTT device discovery message |
130131
| 2.0.1 | Bugfix in environment variable parsing for custom pydantic nested env list parser |
131132
| 2.0.0 | Introduced possibility to configure multiple input sources (kiosks, openapi meters/inverters and kenter meters) |

main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
2020
streamHandler.setFormatter(formatter)
2121
logger.addHandler(streamHandler)
22-
logger.info("PyFusionSolarDataRelay 2.0.1 started")
22+
logger.info("PyFusionSolarDataRelay 2.0.2 started")
2323

2424
# Config
2525
conf = Conf(logger).read_and_validate_config()

modules/write_mqtt.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def append_if_not_empty(base, addition):
7676
raise Exception(f"Exception while publishing to MQTT: '{e}'")
7777

7878
# 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)
8080

8181
def publish_grid_data_to_mqtt(self, measurement: FusionSolarMeterMeasurement):
8282
"""
@@ -139,9 +139,9 @@ def append_if_not_empty(base, addition):
139139
raise Exception(f"Exception while publishing to MQTT: '{e}'")
140140

141141
# 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)
143143

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):
145145
"""
146146
Publish Home Assistant discovery config for each data field.
147147
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
181181
if field_name.endswith("_w"):
182182
# It's a power sensor
183183
device_class = "power"
184-
unit_of_measurement = "W"
184+
unit_of_measurement = "kW"
185185
state_class = "measurement"
186186
elif field_name.endswith("_wh"):
187187
# It's an energy sensor
188188
device_class = "energy"
189-
unit_of_measurement = "Wh"
189+
unit_of_measurement = "kWh"
190190
# For cumulative energy sensors, setting "state_class=total_increasing"
191191
# helps with HA’s Energy Dashboard
192192
state_class = "total_increasing"
@@ -199,7 +199,7 @@ def publish_homeassistant_discovery(self, station_dn_sanitized, device_dn_saniti
199199
# Topic format: homeassistant/sensor/<unique_id>/config
200200
field_name_sanitized = re.sub(r"\W+", "_", field_name).lower()
201201
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"
203203

204204
# Skip if device is already discovery_published
205205
if unique_sensor_id in self.hass_discovery_published:
@@ -210,11 +210,14 @@ def publish_homeassistant_discovery(self, station_dn_sanitized, device_dn_saniti
210210
"name": f"{field_name}",
211211
"state_topic": state_topic,
212212
"uniq_id": unique_sensor_id,
213+
"suggested_display_precision": 3,
213214
"device": {
214215
"identifiers": [device_identifier],
215-
"name": f"{device_name}",
216+
"name": device_name,
217+
"model": device_model,
218+
"manufacturer": "Huawei",
216219
},
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) }}}}",
218221
}
219222

220223
# Add the sensor-specific attributes
@@ -250,4 +253,4 @@ def publish_homeassistant_discovery(self, station_dn_sanitized, device_dn_saniti
250253
self.logger.exception(f"Error publishing HA discovery to MQTT: '{e}'")
251254

252255
# 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

Comments
 (0)