2020-03-19 15:50:17 +00:00
|
|
|
"""The Tesla Powerwall integration base entity."""
|
|
|
|
|
|
|
|
from homeassistant.helpers.entity import Entity
|
|
|
|
|
2020-04-13 19:59:50 +00:00
|
|
|
from .const import DOMAIN, MANUFACTURER, MODEL
|
2020-03-19 15:50:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
class PowerWallEntity(Entity):
|
|
|
|
"""Base class for powerwall entities."""
|
|
|
|
|
2020-04-17 21:21:14 +00:00
|
|
|
def __init__(
|
|
|
|
self, coordinator, site_info, status, device_type, powerwalls_serial_numbers
|
|
|
|
):
|
2020-03-19 15:50:17 +00:00
|
|
|
"""Initialize the sensor."""
|
|
|
|
super().__init__()
|
|
|
|
self._coordinator = coordinator
|
|
|
|
self._site_info = site_info
|
2020-04-13 19:59:50 +00:00
|
|
|
self._device_type = device_type
|
|
|
|
self._version = status.version
|
2020-04-17 21:21:14 +00:00
|
|
|
# The serial numbers of the powerwalls are unique to every site
|
|
|
|
self.base_unique_id = "_".join(powerwalls_serial_numbers)
|
2020-03-19 15:50:17 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def device_info(self):
|
|
|
|
"""Powerwall device info."""
|
2020-03-31 19:55:50 +00:00
|
|
|
device_info = {
|
2020-03-19 15:50:17 +00:00
|
|
|
"identifiers": {(DOMAIN, self.base_unique_id)},
|
2020-04-13 19:59:50 +00:00
|
|
|
"name": self._site_info.site_name,
|
2020-03-19 15:50:17 +00:00
|
|
|
"manufacturer": MANUFACTURER,
|
|
|
|
}
|
2020-03-31 19:55:50 +00:00
|
|
|
model = MODEL
|
2020-04-13 19:59:50 +00:00
|
|
|
model += f" ({self._device_type.name})"
|
2020-03-31 19:55:50 +00:00
|
|
|
device_info["model"] = model
|
2020-04-13 19:59:50 +00:00
|
|
|
device_info["sw_version"] = self._version
|
2020-03-31 19:55:50 +00:00
|
|
|
return device_info
|
2020-03-19 15:50:17 +00:00
|
|
|
|
|
|
|
@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."""
|
2020-04-20 18:21:29 +00:00
|
|
|
self.async_on_remove(
|
|
|
|
self._coordinator.async_add_listener(self.async_write_ha_state)
|
|
|
|
)
|