Enable strict typing for intent (#107282)

This commit is contained in:
Marc Mueller 2024-01-08 10:09:48 +01:00 committed by GitHub
parent 5ef04fcc7b
commit 3c7a9272fa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 32 additions and 8 deletions

View File

@ -228,6 +228,7 @@ homeassistant.components.input_button.*
homeassistant.components.input_select.* homeassistant.components.input_select.*
homeassistant.components.input_text.* homeassistant.components.input_text.*
homeassistant.components.integration.* homeassistant.components.integration.*
homeassistant.components.intent.*
homeassistant.components.ipp.* homeassistant.components.ipp.*
homeassistant.components.iqvia.* homeassistant.components.iqvia.*
homeassistant.components.islamic_prayer_times.* homeassistant.components.islamic_prayer_times.*

View File

@ -1,6 +1,10 @@
"""The Intent integration.""" """The Intent integration."""
import logging from __future__ import annotations
import logging
from typing import Any, Protocol
from aiohttp import web
import voluptuous as vol import voluptuous as vol
from homeassistant.components import http from homeassistant.components import http
@ -69,6 +73,13 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
return True 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): class OnOffIntentHandler(intent.ServiceIntentHandler):
"""Intent handler for on/off that handles covers too.""" """Intent handler for on/off that handles covers too."""
@ -249,7 +260,9 @@ class NevermindIntentHandler(intent.IntentHandler):
return intent_obj.create_response() 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.""" """Process the intents of an integration."""
await platform.async_setup_intents(hass) 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.""" """Handle intent with name/data."""
hass = request.app["hass"] hass: HomeAssistant = request.app["hass"]
language = hass.config.language language = hass.config.language
try: try:
@ -286,7 +299,7 @@ class IntentHandleView(http.HomeAssistantView):
intent_result.async_set_speech(str(err)) intent_result.async_set_speech(str(err))
if intent_result is None: 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") intent_result.async_set_speech("Sorry, I couldn't handle that")
return self.json(intent_result) return self.json(intent_result)

View File

@ -2,7 +2,7 @@
from __future__ import annotations from __future__ import annotations
import asyncio import asyncio
from collections.abc import Collection, Iterable from collections.abc import Collection, Coroutine, Iterable
import dataclasses import dataclasses
from dataclasses import dataclass from dataclasses import dataclass
from enum import Enum from enum import Enum
@ -451,7 +451,7 @@ class ServiceIntentHandler(IntentHandler):
else: else:
speech_name = states[0].name speech_name = states[0].name
service_coros = [] service_coros: list[Coroutine[Any, Any, None]] = []
for state in states: for state in states:
service_coros.append(self.async_call_service(intent_obj, state)) 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. """Run task with timeout to (hopefully) catch validation errors.
After the timeout the task will continue to run in the background. After the timeout the task will continue to run in the background.

View File

@ -2041,6 +2041,16 @@ disallow_untyped_defs = true
warn_return_any = true warn_return_any = true
warn_unreachable = 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.*] [mypy-homeassistant.components.ipp.*]
check_untyped_defs = true check_untyped_defs = true
disallow_incomplete_defs = true disallow_incomplete_defs = true