Skip to content

Commit

Permalink
Add requirements log parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexxIT committed Apr 12, 2024
1 parent cedd6c0 commit 8f03bf9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
4 changes: 4 additions & 0 deletions custom_components/hass_diagnostics/core/smart_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
RE_DOMAIN = re.compile(r"\bcomponents[/.]([0-9a-z_]+)")
RE_DEPRECATED = re.compile(r"\b(is deprecated|is a deprecated|will stop working)\b")
RE_PACKAGE = re.compile(r"/site-packages/([^/]+)")
RE_REQUIREMENTS = re.compile(r"Requirements for ([^ ]+) not found.+")
RE_TEMPLATE = re.compile(r"Template<template=\((.+?)\) renders=", flags=re.DOTALL)
RE_CONNECT_TO_HOST = re.compile(r"Cannot connect to host ([^ :]+)")
RE_CONNECT = re.compile(
Expand Down Expand Up @@ -75,6 +76,9 @@ def parse_log_record(record: LogRecord) -> dict:
entry["category"] = "connection"
elif RE_DEPRECATED.search(message):
entry["category"] = "deprecated"
elif m := RE_REQUIREMENTS.search(text):
entry["domain"] = m[1]
short = m[0]
elif m := RE_TEMPLATE.search(message):
entry["category"] = "template"
short = m[1]
Expand Down
18 changes: 18 additions & 0 deletions tests/test_system_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,3 +313,21 @@ def test_xiaomi_miot():
"name": "homeassistant.components.sensor",
"short": "Platform xiaomi_miot does not generate unique IDs. ID xxx-m...",
}


def test_requirements():
entry = {
"name": "homeassistant",
"message": ["Error doing job: Task exception was never retrieved"],
"level": "ERROR",
"source": ["requirements.py", 318],
"timestamp": 1712919903.9594476,
"exception": 'Traceback (most recent call last):\n File "/usr/src/homeassistant/homeassistant/helpers/discovery_flow.py", line 107, in _async_start\n await gather_with_limited_concurrency(FLOW_INIT_LIMIT, *init_coros)\n File "/usr/src/homeassistant/homeassistant/util/async_.py", line 207, in gather_with_limited_concurrency\n return await gather(\n ^^^^^^^^^^^^^\n File "/usr/src/homeassistant/homeassistant/util/async_.py", line 205, in sem_task\n return await task\n ^^^^^^^^^^\n File "/usr/src/homeassistant/homeassistant/config_entries.py", line 1155, in async_init\n flow, result = await self._async_init(flow_id, handler, context, data)\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n File "/usr/src/homeassistant/homeassistant/config_entries.py", line 1175, in _async_init\n flow = await self.async_create_flow(handler, context=context, data=data)\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n File "/usr/src/homeassistant/homeassistant/config_entries.py", line 1312, in async_create_flow\n handler = await _async_get_flow_handler(\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n File "/usr/src/homeassistant/homeassistant/config_entries.py", line 2608, in _async_get_flow_handler\n await _load_integration(hass, domain, hass_config)\n File "/usr/src/homeassistant/homeassistant/config_entries.py", line 2585, in _load_integration\n await async_process_deps_reqs(hass, hass_config, integration)\n File "/usr/src/homeassistant/homeassistant/setup.py", line 551, in async_process_deps_reqs\n await requirements.async_get_integration_with_requirements(\n File "/usr/src/homeassistant/homeassistant/requirements.py", line 53, in async_get_integration_with_requirements\n return await manager.async_get_integration_with_requirements(domain)\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n File "/usr/src/homeassistant/homeassistant/requirements.py", line 176, in async_get_integration_with_requirements\n await self._async_process_integration(integration, done)\n File "/usr/src/homeassistant/homeassistant/requirements.py", line 193, in _async_process_integration\n await self.async_process_requirements(\n File "/usr/src/homeassistant/homeassistant/requirements.py", line 280, in async_process_requirements\n await self._async_process_requirements(name, missing)\n File "/usr/src/homeassistant/homeassistant/requirements.py", line 318, in _async_process_requirements\n raise RequirementsNotFound(name, list(failures))\nhomeassistant.requirements.RequirementsNotFound: Requirements for tuya_ble not found: [\'pycountry==22.3.5\'].\n',
"count": 1,
"first_occurred": 1712919903.9594476,
}
assert parse_log_entry(entry) == {
"domain": "tuya_ble",
"name": "homeassistant",
"short": "Requirements for tuya_ble not found: ['pycountry==22.3.5'].",
}

0 comments on commit 8f03bf9

Please sign in to comment.