Add diagnostics support to lutron_caseta (#67079)

This commit is contained in:
J. Nick Koston 2022-02-22 14:31:41 -10:00 committed by GitHub
parent bdcdf52225
commit d5a2381f07
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 134 additions and 26 deletions

View file

@ -0,0 +1,31 @@
"""Diagnostics support for lutron_caseta."""
from __future__ import annotations
from typing import Any
from pylutron_caseta.smartbridge import Smartbridge
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from .const import BRIDGE_LEAP, DOMAIN
async def async_get_config_entry_diagnostics(
hass: HomeAssistant, entry: ConfigEntry
) -> dict[str, Any]:
"""Return diagnostics for a config entry."""
bridge: Smartbridge = hass.data[DOMAIN][entry.entry_id][BRIDGE_LEAP]
return {
"entry": {
"title": entry.title,
"data": dict(entry.data),
},
"data": {
"devices": bridge.devices,
"buttons": bridge.buttons,
"scenes": bridge.scenes,
"occupancy_groups": bridge.occupancy_groups,
"areas": bridge.areas,
},
}

View file

@ -1 +1,42 @@
"""Tests for the Lutron Caseta integration."""
class MockBridge:
"""Mock Lutron bridge that emulates configured connected status."""
def __init__(self, can_connect=True):
"""Initialize MockBridge instance with configured mock connectivity."""
self.can_connect = can_connect
self.is_currently_connected = False
self.buttons = {}
self.areas = {}
self.occupancy_groups = {}
self.scenes = self.get_scenes()
self.devices = self.get_devices()
async def connect(self):
"""Connect the mock bridge."""
if self.can_connect:
self.is_currently_connected = True
def is_connected(self):
"""Return whether the mock bridge is connected."""
return self.is_currently_connected
def get_devices(self):
"""Return devices on the bridge."""
return {
"1": {"serial": 1234, "name": "bridge", "model": "model", "type": "type"}
}
def get_devices_by_domain(self, domain):
"""Return devices on the bridge."""
return {}
def get_scenes(self):
"""Return scenes on the bridge."""
return {}
async def close(self):
"""Close the mock bridge connection."""
self.is_currently_connected = False

View file

@ -20,6 +20,8 @@ from homeassistant.components.lutron_caseta.const import (
)
from homeassistant.const import CONF_HOST
from . import MockBridge
from tests.common import MockConfigEntry
ATTR_HOSTNAME = "hostname"
@ -39,32 +41,6 @@ MOCK_ASYNC_PAIR_SUCCESS = {
}
class MockBridge:
"""Mock Lutron bridge that emulates configured connected status."""
def __init__(self, can_connect=True):
"""Initialize MockBridge instance with configured mock connectivity."""
self.can_connect = can_connect
self.is_currently_connected = False
async def connect(self):
"""Connect the mock bridge."""
if self.can_connect:
self.is_currently_connected = True
def is_connected(self):
"""Return whether the mock bridge is connected."""
return self.is_currently_connected
def get_devices(self):
"""Return devices on the bridge."""
return {"1": {"serial": 1234}}
async def close(self):
"""Close the mock bridge connection."""
self.is_currently_connected = False
async def test_bridge_import_flow(hass):
"""Test a bridge entry gets created and set up during the import flow."""

View file

@ -0,0 +1,60 @@
"""Test the Lutron Caseta diagnostics."""
from unittest.mock import patch
from homeassistant.components.lutron_caseta import DOMAIN
from homeassistant.components.lutron_caseta.const import (
CONF_CA_CERTS,
CONF_CERTFILE,
CONF_KEYFILE,
)
from homeassistant.const import CONF_HOST
from . import MockBridge
from tests.common import MockConfigEntry
from tests.components.diagnostics import get_diagnostics_for_config_entry
async def test_diagnostics(hass, hass_client) -> None:
"""Test generating diagnostics for lutron_caseta."""
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()
diag = await get_diagnostics_for_config_entry(hass, hass_client, config_entry)
assert diag == {
"data": {
"areas": {},
"buttons": {},
"devices": {
"1": {
"model": "model",
"name": "bridge",
"serial": 1234,
"type": "type",
}
},
"occupancy_groups": {},
"scenes": {},
},
"entry": {
"data": {"ca_certs": "", "certfile": "", "host": "1.1.1.1", "keyfile": ""},
"title": "Mock Title",
},
}