mirror of
https://github.com/home-assistant/core.git
synced 2025-07-25 06:07:17 +00:00
Fix powerwall data incompatibility with energy integration (#67245)
This commit is contained in:
parent
6fd9e71b8f
commit
3f16c6d6ef
@ -3,7 +3,7 @@ from __future__ import annotations
|
|||||||
|
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from tesla_powerwall import MeterType
|
from tesla_powerwall import Meter, MeterType
|
||||||
|
|
||||||
from homeassistant.components.sensor import (
|
from homeassistant.components.sensor import (
|
||||||
SensorDeviceClass,
|
SensorDeviceClass,
|
||||||
@ -28,7 +28,6 @@ from .models import PowerwallData, PowerwallRuntimeData
|
|||||||
|
|
||||||
_METER_DIRECTION_EXPORT = "export"
|
_METER_DIRECTION_EXPORT = "export"
|
||||||
_METER_DIRECTION_IMPORT = "import"
|
_METER_DIRECTION_IMPORT = "import"
|
||||||
_METER_DIRECTIONS = [_METER_DIRECTION_EXPORT, _METER_DIRECTION_IMPORT]
|
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
@ -42,20 +41,20 @@ async def async_setup_entry(
|
|||||||
assert coordinator is not None
|
assert coordinator is not None
|
||||||
data: PowerwallData = coordinator.data
|
data: PowerwallData = coordinator.data
|
||||||
entities: list[
|
entities: list[
|
||||||
PowerWallEnergySensor | PowerWallEnergyDirectionSensor | PowerWallChargeSensor
|
PowerWallEnergySensor
|
||||||
] = []
|
| PowerWallImportSensor
|
||||||
for meter in data.meters.meters:
|
| PowerWallExportSensor
|
||||||
entities.append(PowerWallEnergySensor(powerwall_data, meter))
|
| PowerWallChargeSensor
|
||||||
for meter_direction in _METER_DIRECTIONS:
|
] = [PowerWallChargeSensor(powerwall_data)]
|
||||||
entities.append(
|
|
||||||
PowerWallEnergyDirectionSensor(
|
|
||||||
powerwall_data,
|
|
||||||
meter,
|
|
||||||
meter_direction,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
entities.append(PowerWallChargeSensor(powerwall_data))
|
for meter in data.meters.meters:
|
||||||
|
entities.extend(
|
||||||
|
[
|
||||||
|
PowerWallEnergySensor(powerwall_data, meter),
|
||||||
|
PowerWallExportSensor(powerwall_data, meter),
|
||||||
|
PowerWallImportSensor(powerwall_data, meter),
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
async_add_entities(entities)
|
async_add_entities(entities)
|
||||||
|
|
||||||
@ -128,18 +127,54 @@ class PowerWallEnergyDirectionSensor(PowerWallEntity, SensorEntity):
|
|||||||
"""Initialize the sensor."""
|
"""Initialize the sensor."""
|
||||||
super().__init__(powerwall_data)
|
super().__init__(powerwall_data)
|
||||||
self._meter = meter
|
self._meter = meter
|
||||||
self._meter_direction = meter_direction
|
self._attr_name = f"Powerwall {meter.value.title()} {meter_direction.title()}"
|
||||||
self._attr_name = (
|
self._attr_unique_id = f"{self.base_unique_id}_{meter.value}_{meter_direction}"
|
||||||
f"Powerwall {self._meter.value.title()} {self._meter_direction.title()}"
|
|
||||||
)
|
@property
|
||||||
self._attr_unique_id = (
|
def available(self) -> bool:
|
||||||
f"{self.base_unique_id}_{self._meter.value}_{self._meter_direction}"
|
"""Check if the reading is actually available.
|
||||||
)
|
|
||||||
|
The device reports 0 when something goes wrong which
|
||||||
|
we do not want to include in statistics and its a
|
||||||
|
transient data error.
|
||||||
|
"""
|
||||||
|
return super().available and self.native_value != 0
|
||||||
|
|
||||||
|
@property
|
||||||
|
def meter(self) -> Meter:
|
||||||
|
"""Get the meter for the sensor."""
|
||||||
|
return self.data.meters.get_meter(self._meter)
|
||||||
|
|
||||||
|
|
||||||
|
class PowerWallExportSensor(PowerWallEnergyDirectionSensor):
|
||||||
|
"""Representation of an Powerwall Export sensor."""
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
powerwall_data: PowerwallRuntimeData,
|
||||||
|
meter: MeterType,
|
||||||
|
) -> None:
|
||||||
|
"""Initialize the sensor."""
|
||||||
|
super().__init__(powerwall_data, meter, _METER_DIRECTION_EXPORT)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def native_value(self) -> float:
|
def native_value(self) -> float:
|
||||||
"""Get the current value in kWh."""
|
"""Get the current value in kWh."""
|
||||||
meter = self.data.meters.get_meter(self._meter)
|
return abs(self.meter.get_energy_exported())
|
||||||
if self._meter_direction == _METER_DIRECTION_EXPORT:
|
|
||||||
return meter.get_energy_exported()
|
|
||||||
return meter.get_energy_imported()
|
class PowerWallImportSensor(PowerWallEnergyDirectionSensor):
|
||||||
|
"""Representation of an Powerwall Import sensor."""
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
powerwall_data: PowerwallRuntimeData,
|
||||||
|
meter: MeterType,
|
||||||
|
) -> None:
|
||||||
|
"""Initialize the sensor."""
|
||||||
|
super().__init__(powerwall_data, meter, _METER_DIRECTION_IMPORT)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def native_value(self) -> float:
|
||||||
|
"""Get the current value in kWh."""
|
||||||
|
return abs(self.meter.get_energy_imported())
|
||||||
|
Loading…
x
Reference in New Issue
Block a user