mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 19:27:45 +00:00
Add last reset to enphase sensors (#53653)
This commit is contained in:
parent
6eb3307734
commit
057d979335
@ -47,9 +47,11 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
except httpx.HTTPError as err:
|
||||
raise UpdateFailed(f"Error communicating with API: {err}") from err
|
||||
|
||||
for condition in SENSORS:
|
||||
if condition != "inverters":
|
||||
data[condition] = await getattr(envoy_reader, condition)()
|
||||
for description in SENSORS:
|
||||
if description.key != "inverters":
|
||||
data[description.key] = await getattr(
|
||||
envoy_reader, description.key
|
||||
)()
|
||||
else:
|
||||
data[
|
||||
"inverters_production"
|
||||
|
@ -1,8 +1,12 @@
|
||||
"""The enphase_envoy component."""
|
||||
|
||||
|
||||
from homeassistant.components.sensor import STATE_CLASS_MEASUREMENT
|
||||
from homeassistant.const import ENERGY_WATT_HOUR, POWER_WATT
|
||||
from homeassistant.components.sensor import (
|
||||
STATE_CLASS_MEASUREMENT,
|
||||
SensorEntityDescription,
|
||||
)
|
||||
from homeassistant.const import DEVICE_CLASS_ENERGY, ENERGY_WATT_HOUR, POWER_WATT
|
||||
from homeassistant.util import dt
|
||||
|
||||
DOMAIN = "enphase_envoy"
|
||||
|
||||
@ -12,22 +16,67 @@ PLATFORMS = ["sensor"]
|
||||
COORDINATOR = "coordinator"
|
||||
NAME = "name"
|
||||
|
||||
SENSORS = {
|
||||
"production": ("Current Energy Production", POWER_WATT, STATE_CLASS_MEASUREMENT),
|
||||
"daily_production": ("Today's Energy Production", ENERGY_WATT_HOUR, None),
|
||||
"seven_days_production": (
|
||||
"Last Seven Days Energy Production",
|
||||
ENERGY_WATT_HOUR,
|
||||
None,
|
||||
SENSORS = (
|
||||
SensorEntityDescription(
|
||||
key="production",
|
||||
name="Current Power Production",
|
||||
unit_of_measurement=POWER_WATT,
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
),
|
||||
"lifetime_production": ("Lifetime Energy Production", ENERGY_WATT_HOUR, None),
|
||||
"consumption": ("Current Energy Consumption", POWER_WATT, STATE_CLASS_MEASUREMENT),
|
||||
"daily_consumption": ("Today's Energy Consumption", ENERGY_WATT_HOUR, None),
|
||||
"seven_days_consumption": (
|
||||
"Last Seven Days Energy Consumption",
|
||||
ENERGY_WATT_HOUR,
|
||||
None,
|
||||
SensorEntityDescription(
|
||||
key="daily_production",
|
||||
name="Today's Energy Production",
|
||||
unit_of_measurement=ENERGY_WATT_HOUR,
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
device_class=DEVICE_CLASS_ENERGY,
|
||||
),
|
||||
"lifetime_consumption": ("Lifetime Energy Consumption", ENERGY_WATT_HOUR, None),
|
||||
"inverters": ("Inverter", POWER_WATT, STATE_CLASS_MEASUREMENT),
|
||||
}
|
||||
SensorEntityDescription(
|
||||
key="seven_days_production",
|
||||
name="Last Seven Days Energy Production",
|
||||
unit_of_measurement=ENERGY_WATT_HOUR,
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
device_class=DEVICE_CLASS_ENERGY,
|
||||
),
|
||||
SensorEntityDescription(
|
||||
key="lifetime_production",
|
||||
name="Lifetime Energy Production",
|
||||
unit_of_measurement=ENERGY_WATT_HOUR,
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
device_class=DEVICE_CLASS_ENERGY,
|
||||
last_reset=dt.utc_from_timestamp(0),
|
||||
),
|
||||
SensorEntityDescription(
|
||||
key="consumption",
|
||||
name="Current Power Consumption",
|
||||
unit_of_measurement=POWER_WATT,
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
),
|
||||
SensorEntityDescription(
|
||||
key="daily_consumption",
|
||||
name="Today's Energy Consumption",
|
||||
unit_of_measurement=ENERGY_WATT_HOUR,
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
device_class=DEVICE_CLASS_ENERGY,
|
||||
),
|
||||
SensorEntityDescription(
|
||||
key="seven_days_consumption",
|
||||
name="Last Seven Days Energy Consumption",
|
||||
unit_of_measurement=ENERGY_WATT_HOUR,
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
device_class=DEVICE_CLASS_ENERGY,
|
||||
),
|
||||
SensorEntityDescription(
|
||||
key="lifetime_consumption",
|
||||
name="Lifetime Energy Consumption",
|
||||
unit_of_measurement=ENERGY_WATT_HOUR,
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
device_class=DEVICE_CLASS_ENERGY,
|
||||
last_reset=dt.utc_from_timestamp(0),
|
||||
),
|
||||
SensorEntityDescription(
|
||||
key="inverters",
|
||||
name="Inverter",
|
||||
unit_of_measurement=POWER_WATT,
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
),
|
||||
)
|
||||
|
@ -56,42 +56,38 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||
name = data[NAME]
|
||||
|
||||
entities = []
|
||||
for condition, sensor in SENSORS.items():
|
||||
for sensor_description in SENSORS:
|
||||
if (
|
||||
condition == "inverters"
|
||||
sensor_description.key == "inverters"
|
||||
and coordinator.data.get("inverters_production") is not None
|
||||
):
|
||||
for inverter in coordinator.data["inverters_production"]:
|
||||
entity_name = f"{name} {sensor[0]} {inverter}"
|
||||
entity_name = f"{name} {sensor_description.name} {inverter}"
|
||||
split_name = entity_name.split(" ")
|
||||
serial_number = split_name[-1]
|
||||
entities.append(
|
||||
Envoy(
|
||||
condition,
|
||||
sensor_description,
|
||||
entity_name,
|
||||
name,
|
||||
config_entry.unique_id,
|
||||
serial_number,
|
||||
sensor[1],
|
||||
sensor[2],
|
||||
coordinator,
|
||||
)
|
||||
)
|
||||
elif condition != "inverters":
|
||||
data = coordinator.data.get(condition)
|
||||
elif sensor_description.key != "inverters":
|
||||
data = coordinator.data.get(sensor_description.key)
|
||||
if isinstance(data, str) and "not available" in data:
|
||||
continue
|
||||
|
||||
entity_name = f"{name} {sensor[0]}"
|
||||
entity_name = f"{name} {sensor_description.name}"
|
||||
entities.append(
|
||||
Envoy(
|
||||
condition,
|
||||
sensor_description,
|
||||
entity_name,
|
||||
name,
|
||||
config_entry.unique_id,
|
||||
None,
|
||||
sensor[1],
|
||||
sensor[2],
|
||||
coordinator,
|
||||
)
|
||||
)
|
||||
@ -104,23 +100,19 @@ class Envoy(CoordinatorEntity, SensorEntity):
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
sensor_type,
|
||||
description,
|
||||
name,
|
||||
device_name,
|
||||
device_serial_number,
|
||||
serial_number,
|
||||
unit,
|
||||
state_class,
|
||||
coordinator,
|
||||
):
|
||||
"""Initialize Envoy entity."""
|
||||
self._type = sensor_type
|
||||
self.entity_description = description
|
||||
self._name = name
|
||||
self._serial_number = serial_number
|
||||
self._device_name = device_name
|
||||
self._device_serial_number = device_serial_number
|
||||
self._unit_of_measurement = unit
|
||||
self._attr_state_class = state_class
|
||||
|
||||
super().__init__(coordinator)
|
||||
|
||||
@ -135,16 +127,16 @@ class Envoy(CoordinatorEntity, SensorEntity):
|
||||
if self._serial_number:
|
||||
return self._serial_number
|
||||
if self._device_serial_number:
|
||||
return f"{self._device_serial_number}_{self._type}"
|
||||
return f"{self._device_serial_number}_{self.entity_description.key}"
|
||||
|
||||
@property
|
||||
def state(self):
|
||||
"""Return the state of the sensor."""
|
||||
if self._type != "inverters":
|
||||
value = self.coordinator.data.get(self._type)
|
||||
if self.entity_description.key != "inverters":
|
||||
value = self.coordinator.data.get(self.entity_description.key)
|
||||
|
||||
elif (
|
||||
self._type == "inverters"
|
||||
self.entity_description.key == "inverters"
|
||||
and self.coordinator.data.get("inverters_production") is not None
|
||||
):
|
||||
value = self.coordinator.data.get("inverters_production").get(
|
||||
@ -155,11 +147,6 @@ class Envoy(CoordinatorEntity, SensorEntity):
|
||||
|
||||
return value
|
||||
|
||||
@property
|
||||
def unit_of_measurement(self):
|
||||
"""Return the unit of measurement of this entity, if any."""
|
||||
return self._unit_of_measurement
|
||||
|
||||
@property
|
||||
def icon(self):
|
||||
"""Icon to use in the frontend, if any."""
|
||||
@ -169,7 +156,7 @@ class Envoy(CoordinatorEntity, SensorEntity):
|
||||
def extra_state_attributes(self):
|
||||
"""Return the state attributes."""
|
||||
if (
|
||||
self._type == "inverters"
|
||||
self.entity_description.key == "inverters"
|
||||
and self.coordinator.data.get("inverters_production") is not None
|
||||
):
|
||||
value = self.coordinator.data.get("inverters_production").get(
|
||||
|
Loading…
x
Reference in New Issue
Block a user