mirror of
https://github.com/home-assistant/core.git
synced 2025-07-18 02:37:08 +00:00
Simplify diagnostics.DownloadDiagnosticsView (#83116)
* Refactor diagnostics.DownloadDiagnosticsView * Simplify
This commit is contained in:
parent
8eeba490d1
commit
155db2f2e1
@ -151,9 +151,7 @@ async def _async_get_json_file_response(
|
|||||||
data: Any,
|
data: Any,
|
||||||
filename: str,
|
filename: str,
|
||||||
domain: str,
|
domain: str,
|
||||||
d_type: DiagnosticsType,
|
|
||||||
d_id: str,
|
d_id: str,
|
||||||
sub_type: DiagnosticsSubType | None = None,
|
|
||||||
sub_id: str | None = None,
|
sub_id: str | None = None,
|
||||||
) -> web.Response:
|
) -> web.Response:
|
||||||
"""Return JSON file from dictionary."""
|
"""Return JSON file from dictionary."""
|
||||||
@ -183,9 +181,11 @@ async def _async_get_json_file_response(
|
|||||||
except TypeError:
|
except TypeError:
|
||||||
_LOGGER.error(
|
_LOGGER.error(
|
||||||
"Failed to serialize to JSON: %s/%s%s. Bad data at %s",
|
"Failed to serialize to JSON: %s/%s%s. Bad data at %s",
|
||||||
d_type.value,
|
DiagnosticsType.CONFIG_ENTRY.value,
|
||||||
d_id,
|
d_id,
|
||||||
f"/{sub_type.value}/{sub_id}" if sub_type is not None else "",
|
f"/{DiagnosticsSubType.DEVICE.value}/{sub_id}"
|
||||||
|
if sub_id is not None
|
||||||
|
else "",
|
||||||
format_unserializable_data(find_paths_unserializable_data(data)),
|
format_unserializable_data(find_paths_unserializable_data(data)),
|
||||||
)
|
)
|
||||||
return web.Response(status=HTTPStatus.INTERNAL_SERVER_ERROR)
|
return web.Response(status=HTTPStatus.INTERNAL_SERVER_ERROR)
|
||||||
@ -213,12 +213,20 @@ class DownloadDiagnosticsView(http.HomeAssistantView):
|
|||||||
sub_id: str | None = None,
|
sub_id: str | None = None,
|
||||||
) -> web.Response:
|
) -> web.Response:
|
||||||
"""Download diagnostics."""
|
"""Download diagnostics."""
|
||||||
# t_type handling
|
# Validate d_type and sub_type
|
||||||
try:
|
try:
|
||||||
d_type = DiagnosticsType(d_type)
|
DiagnosticsType(d_type)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
return web.Response(status=HTTPStatus.BAD_REQUEST)
|
return web.Response(status=HTTPStatus.BAD_REQUEST)
|
||||||
|
|
||||||
|
if sub_type is not None:
|
||||||
|
try:
|
||||||
|
DiagnosticsSubType(sub_type)
|
||||||
|
except ValueError:
|
||||||
|
return web.Response(status=HTTPStatus.BAD_REQUEST)
|
||||||
|
|
||||||
|
device_diagnostics = sub_type is not None
|
||||||
|
|
||||||
hass: HomeAssistant = request.app["hass"]
|
hass: HomeAssistant = request.app["hass"]
|
||||||
|
|
||||||
if (config_entry := hass.config_entries.async_get_entry(d_id)) is None:
|
if (config_entry := hass.config_entries.async_get_entry(d_id)) is None:
|
||||||
@ -230,23 +238,20 @@ class DownloadDiagnosticsView(http.HomeAssistantView):
|
|||||||
|
|
||||||
filename = f"{config_entry.domain}-{config_entry.entry_id}"
|
filename = f"{config_entry.domain}-{config_entry.entry_id}"
|
||||||
|
|
||||||
if sub_type is None:
|
if not device_diagnostics:
|
||||||
|
# Config entry diagnostics
|
||||||
if info.config_entry_diagnostics is None:
|
if info.config_entry_diagnostics is None:
|
||||||
return web.Response(status=HTTPStatus.NOT_FOUND)
|
return web.Response(status=HTTPStatus.NOT_FOUND)
|
||||||
data = await info.config_entry_diagnostics(hass, config_entry)
|
data = await info.config_entry_diagnostics(hass, config_entry)
|
||||||
filename = f"{d_type}-{filename}"
|
filename = f"{DiagnosticsType.CONFIG_ENTRY}-{filename}"
|
||||||
return await _async_get_json_file_response(
|
return await _async_get_json_file_response(
|
||||||
hass, data, filename, config_entry.domain, d_type.value, d_id
|
hass, data, filename, config_entry.domain, d_id
|
||||||
)
|
)
|
||||||
|
|
||||||
# sub_type handling
|
# Device diagnostics
|
||||||
try:
|
|
||||||
sub_type = DiagnosticsSubType(sub_type)
|
|
||||||
except ValueError:
|
|
||||||
return web.Response(status=HTTPStatus.BAD_REQUEST)
|
|
||||||
|
|
||||||
dev_reg = async_get(hass)
|
dev_reg = async_get(hass)
|
||||||
assert sub_id
|
if sub_id is None:
|
||||||
|
return web.Response(status=HTTPStatus.BAD_REQUEST)
|
||||||
|
|
||||||
if (device := dev_reg.async_get(sub_id)) is None:
|
if (device := dev_reg.async_get(sub_id)) is None:
|
||||||
return web.Response(status=HTTPStatus.NOT_FOUND)
|
return web.Response(status=HTTPStatus.NOT_FOUND)
|
||||||
@ -258,5 +263,5 @@ class DownloadDiagnosticsView(http.HomeAssistantView):
|
|||||||
|
|
||||||
data = await info.device_diagnostics(hass, config_entry, device)
|
data = await info.device_diagnostics(hass, config_entry, device)
|
||||||
return await _async_get_json_file_response(
|
return await _async_get_json_file_response(
|
||||||
hass, data, filename, config_entry.domain, d_type, d_id, sub_type, sub_id
|
hass, data, filename, config_entry.domain, d_id, sub_id
|
||||||
)
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user