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