Use updated powerwall client API library (#34139)

* Use updated powerwall client API library

* Increase instant_power precision to 3

* Add @jrester as code owner for powerwall
This commit is contained in:
jrester authored and GitHub committed 2020-04-13 21:59:50 +02:00
1 parent 7bd6f5413d
commit 6924192523
13 files changed
+104 -117

No files matched your search

+10 -18
View File
@@ -4,12 +4,7 @@ from datetime import timedelta
import logging
import requests
from tesla_powerwall import (
ApiError,
MetersResponse,
PowerWall,
PowerWallUnreachableError,
)
from tesla_powerwall import ApiError, Powerwall, PowerwallUnreachableError
import voluptuous as vol
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
@@ -67,10 +62,10 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
hass.data[DOMAIN].setdefault(entry_id, {})
http_session = requests.Session()
power_wall = PowerWall(entry.data[CONF_IP_ADDRESS], http_session=http_session)
power_wall = Powerwall(entry.data[CONF_IP_ADDRESS], http_session=http_session)
try:
powerwall_data = await hass.async_add_executor_job(call_base_info, power_wall)
except (PowerWallUnreachableError, ApiError, ConnectionError):
except (PowerwallUnreachableError, ApiError, ConnectionError):
http_session.close()
raise ConfigEntryNotReady
@@ -108,22 +103,19 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
def call_base_info(power_wall):
"""Wrap powerwall properties to be a callable."""
return {
POWERWALL_API_SITE_INFO: power_wall.site_info,
POWERWALL_API_STATUS: power_wall.status,
POWERWALL_API_DEVICE_TYPE: power_wall.device_type,
POWERWALL_API_SITE_INFO: power_wall.get_site_info(),
POWERWALL_API_STATUS: power_wall.get_status(),
POWERWALL_API_DEVICE_TYPE: power_wall.get_device_type(),
}
def _fetch_powerwall_data(power_wall):
"""Process and update powerwall data."""
meters = power_wall.meters
return {
POWERWALL_API_CHARGE: power_wall.charge,
POWERWALL_API_SITEMASTER: power_wall.sitemaster,
POWERWALL_API_METERS: {
meter: MetersResponse(meters[meter]) for meter in meters
},
POWERWALL_API_GRID_STATUS: power_wall.grid_status,
POWERWALL_API_CHARGE: power_wall.get_charge(),
POWERWALL_API_SITEMASTER: power_wall.get_sitemaster(),
POWERWALL_API_METERS: power_wall.get_meters(),
POWERWALL_API_GRID_STATUS: power_wall.get_grid_status(),
}
@@ -1,6 +1,8 @@
"""Support for August sensors."""
import logging
from tesla_powerwall import GridStatus
from homeassistant.components.binary_sensor import (
DEVICE_CLASS_CONNECTIVITY,
BinarySensorDevice,
@@ -17,13 +19,7 @@ from .const import (
POWERWALL_API_SITE_INFO,
POWERWALL_API_SITEMASTER,
POWERWALL_API_STATUS,
POWERWALL_CONNECTED_KEY,
POWERWALL_COORDINATOR,
POWERWALL_GRID_ONLINE,
POWERWALL_RUNNING_KEY,
SITE_INFO_GRID_CODE,
SITE_INFO_NOMINAL_SYSTEM_POWER_KW,
SITE_INFO_REGION,
)
from .entity import PowerWallEntity
@@ -71,17 +67,15 @@ class PowerWallRunningSensor(PowerWallEntity, BinarySensorDevice):
@property
def is_on(self):
"""Get the powerwall running state."""
return self._coordinator.data[POWERWALL_API_SITEMASTER][POWERWALL_RUNNING_KEY]
return self._coordinator.data[POWERWALL_API_SITEMASTER].running
@property
def device_state_attributes(self):
"""Return the device specific state attributes."""
return {
ATTR_REGION: self._site_info[SITE_INFO_REGION],
ATTR_GRID_CODE: self._site_info[SITE_INFO_GRID_CODE],
ATTR_NOMINAL_SYSTEM_POWER: self._site_info[
SITE_INFO_NOMINAL_SYSTEM_POWER_KW
],
ATTR_REGION: self._site_info.region,
ATTR_GRID_CODE: self._site_info.grid_code,
ATTR_NOMINAL_SYSTEM_POWER: self._site_info.nominal_system_power_kW,
}
@@ -106,7 +100,7 @@ class PowerWallConnectedSensor(PowerWallEntity, BinarySensorDevice):
@property
def is_on(self):
"""Get the powerwall connected to tesla state."""
return self._coordinator.data[POWERWALL_API_SITEMASTER][POWERWALL_CONNECTED_KEY]
return self._coordinator.data[POWERWALL_API_SITEMASTER].connected_to_tesla
class PowerWallGridStatusSensor(PowerWallEntity, BinarySensorDevice):
@@ -130,6 +124,4 @@ class PowerWallGridStatusSensor(PowerWallEntity, BinarySensorDevice):
@property
def is_on(self):
"""Grid is online."""
return (
self._coordinator.data[POWERWALL_API_GRID_STATUS] == POWERWALL_GRID_ONLINE
)
return self._coordinator.data[POWERWALL_API_GRID_STATUS] == GridStatus.CONNECTED
@@ -1,14 +1,13 @@
"""Config flow for Tesla Powerwall integration."""
import logging
from tesla_powerwall import ApiError, PowerWall, PowerWallUnreachableError
from tesla_powerwall import ApiError, Powerwall, PowerwallUnreachableError
import voluptuous as vol
from homeassistant import config_entries, core, exceptions
from homeassistant.const import CONF_IP_ADDRESS
from .const import DOMAIN # pylint:disable=unused-import
from .const import POWERWALL_SITE_NAME
_LOGGER = logging.getLogger(__name__)
@@ -21,20 +20,15 @@ async def validate_input(hass: core.HomeAssistant, data):
Data has the keys from DATA_SCHEMA with values provided by the user.
"""
power_wall = PowerWall(data[CONF_IP_ADDRESS])
power_wall = Powerwall(data[CONF_IP_ADDRESS])
try:
site_info = await hass.async_add_executor_job(call_site_info, power_wall)
except (PowerWallUnreachableError, ApiError, ConnectionError):
site_info = await hass.async_add_executor_job(power_wall.get_site_info)
except (PowerwallUnreachableError, ApiError, ConnectionError):
raise CannotConnect
# Return info that you want to store in the config entry.
return {"title": site_info[POWERWALL_SITE_NAME]}
def call_site_info(power_wall):
"""Wrap site_info to be a callable."""
return power_wall.site_info
return {"title": site_info.site_name}
class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
+9 -21
View File
@@ -2,17 +2,7 @@
from homeassistant.helpers.entity import Entity
from .const import (
DEVICE_TYPE_DEVICE_TYPE,
DOMAIN,
MANUFACTURER,
MODEL,
POWERWALL_SITE_NAME,
SITE_INFO_GRID_CODE,
SITE_INFO_NOMINAL_SYSTEM_ENERGY_KWH,
SITE_INFO_UTILITY,
STATUS_VERSION,
)
from .const import DOMAIN, MANUFACTURER, MODEL
class PowerWallEntity(Entity):
@@ -23,13 +13,13 @@ class PowerWallEntity(Entity):
super().__init__()
self._coordinator = coordinator
self._site_info = site_info
self._device_type = device_type.get(DEVICE_TYPE_DEVICE_TYPE)
self._version = status.get(STATUS_VERSION)
self._device_type = device_type
self._version = status.version
# This group of properties will be unique to to the site
unique_group = (
site_info[SITE_INFO_UTILITY],
site_info[SITE_INFO_GRID_CODE],
str(site_info[SITE_INFO_NOMINAL_SYSTEM_ENERGY_KWH]),
site_info.utility,
site_info.grid_code,
str(site_info.nominal_system_energy_kWh),
)
self.base_unique_id = "_".join(unique_group)
@@ -38,15 +28,13 @@ class PowerWallEntity(Entity):
"""Powerwall device info."""
device_info = {
"identifiers": {(DOMAIN, self.base_unique_id)},
"name": self._site_info[POWERWALL_SITE_NAME],
"name": self._site_info.site_name,
"manufacturer": MANUFACTURER,
}
model = MODEL
if self._device_type:
model += f" ({self._device_type})"
model += f" ({self._device_type.name})"
device_info["model"] = model
if self._version:
device_info["sw_version"] = self._version
device_info["sw_version"] = self._version
return device_info
@property
@@ -3,6 +3,6 @@
"name": "Tesla Powerwall",
"config_flow": true,
"documentation": "https://www.home-assistant.io/integrations/powerwall",
"requirements": ["tesla-powerwall==0.1.3"],
"codeowners": ["@bdraco"]
"requirements": ["tesla-powerwall==0.2.3"],
"codeowners": ["@bdraco", "@jrester"]
}
+13 -8
View File
@@ -1,6 +1,8 @@
"""Support for August sensors."""
import logging
from tesla_powerwall import MeterType
from homeassistant.const import (
DEVICE_CLASS_BATTERY,
DEVICE_CLASS_POWER,
@@ -37,7 +39,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
status = powerwall_data[POWERWALL_API_STATUS]
entities = []
for meter in coordinator.data[POWERWALL_API_METERS]:
for meter in MeterType:
entities.append(
PowerWallEnergySensor(meter, coordinator, site_info, status, device_type)
)
@@ -73,13 +75,13 @@ class PowerWallChargeSensor(PowerWallEntity):
@property
def state(self):
"""Get the current value in percentage."""
return round(self._coordinator.data[POWERWALL_API_CHARGE], 3)
return self._coordinator.data[POWERWALL_API_CHARGE]
class PowerWallEnergySensor(PowerWallEntity):
"""Representation of an Powerwall Energy sensor."""
def __init__(self, meter, coordinator, site_info, status, device_type):
def __init__(self, meter: MeterType, coordinator, site_info, status, device_type):
"""Initialize the sensor."""
super().__init__(coordinator, site_info, status, device_type)
self._meter = meter
@@ -92,7 +94,7 @@ class PowerWallEnergySensor(PowerWallEntity):
@property
def name(self):
"""Device Name."""
return f"Powerwall {self._meter.title()} Now"
return f"Powerwall {self._meter.value.title()} Now"
@property
def device_class(self):
@@ -102,18 +104,21 @@ class PowerWallEnergySensor(PowerWallEntity):
@property
def unique_id(self):
"""Device Uniqueid."""
return f"{self.base_unique_id}_{self._meter}_instant_power"
return f"{self.base_unique_id}_{self._meter.value}_instant_power"
@property
def state(self):
"""Get the current value in kW."""
meter = self._coordinator.data[POWERWALL_API_METERS][self._meter]
return round(float(meter.instant_power / 1000), 3)
return (
self._coordinator.data[POWERWALL_API_METERS]
.get(self._meter)
.get_power(precision=3)
)
@property
def device_state_attributes(self):
"""Return the device specific state attributes."""
meter = self._coordinator.data[POWERWALL_API_METERS][self._meter]
meter = self._coordinator.data[POWERWALL_API_METERS].get(self._meter)
return {
ATTR_FREQUENCY: meter.frequency,
ATTR_ENERGY_EXPORTED: meter.energy_exported,