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