Add unique ids to lutron_caseta scenes (#73383)

This commit is contained in:
J. Nick Koston 2022-06-12 17:14:19 -10:00 committed by GitHub
parent a7f72931ad
commit c6a6d7039e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 6 deletions

View File

@ -679,6 +679,7 @@ omit =
homeassistant/components/lutron_caseta/light.py
homeassistant/components/lutron_caseta/scene.py
homeassistant/components/lutron_caseta/switch.py
homeassistant/components/lutron_caseta/util.py
homeassistant/components/lw12wifi/light.py
homeassistant/components/lyric/__init__.py
homeassistant/components/lyric/api.py

View File

@ -1,12 +1,17 @@
"""Support for Lutron Caseta scenes."""
from typing import Any
from pylutron_caseta.smartbridge import Smartbridge
from homeassistant.components.scene import Scene
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import BRIDGE_LEAP, DOMAIN as CASETA_DOMAIN
from . import _area_and_name_from_name
from .const import BRIDGE_DEVICE, BRIDGE_LEAP, DOMAIN as CASETA_DOMAIN
from .util import serial_to_unique_id
async def async_setup_entry(
@ -20,19 +25,27 @@ async def async_setup_entry(
scene entities.
"""
data = hass.data[CASETA_DOMAIN][config_entry.entry_id]
bridge = data[BRIDGE_LEAP]
bridge: Smartbridge = data[BRIDGE_LEAP]
bridge_device = data[BRIDGE_DEVICE]
scenes = bridge.get_scenes()
async_add_entities(LutronCasetaScene(scenes[scene], bridge) for scene in scenes)
async_add_entities(
LutronCasetaScene(scenes[scene], bridge, bridge_device) for scene in scenes
)
class LutronCasetaScene(Scene):
"""Representation of a Lutron Caseta scene."""
def __init__(self, scene, bridge):
def __init__(self, scene, bridge, bridge_device):
"""Initialize the Lutron Caseta scene."""
self._attr_name = scene["name"]
self._scene_id = scene["scene_id"]
self._bridge = bridge
self._bridge: Smartbridge = bridge
bridge_unique_id = serial_to_unique_id(bridge_device["serial"])
self._attr_device_info = DeviceInfo(
identifiers={(CASETA_DOMAIN, bridge_device["serial"])},
)
self._attr_name = _area_and_name_from_name(scene["name"])[1]
self._attr_unique_id = f"scene_{bridge_unique_id}_{self._scene_id}"
async def async_activate(self, **kwargs: Any) -> None:
"""Activate the scene."""

View File

@ -0,0 +1,7 @@
"""Support for Lutron Caseta."""
from __future__ import annotations
def serial_to_unique_id(serial: int) -> str:
"""Convert a lutron serial number to a unique id."""
return hex(serial)[2:].zfill(8)