1
mirror of https://github.com/home-assistant/core synced 2024-08-28 03:36:46 +02:00
ha-core/homeassistant/components/minecraft_server/coordinator.py
elmurato 7b32e4142e
Make API init async in Minecraft Server (#105403)
* Make api init async

* Remove duplicate assignment of address and set server to None in constructor
2023-12-09 21:15:48 -08:00

45 lines
1.2 KiB
Python

"""The Minecraft Server integration."""
from __future__ import annotations
from datetime import timedelta
import logging
from homeassistant.core import HomeAssistant
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
from .api import (
MinecraftServer,
MinecraftServerConnectionError,
MinecraftServerData,
MinecraftServerNotInitializedError,
)
SCAN_INTERVAL = timedelta(seconds=60)
_LOGGER = logging.getLogger(__name__)
class MinecraftServerCoordinator(DataUpdateCoordinator[MinecraftServerData]):
"""Minecraft Server data update coordinator."""
def __init__(self, hass: HomeAssistant, name: str, api: MinecraftServer) -> None:
"""Initialize coordinator instance."""
self._api = api
super().__init__(
hass=hass,
name=name,
logger=_LOGGER,
update_interval=SCAN_INTERVAL,
)
async def _async_update_data(self) -> MinecraftServerData:
"""Get updated data from the server."""
try:
return await self._api.async_get_data()
except (
MinecraftServerConnectionError,
MinecraftServerNotInitializedError,
) as error:
raise UpdateFailed(error) from error