Add device_id and logbook descriptions to lutron_caseta (#71713)

This commit is contained in:
J. Nick Koston 2022-05-11 22:45:16 -05:00 committed by GitHub
parent 1dc15bb7c8
commit 9cd81db5b3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 119 additions and 1 deletions

View file

@ -12,7 +12,7 @@ from pylutron_caseta.smartbridge import Smartbridge
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
@ -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):
@ -257,6 +258,7 @@ def _async_subscribe_pico_remote_events(
)
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,
@ -266,6 +268,7 @@ def _async_subscribe_pico_remote_events(
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,
},

View file

@ -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
)

View file

@ -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"