diff --git a/homeassistant/components/powerwall/__init__.py b/homeassistant/components/powerwall/__init__.py index 6220f81e9a3..8b06b2a9c6d 100644 --- a/homeassistant/components/powerwall/__init__.py +++ b/homeassistant/components/powerwall/__init__.py @@ -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(), } diff --git a/homeassistant/components/powerwall/binary_sensor.py b/homeassistant/components/powerwall/binary_sensor.py index 1b097b93408..b4104b70f39 100644 --- a/homeassistant/components/powerwall/binary_sensor.py +++ b/homeassistant/components/powerwall/binary_sensor.py @@ -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.""" diff --git a/homeassistant/components/powerwall/const.py b/homeassistant/components/powerwall/const.py index b2cd48df276..8cc0cbc27cd 100644 --- a/homeassistant/components/powerwall/const.py +++ b/homeassistant/components/powerwall/const.py @@ -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" diff --git a/tests/components/powerwall/mocks.py b/tests/components/powerwall/mocks.py index 0e3cec7f60b..9d253c3a74b 100644 --- a/tests/components/powerwall/mocks.py +++ b/tests/components/powerwall/mocks.py @@ -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 diff --git a/tests/components/powerwall/test_binary_sensor.py b/tests/components/powerwall/test_binary_sensor.py index 006ecdfb533..8c9168c5f45 100644 --- a/tests/components/powerwall/test_binary_sensor.py +++ b/tests/components/powerwall/test_binary_sensor.py @@ -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"}