From 4a5cf5cd2b01306bffc7a106ab7dff0388d4fc83 Mon Sep 17 00:00:00 2001 From: jrester <31157644+jrester@users.noreply.github.com> Date: Thu, 23 Apr 2020 17:00:38 +0200 Subject: [PATCH] Powerwall sensor add is_active, round state attributes and change thresholding for charging status sensor (#34582) * Change sensor values and thresholding * Update tests --- .../components/powerwall/binary_sensor.py | 15 ++++------ homeassistant/components/powerwall/const.py | 10 ++----- homeassistant/components/powerwall/sensor.py | 12 ++++---- tests/components/powerwall/test_sensor.py | 30 +++++++++++-------- 4 files changed, 33 insertions(+), 34 deletions(-) diff --git a/homeassistant/components/powerwall/binary_sensor.py b/homeassistant/components/powerwall/binary_sensor.py index 889e6c192ef..877efdd68fa 100644 --- a/homeassistant/components/powerwall/binary_sensor.py +++ b/homeassistant/components/powerwall/binary_sensor.py @@ -14,7 +14,6 @@ from .const import ( ATTR_GRID_CODE, ATTR_NOMINAL_SYSTEM_POWER, ATTR_REGION, - CHARGING_MARGIN_OF_ERROR, DOMAIN, POWERWALL_API_DEVICE_TYPE, POWERWALL_API_GRID_STATUS, @@ -139,7 +138,7 @@ class PowerWallGridStatusSensor(PowerWallEntity, BinarySensorDevice): class PowerWallChargingStatusSensor(PowerWallEntity, BinarySensorDevice): - """Representation of an Powerwall grid status sensor.""" + """Representation of an Powerwall charging status sensor.""" @property def name(self): @@ -158,10 +157,8 @@ class PowerWallChargingStatusSensor(PowerWallEntity, BinarySensorDevice): @property def is_on(self): - """Grid is online.""" - return ( - self._coordinator.data[POWERWALL_API_METERS][ - POWERWALL_BATTERY_METER - ].instant_power - < CHARGING_MARGIN_OF_ERROR - ) + """Powerwall is charging.""" + # is_sending_to returns true for values greater than 100 watts + return self._coordinator.data[POWERWALL_API_METERS][ + POWERWALL_BATTERY_METER + ].is_sending_to() diff --git a/homeassistant/components/powerwall/const.py b/homeassistant/components/powerwall/const.py index 91e501da28f..e2daf1e3760 100644 --- a/homeassistant/components/powerwall/const.py +++ b/homeassistant/components/powerwall/const.py @@ -10,10 +10,11 @@ UPDATE_INTERVAL = 30 ATTR_REGION = "region" ATTR_GRID_CODE = "grid_code" ATTR_FREQUENCY = "frequency" -ATTR_ENERGY_EXPORTED = "energy_exported" -ATTR_ENERGY_IMPORTED = "energy_imported" +ATTR_ENERGY_EXPORTED = "energy_exported_(in_kW)" +ATTR_ENERGY_IMPORTED = "energy_imported_(in_kW)" ATTR_INSTANT_AVERAGE_VOLTAGE = "instant_average_voltage" ATTR_NOMINAL_SYSTEM_POWER = "nominal_system_power_kW" +ATTR_IS_ACTIVE = "is_active" SITE_INFO_UTILITY = "utility" SITE_INFO_GRID_CODE = "grid_code" @@ -44,11 +45,6 @@ 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" diff --git a/homeassistant/components/powerwall/sensor.py b/homeassistant/components/powerwall/sensor.py index 253fae8bd36..e1e968c0353 100644 --- a/homeassistant/components/powerwall/sensor.py +++ b/homeassistant/components/powerwall/sensor.py @@ -1,7 +1,7 @@ """Support for August sensors.""" import logging -from tesla_powerwall import MeterType +from tesla_powerwall import MeterType, convert_to_kw from homeassistant.const import ( DEVICE_CLASS_BATTERY, @@ -14,6 +14,7 @@ from .const import ( ATTR_ENERGY_IMPORTED, ATTR_FREQUENCY, ATTR_INSTANT_AVERAGE_VOLTAGE, + ATTR_IS_ACTIVE, DOMAIN, ENERGY_KILO_WATT, POWERWALL_API_CHARGE, @@ -143,8 +144,9 @@ class PowerWallEnergySensor(PowerWallEntity): """Return the device specific state attributes.""" meter = self._coordinator.data[POWERWALL_API_METERS].get(self._meter) return { - ATTR_FREQUENCY: meter.frequency, - ATTR_ENERGY_EXPORTED: meter.energy_exported, - ATTR_ENERGY_IMPORTED: meter.energy_imported, - ATTR_INSTANT_AVERAGE_VOLTAGE: meter.instant_average_voltage, + ATTR_FREQUENCY: round(meter.frequency, 1), + ATTR_ENERGY_EXPORTED: convert_to_kw(meter.energy_exported), + ATTR_ENERGY_IMPORTED: convert_to_kw(meter.energy_imported), + ATTR_INSTANT_AVERAGE_VOLTAGE: round(meter.instant_average_voltage, 1), + ATTR_IS_ACTIVE: meter.is_active(), } diff --git a/tests/components/powerwall/test_sensor.py b/tests/components/powerwall/test_sensor.py index b6c224080a1..c68d9f0279e 100644 --- a/tests/components/powerwall/test_sensor.py +++ b/tests/components/powerwall/test_sensor.py @@ -36,12 +36,13 @@ async def test_sensors(hass): assert state.state == "0.032" expected_attributes = { "frequency": 60, - "energy_exported": 10429451.9916853, - "energy_imported": 4824191.60668611, - "instant_average_voltage": 120.650001525879, + "energy_exported_(in_kW)": 10429.5, + "energy_imported_(in_kW)": 4824.2, + "instant_average_voltage": 120.7, "unit_of_measurement": "kW", "friendly_name": "Powerwall Site Now", "device_class": "power", + "is_active": False, } # Only test for a subset of attributes in case # HA changes the implementation and a new one appears @@ -52,12 +53,13 @@ async def test_sensors(hass): assert state.state == "1.971" expected_attributes = { "frequency": 60, - "energy_exported": 1056797.48917483, - "energy_imported": 4692987.91889705, - "instant_average_voltage": 120.650001525879, + "energy_exported_(in_kW)": 1056.8, + "energy_imported_(in_kW)": 4693.0, + "instant_average_voltage": 120.7, "unit_of_measurement": "kW", "friendly_name": "Powerwall Load Now", "device_class": "power", + "is_active": True, } # Only test for a subset of attributes in case # HA changes the implementation and a new one appears @@ -67,13 +69,14 @@ async def test_sensors(hass): state = hass.states.get("sensor.powerwall_battery_now") assert state.state == "-8.55" expected_attributes = { - "frequency": 60.014, - "energy_exported": 3620010, - "energy_imported": 4216170, - "instant_average_voltage": 240.56, + "frequency": 60.0, + "energy_exported_(in_kW)": 3620.0, + "energy_imported_(in_kW)": 4216.2, + "instant_average_voltage": 240.6, "unit_of_measurement": "kW", "friendly_name": "Powerwall Battery Now", "device_class": "power", + "is_active": True, } # Only test for a subset of attributes in case # HA changes the implementation and a new one appears @@ -84,12 +87,13 @@ async def test_sensors(hass): assert state.state == "10.49" expected_attributes = { "frequency": 60, - "energy_exported": 9864205.82222448, - "energy_imported": 28177.5358355867, - "instant_average_voltage": 120.685001373291, + "energy_exported_(in_kW)": 9864.2, + "energy_imported_(in_kW)": 28.2, + "instant_average_voltage": 120.7, "unit_of_measurement": "kW", "friendly_name": "Powerwall Solar Now", "device_class": "power", + "is_active": True, } # Only test for a subset of attributes in case # HA changes the implementation and a new one appears