mirror of
https://github.com/home-assistant/core.git
synced 2025-07-15 01:07:10 +00:00
Improve type hints in demo [3/3] (#77186)
This commit is contained in:
parent
7f883b7ff3
commit
4b2e4c8276
@ -1,4 +1,6 @@
|
|||||||
"""Set up the demo environment that mimics interaction with devices."""
|
"""Set up the demo environment that mimics interaction with devices."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
import datetime
|
import datetime
|
||||||
from random import random
|
from random import random
|
||||||
@ -6,7 +8,7 @@ from random import random
|
|||||||
from homeassistant import config_entries, setup
|
from homeassistant import config_entries, setup
|
||||||
from homeassistant.components import persistent_notification
|
from homeassistant.components import persistent_notification
|
||||||
from homeassistant.components.recorder import get_instance
|
from homeassistant.components.recorder import get_instance
|
||||||
from homeassistant.components.recorder.models import StatisticMetaData
|
from homeassistant.components.recorder.models import StatisticData, StatisticMetaData
|
||||||
from homeassistant.components.recorder.statistics import (
|
from homeassistant.components.recorder.statistics import (
|
||||||
async_add_external_statistics,
|
async_add_external_statistics,
|
||||||
get_last_statistics,
|
get_last_statistics,
|
||||||
@ -16,9 +18,10 @@ from homeassistant.const import (
|
|||||||
ATTR_ENTITY_ID,
|
ATTR_ENTITY_ID,
|
||||||
EVENT_HOMEASSISTANT_START,
|
EVENT_HOMEASSISTANT_START,
|
||||||
SOUND_PRESSURE_DB,
|
SOUND_PRESSURE_DB,
|
||||||
|
Platform,
|
||||||
)
|
)
|
||||||
import homeassistant.core as ha
|
import homeassistant.core as ha
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import Event, HomeAssistant
|
||||||
from homeassistant.helpers.discovery import async_load_platform
|
from homeassistant.helpers.discovery import async_load_platform
|
||||||
from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue
|
from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue
|
||||||
from homeassistant.helpers.typing import ConfigType
|
from homeassistant.helpers.typing import ConfigType
|
||||||
@ -27,36 +30,36 @@ import homeassistant.util.dt as dt_util
|
|||||||
DOMAIN = "demo"
|
DOMAIN = "demo"
|
||||||
|
|
||||||
COMPONENTS_WITH_CONFIG_ENTRY_DEMO_PLATFORM = [
|
COMPONENTS_WITH_CONFIG_ENTRY_DEMO_PLATFORM = [
|
||||||
"air_quality",
|
Platform.AIR_QUALITY,
|
||||||
"alarm_control_panel",
|
Platform.ALARM_CONTROL_PANEL,
|
||||||
"binary_sensor",
|
Platform.BINARY_SENSOR,
|
||||||
"button",
|
Platform.BUTTON,
|
||||||
"camera",
|
Platform.CAMERA,
|
||||||
"climate",
|
Platform.CLIMATE,
|
||||||
"cover",
|
Platform.COVER,
|
||||||
"fan",
|
Platform.FAN,
|
||||||
"humidifier",
|
Platform.HUMIDIFIER,
|
||||||
"light",
|
Platform.LIGHT,
|
||||||
"lock",
|
Platform.LOCK,
|
||||||
"media_player",
|
Platform.MEDIA_PLAYER,
|
||||||
"number",
|
Platform.NUMBER,
|
||||||
"select",
|
Platform.SELECT,
|
||||||
"sensor",
|
Platform.SENSOR,
|
||||||
"siren",
|
Platform.SIREN,
|
||||||
"switch",
|
Platform.SWITCH,
|
||||||
"update",
|
Platform.UPDATE,
|
||||||
"vacuum",
|
Platform.VACUUM,
|
||||||
"water_heater",
|
Platform.WATER_HEATER,
|
||||||
]
|
]
|
||||||
|
|
||||||
COMPONENTS_WITH_DEMO_PLATFORM = [
|
COMPONENTS_WITH_DEMO_PLATFORM = [
|
||||||
"tts",
|
Platform.TTS,
|
||||||
"stt",
|
Platform.STT,
|
||||||
"mailbox",
|
Platform.MAILBOX,
|
||||||
"notify",
|
Platform.NOTIFY,
|
||||||
"image_processing",
|
Platform.IMAGE_PROCESSING,
|
||||||
"calendar",
|
Platform.CALENDAR,
|
||||||
"device_tracker",
|
Platform.DEVICE_TRACKER,
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
@ -173,7 +176,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
|||||||
title="Example Notification",
|
title="Example Notification",
|
||||||
)
|
)
|
||||||
|
|
||||||
async def demo_start_listener(_event):
|
async def demo_start_listener(_event: Event) -> None:
|
||||||
"""Finish set up."""
|
"""Finish set up."""
|
||||||
await finish_setup(hass, config)
|
await finish_setup(hass, config)
|
||||||
|
|
||||||
@ -225,8 +228,10 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
def _generate_mean_statistics(start, end, init_value, max_diff):
|
def _generate_mean_statistics(
|
||||||
statistics = []
|
start: datetime.datetime, end: datetime.datetime, init_value: float, max_diff: float
|
||||||
|
) -> list[StatisticData]:
|
||||||
|
statistics: list[StatisticData] = []
|
||||||
mean = init_value
|
mean = init_value
|
||||||
now = start
|
now = start
|
||||||
while now < end:
|
while now < end:
|
||||||
@ -244,10 +249,16 @@ def _generate_mean_statistics(start, end, init_value, max_diff):
|
|||||||
return statistics
|
return statistics
|
||||||
|
|
||||||
|
|
||||||
async def _insert_sum_statistics(hass, metadata, start, end, max_diff):
|
async def _insert_sum_statistics(
|
||||||
statistics = []
|
hass: HomeAssistant,
|
||||||
|
metadata: StatisticMetaData,
|
||||||
|
start: datetime.datetime,
|
||||||
|
end: datetime.datetime,
|
||||||
|
max_diff: float,
|
||||||
|
):
|
||||||
|
statistics: list[StatisticData] = []
|
||||||
now = start
|
now = start
|
||||||
sum_ = 0
|
sum_ = 0.0
|
||||||
statistic_id = metadata["statistic_id"]
|
statistic_id = metadata["statistic_id"]
|
||||||
|
|
||||||
last_stats = await get_instance(hass).async_add_executor_job(
|
last_stats = await get_instance(hass).async_add_executor_job(
|
||||||
@ -349,10 +360,10 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
async def finish_setup(hass, config):
|
async def finish_setup(hass: HomeAssistant, config: ConfigType) -> None:
|
||||||
"""Finish set up once demo platforms are set up."""
|
"""Finish set up once demo platforms are set up."""
|
||||||
switches = None
|
switches: list[str] | None = None
|
||||||
lights = None
|
lights: list[str] | None = None
|
||||||
|
|
||||||
while not switches and not lights:
|
while not switches and not lights:
|
||||||
# Not all platforms might be loaded.
|
# Not all platforms might be loaded.
|
||||||
@ -361,6 +372,8 @@ async def finish_setup(hass, config):
|
|||||||
switches = sorted(hass.states.async_entity_ids("switch"))
|
switches = sorted(hass.states.async_entity_ids("switch"))
|
||||||
lights = sorted(hass.states.async_entity_ids("light"))
|
lights = sorted(hass.states.async_entity_ids("light"))
|
||||||
|
|
||||||
|
assert switches is not None
|
||||||
|
assert lights is not None
|
||||||
# Set up scripts
|
# Set up scripts
|
||||||
await setup.async_setup_component(
|
await setup.async_setup_component(
|
||||||
hass,
|
hass,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user