mirror of
https://github.com/home-assistant/core.git
synced 2025-07-26 22:57:17 +00:00
Move volvooncall coordinator to separate module (#126548)
This commit is contained in:
parent
d2ab7dd9fb
commit
380019dd56
@ -1,11 +1,6 @@
|
|||||||
"""Support for Volvo On Call."""
|
"""Support for Volvo On Call."""
|
||||||
|
|
||||||
import asyncio
|
|
||||||
import logging
|
|
||||||
|
|
||||||
from aiohttp.client_exceptions import ClientResponseError
|
|
||||||
from volvooncall import Connection
|
from volvooncall import Connection
|
||||||
from volvooncall.dashboard import Instrument
|
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
@ -15,25 +10,17 @@ from homeassistant.const import (
|
|||||||
CONF_USERNAME,
|
CONF_USERNAME,
|
||||||
)
|
)
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.exceptions import ConfigEntryAuthFailed
|
|
||||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||||
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
|
||||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
|
||||||
|
|
||||||
from .const import (
|
from .const import (
|
||||||
CONF_MUTABLE,
|
|
||||||
CONF_SCANDINAVIAN_MILES,
|
CONF_SCANDINAVIAN_MILES,
|
||||||
DEFAULT_UPDATE_INTERVAL,
|
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
PLATFORMS,
|
PLATFORMS,
|
||||||
UNIT_SYSTEM_IMPERIAL,
|
|
||||||
UNIT_SYSTEM_METRIC,
|
UNIT_SYSTEM_METRIC,
|
||||||
UNIT_SYSTEM_SCANDINAVIAN_MILES,
|
UNIT_SYSTEM_SCANDINAVIAN_MILES,
|
||||||
VOLVO_DISCOVERY_NEW,
|
|
||||||
)
|
)
|
||||||
from .errors import InvalidAuth
|
from .coordinator import VolvoUpdateCoordinator
|
||||||
|
from .models import VolvoData
|
||||||
_LOGGER = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
@ -82,104 +69,3 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
hass.data[DOMAIN].pop(entry.entry_id)
|
hass.data[DOMAIN].pop(entry.entry_id)
|
||||||
|
|
||||||
return unload_ok
|
return unload_ok
|
||||||
|
|
||||||
|
|
||||||
class VolvoData:
|
|
||||||
"""Hold component state."""
|
|
||||||
|
|
||||||
def __init__(
|
|
||||||
self,
|
|
||||||
hass: HomeAssistant,
|
|
||||||
connection: Connection,
|
|
||||||
entry: ConfigEntry,
|
|
||||||
) -> None:
|
|
||||||
"""Initialize the component state."""
|
|
||||||
self.hass = hass
|
|
||||||
self.vehicles: set[str] = set()
|
|
||||||
self.instruments: set[Instrument] = set()
|
|
||||||
self.config_entry = entry
|
|
||||||
self.connection = connection
|
|
||||||
|
|
||||||
def instrument(self, vin, component, attr, slug_attr):
|
|
||||||
"""Return corresponding instrument."""
|
|
||||||
return next(
|
|
||||||
instrument
|
|
||||||
for instrument in self.instruments
|
|
||||||
if instrument.vehicle.vin == vin
|
|
||||||
and instrument.component == component
|
|
||||||
and instrument.attr == attr
|
|
||||||
and instrument.slug_attr == slug_attr
|
|
||||||
)
|
|
||||||
|
|
||||||
def vehicle_name(self, vehicle):
|
|
||||||
"""Provide a friendly name for a vehicle."""
|
|
||||||
if vehicle.registration_number and vehicle.registration_number != "UNKNOWN":
|
|
||||||
return vehicle.registration_number
|
|
||||||
if vehicle.vin:
|
|
||||||
return vehicle.vin
|
|
||||||
return "Volvo"
|
|
||||||
|
|
||||||
def discover_vehicle(self, vehicle):
|
|
||||||
"""Load relevant platforms."""
|
|
||||||
self.vehicles.add(vehicle.vin)
|
|
||||||
|
|
||||||
dashboard = vehicle.dashboard(
|
|
||||||
mutable=self.config_entry.data[CONF_MUTABLE],
|
|
||||||
scandinavian_miles=(
|
|
||||||
self.config_entry.data[CONF_UNIT_SYSTEM]
|
|
||||||
== UNIT_SYSTEM_SCANDINAVIAN_MILES
|
|
||||||
),
|
|
||||||
usa_units=(
|
|
||||||
self.config_entry.data[CONF_UNIT_SYSTEM] == UNIT_SYSTEM_IMPERIAL
|
|
||||||
),
|
|
||||||
)
|
|
||||||
|
|
||||||
for instrument in (
|
|
||||||
instrument
|
|
||||||
for instrument in dashboard.instruments
|
|
||||||
if instrument.component in PLATFORMS
|
|
||||||
):
|
|
||||||
self.instruments.add(instrument)
|
|
||||||
async_dispatcher_send(self.hass, VOLVO_DISCOVERY_NEW, [instrument])
|
|
||||||
|
|
||||||
async def update(self):
|
|
||||||
"""Update status from the online service."""
|
|
||||||
try:
|
|
||||||
await self.connection.update(journal=True)
|
|
||||||
except ClientResponseError as ex:
|
|
||||||
if ex.status == 401:
|
|
||||||
raise ConfigEntryAuthFailed(ex) from ex
|
|
||||||
raise UpdateFailed(ex) from ex
|
|
||||||
|
|
||||||
for vehicle in self.connection.vehicles:
|
|
||||||
if vehicle.vin not in self.vehicles:
|
|
||||||
self.discover_vehicle(vehicle)
|
|
||||||
|
|
||||||
async def auth_is_valid(self):
|
|
||||||
"""Check if provided username/password/region authenticate."""
|
|
||||||
try:
|
|
||||||
await self.connection.get("customeraccounts")
|
|
||||||
except ClientResponseError as exc:
|
|
||||||
raise InvalidAuth from exc
|
|
||||||
|
|
||||||
|
|
||||||
class VolvoUpdateCoordinator(DataUpdateCoordinator[None]): # pylint: disable=hass-enforce-class-module
|
|
||||||
"""Volvo coordinator."""
|
|
||||||
|
|
||||||
def __init__(self, hass: HomeAssistant, volvo_data: VolvoData) -> None:
|
|
||||||
"""Initialize the data update coordinator."""
|
|
||||||
|
|
||||||
super().__init__(
|
|
||||||
hass,
|
|
||||||
_LOGGER,
|
|
||||||
name="volvooncall",
|
|
||||||
update_interval=DEFAULT_UPDATE_INTERVAL,
|
|
||||||
)
|
|
||||||
|
|
||||||
self.volvo_data = volvo_data
|
|
||||||
|
|
||||||
async def _async_update_data(self) -> None:
|
|
||||||
"""Fetch data from API endpoint."""
|
|
||||||
|
|
||||||
async with asyncio.timeout(10):
|
|
||||||
await self.volvo_data.update()
|
|
||||||
|
@ -16,8 +16,8 @@ from homeassistant.core import HomeAssistant, callback
|
|||||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
from . import VolvoUpdateCoordinator
|
|
||||||
from .const import DOMAIN, VOLVO_DISCOVERY_NEW
|
from .const import DOMAIN, VOLVO_DISCOVERY_NEW
|
||||||
|
from .coordinator import VolvoUpdateCoordinator
|
||||||
from .entity import VolvoEntity
|
from .entity import VolvoEntity
|
||||||
|
|
||||||
|
|
||||||
|
@ -18,7 +18,6 @@ from homeassistant.const import (
|
|||||||
)
|
)
|
||||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||||
|
|
||||||
from . import VolvoData
|
|
||||||
from .const import (
|
from .const import (
|
||||||
CONF_MUTABLE,
|
CONF_MUTABLE,
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
@ -27,6 +26,7 @@ from .const import (
|
|||||||
UNIT_SYSTEM_SCANDINAVIAN_MILES,
|
UNIT_SYSTEM_SCANDINAVIAN_MILES,
|
||||||
)
|
)
|
||||||
from .errors import InvalidAuth
|
from .errors import InvalidAuth
|
||||||
|
from .models import VolvoData
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
34
homeassistant/components/volvooncall/coordinator.py
Normal file
34
homeassistant/components/volvooncall/coordinator.py
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
"""Support for Volvo On Call."""
|
||||||
|
|
||||||
|
import asyncio
|
||||||
|
import logging
|
||||||
|
|
||||||
|
from homeassistant.core import HomeAssistant
|
||||||
|
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
||||||
|
|
||||||
|
from .const import DEFAULT_UPDATE_INTERVAL
|
||||||
|
from .models import VolvoData
|
||||||
|
|
||||||
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class VolvoUpdateCoordinator(DataUpdateCoordinator[None]):
|
||||||
|
"""Volvo coordinator."""
|
||||||
|
|
||||||
|
def __init__(self, hass: HomeAssistant, volvo_data: VolvoData) -> None:
|
||||||
|
"""Initialize the data update coordinator."""
|
||||||
|
|
||||||
|
super().__init__(
|
||||||
|
hass,
|
||||||
|
_LOGGER,
|
||||||
|
name="volvooncall",
|
||||||
|
update_interval=DEFAULT_UPDATE_INTERVAL,
|
||||||
|
)
|
||||||
|
|
||||||
|
self.volvo_data = volvo_data
|
||||||
|
|
||||||
|
async def _async_update_data(self) -> None:
|
||||||
|
"""Fetch data from API endpoint."""
|
||||||
|
|
||||||
|
async with asyncio.timeout(10):
|
||||||
|
await self.volvo_data.update()
|
@ -10,8 +10,8 @@ from homeassistant.core import HomeAssistant, callback
|
|||||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
from . import VolvoUpdateCoordinator
|
|
||||||
from .const import DOMAIN, VOLVO_DISCOVERY_NEW
|
from .const import DOMAIN, VOLVO_DISCOVERY_NEW
|
||||||
|
from .coordinator import VolvoUpdateCoordinator
|
||||||
from .entity import VolvoEntity
|
from .entity import VolvoEntity
|
||||||
|
|
||||||
|
|
||||||
|
@ -3,8 +3,8 @@
|
|||||||
from homeassistant.helpers.device_registry import DeviceInfo
|
from homeassistant.helpers.device_registry import DeviceInfo
|
||||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||||
|
|
||||||
from . import VolvoUpdateCoordinator
|
|
||||||
from .const import DOMAIN
|
from .const import DOMAIN
|
||||||
|
from .coordinator import VolvoUpdateCoordinator
|
||||||
|
|
||||||
|
|
||||||
class VolvoEntity(CoordinatorEntity[VolvoUpdateCoordinator]):
|
class VolvoEntity(CoordinatorEntity[VolvoUpdateCoordinator]):
|
||||||
|
@ -12,8 +12,8 @@ from homeassistant.core import HomeAssistant, callback
|
|||||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
from . import VolvoUpdateCoordinator
|
|
||||||
from .const import DOMAIN, VOLVO_DISCOVERY_NEW
|
from .const import DOMAIN, VOLVO_DISCOVERY_NEW
|
||||||
|
from .coordinator import VolvoUpdateCoordinator
|
||||||
from .entity import VolvoEntity
|
from .entity import VolvoEntity
|
||||||
|
|
||||||
|
|
||||||
|
100
homeassistant/components/volvooncall/models.py
Normal file
100
homeassistant/components/volvooncall/models.py
Normal file
@ -0,0 +1,100 @@
|
|||||||
|
"""Support for Volvo On Call."""
|
||||||
|
|
||||||
|
from aiohttp.client_exceptions import ClientResponseError
|
||||||
|
from volvooncall import Connection
|
||||||
|
from volvooncall.dashboard import Instrument
|
||||||
|
|
||||||
|
from homeassistant.config_entries import ConfigEntry
|
||||||
|
from homeassistant.const import CONF_UNIT_SYSTEM
|
||||||
|
from homeassistant.core import HomeAssistant
|
||||||
|
from homeassistant.exceptions import ConfigEntryAuthFailed
|
||||||
|
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
||||||
|
from homeassistant.helpers.update_coordinator import UpdateFailed
|
||||||
|
|
||||||
|
from .const import (
|
||||||
|
CONF_MUTABLE,
|
||||||
|
PLATFORMS,
|
||||||
|
UNIT_SYSTEM_IMPERIAL,
|
||||||
|
UNIT_SYSTEM_SCANDINAVIAN_MILES,
|
||||||
|
VOLVO_DISCOVERY_NEW,
|
||||||
|
)
|
||||||
|
from .errors import InvalidAuth
|
||||||
|
|
||||||
|
|
||||||
|
class VolvoData:
|
||||||
|
"""Hold component state."""
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
hass: HomeAssistant,
|
||||||
|
connection: Connection,
|
||||||
|
entry: ConfigEntry,
|
||||||
|
) -> None:
|
||||||
|
"""Initialize the component state."""
|
||||||
|
self.hass = hass
|
||||||
|
self.vehicles: set[str] = set()
|
||||||
|
self.instruments: set[Instrument] = set()
|
||||||
|
self.config_entry = entry
|
||||||
|
self.connection = connection
|
||||||
|
|
||||||
|
def instrument(self, vin, component, attr, slug_attr):
|
||||||
|
"""Return corresponding instrument."""
|
||||||
|
return next(
|
||||||
|
instrument
|
||||||
|
for instrument in self.instruments
|
||||||
|
if instrument.vehicle.vin == vin
|
||||||
|
and instrument.component == component
|
||||||
|
and instrument.attr == attr
|
||||||
|
and instrument.slug_attr == slug_attr
|
||||||
|
)
|
||||||
|
|
||||||
|
def vehicle_name(self, vehicle):
|
||||||
|
"""Provide a friendly name for a vehicle."""
|
||||||
|
if vehicle.registration_number and vehicle.registration_number != "UNKNOWN":
|
||||||
|
return vehicle.registration_number
|
||||||
|
if vehicle.vin:
|
||||||
|
return vehicle.vin
|
||||||
|
return "Volvo"
|
||||||
|
|
||||||
|
def discover_vehicle(self, vehicle):
|
||||||
|
"""Load relevant platforms."""
|
||||||
|
self.vehicles.add(vehicle.vin)
|
||||||
|
|
||||||
|
dashboard = vehicle.dashboard(
|
||||||
|
mutable=self.config_entry.data[CONF_MUTABLE],
|
||||||
|
scandinavian_miles=(
|
||||||
|
self.config_entry.data[CONF_UNIT_SYSTEM]
|
||||||
|
== UNIT_SYSTEM_SCANDINAVIAN_MILES
|
||||||
|
),
|
||||||
|
usa_units=(
|
||||||
|
self.config_entry.data[CONF_UNIT_SYSTEM] == UNIT_SYSTEM_IMPERIAL
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
for instrument in (
|
||||||
|
instrument
|
||||||
|
for instrument in dashboard.instruments
|
||||||
|
if instrument.component in PLATFORMS
|
||||||
|
):
|
||||||
|
self.instruments.add(instrument)
|
||||||
|
async_dispatcher_send(self.hass, VOLVO_DISCOVERY_NEW, [instrument])
|
||||||
|
|
||||||
|
async def update(self):
|
||||||
|
"""Update status from the online service."""
|
||||||
|
try:
|
||||||
|
await self.connection.update(journal=True)
|
||||||
|
except ClientResponseError as ex:
|
||||||
|
if ex.status == 401:
|
||||||
|
raise ConfigEntryAuthFailed(ex) from ex
|
||||||
|
raise UpdateFailed(ex) from ex
|
||||||
|
|
||||||
|
for vehicle in self.connection.vehicles:
|
||||||
|
if vehicle.vin not in self.vehicles:
|
||||||
|
self.discover_vehicle(vehicle)
|
||||||
|
|
||||||
|
async def auth_is_valid(self):
|
||||||
|
"""Check if provided username/password/region authenticate."""
|
||||||
|
try:
|
||||||
|
await self.connection.get("customeraccounts")
|
||||||
|
except ClientResponseError as exc:
|
||||||
|
raise InvalidAuth from exc
|
@ -10,8 +10,8 @@ from homeassistant.core import HomeAssistant, callback
|
|||||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
from . import VolvoUpdateCoordinator
|
|
||||||
from .const import DOMAIN, VOLVO_DISCOVERY_NEW
|
from .const import DOMAIN, VOLVO_DISCOVERY_NEW
|
||||||
|
from .coordinator import VolvoUpdateCoordinator
|
||||||
from .entity import VolvoEntity
|
from .entity import VolvoEntity
|
||||||
|
|
||||||
|
|
||||||
|
@ -12,8 +12,8 @@ from homeassistant.core import HomeAssistant, callback
|
|||||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
from . import VolvoUpdateCoordinator
|
|
||||||
from .const import DOMAIN, VOLVO_DISCOVERY_NEW
|
from .const import DOMAIN, VOLVO_DISCOVERY_NEW
|
||||||
|
from .coordinator import VolvoUpdateCoordinator
|
||||||
from .entity import VolvoEntity
|
from .entity import VolvoEntity
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user