mirror of
https://github.com/home-assistant/core.git
synced 2025-07-27 07:07:28 +00:00
Migrate Syncthru to runtime data (#142775)
This commit is contained in:
parent
b957017799
commit
cba0cf0609
@ -4,22 +4,20 @@ from __future__ import annotations
|
|||||||
|
|
||||||
from pysyncthru import SyncThruAPINotSupported
|
from pysyncthru import SyncThruAPINotSupported
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.const import Platform
|
from homeassistant.const import Platform
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
|
||||||
from .const import DOMAIN
|
from .coordinator import SyncThruConfigEntry, SyncthruCoordinator
|
||||||
from .coordinator import SyncthruCoordinator
|
|
||||||
|
|
||||||
PLATFORMS = [Platform.BINARY_SENSOR, Platform.SENSOR]
|
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."""
|
"""Set up config entry."""
|
||||||
|
|
||||||
coordinator = SyncthruCoordinator(hass, entry)
|
coordinator = SyncthruCoordinator(hass, entry)
|
||||||
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
|
||||||
if isinstance(coordinator.last_exception, SyncThruAPINotSupported):
|
if isinstance(coordinator.last_exception, SyncThruAPINotSupported):
|
||||||
# this means that the printer does not support the syncthru JSON API
|
# this means that the printer does not support the syncthru JSON API
|
||||||
# and the config should simply be discarded
|
# and the config should simply be discarded
|
||||||
@ -29,8 +27,6 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
return True
|
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 the config entry."""
|
||||||
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||||
hass.data[DOMAIN].pop(entry.entry_id, None)
|
|
||||||
return unload_ok
|
|
||||||
|
@ -12,12 +12,10 @@ from homeassistant.components.binary_sensor import (
|
|||||||
BinarySensorEntity,
|
BinarySensorEntity,
|
||||||
BinarySensorEntityDescription,
|
BinarySensorEntityDescription,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||||
|
|
||||||
from . import SyncthruCoordinator
|
from .coordinator import SyncThruConfigEntry
|
||||||
from .const import DOMAIN
|
|
||||||
from .entity import SyncthruEntity
|
from .entity import SyncthruEntity
|
||||||
|
|
||||||
SYNCTHRU_STATE_PROBLEM = {
|
SYNCTHRU_STATE_PROBLEM = {
|
||||||
@ -54,12 +52,12 @@ BINARY_SENSORS: tuple[SyncThruBinarySensorDescription, ...] = (
|
|||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
config_entry: ConfigEntry,
|
config_entry: SyncThruConfigEntry,
|
||||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up from config entry."""
|
"""Set up from config entry."""
|
||||||
|
|
||||||
coordinator: SyncthruCoordinator = hass.data[DOMAIN][config_entry.entry_id]
|
coordinator = config_entry.runtime_data
|
||||||
|
|
||||||
async_add_entities(
|
async_add_entities(
|
||||||
SyncThruBinarySensor(coordinator, description) for description in BINARY_SENSORS
|
SyncThruBinarySensor(coordinator, description) for description in BINARY_SENSORS
|
||||||
|
@ -16,11 +16,13 @@ from .const import DOMAIN
|
|||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
type SyncThruConfigEntry = ConfigEntry[SyncthruCoordinator]
|
||||||
|
|
||||||
|
|
||||||
class SyncthruCoordinator(DataUpdateCoordinator[SyncThru]):
|
class SyncthruCoordinator(DataUpdateCoordinator[SyncThru]):
|
||||||
"""Class to manage fetching Syncthru data."""
|
"""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."""
|
"""Initialize the Syncthru coordinator."""
|
||||||
super().__init__(
|
super().__init__(
|
||||||
hass,
|
hass,
|
||||||
|
@ -4,15 +4,14 @@ from __future__ import annotations
|
|||||||
|
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
|
||||||
from .const import DOMAIN
|
from .coordinator import SyncThruConfigEntry
|
||||||
|
|
||||||
|
|
||||||
async def async_get_config_entry_diagnostics(
|
async def async_get_config_entry_diagnostics(
|
||||||
hass: HomeAssistant, entry: ConfigEntry
|
hass: HomeAssistant, entry: SyncThruConfigEntry
|
||||||
) -> dict[str, Any]:
|
) -> dict[str, Any]:
|
||||||
"""Return diagnostics for a config entry."""
|
"""Return diagnostics for a config entry."""
|
||||||
|
|
||||||
return hass.data[DOMAIN][entry.entry_id].data.raw()
|
return entry.runtime_data.data.raw()
|
||||||
|
@ -5,7 +5,8 @@ from homeassistant.helpers.device_registry import DeviceInfo
|
|||||||
from homeassistant.helpers.entity import EntityDescription
|
from homeassistant.helpers.entity import EntityDescription
|
||||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||||
|
|
||||||
from . import DOMAIN, SyncthruCoordinator
|
from .const import DOMAIN
|
||||||
|
from .coordinator import SyncthruCoordinator
|
||||||
|
|
||||||
|
|
||||||
class SyncthruEntity(CoordinatorEntity[SyncthruCoordinator]):
|
class SyncthruEntity(CoordinatorEntity[SyncthruCoordinator]):
|
||||||
|
@ -9,13 +9,11 @@ from typing import Any, cast
|
|||||||
from pysyncthru import SyncThru, SyncthruState
|
from pysyncthru import SyncThru, SyncthruState
|
||||||
|
|
||||||
from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
|
from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.const import PERCENTAGE
|
from homeassistant.const import PERCENTAGE
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||||
|
|
||||||
from . import SyncthruCoordinator
|
from .coordinator import SyncThruConfigEntry
|
||||||
from .const import DOMAIN
|
|
||||||
from .entity import SyncthruEntity
|
from .entity import SyncthruEntity
|
||||||
|
|
||||||
SYNCTHRU_STATE_HUMAN = {
|
SYNCTHRU_STATE_HUMAN = {
|
||||||
@ -118,12 +116,12 @@ SENSOR_TYPES: tuple[SyncThruSensorDescription, ...] = (
|
|||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
config_entry: ConfigEntry,
|
config_entry: SyncThruConfigEntry,
|
||||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up from config entry."""
|
"""Set up from config entry."""
|
||||||
|
|
||||||
coordinator: SyncthruCoordinator = hass.data[DOMAIN][config_entry.entry_id]
|
coordinator = config_entry.runtime_data
|
||||||
printer = coordinator.data
|
printer = coordinator.data
|
||||||
|
|
||||||
supp_toner = printer.toner_status(filter_supported=True)
|
supp_toner = printer.toner_status(filter_supported=True)
|
||||||
|
@ -6,7 +6,7 @@ from unittest.mock import AsyncMock, patch
|
|||||||
from pysyncthru import SyncthruState
|
from pysyncthru import SyncthruState
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from homeassistant.components.syncthru import DOMAIN
|
from homeassistant.components.syncthru.const import DOMAIN
|
||||||
from homeassistant.const import CONF_NAME, CONF_URL
|
from homeassistant.const import CONF_NAME, CONF_URL
|
||||||
|
|
||||||
from tests.common import MockConfigEntry, load_json_object_fixture
|
from tests.common import MockConfigEntry, load_json_object_fixture
|
||||||
|
Loading…
x
Reference in New Issue
Block a user