Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include charging state for powerwall #33432

Merged
merged 2 commits into from
Apr 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion homeassistant/components/powerwall/binary_sensor.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
"""Support for August sensors."""
"""Support for powerwall binary sensors."""
import logging

from tesla_powerwall import GridStatus

from homeassistant.components.binary_sensor import (
DEVICE_CLASS_BATTERY_CHARGING,
DEVICE_CLASS_CONNECTIVITY,
BinarySensorDevice,
)
Expand All @@ -13,13 +14,16 @@
ATTR_GRID_CODE,
ATTR_NOMINAL_SYSTEM_POWER,
ATTR_REGION,
CHARGING_MARGIN_OF_ERROR,
DOMAIN,
POWERWALL_API_DEVICE_TYPE,
POWERWALL_API_GRID_STATUS,
POWERWALL_API_METERS,
POWERWALL_API_SERIAL_NUMBERS,
POWERWALL_API_SITE_INFO,
POWERWALL_API_SITEMASTER,
POWERWALL_API_STATUS,
POWERWALL_BATTERY_METER,
POWERWALL_COORDINATOR,
)
from .entity import PowerWallEntity
Expand All @@ -42,6 +46,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
PowerWallRunningSensor,
PowerWallGridStatusSensor,
PowerWallConnectedSensor,
PowerWallChargingStatusSensor,
):
entities.append(
sensor_class(
Expand Down Expand Up @@ -131,3 +136,32 @@ def unique_id(self):
def is_on(self):
"""Grid is online."""
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
)
6 changes: 6 additions & 0 deletions homeassistant/components/powerwall/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@
POWERWALL_CONNECTED_KEY = "connected_to_tesla"
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"
MANUFACTURER = "Tesla"
Expand Down
10 changes: 10 additions & 0 deletions tests/components/powerwall/test_binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,13 @@ async def test_sensors(hass):
# 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.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())