Fix SolarEdge statistics; missing device_class (#53720)

pull/53726/head
Franck Nijhof 2021-07-29 21:34:22 +02:00 committed by GitHub
parent 3c0e4b1fd9
commit 7c98fc94d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 59 additions and 59 deletions

View File

@ -3,10 +3,16 @@ from datetime import timedelta
import logging
from homeassistant.components.sensor import STATE_CLASS_MEASUREMENT
from homeassistant.const import ENERGY_WATT_HOUR, PERCENTAGE, POWER_WATT
from homeassistant.const import (
DEVICE_CLASS_ENERGY,
DEVICE_CLASS_POWER,
ENERGY_WATT_HOUR,
PERCENTAGE,
POWER_WATT,
)
from homeassistant.util import dt as dt_util
from .models import SolarEdgeSensor
from .models import SolarEdgeSensorEntityDescription
DOMAIN = "solaredge"
@ -29,7 +35,7 @@ SCAN_INTERVAL = timedelta(minutes=15)
# Supported overview sensors
SENSOR_TYPES = [
SolarEdgeSensor(
SolarEdgeSensorEntityDescription(
key="lifetime_energy",
json_key="lifeTimeData",
name="Lifetime energy",
@ -37,138 +43,143 @@ SENSOR_TYPES = [
last_reset=dt_util.utc_from_timestamp(0),
state_class=STATE_CLASS_MEASUREMENT,
unit_of_measurement=ENERGY_WATT_HOUR,
device_class=DEVICE_CLASS_ENERGY,
),
SolarEdgeSensor(
SolarEdgeSensorEntityDescription(
key="energy_this_year",
json_key="lastYearData",
name="Energy this year",
entity_registry_enabled_default=False,
icon="mdi:solar-power",
unit_of_measurement=ENERGY_WATT_HOUR,
device_class=DEVICE_CLASS_ENERGY,
),
SolarEdgeSensor(
SolarEdgeSensorEntityDescription(
key="energy_this_month",
json_key="lastMonthData",
name="Energy this month",
entity_registry_enabled_default=False,
icon="mdi:solar-power",
unit_of_measurement=ENERGY_WATT_HOUR,
device_class=DEVICE_CLASS_ENERGY,
),
SolarEdgeSensor(
SolarEdgeSensorEntityDescription(
key="energy_today",
json_key="lastDayData",
name="Energy today",
entity_registry_enabled_default=False,
icon="mdi:solar-power",
unit_of_measurement=ENERGY_WATT_HOUR,
device_class=DEVICE_CLASS_ENERGY,
),
SolarEdgeSensor(
SolarEdgeSensorEntityDescription(
key="current_power",
json_key="currentPower",
name="Current Power",
icon="mdi:solar-power",
state_class=STATE_CLASS_MEASUREMENT,
unit_of_measurement=POWER_WATT,
device_class=DEVICE_CLASS_POWER,
),
SolarEdgeSensor(
SolarEdgeSensorEntityDescription(
key="site_details",
name="Site details",
entity_registry_enabled_default=False,
),
SolarEdgeSensor(
SolarEdgeSensorEntityDescription(
key="meters",
json_key="meters",
name="Meters",
entity_registry_enabled_default=False,
),
SolarEdgeSensor(
SolarEdgeSensorEntityDescription(
key="sensors",
json_key="sensors",
name="Sensors",
entity_registry_enabled_default=False,
),
SolarEdgeSensor(
SolarEdgeSensorEntityDescription(
key="gateways",
json_key="gateways",
name="Gateways",
entity_registry_enabled_default=False,
),
SolarEdgeSensor(
SolarEdgeSensorEntityDescription(
key="batteries",
json_key="batteries",
name="Batteries",
entity_registry_enabled_default=False,
),
SolarEdgeSensor(
SolarEdgeSensorEntityDescription(
key="inverters",
json_key="inverters",
name="Inverters",
entity_registry_enabled_default=False,
),
SolarEdgeSensor(
SolarEdgeSensorEntityDescription(
key="power_consumption",
json_key="LOAD",
name="Power Consumption",
entity_registry_enabled_default=False,
icon="mdi:flash",
),
SolarEdgeSensor(
SolarEdgeSensorEntityDescription(
key="solar_power",
json_key="PV",
name="Solar Power",
entity_registry_enabled_default=False,
icon="mdi:solar-power",
),
SolarEdgeSensor(
SolarEdgeSensorEntityDescription(
key="grid_power",
json_key="GRID",
name="Grid Power",
entity_registry_enabled_default=False,
icon="mdi:power-plug",
),
SolarEdgeSensor(
SolarEdgeSensorEntityDescription(
key="storage_power",
json_key="STORAGE",
name="Storage Power",
entity_registry_enabled_default=False,
icon="mdi:car-battery",
),
SolarEdgeSensor(
SolarEdgeSensorEntityDescription(
key="purchased_power",
json_key="Purchased",
name="Imported Power",
entity_registry_enabled_default=False,
icon="mdi:flash",
),
SolarEdgeSensor(
SolarEdgeSensorEntityDescription(
key="production_power",
json_key="Production",
name="Production Power",
entity_registry_enabled_default=False,
icon="mdi:flash",
),
SolarEdgeSensor(
SolarEdgeSensorEntityDescription(
key="consumption_power",
json_key="Consumption",
name="Consumption Power",
entity_registry_enabled_default=False,
icon="mdi:flash",
),
SolarEdgeSensor(
SolarEdgeSensorEntityDescription(
key="selfconsumption_power",
json_key="SelfConsumption",
name="SelfConsumption Power",
entity_registry_enabled_default=False,
icon="mdi:flash",
),
SolarEdgeSensor(
SolarEdgeSensorEntityDescription(
key="feedin_power",
json_key="FeedIn",
name="Exported Power",
entity_registry_enabled_default=False,
icon="mdi:flash",
),
SolarEdgeSensor(
SolarEdgeSensorEntityDescription(
key="storage_level",
json_key="STORAGE",
name="Storage Level",

View File

@ -2,20 +2,12 @@
from __future__ import annotations
from dataclasses import dataclass
from datetime import datetime
from homeassistant.components.sensor import SensorEntityDescription
@dataclass
class SolarEdgeSensor:
"""Represents an SolarEdge Sensor."""
key: str
name: str
class SolarEdgeSensorEntityDescription(SensorEntityDescription):
"""Sensor entity description for SolarEdge."""
json_key: str | None = None
device_class: str | None = None
entity_registry_enabled_default: bool = True
icon: str | None = None
last_reset: datetime | None = None
state_class: str | None = None
unit_of_measurement: str | None = None

View File

@ -21,7 +21,7 @@ from .coordinator import (
SolarEdgeOverviewDataService,
SolarEdgePowerFlowDataService,
)
from .models import SolarEdgeSensor
from .models import SolarEdgeSensorEntityDescription
async def async_setup_entry(
@ -68,7 +68,8 @@ class SolarEdgeSensorFactory:
self.services: dict[
str,
tuple[
type[SolarEdgeSensor | SolarEdgeOverviewSensor], SolarEdgeDataService
type[SolarEdgeSensorEntity | SolarEdgeOverviewSensor],
SolarEdgeDataService,
],
] = {"site_details": (SolarEdgeDetailsSensor, details)}
@ -99,7 +100,9 @@ class SolarEdgeSensorFactory:
):
self.services[key] = (SolarEdgeEnergyDetailsSensor, energy)
def create_sensor(self, sensor_type: SolarEdgeSensor) -> SolarEdgeSensor:
def create_sensor(
self, sensor_type: SolarEdgeSensorEntityDescription
) -> SolarEdgeSensorEntityDescription:
"""Create and return a sensor based on the sensor_key."""
sensor_class, service = self.services[sensor_type.key]
@ -109,27 +112,21 @@ class SolarEdgeSensorFactory:
class SolarEdgeSensorEntity(CoordinatorEntity, SensorEntity):
"""Abstract class for a solaredge sensor."""
entity_description: SolarEdgeSensorEntityDescription
def __init__(
self,
platform_name: str,
sensor_type: SolarEdgeSensor,
description: SolarEdgeSensorEntityDescription,
data_service: SolarEdgeDataService,
) -> None:
"""Initialize the sensor."""
super().__init__(data_service.coordinator)
self.platform_name = platform_name
self.sensor_type = sensor_type
self.entity_description = description
self.data_service = data_service
self._attr_device_class = sensor_type.device_class
self._attr_entity_registry_enabled_default = (
sensor_type.entity_registry_enabled_default
)
self._attr_icon = sensor_type.icon
self._attr_last_reset = sensor_type.last_reset
self._attr_name = f"{platform_name} ({sensor_type.name})"
self._attr_state_class = sensor_type.state_class
self._attr_unit_of_measurement = sensor_type.unit_of_measurement
self._attr_name = f"{platform_name} ({description.name})"
class SolarEdgeOverviewSensor(SolarEdgeSensorEntity):
@ -138,7 +135,7 @@ class SolarEdgeOverviewSensor(SolarEdgeSensorEntity):
@property
def state(self) -> str | None:
"""Return the state of the sensor."""
return self.data_service.data.get(self.sensor_type.json_key)
return self.data_service.data.get(self.entity_description.json_key)
class SolarEdgeDetailsSensor(SolarEdgeSensorEntity):
@ -161,12 +158,12 @@ class SolarEdgeInventorySensor(SolarEdgeSensorEntity):
@property
def extra_state_attributes(self) -> dict[str, Any]:
"""Return the state attributes."""
return self.data_service.attributes.get(self.sensor_type.json_key)
return self.data_service.attributes.get(self.entity_description.json_key)
@property
def state(self) -> str | None:
"""Return the state of the sensor."""
return self.data_service.data.get(self.sensor_type.json_key)
return self.data_service.data.get(self.entity_description.json_key)
class SolarEdgeEnergyDetailsSensor(SolarEdgeSensorEntity):
@ -181,12 +178,12 @@ class SolarEdgeEnergyDetailsSensor(SolarEdgeSensorEntity):
@property
def extra_state_attributes(self) -> dict[str, Any]:
"""Return the state attributes."""
return self.data_service.attributes.get(self.sensor_type.json_key)
return self.data_service.attributes.get(self.entity_description.json_key)
@property
def state(self) -> str | None:
"""Return the state of the sensor."""
return self.data_service.data.get(self.sensor_type.json_key)
return self.data_service.data.get(self.entity_description.json_key)
class SolarEdgePowerFlowSensor(SolarEdgeSensorEntity):
@ -197,23 +194,23 @@ class SolarEdgePowerFlowSensor(SolarEdgeSensorEntity):
def __init__(
self,
platform_name: str,
sensor_type: SolarEdgeSensor,
description: SolarEdgeSensorEntityDescription,
data_service: SolarEdgeDataService,
) -> None:
"""Initialize the power flow sensor."""
super().__init__(platform_name, sensor_type, data_service)
super().__init__(platform_name, description, data_service)
self._attr_unit_of_measurement = data_service.unit
@property
def extra_state_attributes(self) -> dict[str, Any]:
"""Return the state attributes."""
return self.data_service.attributes.get(self.sensor_type.json_key)
return self.data_service.attributes.get(self.entity_description.json_key)
@property
def state(self) -> str | None:
"""Return the state of the sensor."""
return self.data_service.data.get(self.sensor_type.json_key)
return self.data_service.data.get(self.entity_description.json_key)
class SolarEdgeStorageLevelSensor(SolarEdgeSensorEntity):
@ -224,7 +221,7 @@ class SolarEdgeStorageLevelSensor(SolarEdgeSensorEntity):
@property
def state(self) -> str | None:
"""Return the state of the sensor."""
attr = self.data_service.attributes.get(self.sensor_type.json_key)
attr = self.data_service.attributes.get(self.entity_description.json_key)
if attr and "soc" in attr:
return attr["soc"]
return None