Home Assistant Unofficial Reference 2024.12.1
models.py
Go to the documentation of this file.
1 """Support for Volvo On Call."""
2 
3 from aiohttp.client_exceptions import ClientResponseError
4 from volvooncall import Connection
5 from volvooncall.dashboard import Instrument
6 
7 from homeassistant.config_entries import ConfigEntry
8 from homeassistant.const import CONF_UNIT_SYSTEM
9 from homeassistant.core import HomeAssistant
10 from homeassistant.exceptions import ConfigEntryAuthFailed
11 from homeassistant.helpers.dispatcher import async_dispatcher_send
12 from homeassistant.helpers.update_coordinator import UpdateFailed
13 
14 from .const import (
15  CONF_MUTABLE,
16  PLATFORMS,
17  UNIT_SYSTEM_IMPERIAL,
18  UNIT_SYSTEM_SCANDINAVIAN_MILES,
19  VOLVO_DISCOVERY_NEW,
20 )
21 from .errors import InvalidAuth
22 
23 
24 class VolvoData:
25  """Hold component state."""
26 
27  def __init__(
28  self,
29  hass: HomeAssistant,
30  connection: Connection,
31  entry: ConfigEntry,
32  ) -> None:
33  """Initialize the component state."""
34  self.hasshass = hass
35  self.vehicles: set[str] = set()
36  self.instruments: set[Instrument] = set()
37  self.config_entryconfig_entry = entry
38  self.connectionconnection = connection
39 
40  def instrument(self, vin, component, attr, slug_attr):
41  """Return corresponding instrument."""
42  return next(
43  instrument
44  for instrument in self.instruments
45  if instrument.vehicle.vin == vin
46  and instrument.component == component
47  and instrument.attr == attr
48  and instrument.slug_attr == slug_attr
49  )
50 
51  def vehicle_name(self, vehicle):
52  """Provide a friendly name for a vehicle."""
53  if vehicle.registration_number and vehicle.registration_number != "UNKNOWN":
54  return vehicle.registration_number
55  if vehicle.vin:
56  return vehicle.vin
57  return "Volvo"
58 
59  def discover_vehicle(self, vehicle):
60  """Load relevant platforms."""
61  self.vehicles.add(vehicle.vin)
62 
63  dashboard = vehicle.dashboard(
64  mutable=self.config_entryconfig_entry.data[CONF_MUTABLE],
65  scandinavian_miles=(
66  self.config_entryconfig_entry.data[CONF_UNIT_SYSTEM]
67  == UNIT_SYSTEM_SCANDINAVIAN_MILES
68  ),
69  usa_units=(
70  self.config_entryconfig_entry.data[CONF_UNIT_SYSTEM] == UNIT_SYSTEM_IMPERIAL
71  ),
72  )
73 
74  for instrument in (
75  instrument
76  for instrument in dashboard.instruments
77  if instrument.component in PLATFORMS
78  ):
79  self.instruments.add(instrument)
80  async_dispatcher_send(self.hasshass, VOLVO_DISCOVERY_NEW, [instrument])
81 
82  async def update(self):
83  """Update status from the online service."""
84  try:
85  await self.connectionconnection.update(journal=True)
86  except ClientResponseError as ex:
87  if ex.status == 401:
88  raise ConfigEntryAuthFailed(ex) from ex
89  raise UpdateFailed(ex) from ex
90 
91  for vehicle in self.connectionconnection.vehicles:
92  if vehicle.vin not in self.vehicles:
93  self.discover_vehiclediscover_vehicle(vehicle)
94 
95  async def auth_is_valid(self):
96  """Check if provided username/password/region authenticate."""
97  try:
98  await self.connectionconnection.get("customeraccounts")
99  except ClientResponseError as exc:
100  raise InvalidAuth from exc
def instrument(self, vin, component, attr, slug_attr)
Definition: models.py:40
None __init__(self, HomeAssistant hass, Connection connection, ConfigEntry entry)
Definition: models.py:32
bool add(self, _T matcher)
Definition: match.py:185
web.Response get(self, web.Request request, str config_key)
Definition: view.py:88
None async_dispatcher_send(HomeAssistant hass, str signal, *Any args)
Definition: dispatcher.py:193