basnijholt / adaptive-lighting

Adaptive Lighting custom component for Home Assistant
https://basnijholt.github.io/adaptive-lighting/
Apache License 2.0
1.71k stars 127 forks source link

Logbook events #851

Open mikz opened 7 months ago

mikz commented 7 months ago

Hi,

I'm debugging my lights getting "manually controlled" even when I'm not controlling them.

It would be extremely helpful if I would have logbook events for manual control and maybe for some other important events, so then I can focus on a right time and reasons.

I'm happy to implement this, I'd just need a go ahead and maybe a few pointers.

But my understanding is that it is just matter of calling the logbook service.

mikz commented 7 months ago

Ah ! Logbook entries are basically just events. That is great news as the event for manual control should be already there: https://github.com/basnijholt/adaptive-lighting/blob/e8bd39f6d4995500624359e709aad14ef93fbfe5/custom_components/adaptive_lighting/switch.py#L392-L396

Then it should be as easy as https://github.com/home-assistant/core/pull/96171

I've tried it with this in custom_components/adaptive_lighting/logbook.py:

"""Describe logbook events."""
from homeassistant.components.logbook import (
    LOGBOOK_ENTRY_CONTEXT_ID,
    LOGBOOK_ENTRY_ENTITY_ID,
    LOGBOOK_ENTRY_MESSAGE,
    LOGBOOK_ENTRY_NAME,
)
from homeassistant.const import ATTR_ENTITY_ID, ATTR_NAME
from homeassistant.core import callback

from .const import DOMAIN

@callback
def async_describe_events(hass, async_describe_event):
    """Describe logbook events."""

    @callback
    def async_describe_logbook_event(event):
        """Describe the logbook event."""
        data = event.data
        return {
            LOGBOOK_ENTRY_NAME: data.get(ATTR_NAME),
            LOGBOOK_ENTRY_MESSAGE: "is manually controlled",
            LOGBOOK_ENTRY_ENTITY_ID: data.get(ATTR_ENTITY_ID),
            LOGBOOK_ENTRY_CONTEXT_ID: event.context_id,
        }

    async_describe_event(DOMAIN, "manual_control", async_describe_logbook_event)

But no luck. I'm not sure if it is because I'm doing something wrong, or not doing something I'm supposed to or just this event not being triggered.

nathang21 commented 7 months ago

I'm also debugging the same thing, having Logbook events would make this so much easier! Let me know if you want help testing a PR.