Avoid references to websocket_api.const in core and other components (#119285)

This commit is contained in:
epenet 2024-06-10 13:49:44 +02:00 committed by GitHub
parent 2e3c3789d3
commit 960d1289ef
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
25 changed files with 49 additions and 63 deletions

View File

@ -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"])

View File

@ -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

View File

@ -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)

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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(

View File

@ -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

View File

@ -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

View File

@ -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"))

View File

@ -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

View File

@ -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

View File

@ -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)
)

View File

@ -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

View File

@ -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(

View File

@ -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

View File

@ -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)

View File

@ -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},
}

View File

@ -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})

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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,
)

View File

@ -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])

View File

@ -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]}",
)