1 """Tessie integration."""
4 from http
import HTTPStatus
7 from aiohttp
import ClientError, ClientResponseError
8 from tesla_fleet_api
import EnergySpecific, Tessie
9 from tesla_fleet_api.const
import Scope
10 from tesla_fleet_api.exceptions
import TeslaFleetError
11 from tessie_api
import get_state_of_all_vehicles
20 from .const
import DOMAIN, MODELS
21 from .coordinator
import (
22 TessieEnergySiteInfoCoordinator,
23 TessieEnergySiteLiveCoordinator,
24 TessieStateUpdateCoordinator,
26 from .models
import TessieData, TessieEnergyData, TessieVehicleData
29 Platform.BINARY_SENSOR,
33 Platform.DEVICE_TRACKER,
35 Platform.MEDIA_PLAYER,
43 _LOGGER = logging.getLogger(__name__)
45 type TessieConfigEntry = ConfigEntry[TessieData]
49 """Set up Tessie config."""
50 api_key = entry.data[CONF_ACCESS_TOKEN]
54 state_of_all_vehicles = await get_state_of_all_vehicles(
59 except ClientResponseError
as e:
60 if e.status == HTTPStatus.UNAUTHORIZED:
61 raise ConfigEntryAuthFailed
from e
62 _LOGGER.error(
"Setup failed, unable to connect to Tessie: %s", e)
64 except ClientError
as e:
65 raise ConfigEntryNotReady
from e
74 data=vehicle[
"last_state"],
77 identifiers={(DOMAIN, vehicle[
"vin"])},
79 configuration_url=
"https://my.tessie.com/",
80 name=vehicle[
"last_state"][
"display_name"],
82 vehicle[
"last_state"][
"vehicle_config"][
"car_type"],
83 vehicle[
"last_state"][
"vehicle_config"][
"car_type"],
85 sw_version=vehicle[
"last_state"][
"vehicle_state"][
"car_version"].split(
88 hw_version=vehicle[
"last_state"][
"vehicle_config"][
"driver_assist"],
89 serial_number=vehicle[
"vin"],
92 for vehicle
in state_of_all_vehicles[
"results"]
93 if vehicle[
"last_state"]
is not None
97 tessie = Tessie(session, api_key)
98 energysites: list[TessieEnergyData] = []
101 scopes = await tessie.scopes()
102 except TeslaFleetError
as e:
103 raise ConfigEntryNotReady
from e
105 if Scope.ENERGY_DEVICE_DATA
in scopes:
107 products = (await tessie.products())[
"response"]
108 except TeslaFleetError
as e:
109 raise ConfigEntryNotReady
from e
111 for product
in products:
112 if "energy_site_id" in product:
113 site_id = product[
"energy_site_id"]
115 product[
"components"][
"battery"]
116 or product[
"components"][
"solar"]
117 or "wall_connectors" in product[
"components"]
120 "Skipping Energy Site %s as it has no components",
125 api = EnergySpecific(tessie.energy, site_id)
133 identifiers={(DOMAIN,
str(site_id))},
134 manufacturer=
"Tesla",
135 name=product.get(
"site_name",
"Energy Site"),
141 await asyncio.gather(
143 energysite.live_coordinator.async_config_entry_first_refresh()
144 for energysite
in energysites
147 energysite.info_coordinator.async_config_entry_first_refresh()
148 for energysite
in energysites
152 entry.runtime_data =
TessieData(vehicles, energysites)
153 await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
159 """Unload Tessie Config."""
160 return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
bool async_unload_entry(HomeAssistant hass, TessieConfigEntry entry)
bool async_setup_entry(HomeAssistant hass, TessieConfigEntry entry)
aiohttp.ClientSession async_get_clientsession(HomeAssistant hass, bool verify_ssl=True, socket.AddressFamily family=socket.AF_UNSPEC, ssl_util.SSLCipherList ssl_cipher=ssl_util.SSLCipherList.PYTHON_DEFAULT)