mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +00:00
Use config entry runtime data in Trafikverket Weather (#116554)
This commit is contained in:
parent
054fb5af31
commit
6cb703aa1d
@ -5,17 +5,18 @@ from __future__ import annotations
|
|||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
|
||||||
from .const import DOMAIN, PLATFORMS
|
from .const import PLATFORMS
|
||||||
from .coordinator import TVDataUpdateCoordinator
|
from .coordinator import TVDataUpdateCoordinator
|
||||||
|
|
||||||
|
TVWeatherConfigEntry = ConfigEntry[TVDataUpdateCoordinator]
|
||||||
|
|
||||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|
||||||
|
async def async_setup_entry(hass: HomeAssistant, entry: TVWeatherConfigEntry) -> bool:
|
||||||
"""Set up Trafikverket Weatherstation from a config entry."""
|
"""Set up Trafikverket Weatherstation from a config entry."""
|
||||||
|
|
||||||
coordinator = TVDataUpdateCoordinator(hass, entry)
|
coordinator = TVDataUpdateCoordinator(hass)
|
||||||
await coordinator.async_config_entry_first_refresh()
|
await coordinator.async_config_entry_first_refresh()
|
||||||
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator
|
entry.runtime_data = coordinator
|
||||||
|
|
||||||
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||||
|
|
||||||
return True
|
return True
|
||||||
@ -23,5 +24,4 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
|
|
||||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
"""Unload Trafikverket Weatherstation config entry."""
|
"""Unload Trafikverket Weatherstation config entry."""
|
||||||
|
|
||||||
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||||
|
@ -4,6 +4,7 @@ from __future__ import annotations
|
|||||||
|
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
import logging
|
import logging
|
||||||
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
from pytrafikverket.exceptions import (
|
from pytrafikverket.exceptions import (
|
||||||
InvalidAuthentication,
|
InvalidAuthentication,
|
||||||
@ -12,7 +13,6 @@ from pytrafikverket.exceptions import (
|
|||||||
)
|
)
|
||||||
from pytrafikverket.trafikverket_weather import TrafikverketWeather, WeatherStationInfo
|
from pytrafikverket.trafikverket_weather import TrafikverketWeather, WeatherStationInfo
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.const import CONF_API_KEY
|
from homeassistant.const import CONF_API_KEY
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.exceptions import ConfigEntryAuthFailed
|
from homeassistant.exceptions import ConfigEntryAuthFailed
|
||||||
@ -21,6 +21,9 @@ from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, Upda
|
|||||||
|
|
||||||
from .const import CONF_STATION, DOMAIN
|
from .const import CONF_STATION, DOMAIN
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from . import TVWeatherConfigEntry
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
TIME_BETWEEN_UPDATES = timedelta(minutes=10)
|
TIME_BETWEEN_UPDATES = timedelta(minutes=10)
|
||||||
|
|
||||||
@ -28,7 +31,9 @@ TIME_BETWEEN_UPDATES = timedelta(minutes=10)
|
|||||||
class TVDataUpdateCoordinator(DataUpdateCoordinator[WeatherStationInfo]):
|
class TVDataUpdateCoordinator(DataUpdateCoordinator[WeatherStationInfo]):
|
||||||
"""A Sensibo Data Update Coordinator."""
|
"""A Sensibo Data Update Coordinator."""
|
||||||
|
|
||||||
def __init__(self, hass: HomeAssistant, entry: ConfigEntry) -> None:
|
config_entry: TVWeatherConfigEntry
|
||||||
|
|
||||||
|
def __init__(self, hass: HomeAssistant) -> None:
|
||||||
"""Initialize the Sensibo coordinator."""
|
"""Initialize the Sensibo coordinator."""
|
||||||
super().__init__(
|
super().__init__(
|
||||||
hass,
|
hass,
|
||||||
@ -37,9 +42,9 @@ class TVDataUpdateCoordinator(DataUpdateCoordinator[WeatherStationInfo]):
|
|||||||
update_interval=TIME_BETWEEN_UPDATES,
|
update_interval=TIME_BETWEEN_UPDATES,
|
||||||
)
|
)
|
||||||
self._weather_api = TrafikverketWeather(
|
self._weather_api = TrafikverketWeather(
|
||||||
async_get_clientsession(hass), entry.data[CONF_API_KEY]
|
async_get_clientsession(hass), self.config_entry.data[CONF_API_KEY]
|
||||||
)
|
)
|
||||||
self._station = entry.data[CONF_STATION]
|
self._station = self.config_entry.data[CONF_STATION]
|
||||||
|
|
||||||
async def _async_update_data(self) -> WeatherStationInfo:
|
async def _async_update_data(self) -> WeatherStationInfo:
|
||||||
"""Fetch data from Trafikverket."""
|
"""Fetch data from Trafikverket."""
|
||||||
|
@ -14,7 +14,6 @@ from homeassistant.components.sensor import (
|
|||||||
SensorEntityDescription,
|
SensorEntityDescription,
|
||||||
SensorStateClass,
|
SensorStateClass,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
DEGREE,
|
DEGREE,
|
||||||
PERCENTAGE,
|
PERCENTAGE,
|
||||||
@ -30,6 +29,7 @@ from homeassistant.helpers.typing import StateType
|
|||||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||||
from homeassistant.util import dt as dt_util
|
from homeassistant.util import dt as dt_util
|
||||||
|
|
||||||
|
from . import TVWeatherConfigEntry
|
||||||
from .const import ATTRIBUTION, CONF_STATION, DOMAIN
|
from .const import ATTRIBUTION, CONF_STATION, DOMAIN
|
||||||
from .coordinator import TVDataUpdateCoordinator
|
from .coordinator import TVDataUpdateCoordinator
|
||||||
|
|
||||||
@ -200,11 +200,13 @@ SENSOR_TYPES: tuple[TrafikverketSensorEntityDescription, ...] = (
|
|||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
hass: HomeAssistant,
|
||||||
|
entry: TVWeatherConfigEntry,
|
||||||
|
async_add_entities: AddEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up the Trafikverket sensor entry."""
|
"""Set up the Trafikverket sensor entry."""
|
||||||
|
|
||||||
coordinator: TVDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
coordinator = entry.runtime_data
|
||||||
|
|
||||||
async_add_entities(
|
async_add_entities(
|
||||||
TrafikverketWeatherStation(
|
TrafikverketWeatherStation(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user