From f7d1cfb62521fab547a9a1eef1e546dcf5b42dc8 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 30 Aug 2020 07:37:11 -0500 Subject: [PATCH] Update powerwall to use CoordinatorEntity (#39389) --- .../components/powerwall/binary_sensor.py | 8 ++--- homeassistant/components/powerwall/entity.py | 30 ++----------------- homeassistant/components/powerwall/sensor.py | 6 ++-- 3 files changed, 10 insertions(+), 34 deletions(-) diff --git a/homeassistant/components/powerwall/binary_sensor.py b/homeassistant/components/powerwall/binary_sensor.py index b3fe9d977d3..f064616c5ab 100644 --- a/homeassistant/components/powerwall/binary_sensor.py +++ b/homeassistant/components/powerwall/binary_sensor.py @@ -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() diff --git a/homeassistant/components/powerwall/entity.py b/homeassistant/components/powerwall/entity.py index c9cfd124ec6..bcc21615066 100644 --- a/homeassistant/components/powerwall/entity.py +++ b/homeassistant/components/powerwall/entity.py @@ -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) - ) diff --git a/homeassistant/components/powerwall/sensor.py b/homeassistant/components/powerwall/sensor.py index e1e968c0353..ade7746df30 100644 --- a/homeassistant/components/powerwall/sensor.py +++ b/homeassistant/components/powerwall/sensor.py @@ -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),