Move Aurora coordinator to separate file (#95130)
This commit is contained in:
parent
8ccb0c3e14
commit
d14f04eb7e
3 changed files with 54 additions and 41 deletions
|
@ -90,6 +90,7 @@ omit =
|
||||||
homeassistant/components/atome/*
|
homeassistant/components/atome/*
|
||||||
homeassistant/components/aurora/__init__.py
|
homeassistant/components/aurora/__init__.py
|
||||||
homeassistant/components/aurora/binary_sensor.py
|
homeassistant/components/aurora/binary_sensor.py
|
||||||
|
homeassistant/components/aurora/coordinator.py
|
||||||
homeassistant/components/aurora/sensor.py
|
homeassistant/components/aurora/sensor.py
|
||||||
homeassistant/components/avea/light.py
|
homeassistant/components/avea/light.py
|
||||||
homeassistant/components/avion/light.py
|
homeassistant/components/avion/light.py
|
||||||
|
|
|
@ -1,9 +1,7 @@
|
||||||
"""The aurora component."""
|
"""The aurora component."""
|
||||||
|
|
||||||
from datetime import timedelta
|
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from aiohttp import ClientError
|
|
||||||
from auroranoaa import AuroraForecast
|
from auroranoaa import AuroraForecast
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
|
@ -14,8 +12,6 @@ from homeassistant.helpers.device_registry import DeviceEntryType
|
||||||
from homeassistant.helpers.entity import DeviceInfo
|
from homeassistant.helpers.entity import DeviceInfo
|
||||||
from homeassistant.helpers.update_coordinator import (
|
from homeassistant.helpers.update_coordinator import (
|
||||||
CoordinatorEntity,
|
CoordinatorEntity,
|
||||||
DataUpdateCoordinator,
|
|
||||||
UpdateFailed,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
from .const import (
|
from .const import (
|
||||||
|
@ -27,6 +23,7 @@ from .const import (
|
||||||
DEFAULT_THRESHOLD,
|
DEFAULT_THRESHOLD,
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
)
|
)
|
||||||
|
from .coordinator import AuroraDataUpdateCoordinator
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
@ -81,43 +78,6 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
return unload_ok
|
return unload_ok
|
||||||
|
|
||||||
|
|
||||||
class AuroraDataUpdateCoordinator(DataUpdateCoordinator):
|
|
||||||
"""Class to manage fetching data from the NOAA Aurora API."""
|
|
||||||
|
|
||||||
def __init__(
|
|
||||||
self,
|
|
||||||
hass: HomeAssistant,
|
|
||||||
name: str,
|
|
||||||
polling_interval: int,
|
|
||||||
api: str,
|
|
||||||
latitude: float,
|
|
||||||
longitude: float,
|
|
||||||
threshold: float,
|
|
||||||
) -> None:
|
|
||||||
"""Initialize the data updater."""
|
|
||||||
|
|
||||||
super().__init__(
|
|
||||||
hass=hass,
|
|
||||||
logger=_LOGGER,
|
|
||||||
name=name,
|
|
||||||
update_interval=timedelta(minutes=polling_interval),
|
|
||||||
)
|
|
||||||
|
|
||||||
self.api = api
|
|
||||||
self.name = name
|
|
||||||
self.latitude = int(latitude)
|
|
||||||
self.longitude = int(longitude)
|
|
||||||
self.threshold = int(threshold)
|
|
||||||
|
|
||||||
async def _async_update_data(self):
|
|
||||||
"""Fetch the data from the NOAA Aurora Forecast."""
|
|
||||||
|
|
||||||
try:
|
|
||||||
return await self.api.get_forecast_data(self.longitude, self.latitude)
|
|
||||||
except ClientError as error:
|
|
||||||
raise UpdateFailed(f"Error updating from NOAA: {error}") from error
|
|
||||||
|
|
||||||
|
|
||||||
class AuroraEntity(CoordinatorEntity[AuroraDataUpdateCoordinator]):
|
class AuroraEntity(CoordinatorEntity[AuroraDataUpdateCoordinator]):
|
||||||
"""Implementation of the base Aurora Entity."""
|
"""Implementation of the base Aurora Entity."""
|
||||||
|
|
||||||
|
|
52
homeassistant/components/aurora/coordinator.py
Normal file
52
homeassistant/components/aurora/coordinator.py
Normal file
|
@ -0,0 +1,52 @@
|
||||||
|
"""The aurora component."""
|
||||||
|
|
||||||
|
from datetime import timedelta
|
||||||
|
import logging
|
||||||
|
|
||||||
|
from aiohttp import ClientError
|
||||||
|
from auroranoaa import AuroraForecast
|
||||||
|
|
||||||
|
from homeassistant.core import HomeAssistant
|
||||||
|
from homeassistant.helpers.update_coordinator import (
|
||||||
|
DataUpdateCoordinator,
|
||||||
|
UpdateFailed,
|
||||||
|
)
|
||||||
|
|
||||||
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class AuroraDataUpdateCoordinator(DataUpdateCoordinator):
|
||||||
|
"""Class to manage fetching data from the NOAA Aurora API."""
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
hass: HomeAssistant,
|
||||||
|
name: str,
|
||||||
|
polling_interval: int,
|
||||||
|
api: AuroraForecast,
|
||||||
|
latitude: float,
|
||||||
|
longitude: float,
|
||||||
|
threshold: float,
|
||||||
|
) -> None:
|
||||||
|
"""Initialize the data updater."""
|
||||||
|
|
||||||
|
super().__init__(
|
||||||
|
hass=hass,
|
||||||
|
logger=_LOGGER,
|
||||||
|
name=name,
|
||||||
|
update_interval=timedelta(minutes=polling_interval),
|
||||||
|
)
|
||||||
|
|
||||||
|
self.api = api
|
||||||
|
self.name = name
|
||||||
|
self.latitude = int(latitude)
|
||||||
|
self.longitude = int(longitude)
|
||||||
|
self.threshold = int(threshold)
|
||||||
|
|
||||||
|
async def _async_update_data(self):
|
||||||
|
"""Fetch the data from the NOAA Aurora Forecast."""
|
||||||
|
|
||||||
|
try:
|
||||||
|
return await self.api.get_forecast_data(self.longitude, self.latitude)
|
||||||
|
except ClientError as error:
|
||||||
|
raise UpdateFailed(f"Error updating from NOAA: {error}") from error
|
Loading…
Add table
Reference in a new issue