Skip to content

Commit

Permalink
Use PEP 695 TypeVar syntax (home-assistant#133049)
Browse files Browse the repository at this point in the history
  • Loading branch information
cdce8p authored Dec 12, 2024
1 parent 0a74825 commit 5c6e4ad
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 20 deletions.
7 changes: 2 additions & 5 deletions homeassistant/components/motionblinds_ble/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from dataclasses import dataclass
import logging
from math import ceil
from typing import Generic, TypeVar

from motionblindsble.const import (
MotionBlindType,
Expand Down Expand Up @@ -45,11 +44,9 @@

PARALLEL_UPDATES = 0

_T = TypeVar("_T")


@dataclass(frozen=True, kw_only=True)
class MotionblindsBLESensorEntityDescription(SensorEntityDescription, Generic[_T]):
class MotionblindsBLESensorEntityDescription[_T](SensorEntityDescription):
"""Entity description of a sensor entity with initial_value attribute."""

initial_value: str | None = None
Expand Down Expand Up @@ -110,7 +107,7 @@ async def async_setup_entry(
async_add_entities(entities)


class MotionblindsBLESensorEntity(MotionblindsBLEEntity, SensorEntity, Generic[_T]):
class MotionblindsBLESensorEntity[_T](MotionblindsBLEEntity, SensorEntity):
"""Representation of a sensor entity."""

entity_description: MotionblindsBLESensorEntityDescription[_T]
Expand Down
7 changes: 3 additions & 4 deletions homeassistant/components/powerfox/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

from collections.abc import Callable
from dataclasses import dataclass
from typing import Generic, TypeVar

from powerfox import Device, PowerMeter, WaterMeter

Expand All @@ -22,11 +21,11 @@
from .coordinator import PowerfoxDataUpdateCoordinator
from .entity import PowerfoxEntity

T = TypeVar("T", PowerMeter, WaterMeter)


@dataclass(frozen=True, kw_only=True)
class PowerfoxSensorEntityDescription(Generic[T], SensorEntityDescription):
class PowerfoxSensorEntityDescription[T: (PowerMeter, WaterMeter)](
SensorEntityDescription
):
"""Describes Poweropti sensor entity."""

value_fn: Callable[[T], float | int | None]
Expand Down
12 changes: 5 additions & 7 deletions homeassistant/components/powerwall/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from collections.abc import Callable
from dataclasses import dataclass
from operator import attrgetter, methodcaller
from typing import TYPE_CHECKING, Generic, TypeVar
from typing import TYPE_CHECKING

from tesla_powerwall import GridState, MeterResponse, MeterType

Expand Down Expand Up @@ -35,14 +35,12 @@
_METER_DIRECTION_EXPORT = "export"
_METER_DIRECTION_IMPORT = "import"

_ValueParamT = TypeVar("_ValueParamT")
_ValueT = TypeVar("_ValueT", bound=float | int | str | None)
type _ValueType = float | int | str | None


@dataclass(frozen=True, kw_only=True)
class PowerwallSensorEntityDescription(
SensorEntityDescription,
Generic[_ValueParamT, _ValueT],
class PowerwallSensorEntityDescription[_ValueParamT, _ValueT: _ValueType](
SensorEntityDescription
):
"""Describes Powerwall entity."""

Expand Down Expand Up @@ -389,7 +387,7 @@ def native_value(self) -> float | None:
return meter.get_energy_imported()


class PowerWallBatterySensor(BatteryEntity, SensorEntity, Generic[_ValueT]):
class PowerWallBatterySensor[_ValueT: _ValueType](BatteryEntity, SensorEntity):
"""Representation of an Powerwall Battery sensor."""

entity_description: PowerwallSensorEntityDescription[BatteryResponse, _ValueT]
Expand Down
7 changes: 3 additions & 4 deletions homeassistant/helpers/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@
RANDOM_MICROSECOND_MAX = 500000

_TypedDictT = TypeVar("_TypedDictT", bound=Mapping[str, Any])
_StateEventDataT = TypeVar("_StateEventDataT", bound=EventStateEventData)


@dataclass(slots=True, frozen=True)
Expand Down Expand Up @@ -333,7 +332,7 @@ def async_track_state_change_event(


@callback
def _async_dispatch_entity_id_event_soon(
def _async_dispatch_entity_id_event_soon[_StateEventDataT: EventStateEventData](
hass: HomeAssistant,
callbacks: dict[str, list[HassJob[[Event[_StateEventDataT]], Any]]],
event: Event[_StateEventDataT],
Expand All @@ -343,7 +342,7 @@ def _async_dispatch_entity_id_event_soon(


@callback
def _async_dispatch_entity_id_event(
def _async_dispatch_entity_id_event[_StateEventDataT: EventStateEventData](
hass: HomeAssistant,
callbacks: dict[str, list[HassJob[[Event[_StateEventDataT]], Any]]],
event: Event[_StateEventDataT],
Expand All @@ -363,7 +362,7 @@ def _async_dispatch_entity_id_event(


@callback
def _async_state_filter(
def _async_state_filter[_StateEventDataT: EventStateEventData](
hass: HomeAssistant,
callbacks: dict[str, list[HassJob[[Event[_StateEventDataT]], Any]]],
event_data: _StateEventDataT,
Expand Down

0 comments on commit 5c6e4ad

Please sign in to comment.