mirror of
https://github.com/home-assistant/core.git
synced 2025-06-25 01:21:51 +02:00
Enable strict typing for intent (#107282)
This commit is contained in:
@ -228,6 +228,7 @@ homeassistant.components.input_button.*
|
||||
homeassistant.components.input_select.*
|
||||
homeassistant.components.input_text.*
|
||||
homeassistant.components.integration.*
|
||||
homeassistant.components.intent.*
|
||||
homeassistant.components.ipp.*
|
||||
homeassistant.components.iqvia.*
|
||||
homeassistant.components.islamic_prayer_times.*
|
||||
|
@ -1,6 +1,10 @@
|
||||
"""The Intent integration."""
|
||||
import logging
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from typing import Any, Protocol
|
||||
|
||||
from aiohttp import web
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components import http
|
||||
@ -69,6 +73,13 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
||||
return True
|
||||
|
||||
|
||||
class IntentPlatformProtocol(Protocol):
|
||||
"""Define the format that intent platforms can have."""
|
||||
|
||||
async def async_setup_intents(self, hass: HomeAssistant) -> None:
|
||||
"""Set up platform intents."""
|
||||
|
||||
|
||||
class OnOffIntentHandler(intent.ServiceIntentHandler):
|
||||
"""Intent handler for on/off that handles covers too."""
|
||||
|
||||
@ -249,7 +260,9 @@ class NevermindIntentHandler(intent.IntentHandler):
|
||||
return intent_obj.create_response()
|
||||
|
||||
|
||||
async def _async_process_intent(hass: HomeAssistant, domain: str, platform):
|
||||
async def _async_process_intent(
|
||||
hass: HomeAssistant, domain: str, platform: IntentPlatformProtocol
|
||||
) -> None:
|
||||
"""Process the intents of an integration."""
|
||||
await platform.async_setup_intents(hass)
|
||||
|
||||
@ -268,9 +281,9 @@ class IntentHandleView(http.HomeAssistantView):
|
||||
}
|
||||
)
|
||||
)
|
||||
async def post(self, request, data):
|
||||
async def post(self, request: web.Request, data: dict[str, Any]) -> web.Response:
|
||||
"""Handle intent with name/data."""
|
||||
hass = request.app["hass"]
|
||||
hass: HomeAssistant = request.app["hass"]
|
||||
language = hass.config.language
|
||||
|
||||
try:
|
||||
@ -286,7 +299,7 @@ class IntentHandleView(http.HomeAssistantView):
|
||||
intent_result.async_set_speech(str(err))
|
||||
|
||||
if intent_result is None:
|
||||
intent_result = intent.IntentResponse(language=language)
|
||||
intent_result = intent.IntentResponse(language=language) # type: ignore[unreachable]
|
||||
intent_result.async_set_speech("Sorry, I couldn't handle that")
|
||||
|
||||
return self.json(intent_result)
|
||||
|
@ -2,7 +2,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
from collections.abc import Collection, Iterable
|
||||
from collections.abc import Collection, Coroutine, Iterable
|
||||
import dataclasses
|
||||
from dataclasses import dataclass
|
||||
from enum import Enum
|
||||
@ -451,7 +451,7 @@ class ServiceIntentHandler(IntentHandler):
|
||||
else:
|
||||
speech_name = states[0].name
|
||||
|
||||
service_coros = []
|
||||
service_coros: list[Coroutine[Any, Any, None]] = []
|
||||
for state in states:
|
||||
service_coros.append(self.async_call_service(intent_obj, state))
|
||||
|
||||
@ -507,7 +507,7 @@ class ServiceIntentHandler(IntentHandler):
|
||||
)
|
||||
)
|
||||
|
||||
async def _run_then_background(self, task: asyncio.Task) -> None:
|
||||
async def _run_then_background(self, task: asyncio.Task[Any]) -> None:
|
||||
"""Run task with timeout to (hopefully) catch validation errors.
|
||||
|
||||
After the timeout the task will continue to run in the background.
|
||||
|
10
mypy.ini
10
mypy.ini
@ -2041,6 +2041,16 @@ disallow_untyped_defs = true
|
||||
warn_return_any = true
|
||||
warn_unreachable = true
|
||||
|
||||
[mypy-homeassistant.components.intent.*]
|
||||
check_untyped_defs = true
|
||||
disallow_incomplete_defs = true
|
||||
disallow_subclassing_any = true
|
||||
disallow_untyped_calls = true
|
||||
disallow_untyped_decorators = true
|
||||
disallow_untyped_defs = true
|
||||
warn_return_any = true
|
||||
warn_unreachable = true
|
||||
|
||||
[mypy-homeassistant.components.ipp.*]
|
||||
check_untyped_defs = true
|
||||
disallow_incomplete_defs = true
|
||||
|
Reference in New Issue
Block a user