Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion homeassistant/components/lutron_caseta/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import voluptuous as vol

from homeassistant import config_entries
from homeassistant.const import ATTR_SUGGESTED_AREA, CONF_HOST, Platform
from homeassistant.const import ATTR_DEVICE_ID, ATTR_SUGGESTED_AREA, CONF_HOST, Platform
from homeassistant.core import HomeAssistant, callback
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers import device_registry as dr
Expand Down Expand Up @@ -229,6 +229,7 @@ def _async_subscribe_pico_remote_events(
button_devices_by_id: dict[int, dict],
):
"""Subscribe to lutron events."""
dev_reg = dr.async_get(hass)

@callback
def _async_button_event(button_id, event_type):
Expand Down Expand Up @@ -257,6 +258,7 @@ def _async_button_event(button_id, event_type):
)
return
lip_button_number = sub_type_to_lip_button[sub_type]
hass_device = dev_reg.async_get_device({(DOMAIN, device["serial"])})

hass.bus.async_fire(
LUTRON_CASETA_BUTTON_EVENT,
Expand All @@ -266,6 +268,7 @@ def _async_button_event(button_id, event_type):
ATTR_BUTTON_NUMBER: lip_button_number,
ATTR_LEAP_BUTTON_NUMBER: button_number,
ATTR_DEVICE_NAME: name,
ATTR_DEVICE_ID: hass_device.id,
ATTR_AREA_NAME: area,
ATTR_ACTION: action,
},
Expand Down
42 changes: 42 additions & 0 deletions homeassistant/components/lutron_caseta/logbook.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"""Describe lutron_caseta logbook events."""
from __future__ import annotations

from collections.abc import Callable

from homeassistant.core import Event, HomeAssistant, callback

from .const import (
ATTR_ACTION,
ATTR_AREA_NAME,
ATTR_DEVICE_NAME,
ATTR_LEAP_BUTTON_NUMBER,
ATTR_TYPE,
DOMAIN,
LUTRON_CASETA_BUTTON_EVENT,
)
from .device_trigger import LEAP_TO_DEVICE_TYPE_SUBTYPE_MAP


@callback
def async_describe_events(
hass: HomeAssistant,
async_describe_event: Callable[[str, str, Callable[[Event], dict[str, str]]], None],
) -> None:
"""Describe logbook events."""

@callback
def async_describe_button_event(event: Event) -> dict[str, str]:
"""Describe lutron_caseta_button_event logbook event."""
data = event.data
device_type = data[ATTR_TYPE]
leap_button_number = data[ATTR_LEAP_BUTTON_NUMBER]
button_map = LEAP_TO_DEVICE_TYPE_SUBTYPE_MAP[device_type]
button_description = button_map[leap_button_number]
return {
"name": f"{data[ATTR_AREA_NAME]} {data[ATTR_DEVICE_NAME]}",
"message": f"{data[ATTR_ACTION]} {button_description}",
}

async_describe_event(
DOMAIN, LUTRON_CASETA_BUTTON_EVENT, async_describe_button_event
)
73 changes: 73 additions & 0 deletions tests/components/lutron_caseta/test_logbook.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
"""The tests for lutron caseta logbook."""
from unittest.mock import patch

from homeassistant.components.lutron_caseta.const import (
ATTR_ACTION,
ATTR_AREA_NAME,
ATTR_BUTTON_NUMBER,
ATTR_DEVICE_NAME,
ATTR_LEAP_BUTTON_NUMBER,
ATTR_SERIAL,
ATTR_TYPE,
CONF_CA_CERTS,
CONF_CERTFILE,
CONF_KEYFILE,
DOMAIN,
LUTRON_CASETA_BUTTON_EVENT,
)
from homeassistant.const import ATTR_DEVICE_ID, CONF_HOST
from homeassistant.setup import async_setup_component

from . import MockBridge

from tests.common import MockConfigEntry
from tests.components.logbook.common import MockRow, mock_humanify


async def test_humanify_lutron_caseta_button_event(hass):
"""Test humanifying lutron_caseta_button_events."""
hass.config.components.add("recorder")
assert await async_setup_component(hass, "logbook", {})
config_entry = MockConfigEntry(
domain=DOMAIN,
data={
CONF_HOST: "1.1.1.1",
CONF_KEYFILE: "",
CONF_CERTFILE: "",
CONF_CA_CERTS: "",
},
unique_id="abc",
)
config_entry.add_to_hass(hass)

with patch(
"homeassistant.components.lutron_caseta.Smartbridge.create_tls",
return_value=MockBridge(can_connect=True),
):
await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()

await hass.async_block_till_done()

(event1,) = mock_humanify(
hass,
[
MockRow(
LUTRON_CASETA_BUTTON_EVENT,
{
ATTR_SERIAL: "123",
ATTR_DEVICE_ID: "1234",
ATTR_TYPE: "Pico3ButtonRaiseLower",
ATTR_LEAP_BUTTON_NUMBER: 3,
ATTR_BUTTON_NUMBER: 3,
ATTR_DEVICE_NAME: "Pico",
ATTR_AREA_NAME: "Living Room",
ATTR_ACTION: "press",
},
),
],
)

assert event1["name"] == "Living Room Pico"
assert event1["domain"] == DOMAIN
assert event1["message"] == "press raise"