Skip to content

Commit

Permalink
Get the currency from the api (#56806)
Browse files Browse the repository at this point in the history
  • Loading branch information
joaophi committed Sep 29, 2021
1 parent 12b2076 commit a967a1d
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions homeassistant/components/growatt_server/sensor.py
Expand Up @@ -5,7 +5,6 @@
import datetime
import json
import logging
import re

import growattServer

Expand All @@ -19,7 +18,6 @@
CONF_PASSWORD,
CONF_URL,
CONF_USERNAME,
CURRENCY_EURO,
DEVICE_CLASS_BATTERY,
DEVICE_CLASS_CURRENT,
DEVICE_CLASS_ENERGY,
Expand Down Expand Up @@ -57,20 +55,21 @@ class GrowattSensorEntityDescription(SensorEntityDescription, GrowattRequiredKey
"""Describes Growatt sensor entity."""

precision: int | None = None
currency: bool = False


TOTAL_SENSOR_TYPES: tuple[GrowattSensorEntityDescription, ...] = (
GrowattSensorEntityDescription(
key="total_money_today",
name="Total money today",
api_key="plantMoneyText",
native_unit_of_measurement=CURRENCY_EURO,
currency=True,
),
GrowattSensorEntityDescription(
key="total_money_total",
name="Money lifetime",
api_key="totalMoneyText",
native_unit_of_measurement=CURRENCY_EURO,
currency=True,
),
GrowattSensorEntityDescription(
key="total_energy_today",
Expand Down Expand Up @@ -975,6 +974,13 @@ def native_value(self):
result = round(result, self.entity_description.precision)
return result

@property
def native_unit_of_measurement(self) -> str | None:
"""Return the unit of measurement of the sensor, if any."""
if self.entity_description.currency:
return self.probe.get_data("currency")
return super().native_unit_of_measurement

def update(self):
"""Get the latest data from the Growat API and updates the state."""
self.probe.update()
Expand Down Expand Up @@ -1003,10 +1009,10 @@ def update(self):
if self.growatt_type == "total":
total_info = self.api.plant_info(self.device_id)
del total_info["deviceList"]
# PlantMoneyText comes in as "3.1/€" remove anything that isn't part of the number
total_info["plantMoneyText"] = re.sub(
r"[^\d.,]", "", total_info["plantMoneyText"]
)
# PlantMoneyText comes in as "3.1/€" split between value and currency
plant_money_text, currency = total_info["plantMoneyText"].split("/")
total_info["plantMoneyText"] = plant_money_text
total_info["currency"] = currency
self.data = total_info
elif self.growatt_type == "inverter":
inverter_info = self.api.inverter_detail(self.device_id)
Expand Down

0 comments on commit a967a1d

Please sign in to comment.