Fully type Tag component (#52540)

pull/52581/head
Yuval Aboulafia 2021-07-05 12:54:00 +03:00 committed by GitHub
parent 1cb298948f
commit 323088ff63
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 29 additions and 7 deletions

View File

@ -78,6 +78,7 @@ homeassistant.components.sun.*
homeassistant.components.switch.* homeassistant.components.switch.*
homeassistant.components.synology_dsm.* homeassistant.components.synology_dsm.*
homeassistant.components.systemmonitor.* homeassistant.components.systemmonitor.*
homeassistant.components.tag.*
homeassistant.components.tcp.* homeassistant.components.tcp.*
homeassistant.components.tts.* homeassistant.components.tts.*
homeassistant.components.upcloud.* homeassistant.components.upcloud.*

View File

@ -7,11 +7,12 @@ import uuid
import voluptuous as vol import voluptuous as vol
from homeassistant.const import CONF_NAME from homeassistant.const import CONF_NAME
from homeassistant.core import HomeAssistant, callback from homeassistant.core import Context, HomeAssistant, callback
from homeassistant.exceptions import HomeAssistantError from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import collection from homeassistant.helpers import collection
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.storage import Store from homeassistant.helpers.storage import Store
from homeassistant.helpers.typing import ConfigType
from homeassistant.loader import bind_hass from homeassistant.loader import bind_hass
import homeassistant.util.dt as dt_util import homeassistant.util.dt as dt_util
@ -75,7 +76,7 @@ class TagStorageCollection(collection.StorageCollection):
return data return data
@callback @callback
def _get_suggested_id(self, info: dict) -> str: def _get_suggested_id(self, info: dict[str, str]) -> str:
"""Suggest an ID based on the config.""" """Suggest an ID based on the config."""
return info[TAG_ID] return info[TAG_ID]
@ -88,7 +89,7 @@ class TagStorageCollection(collection.StorageCollection):
return data return data
async def async_setup(hass: HomeAssistant, config: dict): async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Set up the Tag component.""" """Set up the Tag component."""
hass.data[DOMAIN] = {} hass.data[DOMAIN] = {}
id_manager = TagIDManager() id_manager = TagIDManager()
@ -106,7 +107,9 @@ async def async_setup(hass: HomeAssistant, config: dict):
@bind_hass @bind_hass
async def async_scan_tag(hass, tag_id, device_id, context=None): async def async_scan_tag(
hass: HomeAssistant, tag_id: str, device_id: str, context: Context | None = None
) -> None:
"""Handle when a tag is scanned.""" """Handle when a tag is scanned."""
if DOMAIN not in hass.config.components: if DOMAIN not in hass.config.components:
raise HomeAssistantError("tag component has not been set up.") raise HomeAssistantError("tag component has not been set up.")

View File

@ -1,9 +1,11 @@
"""Support for tag triggers.""" """Support for tag triggers."""
import voluptuous as vol import voluptuous as vol
from homeassistant.components.automation import AutomationActionType
from homeassistant.const import CONF_PLATFORM from homeassistant.const import CONF_PLATFORM
from homeassistant.core import HassJob from homeassistant.core import CALLBACK_TYPE, Event, HassJob, HomeAssistant
from homeassistant.helpers import config_validation as cv from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.typing import ConfigType
from .const import DEVICE_ID, DOMAIN, EVENT_TAG_SCANNED, TAG_ID from .const import DEVICE_ID, DOMAIN, EVENT_TAG_SCANNED, TAG_ID
@ -16,7 +18,12 @@ TRIGGER_SCHEMA = cv.TRIGGER_BASE_SCHEMA.extend(
) )
async def async_attach_trigger(hass, config, action, automation_info): async def async_attach_trigger(
hass: HomeAssistant,
config: ConfigType,
action: AutomationActionType,
automation_info: dict,
) -> CALLBACK_TYPE:
"""Listen for tag_scanned events based on configuration.""" """Listen for tag_scanned events based on configuration."""
trigger_data = automation_info.get("trigger_data", {}) if automation_info else {} trigger_data = automation_info.get("trigger_data", {}) if automation_info else {}
tag_ids = set(config[TAG_ID]) tag_ids = set(config[TAG_ID])
@ -24,7 +31,7 @@ async def async_attach_trigger(hass, config, action, automation_info):
job = HassJob(action) job = HassJob(action)
async def handle_event(event): async def handle_event(event: Event) -> None:
"""Listen for tag scan events and calls the action when data matches.""" """Listen for tag scan events and calls the action when data matches."""
if event.data.get(TAG_ID) not in tag_ids or ( if event.data.get(TAG_ID) not in tag_ids or (
device_ids is not None and event.data.get(DEVICE_ID) not in device_ids device_ids is not None and event.data.get(DEVICE_ID) not in device_ids

View File

@ -869,6 +869,17 @@ no_implicit_optional = true
warn_return_any = true warn_return_any = true
warn_unreachable = true warn_unreachable = true
[mypy-homeassistant.components.tag.*]
check_untyped_defs = true
disallow_incomplete_defs = true
disallow_subclassing_any = true
disallow_untyped_calls = true
disallow_untyped_decorators = true
disallow_untyped_defs = true
no_implicit_optional = true
warn_return_any = true
warn_unreachable = true
[mypy-homeassistant.components.tcp.*] [mypy-homeassistant.components.tcp.*]
check_untyped_defs = true check_untyped_defs = true
disallow_incomplete_defs = true disallow_incomplete_defs = true