From 8cc0df7b7205ab4ac8eebeca704cece73f0ead85 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Tue, 28 Dec 2021 14:33:08 +0100 Subject: [PATCH] Ensure service calls are typed [k-n] (#62917) * Ensure service calls are typed [k-n] * Ensure service calls are typed in mazda * Adjust mazda * Add mazda comment Co-authored-by: epenet --- homeassistant/components/keba/__init__.py | 3 ++- homeassistant/components/lifx/light.py | 4 ++-- homeassistant/components/logbook/__init__.py | 9 +++++++-- homeassistant/components/logger/__init__.py | 4 ++-- homeassistant/components/logi_circle/__init__.py | 3 ++- homeassistant/components/mazda/__init__.py | 10 ++++++++-- homeassistant/components/microsoft_face/__init__.py | 13 +++++++------ homeassistant/components/mill/climate.py | 4 ++-- homeassistant/components/monoprice/media_player.py | 2 +- homeassistant/components/mqtt/__init__.py | 4 ++-- homeassistant/components/ness_alarm/__init__.py | 5 +++-- homeassistant/components/netatmo/__init__.py | 6 +++--- homeassistant/components/netgear_lte/__init__.py | 4 ++-- homeassistant/components/nzbget/__init__.py | 8 ++++---- 14 files changed, 47 insertions(+), 32 deletions(-) diff --git a/homeassistant/components/keba/__init__.py b/homeassistant/components/keba/__init__.py index e1cf9bfd3ea..6d80019ffd7 100644 --- a/homeassistant/components/keba/__init__.py +++ b/homeassistant/components/keba/__init__.py @@ -6,6 +6,7 @@ from keba_kecontact.connection import KebaKeContact import voluptuous as vol from homeassistant.const import CONF_HOST +from homeassistant.core import ServiceCall from homeassistant.helpers import discovery import homeassistant.helpers.config_validation as cv @@ -77,7 +78,7 @@ async def async_setup(hass, config): _LOGGER.warning("Could not set failsafe mode %s", ex) # Register services to hass - async def execute_service(call): + async def execute_service(call: ServiceCall) -> None: """Execute a service to KEBA charging station. This must be a member function as we need access to the keba diff --git a/homeassistant/components/lifx/light.py b/homeassistant/components/lifx/light.py index 998b99ef88f..23069f45e5d 100644 --- a/homeassistant/components/lifx/light.py +++ b/homeassistant/components/lifx/light.py @@ -42,7 +42,7 @@ from homeassistant.const import ( ATTR_SW_VERSION, EVENT_HOMEASSISTANT_STOP, ) -from homeassistant.core import callback +from homeassistant.core import ServiceCall, callback from homeassistant.helpers import entity_platform import homeassistant.helpers.config_validation as cv import homeassistant.helpers.device_registry as dr @@ -298,7 +298,7 @@ class LIFXManager: def register_effects(self): """Register the LIFX effects as hass service calls.""" - async def service_handler(service): + async def service_handler(service: ServiceCall) -> None: """Apply a service, i.e. start an effect.""" entities = await self.platform.async_extract_from_service(service) if entities: diff --git a/homeassistant/components/logbook/__init__.py b/homeassistant/components/logbook/__init__.py index 43cdbec2530..6b9c9e6ff26 100644 --- a/homeassistant/components/logbook/__init__.py +++ b/homeassistant/components/logbook/__init__.py @@ -34,7 +34,12 @@ from homeassistant.const import ( EVENT_LOGBOOK_ENTRY, EVENT_STATE_CHANGED, ) -from homeassistant.core import DOMAIN as HA_DOMAIN, callback, split_entity_id +from homeassistant.core import ( + DOMAIN as HA_DOMAIN, + ServiceCall, + callback, + split_entity_id, +) from homeassistant.exceptions import InvalidEntityFormatError import homeassistant.helpers.config_validation as cv from homeassistant.helpers.entityfilter import ( @@ -130,7 +135,7 @@ async def async_setup(hass, config): hass.data[DOMAIN] = {} @callback - def log_message(service): + def log_message(service: ServiceCall) -> None: """Handle sending notification message service calls.""" message = service.data[ATTR_MESSAGE] name = service.data[ATTR_NAME] diff --git a/homeassistant/components/logger/__init__.py b/homeassistant/components/logger/__init__.py index c7660f2a3f0..ac71ee2944b 100644 --- a/homeassistant/components/logger/__init__.py +++ b/homeassistant/components/logger/__init__.py @@ -3,7 +3,7 @@ import logging import voluptuous as vol -from homeassistant.core import callback +from homeassistant.core import ServiceCall, callback import homeassistant.helpers.config_validation as cv DOMAIN = "logger" @@ -78,7 +78,7 @@ async def async_setup(hass, config): _add_log_filter(logger, value) @callback - def async_service_handler(service): + def async_service_handler(service: ServiceCall) -> None: """Handle logger services.""" if service.service == SERVICE_SET_DEFAULT_LEVEL: set_default_log_level(service.data.get(ATTR_LEVEL)) diff --git a/homeassistant/components/logi_circle/__init__.py b/homeassistant/components/logi_circle/__init__.py index 01034a3a5ca..6d294636ede 100644 --- a/homeassistant/components/logi_circle/__init__.py +++ b/homeassistant/components/logi_circle/__init__.py @@ -20,6 +20,7 @@ from homeassistant.const import ( EVENT_HOMEASSISTANT_STOP, Platform, ) +from homeassistant.core import ServiceCall from homeassistant.helpers import config_validation as cv from homeassistant.helpers.dispatcher import async_dispatcher_send @@ -184,7 +185,7 @@ async def async_setup_entry(hass, entry): hass.config_entries.async_setup_platforms(entry, PLATFORMS) - async def service_handler(service): + async def service_handler(service: ServiceCall) -> None: """Dispatch service calls to target entities.""" params = dict(service.data) diff --git a/homeassistant/components/mazda/__init__.py b/homeassistant/components/mazda/__init__.py index 5650e07daca..32eaeaafac0 100644 --- a/homeassistant/components/mazda/__init__.py +++ b/homeassistant/components/mazda/__init__.py @@ -1,6 +1,9 @@ """The Mazda Connected Services integration.""" +from __future__ import annotations + from datetime import timedelta import logging +from typing import TYPE_CHECKING import async_timeout from pymazda import ( @@ -15,7 +18,7 @@ import voluptuous as vol from homeassistant.config_entries import ConfigEntry from homeassistant.const import CONF_EMAIL, CONF_PASSWORD, CONF_REGION, Platform -from homeassistant.core import HomeAssistant +from homeassistant.core import HomeAssistant, ServiceCall from homeassistant.exceptions import ( ConfigEntryAuthFailed, ConfigEntryNotReady, @@ -67,12 +70,15 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: _LOGGER.error("Error occurred during Mazda login request: %s", ex) raise ConfigEntryNotReady from ex - async def async_handle_service_call(service_call=None): + async def async_handle_service_call(service_call: ServiceCall) -> None: """Handle a service call.""" # Get device entry from device registry dev_reg = device_registry.async_get(hass) device_id = service_call.data["device_id"] device_entry = dev_reg.async_get(device_id) + if TYPE_CHECKING: + # For mypy: it has already been checked in validate_mazda_device_id + assert device_entry # Get vehicle VIN from device identifiers mazda_identifiers = ( diff --git a/homeassistant/components/microsoft_face/__init__.py b/homeassistant/components/microsoft_face/__init__.py index d0f08427b51..1604c77614f 100644 --- a/homeassistant/components/microsoft_face/__init__.py +++ b/homeassistant/components/microsoft_face/__init__.py @@ -9,6 +9,7 @@ import async_timeout import voluptuous as vol from homeassistant.const import ATTR_NAME, CONF_API_KEY, CONF_TIMEOUT, CONTENT_TYPE_JSON +from homeassistant.core import ServiceCall from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers.aiohttp_client import async_get_clientsession import homeassistant.helpers.config_validation as cv @@ -86,7 +87,7 @@ async def async_setup(hass, config): hass.data[DATA_MICROSOFT_FACE] = face - async def async_create_group(service): + async def async_create_group(service: ServiceCall) -> None: """Create a new person group.""" name = service.data[ATTR_NAME] g_id = slugify(name) @@ -104,7 +105,7 @@ async def async_setup(hass, config): DOMAIN, SERVICE_CREATE_GROUP, async_create_group, schema=SCHEMA_GROUP_SERVICE ) - async def async_delete_group(service): + async def async_delete_group(service: ServiceCall) -> None: """Delete a person group.""" g_id = slugify(service.data[ATTR_NAME]) @@ -121,7 +122,7 @@ async def async_setup(hass, config): DOMAIN, SERVICE_DELETE_GROUP, async_delete_group, schema=SCHEMA_GROUP_SERVICE ) - async def async_train_group(service): + async def async_train_group(service: ServiceCall) -> None: """Train a person group.""" g_id = service.data[ATTR_GROUP] @@ -134,7 +135,7 @@ async def async_setup(hass, config): DOMAIN, SERVICE_TRAIN_GROUP, async_train_group, schema=SCHEMA_TRAIN_SERVICE ) - async def async_create_person(service): + async def async_create_person(service: ServiceCall) -> None: """Create a person in a group.""" name = service.data[ATTR_NAME] g_id = service.data[ATTR_GROUP] @@ -153,7 +154,7 @@ async def async_setup(hass, config): DOMAIN, SERVICE_CREATE_PERSON, async_create_person, schema=SCHEMA_PERSON_SERVICE ) - async def async_delete_person(service): + async def async_delete_person(service: ServiceCall) -> None: """Delete a person in a group.""" name = service.data[ATTR_NAME] g_id = service.data[ATTR_GROUP] @@ -171,7 +172,7 @@ async def async_setup(hass, config): DOMAIN, SERVICE_DELETE_PERSON, async_delete_person, schema=SCHEMA_PERSON_SERVICE ) - async def async_face_person(service): + async def async_face_person(service: ServiceCall) -> None: """Add a new face picture to a person.""" g_id = service.data[ATTR_GROUP] p_id = face.store[g_id].get(service.data[ATTR_PERSON]) diff --git a/homeassistant/components/mill/climate.py b/homeassistant/components/mill/climate.py index 91ba10618f0..03ec18a50b3 100644 --- a/homeassistant/components/mill/climate.py +++ b/homeassistant/components/mill/climate.py @@ -19,7 +19,7 @@ from homeassistant.const import ( PRECISION_WHOLE, TEMP_CELSIUS, ) -from homeassistant.core import callback +from homeassistant.core import ServiceCall, callback from homeassistant.helpers import config_validation as cv from homeassistant.helpers.entity import DeviceInfo from homeassistant.helpers.update_coordinator import CoordinatorEntity @@ -65,7 +65,7 @@ async def async_setup_entry(hass, entry, async_add_entities): ] async_add_entities(entities) - async def set_room_temp(service): + async def set_room_temp(service: ServiceCall) -> None: """Set room temp.""" room_name = service.data.get(ATTR_ROOM_NAME) sleep_temp = service.data.get(ATTR_SLEEP_TEMP) diff --git a/homeassistant/components/monoprice/media_player.py b/homeassistant/components/monoprice/media_player.py index 68bad42f78e..01c49fc5f8d 100644 --- a/homeassistant/components/monoprice/media_player.py +++ b/homeassistant/components/monoprice/media_player.py @@ -93,7 +93,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities): entity.restore() @service.verify_domain_control(hass, DOMAIN) - async def async_service_handle(service_call): + async def async_service_handle(service_call: core.ServiceCall) -> None: """Handle for services.""" entities = await platform.async_extract_from_service(service_call) diff --git a/homeassistant/components/mqtt/__init__.py b/homeassistant/components/mqtt/__init__.py index fb9b4707ac8..f909fd3745f 100644 --- a/homeassistant/components/mqtt/__init__.py +++ b/homeassistant/components/mqtt/__init__.py @@ -541,7 +541,7 @@ async def async_setup_entry(hass, entry): hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, async_stop_mqtt) - async def async_publish_service(call: ServiceCall): + async def async_publish_service(call: ServiceCall) -> None: """Handle MQTT publish service calls.""" msg_topic = call.data.get(ATTR_TOPIC) msg_topic_template = call.data.get(ATTR_TOPIC_TEMPLATE) @@ -594,7 +594,7 @@ async def async_setup_entry(hass, entry): DOMAIN, SERVICE_PUBLISH, async_publish_service, schema=MQTT_PUBLISH_SCHEMA ) - async def async_dump_service(call: ServiceCall): + async def async_dump_service(call: ServiceCall) -> None: """Handle MQTT dump service calls.""" messages = [] diff --git a/homeassistant/components/ness_alarm/__init__.py b/homeassistant/components/ness_alarm/__init__.py index e15e0fb2e20..1b72d129f18 100644 --- a/homeassistant/components/ness_alarm/__init__.py +++ b/homeassistant/components/ness_alarm/__init__.py @@ -16,6 +16,7 @@ from homeassistant.const import ( CONF_SCAN_INTERVAL, EVENT_HOMEASSISTANT_STOP, ) +from homeassistant.core import ServiceCall from homeassistant.helpers import config_validation as cv from homeassistant.helpers.discovery import async_load_platform from homeassistant.helpers.dispatcher import async_dispatcher_send @@ -132,10 +133,10 @@ async def async_setup(hass, config): hass.loop.create_task(client.keepalive()) hass.loop.create_task(client.update()) - async def handle_panic(call): + async def handle_panic(call: ServiceCall) -> None: await client.panic(call.data[ATTR_CODE]) - async def handle_aux(call): + async def handle_aux(call: ServiceCall) -> None: await client.aux(call.data[ATTR_OUTPUT_ID], call.data[ATTR_STATE]) hass.services.async_register( diff --git a/homeassistant/components/netatmo/__init__.py b/homeassistant/components/netatmo/__init__.py index e6ea2819eb4..1e1cde039f0 100644 --- a/homeassistant/components/netatmo/__init__.py +++ b/homeassistant/components/netatmo/__init__.py @@ -22,7 +22,7 @@ from homeassistant.const import ( EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP, ) -from homeassistant.core import CoreState, HomeAssistant +from homeassistant.core import CoreState, HomeAssistant, ServiceCall from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady from homeassistant.helpers import ( aiohttp_client, @@ -146,7 +146,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: hass.config_entries.async_setup_platforms(entry, PLATFORMS) - async def unregister_webhook(_: None) -> None: + async def unregister_webhook(call: ServiceCall | None) -> None: if CONF_WEBHOOK_ID not in entry.data: return _LOGGER.debug("Unregister Netatmo webhook (%s)", entry.data[CONF_WEBHOOK_ID]) @@ -163,7 +163,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: "No webhook to be dropped for %s", entry.data[CONF_WEBHOOK_ID] ) - async def register_webhook(_: None) -> None: + async def register_webhook(call: ServiceCall | None) -> None: if CONF_WEBHOOK_ID not in entry.data: data = {**entry.data, CONF_WEBHOOK_ID: secrets.token_hex()} hass.config_entries.async_update_entry(entry, data=data) diff --git a/homeassistant/components/netgear_lte/__init__.py b/homeassistant/components/netgear_lte/__init__.py index 3ee4e14d5e1..dbcee63bb45 100644 --- a/homeassistant/components/netgear_lte/__init__.py +++ b/homeassistant/components/netgear_lte/__init__.py @@ -19,7 +19,7 @@ from homeassistant.const import ( CONF_RECIPIENT, EVENT_HOMEASSISTANT_STOP, ) -from homeassistant.core import callback +from homeassistant.core import ServiceCall, callback from homeassistant.helpers import config_validation as cv, discovery from homeassistant.helpers.aiohttp_client import async_create_clientsession from homeassistant.helpers.dispatcher import ( @@ -179,7 +179,7 @@ async def async_setup(hass, config): ) hass.data[DATA_KEY] = LTEData(websession) - async def service_handler(service): + async def service_handler(service: ServiceCall) -> None: """Apply a service.""" host = service.data.get(ATTR_HOST) conf = {CONF_HOST: host} diff --git a/homeassistant/components/nzbget/__init__.py b/homeassistant/components/nzbget/__init__.py index 40ad666aaf4..d9a41c90535 100644 --- a/homeassistant/components/nzbget/__init__.py +++ b/homeassistant/components/nzbget/__init__.py @@ -12,7 +12,7 @@ from homeassistant.const import ( CONF_USERNAME, Platform, ) -from homeassistant.core import HomeAssistant +from homeassistant.core import HomeAssistant, ServiceCall from homeassistant.helpers import config_validation as cv from homeassistant.helpers.typing import ConfigType from homeassistant.helpers.update_coordinator import CoordinatorEntity @@ -130,15 +130,15 @@ def _async_register_services( ) -> None: """Register integration-level services.""" - def pause(call) -> None: + def pause(call: ServiceCall) -> None: """Service call to pause downloads in NZBGet.""" coordinator.nzbget.pausedownload() - def resume(call) -> None: + def resume(call: ServiceCall) -> None: """Service call to resume downloads in NZBGet.""" coordinator.nzbget.resumedownload() - def set_speed(call) -> None: + def set_speed(call: ServiceCall) -> None: """Service call to rate limit speeds in NZBGet.""" coordinator.nzbget.rate(call.data[ATTR_SPEED])