Skip to content

Commit 890a154

Browse files
authored
Merge pull request #134 from HiDiHo01/main
Use HA system currency and MDI icon instead of hardcoded USD defaults
2 parents a7e8466 + 1a1a4a6 commit 890a154

1 file changed

Lines changed: 23 additions & 2 deletions

File tree

  • custom_components/utility_meter_next_gen

custom_components/utility_meter_next_gen/sensor.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,9 +1140,21 @@ def __init__(
11401140
SensorDeviceClass.MONETARY if device_class is None else device_class
11411141
)
11421142
self._attr_device_info = device_info
1143-
self._attr_icon = "mdi:currency-usd" if icon is None else icon
1143+
_currency_icons = {
1144+
"EUR": "mdi:currency-eur",
1145+
"GBP": "mdi:currency-gbp",
1146+
"USD": "mdi:currency-usd",
1147+
"JPY": "mdi:currency-jpy",
1148+
"CNY": "mdi:currency-cny",
1149+
"INR": "mdi:currency-inr",
1150+
"KRW": "mdi:currency-krw",
1151+
"RUB": "mdi:currency-rub",
1152+
"TRY": "mdi:currency-try",
1153+
"BRL": "mdi:currency-brl",
1154+
}
1155+
self._attr_icon = icon if icon is not None else _currency_icons.get(hass.config.currency, "")
11441156
self._attr_name = name
1145-
self._attr_native_unit_of_measurement = CURRENCY_DOLLAR if uom is None else uom
1157+
self._attr_native_unit_of_measurement = hass.config.currency if uom is None else uom
11461158
self._attr_suggested_display_precision = PRECISION
11471159
self._attr_state_class = (
11481160
SensorStateClass.TOTAL if state_class is None else state_class
@@ -1164,6 +1176,15 @@ def start(self, attributes: Mapping[str, Any]) -> None:
11641176

11651177
async def async_added_to_hass(self) -> None:
11661178
"""Handle added to Hass."""
1179+
# Derive unit from calc sensor if not explicitly set
1180+
if self._source_calc_entity is not None:
1181+
calc_state = self.hass.states.get(self._source_calc_entity)
1182+
if calc_state is not None:
1183+
calc_uom = calc_state.attributes.get("unit_of_measurement", "")
1184+
# e.g. "€/kWh" → take the currency part before "/"
1185+
if "/" in calc_uom:
1186+
self._attr_native_unit_of_measurement = calc_uom.split("/")[0]
1187+
11671188
self.async_on_remove(
11681189
async_track_state_change_event(
11691190
self.hass, self._entity_id, self._async_attribute_sensor_state_listener

0 commit comments

Comments
 (0)