Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get the currency from the api #56806

Merged
merged 2 commits into from Sep 29, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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