Update powerwall to use CoordinatorEntity (#39389)

This commit is contained in:
J. Nick Koston 2020-08-30 07:37:11 -05:00 committed by GitHub
parent ba75856f2b
commit f7d1cfb625
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 34 deletions

View File

@ -74,7 +74,7 @@ class PowerWallRunningSensor(PowerWallEntity, BinarySensorEntity):
@property
def is_on(self):
"""Get the powerwall running state."""
return self._coordinator.data[POWERWALL_API_SITEMASTER].running
return self.coordinator.data[POWERWALL_API_SITEMASTER].running
class PowerWallConnectedSensor(PowerWallEntity, BinarySensorEntity):
@ -98,7 +98,7 @@ class PowerWallConnectedSensor(PowerWallEntity, BinarySensorEntity):
@property
def is_on(self):
"""Get the powerwall connected to tesla state."""
return self._coordinator.data[POWERWALL_API_SITEMASTER].connected_to_tesla
return self.coordinator.data[POWERWALL_API_SITEMASTER].connected_to_tesla
class PowerWallGridStatusSensor(PowerWallEntity, BinarySensorEntity):
@ -122,7 +122,7 @@ class PowerWallGridStatusSensor(PowerWallEntity, BinarySensorEntity):
@property
def is_on(self):
"""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, BinarySensorEntity):
@ -147,6 +147,6 @@ class PowerWallChargingStatusSensor(PowerWallEntity, BinarySensorEntity):
def is_on(self):
"""Powerwall is charging."""
# is_sending_to returns true for values greater than 100 watts
return self._coordinator.data[POWERWALL_API_METERS][
return self.coordinator.data[POWERWALL_API_METERS][
POWERWALL_BATTERY_METER
].is_sending_to()

View File

@ -1,19 +1,18 @@
"""The Tesla Powerwall integration base entity."""
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from .const import DOMAIN, MANUFACTURER, MODEL
class PowerWallEntity(Entity):
class PowerWallEntity(CoordinatorEntity):
"""Base class for powerwall entities."""
def __init__(
self, coordinator, site_info, status, device_type, powerwalls_serial_numbers
):
"""Initialize the sensor."""
super().__init__()
self._coordinator = coordinator
super().__init__(coordinator)
self._site_info = site_info
self._device_type = device_type
self._version = status.version
@ -33,26 +32,3 @@ class PowerWallEntity(Entity):
device_info["model"] = model
device_info["sw_version"] = self._version
return device_info
@property
def available(self):
"""Return True if entity is available."""
return self._coordinator.last_update_success
@property
def should_poll(self):
"""Return False, updates are controlled via coordinator."""
return False
async def async_update(self):
"""Update the entity.
Only used by the generic entity update service.
"""
await self._coordinator.async_request_refresh()
async def async_added_to_hass(self):
"""Subscribe to updates."""
self.async_on_remove(
self._coordinator.async_add_listener(self.async_write_ha_state)
)

View File

@ -89,7 +89,7 @@ class PowerWallChargeSensor(PowerWallEntity):
@property
def state(self):
"""Get the current value in percentage."""
return self._coordinator.data[POWERWALL_API_CHARGE]
return self.coordinator.data[POWERWALL_API_CHARGE]
class PowerWallEnergySensor(PowerWallEntity):
@ -134,7 +134,7 @@ class PowerWallEnergySensor(PowerWallEntity):
def state(self):
"""Get the current value in kW."""
return (
self._coordinator.data[POWERWALL_API_METERS]
self.coordinator.data[POWERWALL_API_METERS]
.get(self._meter)
.get_power(precision=3)
)
@ -142,7 +142,7 @@ class PowerWallEnergySensor(PowerWallEntity):
@property
def device_state_attributes(self):
"""Return the device specific state attributes."""
meter = self._coordinator.data[POWERWALL_API_METERS].get(self._meter)
meter = self.coordinator.data[POWERWALL_API_METERS].get(self._meter)
return {
ATTR_FREQUENCY: round(meter.frequency, 1),
ATTR_ENERGY_EXPORTED: convert_to_kw(meter.energy_exported),