mirror of
https://github.com/home-assistant/core.git
synced 2025-07-26 22:57:17 +00:00
Add powerwall import and export sensors (#54018)
Co-authored-by: Bram Kragten <mail@bramkragten.nl>
This commit is contained in:
parent
3184f0697f
commit
25f3cdde50
@ -9,8 +9,6 @@ POWERWALL_API_CHANGED = "api_changed"
|
|||||||
UPDATE_INTERVAL = 30
|
UPDATE_INTERVAL = 30
|
||||||
|
|
||||||
ATTR_FREQUENCY = "frequency"
|
ATTR_FREQUENCY = "frequency"
|
||||||
ATTR_ENERGY_EXPORTED = "energy_exported_(in_kW)"
|
|
||||||
ATTR_ENERGY_IMPORTED = "energy_imported_(in_kW)"
|
|
||||||
ATTR_INSTANT_AVERAGE_VOLTAGE = "instant_average_voltage"
|
ATTR_INSTANT_AVERAGE_VOLTAGE = "instant_average_voltage"
|
||||||
ATTR_INSTANT_TOTAL_CURRENT = "instant_total_current"
|
ATTR_INSTANT_TOTAL_CURRENT = "instant_total_current"
|
||||||
ATTR_IS_ACTIVE = "is_active"
|
ATTR_IS_ACTIVE = "is_active"
|
||||||
|
@ -6,14 +6,15 @@ from tesla_powerwall import MeterType
|
|||||||
from homeassistant.components.sensor import STATE_CLASS_MEASUREMENT, SensorEntity
|
from homeassistant.components.sensor import STATE_CLASS_MEASUREMENT, SensorEntity
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
DEVICE_CLASS_BATTERY,
|
DEVICE_CLASS_BATTERY,
|
||||||
|
DEVICE_CLASS_ENERGY,
|
||||||
DEVICE_CLASS_POWER,
|
DEVICE_CLASS_POWER,
|
||||||
|
ENERGY_KILO_WATT_HOUR,
|
||||||
PERCENTAGE,
|
PERCENTAGE,
|
||||||
POWER_KILO_WATT,
|
POWER_KILO_WATT,
|
||||||
)
|
)
|
||||||
|
import homeassistant.util.dt as dt_util
|
||||||
|
|
||||||
from .const import (
|
from .const import (
|
||||||
ATTR_ENERGY_EXPORTED,
|
|
||||||
ATTR_ENERGY_IMPORTED,
|
|
||||||
ATTR_FREQUENCY,
|
ATTR_FREQUENCY,
|
||||||
ATTR_INSTANT_AVERAGE_VOLTAGE,
|
ATTR_INSTANT_AVERAGE_VOLTAGE,
|
||||||
ATTR_INSTANT_TOTAL_CURRENT,
|
ATTR_INSTANT_TOTAL_CURRENT,
|
||||||
@ -29,6 +30,11 @@ from .const import (
|
|||||||
)
|
)
|
||||||
from .entity import PowerWallEntity
|
from .entity import PowerWallEntity
|
||||||
|
|
||||||
|
_METER_DIRECTION_EXPORT = "export"
|
||||||
|
_METER_DIRECTION_IMPORT = "import"
|
||||||
|
_METER_DIRECTIONS = [_METER_DIRECTION_EXPORT, _METER_DIRECTION_IMPORT]
|
||||||
|
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
@ -55,6 +61,18 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
|||||||
powerwalls_serial_numbers,
|
powerwalls_serial_numbers,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
for meter_direction in _METER_DIRECTIONS:
|
||||||
|
entities.append(
|
||||||
|
PowerWallEnergyDirectionSensor(
|
||||||
|
meter,
|
||||||
|
coordinator,
|
||||||
|
site_info,
|
||||||
|
status,
|
||||||
|
device_type,
|
||||||
|
powerwalls_serial_numbers,
|
||||||
|
meter_direction,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
entities.append(
|
entities.append(
|
||||||
PowerWallChargeSensor(
|
PowerWallChargeSensor(
|
||||||
@ -124,9 +142,47 @@ class PowerWallEnergySensor(PowerWallEntity, SensorEntity):
|
|||||||
meter = self.coordinator.data[POWERWALL_API_METERS].get_meter(self._meter)
|
meter = self.coordinator.data[POWERWALL_API_METERS].get_meter(self._meter)
|
||||||
return {
|
return {
|
||||||
ATTR_FREQUENCY: round(meter.frequency, 1),
|
ATTR_FREQUENCY: round(meter.frequency, 1),
|
||||||
ATTR_ENERGY_EXPORTED: meter.get_energy_exported(),
|
|
||||||
ATTR_ENERGY_IMPORTED: meter.get_energy_imported(),
|
|
||||||
ATTR_INSTANT_AVERAGE_VOLTAGE: round(meter.average_voltage, 1),
|
ATTR_INSTANT_AVERAGE_VOLTAGE: round(meter.average_voltage, 1),
|
||||||
ATTR_INSTANT_TOTAL_CURRENT: meter.get_instant_total_current(),
|
ATTR_INSTANT_TOTAL_CURRENT: meter.get_instant_total_current(),
|
||||||
ATTR_IS_ACTIVE: meter.is_active(),
|
ATTR_IS_ACTIVE: meter.is_active(),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class PowerWallEnergyDirectionSensor(PowerWallEntity, SensorEntity):
|
||||||
|
"""Representation of an Powerwall Direction Energy sensor."""
|
||||||
|
|
||||||
|
_attr_state_class = STATE_CLASS_MEASUREMENT
|
||||||
|
_attr_unit_of_measurement = ENERGY_KILO_WATT_HOUR
|
||||||
|
_attr_device_class = DEVICE_CLASS_ENERGY
|
||||||
|
_attr_last_reset = dt_util.utc_from_timestamp(0)
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
meter: MeterType,
|
||||||
|
coordinator,
|
||||||
|
site_info,
|
||||||
|
status,
|
||||||
|
device_type,
|
||||||
|
powerwalls_serial_numbers,
|
||||||
|
meter_direction,
|
||||||
|
):
|
||||||
|
"""Initialize the sensor."""
|
||||||
|
super().__init__(
|
||||||
|
coordinator, site_info, status, device_type, powerwalls_serial_numbers
|
||||||
|
)
|
||||||
|
self._meter = meter
|
||||||
|
self._meter_direction = meter_direction
|
||||||
|
self._attr_name = (
|
||||||
|
f"Powerwall {self._meter.value.title()} {self._meter_direction.title()}"
|
||||||
|
)
|
||||||
|
self._attr_unique_id = (
|
||||||
|
f"{self.base_unique_id}_{self._meter.value}_{self._meter_direction}"
|
||||||
|
)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def state(self):
|
||||||
|
"""Get the current value in kWh."""
|
||||||
|
meter = self.coordinator.data[POWERWALL_API_METERS].get_meter(self._meter)
|
||||||
|
if self._meter_direction == _METER_DIRECTION_EXPORT:
|
||||||
|
return meter.get_energy_exported()
|
||||||
|
return meter.get_energy_imported()
|
||||||
|
@ -39,8 +39,6 @@ async def test_sensors(hass):
|
|||||||
assert state.state == "0.032"
|
assert state.state == "0.032"
|
||||||
expected_attributes = {
|
expected_attributes = {
|
||||||
"frequency": 60,
|
"frequency": 60,
|
||||||
"energy_exported_(in_kW)": 10429.5,
|
|
||||||
"energy_imported_(in_kW)": 4824.2,
|
|
||||||
"instant_average_voltage": 120.7,
|
"instant_average_voltage": 120.7,
|
||||||
"unit_of_measurement": "kW",
|
"unit_of_measurement": "kW",
|
||||||
"friendly_name": "Powerwall Site Now",
|
"friendly_name": "Powerwall Site Now",
|
||||||
@ -52,12 +50,16 @@ async def test_sensors(hass):
|
|||||||
for key, value in expected_attributes.items():
|
for key, value in expected_attributes.items():
|
||||||
assert state.attributes[key] == value
|
assert state.attributes[key] == value
|
||||||
|
|
||||||
|
assert float(hass.states.get("sensor.powerwall_site_export").state) == 10429.5
|
||||||
|
assert float(hass.states.get("sensor.powerwall_site_import").state) == 4824.2
|
||||||
|
|
||||||
|
export_attributes = hass.states.get("sensor.powerwall_site_export").attributes
|
||||||
|
assert export_attributes["unit_of_measurement"] == "kWh"
|
||||||
|
|
||||||
state = hass.states.get("sensor.powerwall_load_now")
|
state = hass.states.get("sensor.powerwall_load_now")
|
||||||
assert state.state == "1.971"
|
assert state.state == "1.971"
|
||||||
expected_attributes = {
|
expected_attributes = {
|
||||||
"frequency": 60,
|
"frequency": 60,
|
||||||
"energy_exported_(in_kW)": 1056.8,
|
|
||||||
"energy_imported_(in_kW)": 4693.0,
|
|
||||||
"instant_average_voltage": 120.7,
|
"instant_average_voltage": 120.7,
|
||||||
"unit_of_measurement": "kW",
|
"unit_of_measurement": "kW",
|
||||||
"friendly_name": "Powerwall Load Now",
|
"friendly_name": "Powerwall Load Now",
|
||||||
@ -69,12 +71,13 @@ async def test_sensors(hass):
|
|||||||
for key, value in expected_attributes.items():
|
for key, value in expected_attributes.items():
|
||||||
assert state.attributes[key] == value
|
assert state.attributes[key] == value
|
||||||
|
|
||||||
|
assert float(hass.states.get("sensor.powerwall_load_export").state) == 1056.8
|
||||||
|
assert float(hass.states.get("sensor.powerwall_load_import").state) == 4693.0
|
||||||
|
|
||||||
state = hass.states.get("sensor.powerwall_battery_now")
|
state = hass.states.get("sensor.powerwall_battery_now")
|
||||||
assert state.state == "-8.55"
|
assert state.state == "-8.55"
|
||||||
expected_attributes = {
|
expected_attributes = {
|
||||||
"frequency": 60.0,
|
"frequency": 60.0,
|
||||||
"energy_exported_(in_kW)": 3620.0,
|
|
||||||
"energy_imported_(in_kW)": 4216.2,
|
|
||||||
"instant_average_voltage": 240.6,
|
"instant_average_voltage": 240.6,
|
||||||
"unit_of_measurement": "kW",
|
"unit_of_measurement": "kW",
|
||||||
"friendly_name": "Powerwall Battery Now",
|
"friendly_name": "Powerwall Battery Now",
|
||||||
@ -86,12 +89,13 @@ async def test_sensors(hass):
|
|||||||
for key, value in expected_attributes.items():
|
for key, value in expected_attributes.items():
|
||||||
assert state.attributes[key] == value
|
assert state.attributes[key] == value
|
||||||
|
|
||||||
|
assert float(hass.states.get("sensor.powerwall_battery_export").state) == 3620.0
|
||||||
|
assert float(hass.states.get("sensor.powerwall_battery_import").state) == 4216.2
|
||||||
|
|
||||||
state = hass.states.get("sensor.powerwall_solar_now")
|
state = hass.states.get("sensor.powerwall_solar_now")
|
||||||
assert state.state == "10.49"
|
assert state.state == "10.49"
|
||||||
expected_attributes = {
|
expected_attributes = {
|
||||||
"frequency": 60,
|
"frequency": 60,
|
||||||
"energy_exported_(in_kW)": 9864.2,
|
|
||||||
"energy_imported_(in_kW)": 28.2,
|
|
||||||
"instant_average_voltage": 120.7,
|
"instant_average_voltage": 120.7,
|
||||||
"unit_of_measurement": "kW",
|
"unit_of_measurement": "kW",
|
||||||
"friendly_name": "Powerwall Solar Now",
|
"friendly_name": "Powerwall Solar Now",
|
||||||
@ -103,6 +107,9 @@ async def test_sensors(hass):
|
|||||||
for key, value in expected_attributes.items():
|
for key, value in expected_attributes.items():
|
||||||
assert state.attributes[key] == value
|
assert state.attributes[key] == value
|
||||||
|
|
||||||
|
assert float(hass.states.get("sensor.powerwall_solar_export").state) == 9864.2
|
||||||
|
assert float(hass.states.get("sensor.powerwall_solar_import").state) == 28.2
|
||||||
|
|
||||||
state = hass.states.get("sensor.powerwall_charge")
|
state = hass.states.get("sensor.powerwall_charge")
|
||||||
assert state.state == "47"
|
assert state.state == "47"
|
||||||
expected_attributes = {
|
expected_attributes = {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user