diff --git a/homeassistant/components/assist_pipeline/pipeline.py b/homeassistant/components/assist_pipeline/pipeline.py index 4bc008d895b..1471af2ea41 100644 --- a/homeassistant/components/assist_pipeline/pipeline.py +++ b/homeassistant/components/assist_pipeline/pipeline.py @@ -1648,9 +1648,7 @@ class PipelineStorageCollectionWebsocket( try: await super().ws_delete_item(hass, connection, msg) except PipelinePreferred as exc: - connection.send_error( - msg["id"], websocket_api.const.ERR_NOT_ALLOWED, str(exc) - ) + connection.send_error(msg["id"], websocket_api.ERR_NOT_ALLOWED, str(exc)) @callback def ws_get_item( @@ -1664,7 +1662,7 @@ class PipelineStorageCollectionWebsocket( if item_id not in self.storage_collection.data: connection.send_error( msg["id"], - websocket_api.const.ERR_NOT_FOUND, + websocket_api.ERR_NOT_FOUND, f"Unable to find {self.item_id_key} {item_id}", ) return @@ -1695,7 +1693,7 @@ class PipelineStorageCollectionWebsocket( self.storage_collection.async_set_preferred_item(msg[self.item_id_key]) except ItemNotFound: connection.send_error( - msg["id"], websocket_api.const.ERR_NOT_FOUND, "unknown item" + msg["id"], websocket_api.ERR_NOT_FOUND, "unknown item" ) return connection.send_result(msg["id"]) diff --git a/homeassistant/components/assist_pipeline/websocket_api.py b/homeassistant/components/assist_pipeline/websocket_api.py index 56effd50a3e..18464810525 100644 --- a/homeassistant/components/assist_pipeline/websocket_api.py +++ b/homeassistant/components/assist_pipeline/websocket_api.py @@ -353,7 +353,7 @@ def websocket_get_run( if pipeline_id not in pipeline_data.pipeline_debug: connection.send_error( msg["id"], - websocket_api.const.ERR_NOT_FOUND, + websocket_api.ERR_NOT_FOUND, f"pipeline_id {pipeline_id} not found", ) return @@ -363,7 +363,7 @@ def websocket_get_run( if pipeline_run_id not in pipeline_debug: connection.send_error( msg["id"], - websocket_api.const.ERR_NOT_FOUND, + websocket_api.ERR_NOT_FOUND, f"pipeline_run_id {pipeline_run_id} not found", ) return diff --git a/homeassistant/components/auth/__init__.py b/homeassistant/components/auth/__init__.py index 6e4bbac8b63..cef7af4df92 100644 --- a/homeassistant/components/auth/__init__.py +++ b/homeassistant/components/auth/__init__.py @@ -544,7 +544,7 @@ async def websocket_create_long_lived_access_token( try: access_token = hass.auth.async_create_access_token(refresh_token) except InvalidAuthError as exc: - connection.send_error(msg["id"], websocket_api.const.ERR_UNAUTHORIZED, str(exc)) + connection.send_error(msg["id"], websocket_api.ERR_UNAUTHORIZED, str(exc)) return connection.send_result(msg["id"], access_token) diff --git a/homeassistant/components/automation/__init__.py b/homeassistant/components/automation/__init__.py index 977008df1f8..deb3613d668 100644 --- a/homeassistant/components/automation/__init__.py +++ b/homeassistant/components/automation/__init__.py @@ -1208,7 +1208,7 @@ def websocket_config( if automation is None: connection.send_error( - msg["id"], websocket_api.const.ERR_NOT_FOUND, "Entity not found" + msg["id"], websocket_api.ERR_NOT_FOUND, "Entity not found" ) return diff --git a/homeassistant/components/cloud/http_api.py b/homeassistant/components/cloud/http_api.py index 757bd27e212..bd2860b19df 100644 --- a/homeassistant/components/cloud/http_api.py +++ b/homeassistant/components/cloud/http_api.py @@ -642,7 +642,7 @@ async def google_assistant_get( if not state: connection.send_error( msg["id"], - websocket_api.const.ERR_NOT_FOUND, + websocket_api.ERR_NOT_FOUND, f"{entity_id} unknown", ) return @@ -651,7 +651,7 @@ async def google_assistant_get( if entity_id in CLOUD_NEVER_EXPOSED_ENTITIES or not entity.is_supported(): connection.send_error( msg["id"], - websocket_api.const.ERR_NOT_SUPPORTED, + websocket_api.ERR_NOT_SUPPORTED, f"{entity_id} not supported by Google assistant", ) return @@ -755,7 +755,7 @@ async def alexa_get( ): connection.send_error( msg["id"], - websocket_api.const.ERR_NOT_SUPPORTED, + websocket_api.ERR_NOT_SUPPORTED, f"{entity_id} not supported by Alexa", ) return diff --git a/homeassistant/components/config/auth.py b/homeassistant/components/config/auth.py index 266c06d6ee8..1b3fa71d7ea 100644 --- a/homeassistant/components/config/auth.py +++ b/homeassistant/components/config/auth.py @@ -121,7 +121,7 @@ async def websocket_update( if not (user := await hass.auth.async_get_user(msg.pop("user_id"))): connection.send_message( websocket_api.error_message( - msg["id"], websocket_api.const.ERR_NOT_FOUND, "User not found" + msg["id"], websocket_api.ERR_NOT_FOUND, "User not found" ) ) return diff --git a/homeassistant/components/config/config_entries.py b/homeassistant/components/config/config_entries.py index 8eb4eb22fb5..b16701f8bd0 100644 --- a/homeassistant/components/config/config_entries.py +++ b/homeassistant/components/config/config_entries.py @@ -311,9 +311,7 @@ def send_entry_not_found( connection: websocket_api.ActiveConnection, msg_id: int ) -> None: """Send Config entry not found error.""" - connection.send_error( - msg_id, websocket_api.const.ERR_NOT_FOUND, "Config entry not found" - ) + connection.send_error(msg_id, websocket_api.ERR_NOT_FOUND, "Config entry not found") def get_entry( diff --git a/homeassistant/components/config/device_registry.py b/homeassistant/components/config/device_registry.py index 2cc05978267..a5d506e5a8d 100644 --- a/homeassistant/components/config/device_registry.py +++ b/homeassistant/components/config/device_registry.py @@ -42,7 +42,7 @@ def websocket_list_devices( registry = dr.async_get(hass) # Build start of response message msg_json_prefix = ( - f'{{"id":{msg["id"]},"type": "{websocket_api.const.TYPE_RESULT}",' + f'{{"id":{msg["id"]},"type": "{websocket_api.TYPE_RESULT}",' f'"success":true,"result": [' ).encode() # Concatenate cached entity registry item JSON serializations diff --git a/homeassistant/components/config/entity_registry.py b/homeassistant/components/config/entity_registry.py index 7cdec324340..bf7a9087d56 100644 --- a/homeassistant/components/config/entity_registry.py +++ b/homeassistant/components/config/entity_registry.py @@ -43,7 +43,7 @@ def websocket_list_entities( registry = er.async_get(hass) # Build start of response message msg_json_prefix = ( - f'{{"id":{msg["id"]},"type": "{websocket_api.const.TYPE_RESULT}",' + f'{{"id":{msg["id"]},"type": "{websocket_api.TYPE_RESULT}",' '"success":true,"result": [' ).encode() # Concatenate cached entity registry item JSON serializations @@ -74,7 +74,7 @@ def websocket_list_entities_for_display( registry = er.async_get(hass) # Build start of response message msg_json_prefix = ( - f'{{"id":{msg["id"]},"type":"{websocket_api.const.TYPE_RESULT}","success":true,' + f'{{"id":{msg["id"]},"type":"{websocket_api.TYPE_RESULT}","success":true,' f'"result":{{"entity_categories":{_ENTITY_CATEGORIES_JSON},"entities":[' ).encode() # Concatenate cached entity registry item JSON serializations diff --git a/homeassistant/components/conversation/http.py b/homeassistant/components/conversation/http.py index e0821e14738..591298cbac1 100644 --- a/homeassistant/components/conversation/http.py +++ b/homeassistant/components/conversation/http.py @@ -94,9 +94,7 @@ async def websocket_prepare( agent = async_get_agent(hass, msg.get("agent_id")) if agent is None: - connection.send_error( - msg["id"], websocket_api.const.ERR_NOT_FOUND, "Agent not found" - ) + connection.send_error(msg["id"], websocket_api.ERR_NOT_FOUND, "Agent not found") return await agent.async_prepare(msg.get("language")) diff --git a/homeassistant/components/device_automation/__init__.py b/homeassistant/components/device_automation/__init__.py index b79c9e56a95..567b8fcc2d2 100644 --- a/homeassistant/components/device_automation/__init__.py +++ b/homeassistant/components/device_automation/__init__.py @@ -369,7 +369,7 @@ def handle_device_errors( await func(hass, connection, msg) except DeviceNotFound: connection.send_error( - msg["id"], websocket_api.const.ERR_NOT_FOUND, "Device not found" + msg["id"], websocket_api.ERR_NOT_FOUND, "Device not found" ) return with_error_handling diff --git a/homeassistant/components/homeassistant/exposed_entities.py b/homeassistant/components/homeassistant/exposed_entities.py index 82848b0e273..68632223045 100644 --- a/homeassistant/components/homeassistant/exposed_entities.py +++ b/homeassistant/components/homeassistant/exposed_entities.py @@ -417,7 +417,7 @@ def ws_expose_entity( None, ): connection.send_error( - msg["id"], websocket_api.const.ERR_NOT_ALLOWED, f"can't expose '{blocked}'" + msg["id"], websocket_api.ERR_NOT_ALLOWED, f"can't expose '{blocked}'" ) return diff --git a/homeassistant/components/insteon/api/device.py b/homeassistant/components/insteon/api/device.py index e8bd08bc4ee..ff688eef40c 100644 --- a/homeassistant/components/insteon/api/device.py +++ b/homeassistant/components/insteon/api/device.py @@ -65,7 +65,7 @@ async def async_device_name(dev_registry, address): def notify_device_not_found(connection, msg, text): """Notify the caller that the device was not found.""" connection.send_message( - websocket_api.error_message(msg[ID], websocket_api.const.ERR_NOT_FOUND, text) + websocket_api.error_message(msg[ID], websocket_api.ERR_NOT_FOUND, text) ) diff --git a/homeassistant/components/knx/websocket.py b/homeassistant/components/knx/websocket.py index f6869902793..dc5b5e483be 100644 --- a/homeassistant/components/knx/websocket.py +++ b/homeassistant/components/knx/websocket.py @@ -131,7 +131,7 @@ async def ws_project_file_process( except (ValueError, XknxProjectException) as err: # ValueError could raise from file_upload integration connection.send_error( - msg["id"], websocket_api.const.ERR_HOME_ASSISTANT_ERROR, str(err) + msg["id"], websocket_api.ERR_HOME_ASSISTANT_ERROR, str(err) ) return diff --git a/homeassistant/components/logger/websocket_api.py b/homeassistant/components/logger/websocket_api.py index fafc2d3eedb..6d34b10bd34 100644 --- a/homeassistant/components/logger/websocket_api.py +++ b/homeassistant/components/logger/websocket_api.py @@ -65,7 +65,7 @@ async def handle_integration_log_level( await async_get_integration(hass, msg["integration"]) except IntegrationNotFound: connection.send_error( - msg["id"], websocket_api.const.ERR_NOT_FOUND, "Integration not found" + msg["id"], websocket_api.ERR_NOT_FOUND, "Integration not found" ) return await async_get_domain_config(hass).settings.async_update( diff --git a/homeassistant/components/script/__init__.py b/homeassistant/components/script/__init__.py index 65cea1e2e4c..f19a48fea33 100644 --- a/homeassistant/components/script/__init__.py +++ b/homeassistant/components/script/__init__.py @@ -717,7 +717,7 @@ def websocket_config( if script is None: connection.send_error( - msg["id"], websocket_api.const.ERR_NOT_FOUND, "Entity not found" + msg["id"], websocket_api.ERR_NOT_FOUND, "Entity not found" ) return diff --git a/homeassistant/components/shopping_list/__init__.py b/homeassistant/components/shopping_list/__init__.py index 1176192bdcd..20d3078228c 100644 --- a/homeassistant/components/shopping_list/__init__.py +++ b/homeassistant/components/shopping_list/__init__.py @@ -582,12 +582,12 @@ def websocket_handle_reorder( except NoMatchingShoppingListItem: connection.send_error( msg_id, - websocket_api.const.ERR_NOT_FOUND, + websocket_api.ERR_NOT_FOUND, "One or more item id(s) not found.", ) return except vol.Invalid as err: - connection.send_error(msg_id, websocket_api.const.ERR_INVALID_FORMAT, f"{err}") + connection.send_error(msg_id, websocket_api.ERR_INVALID_FORMAT, f"{err}") return connection.send_result(msg_id) diff --git a/homeassistant/components/template/config_flow.py b/homeassistant/components/template/config_flow.py index 5d0cb99826f..8a5ecca5b4b 100644 --- a/homeassistant/components/template/config_flow.py +++ b/homeassistant/components/template/config_flow.py @@ -344,7 +344,7 @@ def ws_start_preview( connection.send_message( { "id": msg["id"], - "type": websocket_api.const.TYPE_RESULT, + "type": websocket_api.TYPE_RESULT, "success": False, "error": {"code": "invalid_user_input", "message": errors}, } diff --git a/homeassistant/components/thread/websocket_api.py b/homeassistant/components/thread/websocket_api.py index 687c4067caf..d436a5ffb72 100644 --- a/homeassistant/components/thread/websocket_api.py +++ b/homeassistant/components/thread/websocket_api.py @@ -44,9 +44,7 @@ async def ws_add_dataset( try: await dataset_store.async_add_dataset(hass, source, tlv) except TLVError as exc: - connection.send_error( - msg["id"], websocket_api.const.ERR_INVALID_FORMAT, str(exc) - ) + connection.send_error(msg["id"], websocket_api.ERR_INVALID_FORMAT, str(exc)) return connection.send_result(msg["id"]) @@ -94,9 +92,7 @@ async def ws_set_preferred_dataset( try: store.preferred_dataset = dataset_id except KeyError: - connection.send_error( - msg["id"], websocket_api.const.ERR_NOT_FOUND, "unknown dataset" - ) + connection.send_error(msg["id"], websocket_api.ERR_NOT_FOUND, "unknown dataset") return connection.send_result(msg["id"]) @@ -120,10 +116,10 @@ async def ws_delete_dataset( try: store.async_delete(dataset_id) except KeyError as exc: - connection.send_error(msg["id"], websocket_api.const.ERR_NOT_FOUND, str(exc)) + connection.send_error(msg["id"], websocket_api.ERR_NOT_FOUND, str(exc)) return except dataset_store.DatasetPreferredError as exc: - connection.send_error(msg["id"], websocket_api.const.ERR_NOT_ALLOWED, str(exc)) + connection.send_error(msg["id"], websocket_api.ERR_NOT_ALLOWED, str(exc)) return connection.send_result(msg["id"]) @@ -145,9 +141,7 @@ async def ws_get_dataset( store = await dataset_store.async_get_store(hass) if not (dataset := store.async_get(dataset_id)): - connection.send_error( - msg["id"], websocket_api.const.ERR_NOT_FOUND, "unknown dataset" - ) + connection.send_error(msg["id"], websocket_api.ERR_NOT_FOUND, "unknown dataset") return connection.send_result(msg["id"], {"tlv": dataset.tlv}) diff --git a/homeassistant/components/tts/__init__.py b/homeassistant/components/tts/__init__.py index 3055bf46ca7..15cd10552ed 100644 --- a/homeassistant/components/tts/__init__.py +++ b/homeassistant/components/tts/__init__.py @@ -1114,7 +1114,7 @@ def websocket_get_engine( if not provider: connection.send_error( msg["id"], - websocket_api.const.ERR_NOT_FOUND, + websocket_api.ERR_NOT_FOUND, f"tts engine {engine_id} not found", ) return @@ -1149,7 +1149,7 @@ def websocket_list_engine_voices( if not engine_instance: connection.send_error( msg["id"], - websocket_api.const.ERR_NOT_FOUND, + websocket_api.ERR_NOT_FOUND, f"tts engine {engine_id} not found", ) return diff --git a/homeassistant/components/update/__init__.py b/homeassistant/components/update/__init__.py index 57d63c92ede..352237bf201 100644 --- a/homeassistant/components/update/__init__.py +++ b/homeassistant/components/update/__init__.py @@ -495,14 +495,14 @@ async def websocket_release_notes( if entity is None: connection.send_error( - msg["id"], websocket_api.const.ERR_NOT_FOUND, "Entity not found" + msg["id"], websocket_api.ERR_NOT_FOUND, "Entity not found" ) return if UpdateEntityFeature.RELEASE_NOTES not in entity.supported_features_compat: connection.send_error( msg["id"], - websocket_api.const.ERR_NOT_SUPPORTED, + websocket_api.ERR_NOT_SUPPORTED, "Entity does not support release notes", ) return diff --git a/homeassistant/components/wake_word/__init__.py b/homeassistant/components/wake_word/__init__.py index f05a61e34dc..5ce592aacd8 100644 --- a/homeassistant/components/wake_word/__init__.py +++ b/homeassistant/components/wake_word/__init__.py @@ -147,7 +147,7 @@ async def websocket_entity_info( if entity is None: connection.send_error( - msg["id"], websocket_api.const.ERR_NOT_FOUND, "Entity not found" + msg["id"], websocket_api.ERR_NOT_FOUND, "Entity not found" ) return @@ -156,7 +156,7 @@ async def websocket_entity_info( wake_words = await entity.get_supported_wake_words() except TimeoutError: connection.send_error( - msg["id"], websocket_api.const.ERR_TIMEOUT, "Timeout fetching wake words" + msg["id"], websocket_api.ERR_TIMEOUT, "Timeout fetching wake words" ) return diff --git a/homeassistant/components/websocket_api/__init__.py b/homeassistant/components/websocket_api/__init__.py index aad161eba34..d8427bff10e 100644 --- a/homeassistant/components/websocket_api/__init__.py +++ b/homeassistant/components/websocket_api/__init__.py @@ -16,6 +16,7 @@ from .connection import ActiveConnection, current_connection # noqa: F401 from .const import ( # noqa: F401 ERR_HOME_ASSISTANT_ERROR, ERR_INVALID_FORMAT, + ERR_NOT_ALLOWED, ERR_NOT_FOUND, ERR_NOT_SUPPORTED, ERR_SERVICE_VALIDATION_ERROR, @@ -24,6 +25,7 @@ from .const import ( # noqa: F401 ERR_UNAUTHORIZED, ERR_UNKNOWN_COMMAND, ERR_UNKNOWN_ERROR, + TYPE_RESULT, AsyncWebSocketCommandHandler, WebSocketCommandHandler, ) diff --git a/homeassistant/components/zha/websocket_api.py b/homeassistant/components/zha/websocket_api.py index 70be438bf24..1a51a06243e 100644 --- a/homeassistant/components/zha/websocket_api.py +++ b/homeassistant/components/zha/websocket_api.py @@ -443,7 +443,7 @@ async def websocket_get_device( if not (zha_device := zha_gateway.devices.get(ieee)): connection.send_message( websocket_api.error_message( - msg[ID], websocket_api.const.ERR_NOT_FOUND, "ZHA Device not found" + msg[ID], websocket_api.ERR_NOT_FOUND, "ZHA Device not found" ) ) return @@ -470,7 +470,7 @@ async def websocket_get_group( if not (zha_group := zha_gateway.groups.get(group_id)): connection.send_message( websocket_api.error_message( - msg[ID], websocket_api.const.ERR_NOT_FOUND, "ZHA Group not found" + msg[ID], websocket_api.ERR_NOT_FOUND, "ZHA Group not found" ) ) return @@ -548,7 +548,7 @@ async def websocket_add_group_members( if not (zha_group := zha_gateway.groups.get(group_id)): connection.send_message( websocket_api.error_message( - msg[ID], websocket_api.const.ERR_NOT_FOUND, "ZHA Group not found" + msg[ID], websocket_api.ERR_NOT_FOUND, "ZHA Group not found" ) ) return @@ -578,7 +578,7 @@ async def websocket_remove_group_members( if not (zha_group := zha_gateway.groups.get(group_id)): connection.send_message( websocket_api.error_message( - msg[ID], websocket_api.const.ERR_NOT_FOUND, "ZHA Group not found" + msg[ID], websocket_api.ERR_NOT_FOUND, "ZHA Group not found" ) ) return @@ -1214,7 +1214,7 @@ async def websocket_restore_network_backup( try: await application_controller.backups.restore_backup(backup) except ValueError as err: - connection.send_error(msg[ID], websocket_api.const.ERR_INVALID_FORMAT, str(err)) + connection.send_error(msg[ID], websocket_api.ERR_INVALID_FORMAT, str(err)) else: connection.send_result(msg[ID]) diff --git a/homeassistant/helpers/collection.py b/homeassistant/helpers/collection.py index c69295ed1b1..bf65b47f451 100644 --- a/homeassistant/helpers/collection.py +++ b/homeassistant/helpers/collection.py @@ -619,13 +619,11 @@ class StorageCollectionWebsocket[_StorageCollectionT: StorageCollection]: except vol.Invalid as err: connection.send_error( msg["id"], - websocket_api.const.ERR_INVALID_FORMAT, + websocket_api.ERR_INVALID_FORMAT, humanize_error(data, err), ) except ValueError as err: - connection.send_error( - msg["id"], websocket_api.const.ERR_INVALID_FORMAT, str(err) - ) + connection.send_error(msg["id"], websocket_api.ERR_INVALID_FORMAT, str(err)) async def ws_update_item( self, hass: HomeAssistant, connection: websocket_api.ActiveConnection, msg: dict @@ -642,19 +640,17 @@ class StorageCollectionWebsocket[_StorageCollectionT: StorageCollection]: except ItemNotFound: connection.send_error( msg["id"], - websocket_api.const.ERR_NOT_FOUND, + websocket_api.ERR_NOT_FOUND, f"Unable to find {self.item_id_key} {item_id}", ) except vol.Invalid as err: connection.send_error( msg["id"], - websocket_api.const.ERR_INVALID_FORMAT, + websocket_api.ERR_INVALID_FORMAT, humanize_error(data, err), ) except ValueError as err: - connection.send_error( - msg_id, websocket_api.const.ERR_INVALID_FORMAT, str(err) - ) + connection.send_error(msg_id, websocket_api.ERR_INVALID_FORMAT, str(err)) async def ws_delete_item( self, hass: HomeAssistant, connection: websocket_api.ActiveConnection, msg: dict @@ -665,7 +661,7 @@ class StorageCollectionWebsocket[_StorageCollectionT: StorageCollection]: except ItemNotFound: connection.send_error( msg["id"], - websocket_api.const.ERR_NOT_FOUND, + websocket_api.ERR_NOT_FOUND, f"Unable to find {self.item_id_key} {msg[self.item_id_key]}", )