Coverage for custom_components/bouncie/__init__.py: 100%
38 statements
« prev ^ index » next coverage.py v7.2.4, created at 2025-01-04 23:39 +0000
« prev ^ index » next coverage.py v7.2.4, created at 2025-01-04 23:39 +0000
1"""The bouncie integration."""
2from __future__ import annotations
4import datetime
6from homeassistant.config_entries import ConfigEntry
7from homeassistant.const import CONF_SCAN_INTERVAL, Platform
8from homeassistant.core import HomeAssistant
9from homeassistant.helpers.typing import ConfigType
11from .const import DOMAIN, LOGGER, VEHICLE_MODEL_KEY
12from .coordinator import BouncieDataUpdateCoordinator
14PLATFORMS: list[Platform] = [Platform.SENSOR, Platform.DEVICE_TRACKER]
17async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
18 """Set up this integration using YAML is not supported."""
19 return True
22async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
23 """Set up bouncie from a config entry."""
25 coordinator = BouncieDataUpdateCoordinator(
26 hass=hass,
27 logger=LOGGER,
28 config_entry=entry,
29 update_interval=datetime.timedelta(seconds=entry.data[CONF_SCAN_INTERVAL]),
30 )
31 await coordinator.async_config_entry_first_refresh()
32 hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator
34 await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
36 return True
39async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
40 """Unload a config entry."""
41 if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
42 hass.data[DOMAIN].pop(entry.entry_id)
44 return unload_ok
47def patch_missing_data(vehicle_info):
48 """Fill in missing data."""
49 if "mil" not in vehicle_info["stats"]:
50 vehicle_info["stats"]["mil"] = {
51 "milOn": "Not available",
52 "lastUpdated": "Not available",
53 }
54 if "battery" not in vehicle_info["stats"]:
55 vehicle_info["stats"]["battery"] = {
56 "status": "Not available",
57 "lastUpdated": "Not available",
58 }
59 if "location" not in vehicle_info["stats"]:
60 vehicle_info["stats"]["location"] = {
61 "address": "Not available",
62 }
63 if "fuelLevel" not in vehicle_info["stats"]:
64 vehicle_info["stats"]["fuelLevel"] = -1
65 if "nickName" not in vehicle_info:
66 vehicle_info["nickName"] = (
67 str(vehicle_info[VEHICLE_MODEL_KEY]["year"])
68 + " "
69 + str(vehicle_info[VEHICLE_MODEL_KEY]["make"])
70 + " "
71 + str(vehicle_info[VEHICLE_MODEL_KEY]["name"])
72 )
73 if (
74 "qualifiedDtcList" not in vehicle_info["stats"]["mil"]
75 or len(vehicle_info["stats"]["mil"]["qualifiedDtcList"]) == 0
76 ):
77 vehicle_info["stats"]["mil"]["dtcCount"] = 0
78 vehicle_info["stats"]["mil"]["dtcDetails"] = "Not available"
79 else:
80 vehicle_info["stats"]["mil"]["dtcCount"] = len(vehicle_info["stats"]["mil"]["qualifiedDtcList"])
81 vehicle_info["stats"]["mil"]["dtcDetails"] = vehicle_info["stats"]["mil"]["qualifiedDtcList"]
83 return vehicle_info