Include charging state for powerwall (#33432)

* Switch to binary sensor for charging status per review

* Powerwall charging margin of error is much higher than expected
This commit is contained in:
J. Nick Koston 2020-04-19 20:50:42 -05:00 committed by GitHub
parent 675525f47c
commit 75e5f085d3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 51 additions and 1 deletions

View file

@ -1,9 +1,10 @@
"""Support for August sensors.""" """Support for powerwall binary sensors."""
import logging import logging
from tesla_powerwall import GridStatus from tesla_powerwall import GridStatus
from homeassistant.components.binary_sensor import ( from homeassistant.components.binary_sensor import (
DEVICE_CLASS_BATTERY_CHARGING,
DEVICE_CLASS_CONNECTIVITY, DEVICE_CLASS_CONNECTIVITY,
BinarySensorDevice, BinarySensorDevice,
) )
@ -13,13 +14,16 @@ from .const import (
ATTR_GRID_CODE, ATTR_GRID_CODE,
ATTR_NOMINAL_SYSTEM_POWER, ATTR_NOMINAL_SYSTEM_POWER,
ATTR_REGION, ATTR_REGION,
CHARGING_MARGIN_OF_ERROR,
DOMAIN, DOMAIN,
POWERWALL_API_DEVICE_TYPE, POWERWALL_API_DEVICE_TYPE,
POWERWALL_API_GRID_STATUS, POWERWALL_API_GRID_STATUS,
POWERWALL_API_METERS,
POWERWALL_API_SERIAL_NUMBERS, POWERWALL_API_SERIAL_NUMBERS,
POWERWALL_API_SITE_INFO, POWERWALL_API_SITE_INFO,
POWERWALL_API_SITEMASTER, POWERWALL_API_SITEMASTER,
POWERWALL_API_STATUS, POWERWALL_API_STATUS,
POWERWALL_BATTERY_METER,
POWERWALL_COORDINATOR, POWERWALL_COORDINATOR,
) )
from .entity import PowerWallEntity from .entity import PowerWallEntity
@ -42,6 +46,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
PowerWallRunningSensor, PowerWallRunningSensor,
PowerWallGridStatusSensor, PowerWallGridStatusSensor,
PowerWallConnectedSensor, PowerWallConnectedSensor,
PowerWallChargingStatusSensor,
): ):
entities.append( entities.append(
sensor_class( sensor_class(
@ -131,3 +136,32 @@ class PowerWallGridStatusSensor(PowerWallEntity, BinarySensorDevice):
def is_on(self): def is_on(self):
"""Grid is online.""" """Grid is online."""
return self._coordinator.data[POWERWALL_API_GRID_STATUS] == GridStatus.CONNECTED return self._coordinator.data[POWERWALL_API_GRID_STATUS] == GridStatus.CONNECTED
class PowerWallChargingStatusSensor(PowerWallEntity, BinarySensorDevice):
"""Representation of an Powerwall grid status sensor."""
@property
def name(self):
"""Device Name."""
return "Powerwall Charging"
@property
def device_class(self):
"""Device Class."""
return DEVICE_CLASS_BATTERY_CHARGING
@property
def unique_id(self):
"""Device Uniqueid."""
return f"{self.base_unique_id}_powerwall_charging"
@property
def is_on(self):
"""Grid is online."""
return (
self._coordinator.data[POWERWALL_API_METERS][
POWERWALL_BATTERY_METER
].instant_power
< CHARGING_MARGIN_OF_ERROR
)

View file

@ -42,6 +42,12 @@ POWERWALL_GRID_ONLINE = "SystemGridConnected"
POWERWALL_CONNECTED_KEY = "connected_to_tesla" POWERWALL_CONNECTED_KEY = "connected_to_tesla"
POWERWALL_RUNNING_KEY = "running" POWERWALL_RUNNING_KEY = "running"
POWERWALL_BATTERY_METER = "battery"
# We only declare charging if they are getting
# at least 40W incoming as measuring the fields
# is not an exact science because of interference
CHARGING_MARGIN_OF_ERROR = -40
MODEL = "PowerWall 2" MODEL = "PowerWall 2"
MANUFACTURER = "Tesla" MANUFACTURER = "Tesla"

View file

@ -52,3 +52,13 @@ async def test_sensors(hass):
# Only test for a subset of attributes in case # Only test for a subset of attributes in case
# HA changes the implementation and a new one appears # HA changes the implementation and a new one appears
assert all(item in state.attributes.items() for item in expected_attributes.items()) assert all(item in state.attributes.items() for item in expected_attributes.items())
state = hass.states.get("binary_sensor.powerwall_charging")
assert state.state == STATE_ON
expected_attributes = {
"friendly_name": "Powerwall Charging",
"device_class": "battery_charging",
}
# 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())