mirror of
https://github.com/home-assistant/core.git
synced 2025-07-16 17:57:11 +00:00
Use ConfigEntry.runtime_data to store Minecraft Server runtime data (#139039)
This commit is contained in:
parent
4b342b7dd4
commit
f369ded93d
@ -9,15 +9,13 @@ import dns.rdata
|
||||
import dns.rdataclass
|
||||
import dns.rdatatype
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import CONF_ADDRESS, CONF_HOST, CONF_PORT, CONF_TYPE, Platform
|
||||
from homeassistant.const import CONF_ADDRESS, CONF_HOST, CONF_PORT, Platform
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.exceptions import ConfigEntryNotReady
|
||||
from homeassistant.helpers import device_registry as dr, entity_registry as er
|
||||
|
||||
from .api import MinecraftServer, MinecraftServerAddressError, MinecraftServerType
|
||||
from .const import DOMAIN, KEY_LATENCY, KEY_MOTD
|
||||
from .coordinator import MinecraftServerCoordinator
|
||||
from .coordinator import MinecraftServerConfigEntry, MinecraftServerCoordinator
|
||||
|
||||
PLATFORMS = [Platform.BINARY_SENSOR, Platform.SENSOR]
|
||||
|
||||
@ -31,32 +29,18 @@ def load_dnspython_rdata_classes() -> None:
|
||||
dns.rdata.get_rdata_class(dns.rdataclass.IN, rdtype) # type: ignore[no-untyped-call]
|
||||
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant, entry: MinecraftServerConfigEntry
|
||||
) -> bool:
|
||||
"""Set up Minecraft Server from a config entry."""
|
||||
|
||||
# Workaround to avoid blocking imports from dnspython (https://github.com/rthalley/dnspython/issues/1083)
|
||||
await hass.async_add_executor_job(load_dnspython_rdata_classes)
|
||||
|
||||
# Create API instance.
|
||||
api = MinecraftServer(
|
||||
hass,
|
||||
entry.data.get(CONF_TYPE, MinecraftServerType.JAVA_EDITION),
|
||||
entry.data[CONF_ADDRESS],
|
||||
)
|
||||
|
||||
# Initialize API instance.
|
||||
try:
|
||||
await api.async_initialize()
|
||||
except MinecraftServerAddressError as error:
|
||||
raise ConfigEntryNotReady(f"Initialization failed: {error}") from error
|
||||
|
||||
# Create coordinator instance.
|
||||
coordinator = MinecraftServerCoordinator(hass, entry, api)
|
||||
# Create coordinator instance and store it.
|
||||
coordinator = MinecraftServerCoordinator(hass, entry)
|
||||
await coordinator.async_config_entry_first_refresh()
|
||||
|
||||
# Store coordinator instance.
|
||||
domain_data = hass.data.setdefault(DOMAIN, {})
|
||||
domain_data[entry.entry_id] = coordinator
|
||||
entry.runtime_data = coordinator
|
||||
|
||||
# Set up platforms.
|
||||
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||
@ -64,21 +48,16 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
return True
|
||||
|
||||
|
||||
async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
|
||||
async def async_unload_entry(
|
||||
hass: HomeAssistant, config_entry: MinecraftServerConfigEntry
|
||||
) -> bool:
|
||||
"""Unload Minecraft Server config entry."""
|
||||
|
||||
# Unload platforms.
|
||||
unload_ok = await hass.config_entries.async_unload_platforms(
|
||||
config_entry, PLATFORMS
|
||||
)
|
||||
|
||||
# Clean up.
|
||||
hass.data[DOMAIN].pop(config_entry.entry_id)
|
||||
|
||||
return unload_ok
|
||||
return await hass.config_entries.async_unload_platforms(config_entry, PLATFORMS)
|
||||
|
||||
|
||||
async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
|
||||
async def async_migrate_entry(
|
||||
hass: HomeAssistant, config_entry: MinecraftServerConfigEntry
|
||||
) -> bool:
|
||||
"""Migrate old config entry to a new format."""
|
||||
|
||||
# 1 --> 2: Use config entry ID as base for unique IDs.
|
||||
@ -152,7 +131,9 @@ async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry) ->
|
||||
|
||||
|
||||
async def _async_migrate_device_identifiers(
|
||||
hass: HomeAssistant, config_entry: ConfigEntry, old_unique_id: str | None
|
||||
hass: HomeAssistant,
|
||||
config_entry: MinecraftServerConfigEntry,
|
||||
old_unique_id: str | None,
|
||||
) -> None:
|
||||
"""Migrate the device identifiers to the new format."""
|
||||
device_registry = dr.async_get(hass)
|
||||
|
@ -5,12 +5,10 @@ from homeassistant.components.binary_sensor import (
|
||||
BinarySensorEntity,
|
||||
BinarySensorEntityDescription,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||
|
||||
from .const import DOMAIN
|
||||
from .coordinator import MinecraftServerCoordinator
|
||||
from .coordinator import MinecraftServerConfigEntry, MinecraftServerCoordinator
|
||||
from .entity import MinecraftServerEntity
|
||||
|
||||
KEY_STATUS = "status"
|
||||
@ -27,11 +25,11 @@ BINARY_SENSOR_DESCRIPTIONS = [
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
config_entry: ConfigEntry,
|
||||
config_entry: MinecraftServerConfigEntry,
|
||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up the Minecraft Server binary sensor platform."""
|
||||
coordinator = hass.data[DOMAIN][config_entry.entry_id]
|
||||
coordinator = config_entry.runtime_data
|
||||
|
||||
# Add binary sensor entities.
|
||||
async_add_entities(
|
||||
@ -49,7 +47,7 @@ class MinecraftServerBinarySensorEntity(MinecraftServerEntity, BinarySensorEntit
|
||||
self,
|
||||
coordinator: MinecraftServerCoordinator,
|
||||
description: BinarySensorEntityDescription,
|
||||
config_entry: ConfigEntry,
|
||||
config_entry: MinecraftServerConfigEntry,
|
||||
) -> None:
|
||||
"""Initialize binary sensor base entity."""
|
||||
super().__init__(coordinator, config_entry)
|
||||
|
@ -6,17 +6,22 @@ from datetime import timedelta
|
||||
import logging
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import CONF_NAME
|
||||
from homeassistant.const import CONF_ADDRESS, CONF_NAME, CONF_TYPE
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import ConfigEntryNotReady
|
||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
||||
|
||||
from .api import (
|
||||
MinecraftServer,
|
||||
MinecraftServerAddressError,
|
||||
MinecraftServerConnectionError,
|
||||
MinecraftServerData,
|
||||
MinecraftServerNotInitializedError,
|
||||
MinecraftServerType,
|
||||
)
|
||||
|
||||
type MinecraftServerConfigEntry = ConfigEntry[MinecraftServerCoordinator]
|
||||
|
||||
SCAN_INTERVAL = timedelta(seconds=60)
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
@ -25,16 +30,15 @@ _LOGGER = logging.getLogger(__name__)
|
||||
class MinecraftServerCoordinator(DataUpdateCoordinator[MinecraftServerData]):
|
||||
"""Minecraft Server data update coordinator."""
|
||||
|
||||
config_entry: ConfigEntry
|
||||
config_entry: MinecraftServerConfigEntry
|
||||
_api: MinecraftServer
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
hass: HomeAssistant,
|
||||
config_entry: ConfigEntry,
|
||||
api: MinecraftServer,
|
||||
config_entry: MinecraftServerConfigEntry,
|
||||
) -> None:
|
||||
"""Initialize coordinator instance."""
|
||||
self._api = api
|
||||
|
||||
super().__init__(
|
||||
hass=hass,
|
||||
@ -44,6 +48,22 @@ class MinecraftServerCoordinator(DataUpdateCoordinator[MinecraftServerData]):
|
||||
update_interval=SCAN_INTERVAL,
|
||||
)
|
||||
|
||||
async def _async_setup(self) -> None:
|
||||
"""Set up the Minecraft Server data coordinator."""
|
||||
|
||||
# Create API instance.
|
||||
self._api = MinecraftServer(
|
||||
self.hass,
|
||||
self.config_entry.data.get(CONF_TYPE, MinecraftServerType.JAVA_EDITION),
|
||||
self.config_entry.data[CONF_ADDRESS],
|
||||
)
|
||||
|
||||
# Initialize API instance.
|
||||
try:
|
||||
await self._api.async_initialize()
|
||||
except MinecraftServerAddressError as error:
|
||||
raise ConfigEntryNotReady(f"Initialization failed: {error}") from error
|
||||
|
||||
async def _async_update_data(self) -> MinecraftServerData:
|
||||
"""Get updated data from the server."""
|
||||
try:
|
||||
|
@ -5,20 +5,19 @@ from dataclasses import asdict
|
||||
from typing import Any
|
||||
|
||||
from homeassistant.components.diagnostics import async_redact_data
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import CONF_ADDRESS, CONF_NAME
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from .const import DOMAIN
|
||||
from .coordinator import MinecraftServerConfigEntry
|
||||
|
||||
TO_REDACT: Iterable[Any] = {CONF_ADDRESS, CONF_NAME, "players_list"}
|
||||
|
||||
|
||||
async def async_get_config_entry_diagnostics(
|
||||
hass: HomeAssistant, config_entry: ConfigEntry
|
||||
hass: HomeAssistant, config_entry: MinecraftServerConfigEntry
|
||||
) -> dict[str, Any]:
|
||||
"""Return diagnostics for a config entry."""
|
||||
coordinator = hass.data[DOMAIN][config_entry.entry_id]
|
||||
coordinator = config_entry.runtime_data
|
||||
|
||||
return {
|
||||
"config_entry": {
|
||||
|
@ -29,7 +29,7 @@ rules:
|
||||
status: done
|
||||
comment: Using confid entry ID as the dependency mcstatus doesn't provide a unique information.
|
||||
has-entity-name: done
|
||||
runtime-data: todo
|
||||
runtime-data: done
|
||||
test-before-configure: done
|
||||
test-before-setup:
|
||||
status: done
|
||||
|
@ -7,15 +7,14 @@ from dataclasses import dataclass
|
||||
from typing import Any
|
||||
|
||||
from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import CONF_TYPE, EntityCategory, UnitOfTime
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||
from homeassistant.helpers.typing import StateType
|
||||
|
||||
from .api import MinecraftServerData, MinecraftServerType
|
||||
from .const import DOMAIN, KEY_LATENCY, KEY_MOTD
|
||||
from .coordinator import MinecraftServerCoordinator
|
||||
from .const import KEY_LATENCY, KEY_MOTD
|
||||
from .coordinator import MinecraftServerConfigEntry, MinecraftServerCoordinator
|
||||
from .entity import MinecraftServerEntity
|
||||
|
||||
ATTR_PLAYERS_LIST = "players_list"
|
||||
@ -158,11 +157,11 @@ SENSOR_DESCRIPTIONS = [
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
config_entry: ConfigEntry,
|
||||
config_entry: MinecraftServerConfigEntry,
|
||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up the Minecraft Server sensor platform."""
|
||||
coordinator = hass.data[DOMAIN][config_entry.entry_id]
|
||||
coordinator = config_entry.runtime_data
|
||||
|
||||
# Add sensor entities.
|
||||
async_add_entities(
|
||||
@ -184,7 +183,7 @@ class MinecraftServerSensorEntity(MinecraftServerEntity, SensorEntity):
|
||||
self,
|
||||
coordinator: MinecraftServerCoordinator,
|
||||
description: MinecraftServerSensorEntityDescription,
|
||||
config_entry: ConfigEntry,
|
||||
config_entry: MinecraftServerConfigEntry,
|
||||
) -> None:
|
||||
"""Initialize sensor base entity."""
|
||||
super().__init__(coordinator, config_entry)
|
||||
|
Loading…
x
Reference in New Issue
Block a user