From ad7a0d799d221fe4515923c4821910b68b64cb2f Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Mon, 3 Jan 2022 09:10:43 +0100 Subject: [PATCH] Add type hints to google_assistant (#62748) * Add type hints to google_assistant * Fix pylint * Adjust type hint * Fix black * Revert changes to smart_home Co-authored-by: epenet --- .../components/google_assistant/helpers.py | 14 ++++++++++---- homeassistant/components/google_assistant/http.py | 8 +++++++- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/google_assistant/helpers.py b/homeassistant/components/google_assistant/helpers.py index 238ee8d9576..5c022684fc7 100644 --- a/homeassistant/components/google_assistant/helpers.py +++ b/homeassistant/components/google_assistant/helpers.py @@ -46,7 +46,7 @@ _LOGGER = logging.getLogger(__name__) async def _get_entity_and_device( - hass, entity_id + hass: HomeAssistant, entity_id: str ) -> tuple[RegistryEntry, DeviceEntry] | None: """Fetch the entity and device entries for a entity_id.""" dev_reg, ent_reg = await gather( @@ -60,7 +60,11 @@ async def _get_entity_and_device( return entity_entry, device_entry -async def _get_area(hass, entity_entry, device_entry) -> AreaEntry | None: +async def _get_area( + hass: HomeAssistant, + entity_entry: RegistryEntry | None, + device_entry: DeviceEntry | None, +) -> AreaEntry | None: """Calculate the area for an entity.""" if entity_entry and entity_entry.area_id: area_id = entity_entry.area_id @@ -73,7 +77,7 @@ async def _get_area(hass, entity_entry, device_entry) -> AreaEntry | None: return area_reg.areas.get(area_id) -async def _get_device_info(device_entry) -> dict[str, str] | None: +async def _get_device_info(device_entry: DeviceEntry | None) -> dict[str, str] | None: """Retrieve the device info for a device.""" if not device_entry: return None @@ -593,7 +597,9 @@ def deep_update(target, source): @callback -def async_get_entities(hass, config) -> list[GoogleEntity]: +def async_get_entities( + hass: HomeAssistant, config: AbstractConfig +) -> list[GoogleEntity]: """Return all entities that are supported by Google.""" entities = [] for state in hass.states.async_all(): diff --git a/homeassistant/components/google_assistant/http.py b/homeassistant/components/google_assistant/http.py index e7a73351f60..990addecaeb 100644 --- a/homeassistant/components/google_assistant/http.py +++ b/homeassistant/components/google_assistant/http.py @@ -1,8 +1,11 @@ """Support for Google Actions Smart Home Control.""" +from __future__ import annotations + import asyncio from datetime import timedelta from http import HTTPStatus import logging +from typing import Any from uuid import uuid4 from aiohttp import ClientError, ClientResponseError @@ -12,6 +15,7 @@ import jwt # Typing imports from homeassistant.components.http import HomeAssistantView from homeassistant.const import CLOUD_NEVER_EXPOSED_ENTITIES, ENTITY_CATEGORIES +from homeassistant.core import HomeAssistant from homeassistant.helpers import entity_registry as er from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.util import dt as dt_util @@ -52,7 +56,9 @@ def _get_homegraph_jwt(time, iss, key): return jwt.encode(jwt_raw, key, algorithm="RS256") -async def _get_homegraph_token(hass, jwt_signed): +async def _get_homegraph_token( + hass: HomeAssistant, jwt_signed: str +) -> dict[str, Any] | list[str, Any] | Any: headers = { "Authorization": f"Bearer {jwt_signed}", "Content-Type": "application/x-www-form-urlencoded",