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