2024-01-25 11:54:47 +00:00
|
|
|
"""Teslemetry integration."""
|
2024-03-08 18:15:59 +00:00
|
|
|
|
2024-01-25 11:54:47 +00:00
|
|
|
import asyncio
|
|
|
|
from typing import Final
|
|
|
|
|
2024-03-04 17:42:56 +00:00
|
|
|
from tesla_fleet_api import EnergySpecific, Teslemetry, VehicleSpecific
|
2024-04-23 20:11:41 +00:00
|
|
|
from tesla_fleet_api.const import Scope
|
2024-03-06 08:09:15 +00:00
|
|
|
from tesla_fleet_api.exceptions import (
|
|
|
|
InvalidToken,
|
|
|
|
SubscriptionRequired,
|
|
|
|
TeslaFleetError,
|
|
|
|
)
|
2024-01-25 11:54:47 +00:00
|
|
|
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.const import CONF_ACCESS_TOKEN, Platform
|
|
|
|
from homeassistant.core import HomeAssistant
|
2024-04-08 07:44:51 +00:00
|
|
|
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
|
2024-01-25 11:54:47 +00:00
|
|
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
2024-05-10 08:52:33 +00:00
|
|
|
from homeassistant.helpers.device_registry import DeviceInfo
|
2024-01-25 11:54:47 +00:00
|
|
|
|
2024-05-10 08:52:33 +00:00
|
|
|
from .const import DOMAIN, MODELS
|
2024-03-04 17:42:56 +00:00
|
|
|
from .coordinator import (
|
2024-05-10 10:38:20 +00:00
|
|
|
TeslemetryEnergySiteInfoCoordinator,
|
2024-05-10 08:52:33 +00:00
|
|
|
TeslemetryEnergySiteLiveCoordinator,
|
2024-03-04 17:42:56 +00:00
|
|
|
TeslemetryVehicleDataCoordinator,
|
|
|
|
)
|
|
|
|
from .models import TeslemetryData, TeslemetryEnergyData, TeslemetryVehicleData
|
2024-01-25 11:54:47 +00:00
|
|
|
|
2024-05-10 08:52:33 +00:00
|
|
|
PLATFORMS: Final = [
|
2024-05-24 06:55:27 +00:00
|
|
|
Platform.BINARY_SENSOR,
|
2024-05-10 08:52:33 +00:00
|
|
|
Platform.CLIMATE,
|
2024-05-24 10:09:23 +00:00
|
|
|
Platform.COVER,
|
2024-05-25 11:29:27 +00:00
|
|
|
Platform.DEVICE_TRACKER,
|
2024-05-24 06:26:29 +00:00
|
|
|
Platform.LOCK,
|
2024-05-15 16:17:28 +00:00
|
|
|
Platform.SELECT,
|
2024-05-10 08:52:33 +00:00
|
|
|
Platform.SENSOR,
|
2024-05-23 12:28:18 +00:00
|
|
|
Platform.SWITCH,
|
2024-05-10 08:52:33 +00:00
|
|
|
]
|
2024-01-25 11:54:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|
|
|
"""Set up Teslemetry config."""
|
|
|
|
|
|
|
|
access_token = entry.data[CONF_ACCESS_TOKEN]
|
2024-05-10 08:52:33 +00:00
|
|
|
session = async_get_clientsession(hass)
|
2024-01-25 11:54:47 +00:00
|
|
|
|
|
|
|
# Create API connection
|
|
|
|
teslemetry = Teslemetry(
|
2024-05-10 08:52:33 +00:00
|
|
|
session=session,
|
2024-01-25 11:54:47 +00:00
|
|
|
access_token=access_token,
|
|
|
|
)
|
|
|
|
try:
|
2024-04-23 20:11:41 +00:00
|
|
|
scopes = (await teslemetry.metadata())["scopes"]
|
2024-01-25 11:54:47 +00:00
|
|
|
products = (await teslemetry.products())["response"]
|
2024-04-08 07:44:51 +00:00
|
|
|
except InvalidToken as e:
|
|
|
|
raise ConfigEntryAuthFailed from e
|
|
|
|
except SubscriptionRequired as e:
|
|
|
|
raise ConfigEntryAuthFailed from e
|
2024-01-25 11:54:47 +00:00
|
|
|
except TeslaFleetError as e:
|
|
|
|
raise ConfigEntryNotReady from e
|
|
|
|
|
|
|
|
# Create array of classes
|
2024-03-04 17:42:56 +00:00
|
|
|
vehicles: list[TeslemetryVehicleData] = []
|
|
|
|
energysites: list[TeslemetryEnergyData] = []
|
2024-01-25 11:54:47 +00:00
|
|
|
for product in products:
|
2024-04-23 20:11:41 +00:00
|
|
|
if "vin" in product and Scope.VEHICLE_DEVICE_DATA in scopes:
|
2024-05-10 08:52:33 +00:00
|
|
|
# Remove the protobuff 'cached_data' that we do not use to save memory
|
|
|
|
product.pop("cached_data", None)
|
2024-03-04 17:42:56 +00:00
|
|
|
vin = product["vin"]
|
|
|
|
api = VehicleSpecific(teslemetry.vehicle, vin)
|
2024-05-10 08:52:33 +00:00
|
|
|
coordinator = TeslemetryVehicleDataCoordinator(hass, api, product)
|
|
|
|
device = DeviceInfo(
|
|
|
|
identifiers={(DOMAIN, vin)},
|
|
|
|
manufacturer="Tesla",
|
|
|
|
configuration_url="https://teslemetry.com/console",
|
|
|
|
name=product["display_name"],
|
|
|
|
model=MODELS.get(vin[3]),
|
|
|
|
serial_number=vin,
|
|
|
|
)
|
|
|
|
|
2024-03-04 17:42:56 +00:00
|
|
|
vehicles.append(
|
|
|
|
TeslemetryVehicleData(
|
|
|
|
api=api,
|
|
|
|
coordinator=coordinator,
|
|
|
|
vin=vin,
|
2024-05-10 08:52:33 +00:00
|
|
|
device=device,
|
2024-03-04 17:42:56 +00:00
|
|
|
)
|
|
|
|
)
|
2024-04-23 20:11:41 +00:00
|
|
|
elif "energy_site_id" in product and Scope.ENERGY_DEVICE_DATA in scopes:
|
2024-03-04 17:42:56 +00:00
|
|
|
site_id = product["energy_site_id"]
|
|
|
|
api = EnergySpecific(teslemetry.energy, site_id)
|
2024-05-10 08:52:33 +00:00
|
|
|
live_coordinator = TeslemetryEnergySiteLiveCoordinator(hass, api)
|
2024-05-10 10:38:20 +00:00
|
|
|
info_coordinator = TeslemetryEnergySiteInfoCoordinator(hass, api, product)
|
2024-05-10 08:52:33 +00:00
|
|
|
device = DeviceInfo(
|
|
|
|
identifiers={(DOMAIN, str(site_id))},
|
|
|
|
manufacturer="Tesla",
|
|
|
|
configuration_url="https://teslemetry.com/console",
|
|
|
|
name=product.get("site_name", "Energy Site"),
|
|
|
|
)
|
|
|
|
|
2024-03-04 17:42:56 +00:00
|
|
|
energysites.append(
|
|
|
|
TeslemetryEnergyData(
|
|
|
|
api=api,
|
2024-05-10 08:52:33 +00:00
|
|
|
live_coordinator=live_coordinator,
|
2024-05-10 10:38:20 +00:00
|
|
|
info_coordinator=info_coordinator,
|
2024-03-04 17:42:56 +00:00
|
|
|
id=site_id,
|
2024-05-10 08:52:33 +00:00
|
|
|
device=device,
|
2024-03-04 17:42:56 +00:00
|
|
|
)
|
2024-01-25 11:54:47 +00:00
|
|
|
)
|
|
|
|
|
2024-05-10 08:52:33 +00:00
|
|
|
# Run all first refreshes
|
2024-01-25 11:54:47 +00:00
|
|
|
await asyncio.gather(
|
2024-03-04 17:42:56 +00:00
|
|
|
*(
|
|
|
|
vehicle.coordinator.async_config_entry_first_refresh()
|
|
|
|
for vehicle in vehicles
|
|
|
|
),
|
|
|
|
*(
|
2024-05-10 08:52:33 +00:00
|
|
|
energysite.live_coordinator.async_config_entry_first_refresh()
|
2024-03-04 17:42:56 +00:00
|
|
|
for energysite in energysites
|
|
|
|
),
|
2024-05-10 10:38:20 +00:00
|
|
|
*(
|
|
|
|
energysite.info_coordinator.async_config_entry_first_refresh()
|
|
|
|
for energysite in energysites
|
|
|
|
),
|
2024-01-25 11:54:47 +00:00
|
|
|
)
|
|
|
|
|
2024-05-24 07:18:22 +00:00
|
|
|
# Add energy device models
|
|
|
|
for energysite in energysites:
|
|
|
|
models = set()
|
|
|
|
for gateway in energysite.info_coordinator.data.get("components_gateways", []):
|
|
|
|
if gateway.get("part_name"):
|
|
|
|
models.add(gateway["part_name"])
|
|
|
|
for battery in energysite.info_coordinator.data.get("components_batteries", []):
|
|
|
|
if battery.get("part_name"):
|
|
|
|
models.add(battery["part_name"])
|
|
|
|
if models:
|
|
|
|
energysite.device["model"] = ", ".join(sorted(models))
|
|
|
|
|
2024-01-25 11:54:47 +00:00
|
|
|
# Setup Platforms
|
2024-05-12 12:25:09 +00:00
|
|
|
entry.runtime_data = TeslemetryData(vehicles, energysites, scopes)
|
2024-01-25 11:54:47 +00:00
|
|
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|
|
|
"""Unload Teslemetry Config."""
|
2024-05-12 17:53:22 +00:00
|
|
|
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|