Fix powerwall units (kW) (#33954)

* Fix powerwall units (kW)

* Fix test

Co-authored-by: Paulus Schoutsen <balloob@gmail.com>
This commit is contained in:
J. Nick Koston 2020-04-10 11:33:58 -05:00 committed by GitHub
parent 7b9585cd8e
commit ca0648afe8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 21 additions and 16 deletions

View File

@ -129,7 +129,7 @@ class PowerWallGridStatusSensor(PowerWallEntity, BinarySensorDevice):
@property @property
def is_on(self): def is_on(self):
"""Get the current value in kWh.""" """Grid is online."""
return ( return (
self._coordinator.data[POWERWALL_API_GRID_STATUS] == POWERWALL_GRID_ONLINE self._coordinator.data[POWERWALL_API_GRID_STATUS] == POWERWALL_GRID_ONLINE
) )

View File

@ -2,12 +2,10 @@
DOMAIN = "powerwall" DOMAIN = "powerwall"
POWERWALL_SITE_NAME = "site_name"
POWERWALL_OBJECT = "powerwall" POWERWALL_OBJECT = "powerwall"
POWERWALL_COORDINATOR = "coordinator" POWERWALL_COORDINATOR = "coordinator"
UPDATE_INTERVAL = 60 UPDATE_INTERVAL = 30
ATTR_REGION = "region" ATTR_REGION = "region"
ATTR_GRID_CODE = "grid_code" ATTR_GRID_CODE = "grid_code"
@ -46,3 +44,5 @@ POWERWALL_RUNNING_KEY = "running"
MODEL = "PowerWall 2" MODEL = "PowerWall 2"
MANUFACTURER = "Tesla" MANUFACTURER = "Tesla"
ENERGY_KILO_WATT = "kW"

View File

@ -4,7 +4,6 @@ import logging
from homeassistant.const import ( from homeassistant.const import (
DEVICE_CLASS_BATTERY, DEVICE_CLASS_BATTERY,
DEVICE_CLASS_POWER, DEVICE_CLASS_POWER,
ENERGY_KILO_WATT_HOUR,
UNIT_PERCENTAGE, UNIT_PERCENTAGE,
) )
@ -14,6 +13,7 @@ from .const import (
ATTR_FREQUENCY, ATTR_FREQUENCY,
ATTR_INSTANT_AVERAGE_VOLTAGE, ATTR_INSTANT_AVERAGE_VOLTAGE,
DOMAIN, DOMAIN,
ENERGY_KILO_WATT,
POWERWALL_API_CHARGE, POWERWALL_API_CHARGE,
POWERWALL_API_DEVICE_TYPE, POWERWALL_API_DEVICE_TYPE,
POWERWALL_API_METERS, POWERWALL_API_METERS,
@ -87,7 +87,7 @@ class PowerWallEnergySensor(PowerWallEntity):
@property @property
def unit_of_measurement(self): def unit_of_measurement(self):
"""Return the unit of measurement.""" """Return the unit of measurement."""
return ENERGY_KILO_WATT_HOUR return ENERGY_KILO_WATT
@property @property
def name(self): def name(self):
@ -106,7 +106,7 @@ class PowerWallEnergySensor(PowerWallEntity):
@property @property
def state(self): def state(self):
"""Get the current value in kWh.""" """Get the current value in kW."""
meter = self._coordinator.data[POWERWALL_API_METERS][self._meter] meter = self._coordinator.data[POWERWALL_API_METERS][self._meter]
return round(float(meter.instant_power / 1000), 3) return round(float(meter.instant_power / 1000), 3)

View File

@ -39,13 +39,14 @@ async def test_sensors(hass):
"energy_exported": 10429451.9916853, "energy_exported": 10429451.9916853,
"energy_imported": 4824191.60668611, "energy_imported": 4824191.60668611,
"instant_average_voltage": 120.650001525879, "instant_average_voltage": 120.650001525879,
"unit_of_measurement": "kWh", "unit_of_measurement": "kW",
"friendly_name": "Powerwall Site Now", "friendly_name": "Powerwall Site Now",
"device_class": "power", "device_class": "power",
} }
# 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()) for key, value in expected_attributes.items():
assert state.attributes[key] == value
state = hass.states.get("sensor.powerwall_load_now") state = hass.states.get("sensor.powerwall_load_now")
assert state.state == "1.971" assert state.state == "1.971"
@ -54,13 +55,14 @@ async def test_sensors(hass):
"energy_exported": 1056797.48917483, "energy_exported": 1056797.48917483,
"energy_imported": 4692987.91889705, "energy_imported": 4692987.91889705,
"instant_average_voltage": 120.650001525879, "instant_average_voltage": 120.650001525879,
"unit_of_measurement": "kWh", "unit_of_measurement": "kW",
"friendly_name": "Powerwall Load Now", "friendly_name": "Powerwall Load Now",
"device_class": "power", "device_class": "power",
} }
# 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()) for key, value in expected_attributes.items():
assert state.attributes[key] == value
state = hass.states.get("sensor.powerwall_battery_now") state = hass.states.get("sensor.powerwall_battery_now")
assert state.state == "-8.55" assert state.state == "-8.55"
@ -69,13 +71,14 @@ async def test_sensors(hass):
"energy_exported": 3620010, "energy_exported": 3620010,
"energy_imported": 4216170, "energy_imported": 4216170,
"instant_average_voltage": 240.56, "instant_average_voltage": 240.56,
"unit_of_measurement": "kWh", "unit_of_measurement": "kW",
"friendly_name": "Powerwall Battery Now", "friendly_name": "Powerwall Battery Now",
"device_class": "power", "device_class": "power",
} }
# 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()) for key, value in expected_attributes.items():
assert state.attributes[key] == value
state = hass.states.get("sensor.powerwall_solar_now") state = hass.states.get("sensor.powerwall_solar_now")
assert state.state == "10.49" assert state.state == "10.49"
@ -84,13 +87,14 @@ async def test_sensors(hass):
"energy_exported": 9864205.82222448, "energy_exported": 9864205.82222448,
"energy_imported": 28177.5358355867, "energy_imported": 28177.5358355867,
"instant_average_voltage": 120.685001373291, "instant_average_voltage": 120.685001373291,
"unit_of_measurement": "kWh", "unit_of_measurement": "kW",
"friendly_name": "Powerwall Solar Now", "friendly_name": "Powerwall Solar Now",
"device_class": "power", "device_class": "power",
} }
# 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()) for key, value in expected_attributes.items():
assert state.attributes[key] == value
state = hass.states.get("sensor.powerwall_charge") state = hass.states.get("sensor.powerwall_charge")
assert state.state == "47.32" assert state.state == "47.32"
@ -101,4 +105,5 @@ 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()) for key, value in expected_attributes.items():
assert state.attributes[key] == value