Skip to content

Commit 157bed8

Browse files
authored
feat(ha): dynamic SoC icon reflects charging state (#458)
Adds icon_template to the SoC MQTT discovery payload so HA renders mdi:battery-charging-{N} when the vehicle is charging and the standard mdi:battery-{N} icons otherwise. Thresholds match HA's native battery device_class icon mapping. Requires HA 2024.x for icon_template support in MQTT sensor discovery.
1 parent 35f1dcf commit 157bed8

2 files changed

Lines changed: 33 additions & 0 deletions

File tree

src/integrations/home_assistant/base.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ def _publish_sensor(
270270
state_class: str | None = None,
271271
unit_of_measurement: str | None = None,
272272
icon: str | None = None,
273+
icon_template: str | None = None,
273274
value_template: str = "{{ value }}",
274275
custom_availability: HaCustomAvailabilityConfig | None = None,
275276
) -> str:
@@ -288,6 +289,8 @@ def _publish_sensor(
288289
payload["unit_of_measurement"] = unit_of_measurement
289290
if icon is not None:
290291
payload["icon"] = icon
292+
if icon_template is not None:
293+
payload["icon_template"] = icon_template
291294

292295
return self._publish_ha_discovery_message(
293296
"sensor", name, payload, custom_availability

src/integrations/home_assistant/discovery.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,42 @@ def __publish_ha_discovery_messages_real(self) -> None:
132132
)
133133

134134
# Standard sensors
135+
charging_entity_id = f"binary_sensor.{self.vin}_battery_charging"
136+
soc_icon_template = (
137+
"{% set soc = value | int %}"
138+
f"{{% if is_state('{charging_entity_id}', 'on') %}}"
139+
"{%- if soc >= 95 %}mdi:battery-charging-100"
140+
"{%- elif soc >= 85 %}mdi:battery-charging-90"
141+
"{%- elif soc >= 75 %}mdi:battery-charging-80"
142+
"{%- elif soc >= 65 %}mdi:battery-charging-70"
143+
"{%- elif soc >= 55 %}mdi:battery-charging-60"
144+
"{%- elif soc >= 45 %}mdi:battery-charging-50"
145+
"{%- elif soc >= 35 %}mdi:battery-charging-40"
146+
"{%- elif soc >= 25 %}mdi:battery-charging-30"
147+
"{%- elif soc >= 15 %}mdi:battery-charging-20"
148+
"{%- elif soc >= 5 %}mdi:battery-charging-10"
149+
"{%- else %}mdi:battery-charging-outline{%- endif %}"
150+
"{% else %}"
151+
"{%- if soc >= 95 %}mdi:battery"
152+
"{%- elif soc >= 85 %}mdi:battery-90"
153+
"{%- elif soc >= 75 %}mdi:battery-80"
154+
"{%- elif soc >= 65 %}mdi:battery-70"
155+
"{%- elif soc >= 55 %}mdi:battery-60"
156+
"{%- elif soc >= 45 %}mdi:battery-50"
157+
"{%- elif soc >= 35 %}mdi:battery-40"
158+
"{%- elif soc >= 25 %}mdi:battery-30"
159+
"{%- elif soc >= 15 %}mdi:battery-20"
160+
"{%- elif soc >= 5 %}mdi:battery-10"
161+
"{%- else %}mdi:battery-outline{%- endif %}"
162+
"{% endif %}"
163+
)
135164
self._publish_sensor(
136165
mqtt_topics.DRIVETRAIN_SOC,
137166
"SoC",
138167
device_class="battery",
139168
state_class="measurement",
140169
unit_of_measurement="%",
170+
icon_template=soc_icon_template,
141171
)
142172
self._publish_sensor(
143173
mqtt_topics.DRIVETRAIN_SOC_KWH,

0 commit comments

Comments
 (0)