mirror of
https://github.com/home-assistant/core.git
synced 2025-07-20 11:47:06 +00:00
Add SolarEdge battery level and dynamic icon for storage sensor (#37826)
* Add SolarEdge battery level and dynamic icon for storage sensor * Add SolarEdge battery storage sensor * Fix isort warning * Remove charging attribute * Fix isort warning * Apply suggestions from code review * Update homeassistant/components/solaredge/sensor.py Co-authored-by: Chris Talkington <chris@talkingtontech.com>
This commit is contained in:
parent
d7d7ee6524
commit
3d308e0599
@ -1,7 +1,7 @@
|
|||||||
"""Constants for the SolarEdge Monitoring API."""
|
"""Constants for the SolarEdge Monitoring API."""
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
|
|
||||||
from homeassistant.const import ENERGY_WATT_HOUR, POWER_WATT
|
from homeassistant.const import ENERGY_WATT_HOUR, POWER_WATT, UNIT_PERCENTAGE
|
||||||
|
|
||||||
DOMAIN = "solaredge"
|
DOMAIN = "solaredge"
|
||||||
|
|
||||||
@ -77,4 +77,5 @@ SENSOR_TYPES = {
|
|||||||
False,
|
False,
|
||||||
],
|
],
|
||||||
"feedin_power": ["FeedIn", "Exported Power", None, "mdi:flash", False],
|
"feedin_power": ["FeedIn", "Exported Power", None, "mdi:flash", False],
|
||||||
|
"storage_level": ["STORAGE", "Storage Level", UNIT_PERCENTAGE, None, False],
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@ from requests.exceptions import ConnectTimeout, HTTPError
|
|||||||
import solaredge
|
import solaredge
|
||||||
from stringcase import snakecase
|
from stringcase import snakecase
|
||||||
|
|
||||||
from homeassistant.const import CONF_API_KEY
|
from homeassistant.const import CONF_API_KEY, DEVICE_CLASS_BATTERY, DEVICE_CLASS_POWER
|
||||||
from homeassistant.helpers.entity import Entity
|
from homeassistant.helpers.entity import Entity
|
||||||
from homeassistant.util import Throttle
|
from homeassistant.util import Throttle
|
||||||
|
|
||||||
@ -83,6 +83,9 @@ class SolarEdgeSensorFactory:
|
|||||||
for key in ["power_consumption", "solar_power", "grid_power", "storage_power"]:
|
for key in ["power_consumption", "solar_power", "grid_power", "storage_power"]:
|
||||||
self.services[key] = (SolarEdgePowerFlowSensor, flow)
|
self.services[key] = (SolarEdgePowerFlowSensor, flow)
|
||||||
|
|
||||||
|
for key in ["storage_level"]:
|
||||||
|
self.services[key] = (SolarEdgeStorageLevelSensor, flow)
|
||||||
|
|
||||||
for key in [
|
for key in [
|
||||||
"purchased_power",
|
"purchased_power",
|
||||||
"production_power",
|
"production_power",
|
||||||
@ -233,6 +236,11 @@ class SolarEdgePowerFlowSensor(SolarEdgeSensor):
|
|||||||
"""Return the state attributes."""
|
"""Return the state attributes."""
|
||||||
return self._attributes
|
return self._attributes
|
||||||
|
|
||||||
|
@property
|
||||||
|
def device_class(self):
|
||||||
|
"""Device Class."""
|
||||||
|
return DEVICE_CLASS_POWER
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
"""Get the latest inventory data and update state and attributes."""
|
"""Get the latest inventory data and update state and attributes."""
|
||||||
self.data_service.update()
|
self.data_service.update()
|
||||||
@ -241,6 +249,27 @@ class SolarEdgePowerFlowSensor(SolarEdgeSensor):
|
|||||||
self._unit_of_measurement = self.data_service.unit
|
self._unit_of_measurement = self.data_service.unit
|
||||||
|
|
||||||
|
|
||||||
|
class SolarEdgeStorageLevelSensor(SolarEdgeSensor):
|
||||||
|
"""Representation of an SolarEdge Monitoring API storage level sensor."""
|
||||||
|
|
||||||
|
def __init__(self, platform_name, sensor_key, data_service):
|
||||||
|
"""Initialize the storage level sensor."""
|
||||||
|
super().__init__(platform_name, sensor_key, data_service)
|
||||||
|
|
||||||
|
self._json_key = SENSOR_TYPES[self.sensor_key][0]
|
||||||
|
|
||||||
|
@property
|
||||||
|
def device_class(self):
|
||||||
|
"""Return the device_class of the device."""
|
||||||
|
return DEVICE_CLASS_BATTERY
|
||||||
|
|
||||||
|
def update(self):
|
||||||
|
"""Get the latest inventory data and update state and attributes."""
|
||||||
|
self.data_service.update()
|
||||||
|
attr = self.data_service.attributes.get(self._json_key)
|
||||||
|
self._state = attr["soc"]
|
||||||
|
|
||||||
|
|
||||||
class SolarEdgeDataService:
|
class SolarEdgeDataService:
|
||||||
"""Get and update the latest data."""
|
"""Get and update the latest data."""
|
||||||
|
|
||||||
@ -470,6 +499,7 @@ class SolarEdgePowerFlowDataService(SolarEdgeDataService):
|
|||||||
charge = key.lower() in power_to
|
charge = key.lower() in power_to
|
||||||
self.data[key] *= -1 if charge else 1
|
self.data[key] *= -1 if charge else 1
|
||||||
self.attributes[key]["flow"] = "charge" if charge else "discharge"
|
self.attributes[key]["flow"] = "charge" if charge else "discharge"
|
||||||
|
self.attributes[key]["soc"] = value["chargeLevel"]
|
||||||
|
|
||||||
_LOGGER.debug(
|
_LOGGER.debug(
|
||||||
"Updated SolarEdge power flow: %s, %s", self.data, self.attributes
|
"Updated SolarEdge power flow: %s, %s", self.data, self.attributes
|
||||||
|
Loading…
x
Reference in New Issue
Block a user