From 83a10cca539e9c03ac4f8658473831496c58c3e2 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Wed, 9 Feb 2022 19:09:55 +0100 Subject: [PATCH] Enable basic type checking for config (#66197) --- homeassistant/components/config/auth.py | 4 +- .../components/config/config_entries.py | 6 +-- homeassistant/components/config/core.py | 4 +- .../components/config/entity_registry.py | 47 +++++++++---------- mypy.ini | 12 ----- script/hassfest/mypy_config.py | 4 -- 6 files changed, 28 insertions(+), 49 deletions(-) diff --git a/homeassistant/components/config/auth.py b/homeassistant/components/config/auth.py index d2f630a8b6d..15fc6634f5b 100644 --- a/homeassistant/components/config/auth.py +++ b/homeassistant/components/config/auth.py @@ -60,7 +60,6 @@ async def websocket_delete(hass, connection, msg): @websocket_api.require_admin -@websocket_api.async_response @websocket_api.websocket_command( { vol.Required("type"): "config/auth/create", @@ -69,6 +68,7 @@ async def websocket_delete(hass, connection, msg): vol.Optional("local_only"): bool, } ) +@websocket_api.async_response async def websocket_create(hass, connection, msg): """Create a user.""" user = await hass.auth.async_create_user( @@ -81,7 +81,6 @@ async def websocket_create(hass, connection, msg): @websocket_api.require_admin -@websocket_api.async_response @websocket_api.websocket_command( { vol.Required("type"): "config/auth/update", @@ -92,6 +91,7 @@ async def websocket_create(hass, connection, msg): vol.Optional("local_only"): bool, } ) +@websocket_api.async_response async def websocket_update(hass, connection, msg): """Update a user.""" if not (user := await hass.auth.async_get_user(msg.pop("user_id"))): diff --git a/homeassistant/components/config/config_entries.py b/homeassistant/components/config/config_entries.py index 887c0517d05..2cf7005cb66 100644 --- a/homeassistant/components/config/config_entries.py +++ b/homeassistant/components/config/config_entries.py @@ -262,7 +262,6 @@ def get_entry( @websocket_api.require_admin -@websocket_api.async_response @websocket_api.websocket_command( { "type": "config_entries/update", @@ -272,6 +271,7 @@ def get_entry( vol.Optional("pref_disable_polling"): bool, } ) +@websocket_api.async_response async def config_entry_update(hass, connection, msg): """Update config entry.""" changes = dict(msg) @@ -305,7 +305,6 @@ async def config_entry_update(hass, connection, msg): @websocket_api.require_admin -@websocket_api.async_response @websocket_api.websocket_command( { "type": "config_entries/disable", @@ -315,6 +314,7 @@ async def config_entry_update(hass, connection, msg): "disabled_by": vol.Any(config_entries.ConfigEntryDisabler.USER.value, None), } ) +@websocket_api.async_response async def config_entry_disable(hass, connection, msg): """Disable config entry.""" disabled_by = msg["disabled_by"] @@ -339,10 +339,10 @@ async def config_entry_disable(hass, connection, msg): @websocket_api.require_admin -@websocket_api.async_response @websocket_api.websocket_command( {"type": "config_entries/ignore_flow", "flow_id": str, "title": str} ) +@websocket_api.async_response async def ignore_config_flow(hass, connection, msg): """Ignore a config flow.""" flow = next( diff --git a/homeassistant/components/config/core.py b/homeassistant/components/config/core.py index e9e54a688c4..3f665e475f0 100644 --- a/homeassistant/components/config/core.py +++ b/homeassistant/components/config/core.py @@ -35,7 +35,6 @@ class CheckConfigView(HomeAssistantView): @websocket_api.require_admin -@websocket_api.async_response @websocket_api.websocket_command( { "type": "config/core/update", @@ -50,6 +49,7 @@ class CheckConfigView(HomeAssistantView): vol.Optional("currency"): cv.currency, } ) +@websocket_api.async_response async def websocket_update_config(hass, connection, msg): """Handle update core config command.""" data = dict(msg) @@ -64,8 +64,8 @@ async def websocket_update_config(hass, connection, msg): @websocket_api.require_admin -@websocket_api.async_response @websocket_api.websocket_command({"type": "config/core/detect"}) +@websocket_api.async_response async def websocket_detect_config(hass, connection, msg): """Detect core config.""" session = async_get_clientsession(hass) diff --git a/homeassistant/components/config/entity_registry.py b/homeassistant/components/config/entity_registry.py index 26a2d930d18..f5ffc574b86 100644 --- a/homeassistant/components/config/entity_registry.py +++ b/homeassistant/components/config/entity_registry.py @@ -4,15 +4,12 @@ import voluptuous as vol from homeassistant import config_entries from homeassistant.components import websocket_api from homeassistant.components.websocket_api.const import ERR_NOT_FOUND -from homeassistant.components.websocket_api.decorators import ( - async_response, - require_admin, -) +from homeassistant.components.websocket_api.decorators import require_admin from homeassistant.core import callback -from homeassistant.helpers import config_validation as cv -from homeassistant.helpers.entity_registry import ( - RegistryEntryDisabler, - async_get_registry, +from homeassistant.helpers import ( + config_validation as cv, + device_registry as dr, + entity_registry as er, ) @@ -25,14 +22,11 @@ async def async_setup(hass): return True -@async_response @websocket_api.websocket_command({vol.Required("type"): "config/entity_registry/list"}) -async def websocket_list_entities(hass, connection, msg): - """Handle list registry entries command. - - Async friendly. - """ - registry = await async_get_registry(hass) +@callback +def websocket_list_entities(hass, connection, msg): + """Handle list registry entries command.""" + registry = er.async_get(hass) connection.send_message( websocket_api.result_message( msg["id"], [_entry_dict(entry) for entry in registry.entities.values()] @@ -40,19 +34,19 @@ async def websocket_list_entities(hass, connection, msg): ) -@async_response @websocket_api.websocket_command( { vol.Required("type"): "config/entity_registry/get", vol.Required("entity_id"): cv.entity_id, } ) -async def websocket_get_entity(hass, connection, msg): +@callback +def websocket_get_entity(hass, connection, msg): """Handle get entity registry entry command. Async friendly. """ - registry = await async_get_registry(hass) + registry = er.async_get(hass) if (entry := registry.entities.get(msg["entity_id"])) is None: connection.send_message( @@ -66,7 +60,6 @@ async def websocket_get_entity(hass, connection, msg): @require_admin -@async_response @websocket_api.websocket_command( { vol.Required("type"): "config/entity_registry/update", @@ -81,17 +74,19 @@ async def websocket_get_entity(hass, connection, msg): vol.Optional("disabled_by"): vol.Any( None, vol.All( - vol.Coerce(RegistryEntryDisabler), RegistryEntryDisabler.USER.value + vol.Coerce(er.RegistryEntryDisabler), + er.RegistryEntryDisabler.USER.value, ), ), } ) -async def websocket_update_entity(hass, connection, msg): +@callback +def websocket_update_entity(hass, connection, msg): """Handle update entity websocket command. Async friendly. """ - registry = await async_get_registry(hass) + registry = er.async_get(hass) if msg["entity_id"] not in registry.entities: connection.send_message( @@ -120,7 +115,7 @@ async def websocket_update_entity(hass, connection, msg): if "disabled_by" in msg and msg["disabled_by"] is None: entity = registry.entities[msg["entity_id"]] if entity.device_id: - device_registry = await hass.helpers.device_registry.async_get_registry() + device_registry = dr.async_get(hass) device = device_registry.async_get(entity.device_id) if device.disabled: connection.send_message( @@ -149,19 +144,19 @@ async def websocket_update_entity(hass, connection, msg): @require_admin -@async_response @websocket_api.websocket_command( { vol.Required("type"): "config/entity_registry/remove", vol.Required("entity_id"): cv.entity_id, } ) -async def websocket_remove_entity(hass, connection, msg): +@callback +def websocket_remove_entity(hass, connection, msg): """Handle remove entity websocket command. Async friendly. """ - registry = await async_get_registry(hass) + registry = er.async_get(hass) if msg["entity_id"] not in registry.entities: connection.send_message( diff --git a/mypy.ini b/mypy.ini index 31788f3643d..e2b406301da 100644 --- a/mypy.ini +++ b/mypy.ini @@ -2145,18 +2145,6 @@ ignore_errors = true [mypy-homeassistant.components.cloud.http_api] ignore_errors = true -[mypy-homeassistant.components.config.auth] -ignore_errors = true - -[mypy-homeassistant.components.config.config_entries] -ignore_errors = true - -[mypy-homeassistant.components.config.core] -ignore_errors = true - -[mypy-homeassistant.components.config.entity_registry] -ignore_errors = true - [mypy-homeassistant.components.conversation] ignore_errors = true diff --git a/script/hassfest/mypy_config.py b/script/hassfest/mypy_config.py index 5e3d9bd4b11..19bd3ee77f3 100644 --- a/script/hassfest/mypy_config.py +++ b/script/hassfest/mypy_config.py @@ -21,10 +21,6 @@ IGNORED_MODULES: Final[list[str]] = [ "homeassistant.components.blueprint.websocket_api", "homeassistant.components.cloud.client", "homeassistant.components.cloud.http_api", - "homeassistant.components.config.auth", - "homeassistant.components.config.config_entries", - "homeassistant.components.config.core", - "homeassistant.components.config.entity_registry", "homeassistant.components.conversation", "homeassistant.components.conversation.default_agent", "homeassistant.components.deconz",