Add version and device type to powerwall device_info (#33453)

* Add version and device type to powerwall device_info

* Upstream powerwall now supports a http_session
This commit is contained in:
J. Nick Koston authored and GitHub committed 2020-03-31 12:55:50 -07:00
1 parent b88f56cbfb
commit e6ed2f0377
13 files changed
+95 -31

No files matched your search

+27 -11
View File
@@ -3,6 +3,7 @@ import asyncio
from datetime import timedelta
import logging
import requests
from tesla_powerwall import (
ApiError,
MetersResponse,
@@ -21,12 +22,15 @@ from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
from .const import (
DOMAIN,
POWERWALL_API_CHARGE,
POWERWALL_API_DEVICE_TYPE,
POWERWALL_API_GRID_STATUS,
POWERWALL_API_METERS,
POWERWALL_API_SITE_INFO,
POWERWALL_API_SITEMASTER,
POWERWALL_API_STATUS,
POWERWALL_COORDINATOR,
POWERWALL_HTTP_SESSION,
POWERWALL_OBJECT,
POWERWALL_SITE_INFO,
UPDATE_INTERVAL,
)
@@ -62,10 +66,12 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
entry_id = entry.entry_id
hass.data[DOMAIN].setdefault(entry_id, {})
power_wall = PowerWall(entry.data[CONF_IP_ADDRESS])
http_session = requests.Session()
power_wall = PowerWall(entry.data[CONF_IP_ADDRESS], http_session=http_session)
try:
site_info = await hass.async_add_executor_job(call_site_info, power_wall)
powerwall_data = await hass.async_add_executor_job(call_base_info, power_wall)
except (PowerWallUnreachableError, ApiError, ConnectionError):
http_session.close()
raise ConfigEntryNotReady
async def async_update_data():
@@ -80,11 +86,14 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
update_interval=timedelta(seconds=UPDATE_INTERVAL),
)
hass.data[DOMAIN][entry.entry_id] = {
POWERWALL_OBJECT: power_wall,
POWERWALL_COORDINATOR: coordinator,
POWERWALL_SITE_INFO: site_info,
}
hass.data[DOMAIN][entry.entry_id] = powerwall_data
hass.data[DOMAIN][entry.entry_id].update(
{
POWERWALL_OBJECT: power_wall,
POWERWALL_COORDINATOR: coordinator,
POWERWALL_HTTP_SESSION: http_session,
}
)
await coordinator.async_refresh()
@@ -96,9 +105,13 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
return True
def call_site_info(power_wall):
"""Wrap site_info to be a callable."""
return power_wall.site_info
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,
}
def _fetch_powerwall_data(power_wall):
@@ -124,6 +137,9 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
]
)
)
hass.data[DOMAIN][entry.entry_id][POWERWALL_HTTP_SESSION].close()
if unload_ok:
hass.data[DOMAIN].pop(entry.entry_id)