Skip to content

Commit

Permalink
Add import from system_log
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexxIT committed Apr 11, 2024
1 parent 3a0dc03 commit 6874e74
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
10 changes: 10 additions & 0 deletions custom_components/hass_diagnostics/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
import logging

from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant

_LOGGER = logging.getLogger(__name__)

DOMAIN = "hass_diagnostics"

PLATFORMS = ["sensor"]


async def async_setup(hass: HomeAssistant, config: dict) -> bool:
_LOGGER.debug("async_setup")
return True


async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
_LOGGER.debug("async_setup_entry")
await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS)
return True
28 changes: 27 additions & 1 deletion custom_components/hass_diagnostics/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,25 @@

from . import DOMAIN

_LOGGER = logging.getLogger(__name__)


async def async_setup_entry(
hass: HomeAssistant, config_entry: ConfigEntry, async_add_entities
):
_LOGGER.debug("async_setup_entry sernsor")

system_log = SystemLogSensor()

# import log records from global system_log
if log := hass.data.get("system_log"):
for entry in log.records.values():
record = convert_log_entry_to_record(entry.to_dict())
system_log.emit(record)

data = hass.data.setdefault(DOMAIN, {})
data["system_log"] = system_log = SystemLogSensor()
data["system_log"] = system_log

async_add_entities([system_log], False)


Expand Down Expand Up @@ -123,3 +136,16 @@ def parse_log_record(record: logging.LogRecord) -> dict:
entry["short"] = short.replace("\n", " ")

return entry


def convert_log_entry_to_record(entry: dict):
args = {
"name": entry["name"],
"levelname": entry["level"],
"created": entry["timestamp"],
"pathname": entry["source"][0],
"message": entry["message"][0],
"exc_info": entry["exception"] != "",
"exc_text": entry["exception"],
}
return type("LogRecord", (), args)()

0 comments on commit 6874e74

Please sign in to comment.