Migrate Syncthru to runtime data (#142775)

This commit is contained in:
Joost Lekkerkerker 2025-04-12 20:59:59 +02:00 committed by GitHub
parent b957017799
commit cba0cf0609
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 20 additions and 26 deletions

View File

@ -4,22 +4,20 @@ from __future__ import annotations
from pysyncthru import SyncThruAPINotSupported
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from .const import DOMAIN
from .coordinator import SyncthruCoordinator
from .coordinator import SyncThruConfigEntry, SyncthruCoordinator
PLATFORMS = [Platform.BINARY_SENSOR, Platform.SENSOR]
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def async_setup_entry(hass: HomeAssistant, entry: SyncThruConfigEntry) -> bool:
"""Set up config entry."""
coordinator = SyncthruCoordinator(hass, entry)
await coordinator.async_config_entry_first_refresh()
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator
entry.runtime_data = coordinator
if isinstance(coordinator.last_exception, SyncThruAPINotSupported):
# this means that the printer does not support the syncthru JSON API
# and the config should simply be discarded
@ -29,8 +27,6 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
return True
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def async_unload_entry(hass: HomeAssistant, entry: SyncThruConfigEntry) -> bool:
"""Unload the config entry."""
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
hass.data[DOMAIN].pop(entry.entry_id, None)
return unload_ok
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)

View File

@ -12,12 +12,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 . import SyncthruCoordinator
from .const import DOMAIN
from .coordinator import SyncThruConfigEntry
from .entity import SyncthruEntity
SYNCTHRU_STATE_PROBLEM = {
@ -54,12 +52,12 @@ BINARY_SENSORS: tuple[SyncThruBinarySensorDescription, ...] = (
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
config_entry: SyncThruConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
"""Set up from config entry."""
coordinator: SyncthruCoordinator = hass.data[DOMAIN][config_entry.entry_id]
coordinator = config_entry.runtime_data
async_add_entities(
SyncThruBinarySensor(coordinator, description) for description in BINARY_SENSORS

View File

@ -16,11 +16,13 @@ from .const import DOMAIN
_LOGGER = logging.getLogger(__name__)
type SyncThruConfigEntry = ConfigEntry[SyncthruCoordinator]
class SyncthruCoordinator(DataUpdateCoordinator[SyncThru]):
"""Class to manage fetching Syncthru data."""
def __init__(self, hass: HomeAssistant, entry: ConfigEntry) -> None:
def __init__(self, hass: HomeAssistant, entry: SyncThruConfigEntry) -> None:
"""Initialize the Syncthru coordinator."""
super().__init__(
hass,

View File

@ -4,15 +4,14 @@ from __future__ import annotations
from typing import Any
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from .const import DOMAIN
from .coordinator import SyncThruConfigEntry
async def async_get_config_entry_diagnostics(
hass: HomeAssistant, entry: ConfigEntry
hass: HomeAssistant, entry: SyncThruConfigEntry
) -> dict[str, Any]:
"""Return diagnostics for a config entry."""
return hass.data[DOMAIN][entry.entry_id].data.raw()
return entry.runtime_data.data.raw()

View File

@ -5,7 +5,8 @@ from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity import EntityDescription
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from . import DOMAIN, SyncthruCoordinator
from .const import DOMAIN
from .coordinator import SyncthruCoordinator
class SyncthruEntity(CoordinatorEntity[SyncthruCoordinator]):

View File

@ -9,13 +9,11 @@ from typing import Any, cast
from pysyncthru import SyncThru, SyncthruState
from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import PERCENTAGE
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from . import SyncthruCoordinator
from .const import DOMAIN
from .coordinator import SyncThruConfigEntry
from .entity import SyncthruEntity
SYNCTHRU_STATE_HUMAN = {
@ -118,12 +116,12 @@ SENSOR_TYPES: tuple[SyncThruSensorDescription, ...] = (
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
config_entry: SyncThruConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
"""Set up from config entry."""
coordinator: SyncthruCoordinator = hass.data[DOMAIN][config_entry.entry_id]
coordinator = config_entry.runtime_data
printer = coordinator.data
supp_toner = printer.toner_status(filter_supported=True)

View File

@ -6,7 +6,7 @@ from unittest.mock import AsyncMock, patch
from pysyncthru import SyncthruState
import pytest
from homeassistant.components.syncthru import DOMAIN
from homeassistant.components.syncthru.const import DOMAIN
from homeassistant.const import CONF_NAME, CONF_URL
from tests.common import MockConfigEntry, load_json_object_fixture