Home Assistant Unofficial Reference 2024.12.1
device_tracker.py
Go to the documentation of this file.
1 """Support for FleetGO Platform."""
2 
3 from __future__ import annotations
4 
5 import logging
6 
7 import requests
8 from ritassist import API
9 import voluptuous as vol
10 
12  PLATFORM_SCHEMA as DEVICE_TRACKER_PLATFORM_SCHEMA,
13  SeeCallback,
14 )
15 from homeassistant.const import (
16  CONF_CLIENT_ID,
17  CONF_CLIENT_SECRET,
18  CONF_INCLUDE,
19  CONF_PASSWORD,
20  CONF_USERNAME,
21 )
22 from homeassistant.core import HomeAssistant
24 from homeassistant.helpers.event import track_utc_time_change
25 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
26 
27 _LOGGER = logging.getLogger(__name__)
28 
29 PLATFORM_SCHEMA = DEVICE_TRACKER_PLATFORM_SCHEMA.extend(
30  {
31  vol.Required(CONF_USERNAME): cv.string,
32  vol.Required(CONF_PASSWORD): cv.string,
33  vol.Required(CONF_CLIENT_ID): cv.string,
34  vol.Required(CONF_CLIENT_SECRET): cv.string,
35  vol.Optional(CONF_INCLUDE, default=[]): vol.All(cv.ensure_list, [cv.string]),
36  }
37 )
38 
39 
41  hass: HomeAssistant,
42  config: ConfigType,
43  see: SeeCallback,
44  discovery_info: DiscoveryInfoType | None = None,
45 ) -> bool:
46  """Set up the DeviceScanner and check if login is valid."""
47  scanner = FleetGoDeviceScanner(config, see)
48  if not scanner.login(hass):
49  _LOGGER.error("FleetGO authentication failed")
50  return False
51  return True
52 
53 
55  """Define a scanner for the FleetGO platform."""
56 
57  def __init__(self, config, see: SeeCallback) -> None:
58  """Initialize FleetGoDeviceScanner."""
59  self._include_include = config.get(CONF_INCLUDE)
60  self._see_see = see
61 
62  self._api_api = API(
63  config.get(CONF_CLIENT_ID),
64  config.get(CONF_CLIENT_SECRET),
65  config.get(CONF_USERNAME),
66  config.get(CONF_PASSWORD),
67  )
68 
69  def setup(self, hass):
70  """Set up a timer and start gathering devices."""
71  self._refresh_refresh()
73  hass, lambda now: self._refresh_refresh(), second=range(0, 60, 30)
74  )
75 
76  def login(self, hass):
77  """Perform a login on the FleetGO API."""
78  if self._api_api.login():
79  self.setupsetup(hass)
80  return True
81  return False
82 
83  def _refresh(self) -> None:
84  """Refresh device information from the platform."""
85  try:
86  devices = self._api_api.get_devices()
87 
88  for device in devices:
89  if not self._include_include or device.license_plate in self._include_include:
90  if device.active or device.current_address is None:
91  device.get_map_details()
92 
93  self._see_see(
94  dev_id=device.plate_as_id,
95  gps=(device.latitude, device.longitude),
96  attributes=device.state_attributes,
97  icon="mdi:car",
98  )
99 
100  except requests.exceptions.ConnectionError:
101  _LOGGER.error("ConnectionError: Could not connect to FleetGO")
bool setup_scanner(HomeAssistant hass, ConfigType config, SeeCallback see, DiscoveryInfoType|None discovery_info=None)