mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +00:00
Add SignificantChangeProtocol to improve platform typing (#117002)
This commit is contained in:
parent
7148c849d6
commit
018e7731ae
@ -29,17 +29,18 @@ The following cases will never be passed to your function:
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from collections.abc import Callable
|
from collections.abc import Callable, Mapping
|
||||||
from types import MappingProxyType
|
from types import MappingProxyType
|
||||||
from typing import Any
|
from typing import Any, Protocol
|
||||||
|
|
||||||
from homeassistant.const import STATE_UNAVAILABLE, STATE_UNKNOWN
|
from homeassistant.const import STATE_UNAVAILABLE, STATE_UNKNOWN
|
||||||
from homeassistant.core import HomeAssistant, State, callback
|
from homeassistant.core import HomeAssistant, State, callback
|
||||||
|
from homeassistant.util.hass_dict import HassKey
|
||||||
|
|
||||||
from .integration_platform import async_process_integration_platforms
|
from .integration_platform import async_process_integration_platforms
|
||||||
|
|
||||||
PLATFORM = "significant_change"
|
PLATFORM = "significant_change"
|
||||||
DATA_FUNCTIONS = "significant_change"
|
DATA_FUNCTIONS: HassKey[dict[str, CheckTypeFunc]] = HassKey("significant_change")
|
||||||
CheckTypeFunc = Callable[
|
CheckTypeFunc = Callable[
|
||||||
[
|
[
|
||||||
HomeAssistant,
|
HomeAssistant,
|
||||||
@ -65,6 +66,20 @@ ExtraCheckTypeFunc = Callable[
|
|||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
class SignificantChangeProtocol(Protocol):
|
||||||
|
"""Define the format of significant_change platforms."""
|
||||||
|
|
||||||
|
def async_check_significant_change(
|
||||||
|
self,
|
||||||
|
hass: HomeAssistant,
|
||||||
|
old_state: str,
|
||||||
|
old_attrs: Mapping[str, Any],
|
||||||
|
new_state: str,
|
||||||
|
new_attrs: Mapping[str, Any],
|
||||||
|
) -> bool | None:
|
||||||
|
"""Test if state significantly changed."""
|
||||||
|
|
||||||
|
|
||||||
async def create_checker(
|
async def create_checker(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
_domain: str,
|
_domain: str,
|
||||||
@ -85,7 +100,9 @@ async def _initialize(hass: HomeAssistant) -> None:
|
|||||||
|
|
||||||
@callback
|
@callback
|
||||||
def process_platform(
|
def process_platform(
|
||||||
hass: HomeAssistant, component_name: str, platform: Any
|
hass: HomeAssistant,
|
||||||
|
component_name: str,
|
||||||
|
platform: SignificantChangeProtocol,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Process a significant change platform."""
|
"""Process a significant change platform."""
|
||||||
functions[component_name] = platform.async_check_significant_change
|
functions[component_name] = platform.async_check_significant_change
|
||||||
@ -206,7 +223,7 @@ class SignificantlyChangedChecker:
|
|||||||
self.last_approved_entities[new_state.entity_id] = (new_state, extra_arg)
|
self.last_approved_entities[new_state.entity_id] = (new_state, extra_arg)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
functions: dict[str, CheckTypeFunc] | None = self.hass.data.get(DATA_FUNCTIONS)
|
functions = self.hass.data.get(DATA_FUNCTIONS)
|
||||||
|
|
||||||
if functions is None:
|
if functions is None:
|
||||||
raise RuntimeError("Significant Change not initialized")
|
raise RuntimeError("Significant Change not initialized")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user