Add grid services active sensor to telsa powerwall integration (#56317)

Co-authored-by: Franck Nijhof <git@frenck.dev>
This commit is contained in:
some-guy-in-oz 2021-10-23 10:55:25 +10:30 committed by GitHub
parent 1b42caa34a
commit b2d67f8d19
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 42 additions and 0 deletions

View File

@ -23,6 +23,7 @@ from .const import (
POWERWALL_API_CHANGED,
POWERWALL_API_CHARGE,
POWERWALL_API_DEVICE_TYPE,
POWERWALL_API_GRID_SERVICES_ACTIVE,
POWERWALL_API_GRID_STATUS,
POWERWALL_API_METERS,
POWERWALL_API_SERIAL_NUMBERS,
@ -225,6 +226,7 @@ def _fetch_powerwall_data(power_wall):
POWERWALL_API_CHARGE: power_wall.get_charge(),
POWERWALL_API_SITEMASTER: power_wall.get_sitemaster(),
POWERWALL_API_METERS: power_wall.get_meters(),
POWERWALL_API_GRID_SERVICES_ACTIVE: power_wall.is_grid_services_active(),
POWERWALL_API_GRID_STATUS: power_wall.get_grid_status(),
}

View File

@ -11,6 +11,7 @@ from homeassistant.const import DEVICE_CLASS_POWER
from .const import (
DOMAIN,
POWERWALL_API_DEVICE_TYPE,
POWERWALL_API_GRID_SERVICES_ACTIVE,
POWERWALL_API_GRID_STATUS,
POWERWALL_API_METERS,
POWERWALL_API_SERIAL_NUMBERS,
@ -35,6 +36,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
entities = []
for sensor_class in (
PowerWallRunningSensor,
PowerWallGridServicesActiveSensor,
PowerWallGridStatusSensor,
PowerWallConnectedSensor,
PowerWallChargingStatusSensor,
@ -96,6 +98,30 @@ class PowerWallConnectedSensor(PowerWallEntity, BinarySensorEntity):
return self.coordinator.data[POWERWALL_API_SITEMASTER].is_connected_to_tesla
class PowerWallGridServicesActiveSensor(PowerWallEntity, BinarySensorEntity):
"""Representation of a Powerwall grid services active sensor."""
@property
def name(self):
"""Device Name."""
return "Grid Services Active"
@property
def device_class(self):
"""Device Class."""
return DEVICE_CLASS_POWER
@property
def unique_id(self):
"""Device Uniqueid."""
return f"{self.base_unique_id}_grid_services_active"
@property
def is_on(self):
"""Grid services is active."""
return self.coordinator.data[POWERWALL_API_GRID_SERVICES_ACTIVE]
class PowerWallGridStatusSensor(PowerWallEntity, BinarySensorEntity):
"""Representation of an Powerwall grid status sensor."""

View File

@ -19,6 +19,7 @@ POWERWALL_SITE_NAME = "site_name"
POWERWALL_API_METERS = "meters"
POWERWALL_API_CHARGE = "charge"
POWERWALL_API_GRID_SERVICES_ACTIVE = "grid_services_active"
POWERWALL_API_GRID_STATUS = "grid_status"
POWERWALL_API_SITEMASTER = "sitemaster"
POWERWALL_API_STATUS = "status"

View File

@ -30,6 +30,7 @@ async def _mock_powerwall_with_fixtures(hass):
charge=47.34587394586,
sitemaster=SiteMaster(sitemaster),
meters=MetersAggregates(meters),
grid_services_active=True,
grid_status=GridStatus.CONNECTED,
status=PowerwallStatus(status),
device_type=DeviceType(device_type["device_type"]),
@ -42,6 +43,7 @@ def _mock_powerwall_return_value(
charge=None,
sitemaster=None,
meters=None,
grid_services_active=None,
grid_status=None,
status=None,
device_type=None,
@ -56,6 +58,7 @@ def _mock_powerwall_return_value(
powerwall_mock.get_status = Mock(return_value=status)
powerwall_mock.get_device_type = Mock(return_value=device_type)
powerwall_mock.get_serial_numbers = Mock(return_value=serial_numbers)
powerwall_mock.is_grid_services_active = Mock(return_value=grid_services_active)
return powerwall_mock

View File

@ -26,6 +26,16 @@ async def test_sensors(hass):
assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
state = hass.states.get("binary_sensor.grid_services_active")
assert state.state == STATE_ON
expected_attributes = {
"friendly_name": "Grid Services Active",
"device_class": "power",
}
# Only test for a subset of attributes in case
# HA changes the implementation and a new one appears
assert all(item in state.attributes.items() for item in expected_attributes.items())
state = hass.states.get("binary_sensor.grid_status")
assert state.state == STATE_ON
expected_attributes = {"friendly_name": "Grid Status", "device_class": "power"}