mirror of
https://github.com/home-assistant/core.git
synced 2025-07-14 16:57:10 +00:00
Use PEP 695 TypeVar syntax for paperless_ngx (#147156)
This commit is contained in:
parent
b003429912
commit
cf67a68454
@ -5,7 +5,6 @@ from __future__ import annotations
|
|||||||
from abc import abstractmethod
|
from abc import abstractmethod
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
from typing import TypeVar
|
|
||||||
|
|
||||||
from pypaperless import Paperless
|
from pypaperless import Paperless
|
||||||
from pypaperless.exceptions import (
|
from pypaperless.exceptions import (
|
||||||
@ -25,8 +24,6 @@ from .const import DOMAIN, LOGGER
|
|||||||
|
|
||||||
type PaperlessConfigEntry = ConfigEntry[PaperlessData]
|
type PaperlessConfigEntry = ConfigEntry[PaperlessData]
|
||||||
|
|
||||||
TData = TypeVar("TData")
|
|
||||||
|
|
||||||
UPDATE_INTERVAL_STATISTICS = timedelta(seconds=120)
|
UPDATE_INTERVAL_STATISTICS = timedelta(seconds=120)
|
||||||
UPDATE_INTERVAL_STATUS = timedelta(seconds=300)
|
UPDATE_INTERVAL_STATUS = timedelta(seconds=300)
|
||||||
|
|
||||||
@ -39,7 +36,7 @@ class PaperlessData:
|
|||||||
status: PaperlessStatusCoordinator
|
status: PaperlessStatusCoordinator
|
||||||
|
|
||||||
|
|
||||||
class PaperlessCoordinator(DataUpdateCoordinator[TData]):
|
class PaperlessCoordinator[DataT](DataUpdateCoordinator[DataT]):
|
||||||
"""Coordinator to manage fetching Paperless-ngx API."""
|
"""Coordinator to manage fetching Paperless-ngx API."""
|
||||||
|
|
||||||
config_entry: PaperlessConfigEntry
|
config_entry: PaperlessConfigEntry
|
||||||
@ -63,7 +60,7 @@ class PaperlessCoordinator(DataUpdateCoordinator[TData]):
|
|||||||
update_interval=update_interval,
|
update_interval=update_interval,
|
||||||
)
|
)
|
||||||
|
|
||||||
async def _async_update_data(self) -> TData:
|
async def _async_update_data(self) -> DataT:
|
||||||
"""Update data via internal method."""
|
"""Update data via internal method."""
|
||||||
try:
|
try:
|
||||||
return await self._async_update_data_internal()
|
return await self._async_update_data_internal()
|
||||||
@ -89,7 +86,7 @@ class PaperlessCoordinator(DataUpdateCoordinator[TData]):
|
|||||||
) from err
|
) from err
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
async def _async_update_data_internal(self) -> TData:
|
async def _async_update_data_internal(self) -> DataT:
|
||||||
"""Update data via paperless-ngx API."""
|
"""Update data via paperless-ngx API."""
|
||||||
|
|
||||||
|
|
||||||
|
@ -2,8 +2,6 @@
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from typing import Generic, TypeVar
|
|
||||||
|
|
||||||
from homeassistant.components.sensor import EntityDescription
|
from homeassistant.components.sensor import EntityDescription
|
||||||
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
|
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
|
||||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||||
@ -11,17 +9,17 @@ from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
|||||||
from .const import DOMAIN
|
from .const import DOMAIN
|
||||||
from .coordinator import PaperlessCoordinator
|
from .coordinator import PaperlessCoordinator
|
||||||
|
|
||||||
TCoordinator = TypeVar("TCoordinator", bound=PaperlessCoordinator)
|
|
||||||
|
|
||||||
|
class PaperlessEntity[CoordinatorT: PaperlessCoordinator](
|
||||||
class PaperlessEntity(CoordinatorEntity[TCoordinator], Generic[TCoordinator]):
|
CoordinatorEntity[CoordinatorT]
|
||||||
|
):
|
||||||
"""Defines a base Paperless-ngx entity."""
|
"""Defines a base Paperless-ngx entity."""
|
||||||
|
|
||||||
_attr_has_entity_name = True
|
_attr_has_entity_name = True
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
coordinator: TCoordinator,
|
coordinator: CoordinatorT,
|
||||||
description: EntityDescription,
|
description: EntityDescription,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize the Paperless-ngx entity."""
|
"""Initialize the Paperless-ngx entity."""
|
||||||
|
@ -4,7 +4,6 @@ from __future__ import annotations
|
|||||||
|
|
||||||
from collections.abc import Callable
|
from collections.abc import Callable
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from typing import Generic
|
|
||||||
|
|
||||||
from pypaperless.models import Statistic, Status
|
from pypaperless.models import Statistic, Status
|
||||||
from pypaperless.models.common import StatusType
|
from pypaperless.models.common import StatusType
|
||||||
@ -23,23 +22,23 @@ from homeassistant.util.unit_conversion import InformationConverter
|
|||||||
|
|
||||||
from .coordinator import (
|
from .coordinator import (
|
||||||
PaperlessConfigEntry,
|
PaperlessConfigEntry,
|
||||||
|
PaperlessCoordinator,
|
||||||
PaperlessStatisticCoordinator,
|
PaperlessStatisticCoordinator,
|
||||||
PaperlessStatusCoordinator,
|
PaperlessStatusCoordinator,
|
||||||
TData,
|
|
||||||
)
|
)
|
||||||
from .entity import PaperlessEntity, TCoordinator
|
from .entity import PaperlessEntity
|
||||||
|
|
||||||
PARALLEL_UPDATES = 0
|
PARALLEL_UPDATES = 0
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True, kw_only=True)
|
@dataclass(frozen=True, kw_only=True)
|
||||||
class PaperlessEntityDescription(SensorEntityDescription, Generic[TData]):
|
class PaperlessEntityDescription[DataT](SensorEntityDescription):
|
||||||
"""Describes Paperless-ngx sensor entity."""
|
"""Describes Paperless-ngx sensor entity."""
|
||||||
|
|
||||||
value_fn: Callable[[TData], StateType]
|
value_fn: Callable[[DataT], StateType]
|
||||||
|
|
||||||
|
|
||||||
SENSOR_STATISTICS: tuple[PaperlessEntityDescription, ...] = (
|
SENSOR_STATISTICS: tuple[PaperlessEntityDescription[Statistic], ...] = (
|
||||||
PaperlessEntityDescription[Statistic](
|
PaperlessEntityDescription[Statistic](
|
||||||
key="documents_total",
|
key="documents_total",
|
||||||
translation_key="documents_total",
|
translation_key="documents_total",
|
||||||
@ -78,7 +77,7 @@ SENSOR_STATISTICS: tuple[PaperlessEntityDescription, ...] = (
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
SENSOR_STATUS: tuple[PaperlessEntityDescription, ...] = (
|
SENSOR_STATUS: tuple[PaperlessEntityDescription[Status], ...] = (
|
||||||
PaperlessEntityDescription[Status](
|
PaperlessEntityDescription[Status](
|
||||||
key="storage_total",
|
key="storage_total",
|
||||||
translation_key="storage_total",
|
translation_key="storage_total",
|
||||||
@ -258,7 +257,9 @@ async def async_setup_entry(
|
|||||||
async_add_entities(entities)
|
async_add_entities(entities)
|
||||||
|
|
||||||
|
|
||||||
class PaperlessSensor(PaperlessEntity[TCoordinator], SensorEntity):
|
class PaperlessSensor[CoordinatorT: PaperlessCoordinator](
|
||||||
|
PaperlessEntity[CoordinatorT], SensorEntity
|
||||||
|
):
|
||||||
"""Defines a Paperless-ngx sensor entity."""
|
"""Defines a Paperless-ngx sensor entity."""
|
||||||
|
|
||||||
entity_description: PaperlessEntityDescription
|
entity_description: PaperlessEntityDescription
|
||||||
|
Loading…
x
Reference in New Issue
Block a user