mirror of
https://github.com/home-assistant/core.git
synced 2025-04-24 01:08:12 +00:00
Add Self typing (4) [mypy 1.0] (#87601)
This commit is contained in:
parent
f7b39aa4a8
commit
ea4e2ab4aa
@ -11,6 +11,8 @@ from math import floor, log10
|
||||
import re
|
||||
from typing import Any, Final, cast, final
|
||||
|
||||
from typing_extensions import Self
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
|
||||
# pylint: disable=[hass-deprecated-import]
|
||||
@ -767,7 +769,7 @@ class SensorExtraStoredData(ExtraStoredData):
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, restored: dict[str, Any]) -> SensorExtraStoredData | None:
|
||||
def from_dict(cls, restored: dict[str, Any]) -> Self | None:
|
||||
"""Initialize a stored sensor state from a dict."""
|
||||
try:
|
||||
native_value = restored["native_value"]
|
||||
|
@ -12,6 +12,7 @@ from typing import Any, cast
|
||||
|
||||
import attr
|
||||
import av
|
||||
from typing_extensions import Self
|
||||
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
@ -399,7 +400,7 @@ class PeekIterator(Iterator):
|
||||
# A pointer to either _iterator or _buffer
|
||||
self._next = self._iterator.__next__
|
||||
|
||||
def __iter__(self) -> Iterator:
|
||||
def __iter__(self) -> Self:
|
||||
"""Return an iterator."""
|
||||
return self
|
||||
|
||||
|
@ -7,6 +7,7 @@ from functools import partial
|
||||
import logging
|
||||
from typing import Any
|
||||
|
||||
from typing_extensions import Self
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components.binary_sensor import (
|
||||
@ -473,7 +474,7 @@ class AutoOffExtraStoredData(ExtraStoredData):
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, restored: dict[str, Any]) -> AutoOffExtraStoredData | None:
|
||||
def from_dict(cls, restored: dict[str, Any]) -> Self | None:
|
||||
"""Initialize a stored binary sensor state from a dict."""
|
||||
try:
|
||||
auto_off_time = restored["auto_off_time"]
|
||||
|
@ -5,6 +5,7 @@ from collections.abc import Callable
|
||||
from datetime import datetime, timedelta
|
||||
import logging
|
||||
|
||||
from typing_extensions import Self
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.const import (
|
||||
@ -207,14 +208,14 @@ class Timer(collection.CollectionEntity, RestoreEntity):
|
||||
self._attr_force_update = True
|
||||
|
||||
@classmethod
|
||||
def from_storage(cls, config: ConfigType) -> Timer:
|
||||
def from_storage(cls, config: ConfigType) -> Self:
|
||||
"""Return entity instance initialized from storage."""
|
||||
timer = cls(config)
|
||||
timer.editable = True
|
||||
return timer
|
||||
|
||||
@classmethod
|
||||
def from_yaml(cls, config: ConfigType) -> Timer:
|
||||
def from_yaml(cls, config: ConfigType) -> Self:
|
||||
"""Return entity instance initialized from yaml."""
|
||||
timer = cls(config)
|
||||
timer.entity_id = ENTITY_ID_FORMAT.format(config[CONF_ID])
|
||||
|
@ -8,6 +8,7 @@ import struct
|
||||
from typing import Any, Literal, overload
|
||||
|
||||
from tuya_iot import TuyaDevice, TuyaDeviceManager
|
||||
from typing_extensions import Self
|
||||
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
from homeassistant.helpers.entity import DeviceInfo, Entity
|
||||
@ -112,12 +113,12 @@ class ElectricityTypeData:
|
||||
voltage: str | None = None
|
||||
|
||||
@classmethod
|
||||
def from_json(cls, data: str) -> ElectricityTypeData:
|
||||
def from_json(cls, data: str) -> Self:
|
||||
"""Load JSON string and return a ElectricityTypeData object."""
|
||||
return cls(**json.loads(data.lower()))
|
||||
|
||||
@classmethod
|
||||
def from_raw(cls, data: str) -> ElectricityTypeData:
|
||||
def from_raw(cls, data: str) -> Self:
|
||||
"""Decode base64 string and return a ElectricityTypeData object."""
|
||||
raw = base64.b64decode(data)
|
||||
voltage = struct.unpack(">H", raw[0:2])[0] / 10.0
|
||||
|
@ -8,6 +8,7 @@ import logging
|
||||
from typing import Any
|
||||
|
||||
from croniter import croniter
|
||||
from typing_extensions import Self
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components.sensor import (
|
||||
@ -274,7 +275,7 @@ class UtilitySensorExtraStoredData(SensorExtraStoredData):
|
||||
return data
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, restored: dict[str, Any]) -> UtilitySensorExtraStoredData | None:
|
||||
def from_dict(cls, restored: dict[str, Any]) -> Self | None:
|
||||
"""Initialize a stored sensor state from a dict."""
|
||||
extra = SensorExtraStoredData.from_dict(restored)
|
||||
if extra is None:
|
||||
|
@ -12,6 +12,7 @@ from urllib.parse import urlparse
|
||||
import async_timeout
|
||||
from async_upnp_client.search import SsdpSearchListener
|
||||
from async_upnp_client.utils import CaseInsensitiveDict
|
||||
from typing_extensions import Self
|
||||
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.components import network, ssdp
|
||||
@ -35,7 +36,7 @@ _LOGGER = logging.getLogger(__name__)
|
||||
class YeelightScanner:
|
||||
"""Scan for Yeelight devices."""
|
||||
|
||||
_scanner: YeelightScanner | None = None
|
||||
_scanner: Self | None = None
|
||||
|
||||
@classmethod
|
||||
@callback
|
||||
|
@ -5,6 +5,7 @@ from collections.abc import Callable
|
||||
import logging
|
||||
from typing import Any, cast
|
||||
|
||||
from typing_extensions import Self
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant import config_entries
|
||||
@ -297,7 +298,7 @@ class Zone(collection.CollectionEntity):
|
||||
self._persons_in_zone: set[str] = set()
|
||||
|
||||
@classmethod
|
||||
def from_storage(cls, config: ConfigType) -> Zone:
|
||||
def from_storage(cls, config: ConfigType) -> Self:
|
||||
"""Return entity instance initialized from storage."""
|
||||
zone = cls(config)
|
||||
zone.editable = True
|
||||
@ -305,7 +306,7 @@ class Zone(collection.CollectionEntity):
|
||||
return zone
|
||||
|
||||
@classmethod
|
||||
def from_yaml(cls, config: ConfigType) -> Zone:
|
||||
def from_yaml(cls, config: ConfigType) -> Self:
|
||||
"""Return entity instance initialized from yaml."""
|
||||
zone = cls(config)
|
||||
zone.editable = False
|
||||
|
@ -7,6 +7,7 @@ import os
|
||||
from pathlib import Path
|
||||
from typing import NamedTuple
|
||||
|
||||
from typing_extensions import Self
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant import loader
|
||||
@ -54,7 +55,7 @@ class HomeAssistantConfig(OrderedDict):
|
||||
message: str,
|
||||
domain: str | None = None,
|
||||
config: ConfigType | None = None,
|
||||
) -> HomeAssistantConfig:
|
||||
) -> Self:
|
||||
"""Add a single error."""
|
||||
self.errors.append(CheckConfigError(str(message), domain, config))
|
||||
return self
|
||||
|
@ -6,6 +6,7 @@ import sys
|
||||
from typing import Any
|
||||
|
||||
import httpx
|
||||
from typing_extensions import Self
|
||||
|
||||
from homeassistant.const import APPLICATION_NAME, EVENT_HOMEASSISTANT_CLOSE, __version__
|
||||
from homeassistant.core import Event, HomeAssistant, callback
|
||||
@ -41,7 +42,7 @@ def get_async_client(hass: HomeAssistant, verify_ssl: bool = True) -> httpx.Asyn
|
||||
class HassHttpXAsyncClient(httpx.AsyncClient):
|
||||
"""httpx AsyncClient that suppresses context management."""
|
||||
|
||||
async def __aenter__(self: HassHttpXAsyncClient) -> HassHttpXAsyncClient:
|
||||
async def __aenter__(self) -> Self:
|
||||
"""Prevent an integration from reopen of the client via context manager."""
|
||||
return self
|
||||
|
||||
|
@ -10,6 +10,8 @@ import enum
|
||||
from types import TracebackType
|
||||
from typing import Any
|
||||
|
||||
from typing_extensions import Self
|
||||
|
||||
from .async_ import run_callback_threadsafe
|
||||
|
||||
ZONE_GLOBAL = "global"
|
||||
@ -32,7 +34,7 @@ class _GlobalFreezeContext:
|
||||
self._loop: asyncio.AbstractEventLoop = asyncio.get_running_loop()
|
||||
self._manager: TimeoutManager = manager
|
||||
|
||||
async def __aenter__(self) -> _GlobalFreezeContext:
|
||||
async def __aenter__(self) -> Self:
|
||||
self._enter()
|
||||
return self
|
||||
|
||||
@ -45,7 +47,7 @@ class _GlobalFreezeContext:
|
||||
self._exit()
|
||||
return None
|
||||
|
||||
def __enter__(self) -> _GlobalFreezeContext:
|
||||
def __enter__(self) -> Self:
|
||||
self._loop.call_soon_threadsafe(self._enter)
|
||||
return self
|
||||
|
||||
@ -100,7 +102,7 @@ class _ZoneFreezeContext:
|
||||
self._loop: asyncio.AbstractEventLoop = asyncio.get_running_loop()
|
||||
self._zone: _ZoneTimeoutManager = zone
|
||||
|
||||
async def __aenter__(self) -> _ZoneFreezeContext:
|
||||
async def __aenter__(self) -> Self:
|
||||
self._enter()
|
||||
return self
|
||||
|
||||
@ -113,7 +115,7 @@ class _ZoneFreezeContext:
|
||||
self._exit()
|
||||
return None
|
||||
|
||||
def __enter__(self) -> _ZoneFreezeContext:
|
||||
def __enter__(self) -> Self:
|
||||
self._loop.call_soon_threadsafe(self._enter)
|
||||
return self
|
||||
|
||||
@ -161,7 +163,7 @@ class _GlobalTaskContext:
|
||||
self._state: _State = _State.INIT
|
||||
self._cool_down: float = cool_down
|
||||
|
||||
async def __aenter__(self) -> _GlobalTaskContext:
|
||||
async def __aenter__(self) -> Self:
|
||||
self._manager.global_tasks.append(self)
|
||||
self._start_timer()
|
||||
self._state = _State.ACTIVE
|
||||
@ -271,7 +273,7 @@ class _ZoneTaskContext:
|
||||
"""Return state of the Zone task."""
|
||||
return self._state
|
||||
|
||||
async def __aenter__(self) -> _ZoneTaskContext:
|
||||
async def __aenter__(self) -> Self:
|
||||
self._zone.enter_task(self)
|
||||
self._state = _State.ACTIVE
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user