Small cleanups lutron_caseta (#72099)

This commit is contained in:
J. Nick Koston 2022-05-18 15:35:35 -05:00 committed by GitHub
parent 18b40990a2
commit 2745573610
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 41 additions and 99 deletions

View File

@ -353,3 +353,12 @@ class LutronCasetaDevice(Entity):
def extra_state_attributes(self):
"""Return the state attributes."""
return {"device_id": self.device_id, "zone_id": self._device["zone"]}
class LutronCasetaDeviceUpdatableEntity(LutronCasetaDevice):
"""A lutron_caseta entity that can update by syncing data from the bridge."""
async def async_update(self):
"""Update when forcing a refresh of the device."""
self._device = self._smartbridge.get_device_by_id(self.device_id)
_LOGGER.debug(self._device)

View File

@ -26,22 +26,21 @@ async def async_setup_entry(
Adds occupancy groups from the Caseta bridge associated with the
config_entry as binary_sensor entities.
"""
entities = []
data = hass.data[CASETA_DOMAIN][config_entry.entry_id]
bridge = data[BRIDGE_LEAP]
bridge_device = data[BRIDGE_DEVICE]
occupancy_groups = bridge.occupancy_groups
for occupancy_group in occupancy_groups.values():
entity = LutronOccupancySensor(occupancy_group, bridge, bridge_device)
entities.append(entity)
async_add_entities(entities, True)
async_add_entities(
LutronOccupancySensor(occupancy_group, bridge, bridge_device)
for occupancy_group in occupancy_groups.values()
)
class LutronOccupancySensor(LutronCasetaDevice, BinarySensorEntity):
"""Representation of a Lutron occupancy group."""
_attr_device_class = BinarySensorDeviceClass.OCCUPANCY
def __init__(self, device, bridge, bridge_device):
"""Init an occupancy sensor."""
super().__init__(device, bridge, bridge_device)
@ -59,11 +58,6 @@ class LutronOccupancySensor(LutronCasetaDevice, BinarySensorEntity):
info[ATTR_SUGGESTED_AREA] = area
self._attr_device_info = info
@property
def device_class(self):
"""Flag supported features."""
return BinarySensorDeviceClass.OCCUPANCY
@property
def is_on(self):
"""Return the brightness of the light."""

View File

@ -1,5 +1,4 @@
"""Support for Lutron Caseta shades."""
import logging
from homeassistant.components.cover import (
ATTR_POSITION,
@ -12,11 +11,9 @@ from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import LutronCasetaDevice
from . import LutronCasetaDeviceUpdatableEntity
from .const import BRIDGE_DEVICE, BRIDGE_LEAP, DOMAIN as CASETA_DOMAIN
_LOGGER = logging.getLogger(__name__)
async def async_setup_entry(
hass: HomeAssistant,
@ -28,20 +25,17 @@ async def async_setup_entry(
Adds shades from the Caseta bridge associated with the config_entry as
cover entities.
"""
entities = []
data = hass.data[CASETA_DOMAIN][config_entry.entry_id]
bridge = data[BRIDGE_LEAP]
bridge_device = data[BRIDGE_DEVICE]
cover_devices = bridge.get_devices_by_domain(DOMAIN)
for cover_device in cover_devices:
entity = LutronCasetaCover(cover_device, bridge, bridge_device)
entities.append(entity)
async_add_entities(entities, True)
async_add_entities(
LutronCasetaCover(cover_device, bridge, bridge_device)
for cover_device in cover_devices
)
class LutronCasetaCover(LutronCasetaDevice, CoverEntity):
class LutronCasetaCover(LutronCasetaDeviceUpdatableEntity, CoverEntity):
"""Representation of a Lutron shade."""
_attr_supported_features = (
@ -50,6 +44,7 @@ class LutronCasetaCover(LutronCasetaDevice, CoverEntity):
| CoverEntityFeature.STOP
| CoverEntityFeature.SET_POSITION
)
_attr_device_class = CoverDeviceClass.SHADE
@property
def is_closed(self):
@ -61,11 +56,6 @@ class LutronCasetaCover(LutronCasetaDevice, CoverEntity):
"""Return the current position of cover."""
return self._device["current_state"]
@property
def device_class(self):
"""Return the device class."""
return CoverDeviceClass.SHADE
async def async_stop_cover(self, **kwargs):
"""Top the cover."""
await self._smartbridge.stop_cover(self.device_id)
@ -87,8 +77,3 @@ class LutronCasetaCover(LutronCasetaDevice, CoverEntity):
if ATTR_POSITION in kwargs:
position = kwargs[ATTR_POSITION]
await self._smartbridge.set_value(self.device_id, position)
async def async_update(self):
"""Call when forcing a refresh of the device."""
self._device = self._smartbridge.get_device_by_id(self.device_id)
_LOGGER.debug(self._device)

View File

@ -1,8 +1,6 @@
"""Support for Lutron Caseta fans."""
from __future__ import annotations
import logging
from pylutron_caseta import FAN_HIGH, FAN_LOW, FAN_MEDIUM, FAN_MEDIUM_HIGH, FAN_OFF
from homeassistant.components.fan import DOMAIN, FanEntity, FanEntityFeature
@ -14,11 +12,9 @@ from homeassistant.util.percentage import (
percentage_to_ordered_list_item,
)
from . import LutronCasetaDevice
from . import LutronCasetaDeviceUpdatableEntity
from .const import BRIDGE_DEVICE, BRIDGE_LEAP, DOMAIN as CASETA_DOMAIN
_LOGGER = logging.getLogger(__name__)
DEFAULT_ON_PERCENTAGE = 50
ORDERED_NAMED_FAN_SPEEDS = [FAN_LOW, FAN_MEDIUM, FAN_MEDIUM_HIGH, FAN_HIGH]
@ -33,23 +29,20 @@ async def async_setup_entry(
Adds fan controllers from the Caseta bridge associated with the config_entry
as fan entities.
"""
entities = []
data = hass.data[CASETA_DOMAIN][config_entry.entry_id]
bridge = data[BRIDGE_LEAP]
bridge_device = data[BRIDGE_DEVICE]
fan_devices = bridge.get_devices_by_domain(DOMAIN)
for fan_device in fan_devices:
entity = LutronCasetaFan(fan_device, bridge, bridge_device)
entities.append(entity)
async_add_entities(entities, True)
async_add_entities(
LutronCasetaFan(fan_device, bridge, bridge_device) for fan_device in fan_devices
)
class LutronCasetaFan(LutronCasetaDevice, FanEntity):
class LutronCasetaFan(LutronCasetaDeviceUpdatableEntity, FanEntity):
"""Representation of a Lutron Caseta fan. Including Fan Speed."""
_attr_supported_features = FanEntityFeature.SET_SPEED
_attr_speed_count = len(ORDERED_NAMED_FAN_SPEEDS)
@property
def percentage(self) -> int | None:
@ -62,11 +55,6 @@ class LutronCasetaFan(LutronCasetaDevice, FanEntity):
ORDERED_NAMED_FAN_SPEEDS, self._device["fan_speed"]
)
@property
def speed_count(self) -> int:
"""Return the number of speeds the fan supports."""
return len(ORDERED_NAMED_FAN_SPEEDS)
async def async_turn_on(
self,
percentage: int = None,
@ -98,8 +86,3 @@ class LutronCasetaFan(LutronCasetaDevice, FanEntity):
def is_on(self):
"""Return true if device is on."""
return self.percentage and self.percentage > 0
async def async_update(self):
"""Update when forcing a refresh of the device."""
self._device = self._smartbridge.get_device_by_id(self.device_id)
_LOGGER.debug("State of this lutron fan device is %s", self._device)

View File

@ -1,6 +1,5 @@
"""Support for Lutron Caseta lights."""
from datetime import timedelta
import logging
from homeassistant.components.light import (
ATTR_BRIGHTNESS,
@ -14,11 +13,9 @@ from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import LutronCasetaDevice
from . import LutronCasetaDeviceUpdatableEntity
from .const import BRIDGE_DEVICE, BRIDGE_LEAP, DOMAIN as CASETA_DOMAIN
_LOGGER = logging.getLogger(__name__)
def to_lutron_level(level):
"""Convert the given Home Assistant light level (0-255) to Lutron (0-100)."""
@ -40,20 +37,17 @@ async def async_setup_entry(
Adds dimmers from the Caseta bridge associated with the config_entry as
light entities.
"""
entities = []
data = hass.data[CASETA_DOMAIN][config_entry.entry_id]
bridge = data[BRIDGE_LEAP]
bridge_device = data[BRIDGE_DEVICE]
light_devices = bridge.get_devices_by_domain(DOMAIN)
for light_device in light_devices:
entity = LutronCasetaLight(light_device, bridge, bridge_device)
entities.append(entity)
async_add_entities(entities, True)
async_add_entities(
LutronCasetaLight(light_device, bridge, bridge_device)
for light_device in light_devices
)
class LutronCasetaLight(LutronCasetaDevice, LightEntity):
class LutronCasetaLight(LutronCasetaDeviceUpdatableEntity, LightEntity):
"""Representation of a Lutron Light, including dimmable."""
_attr_color_mode = ColorMode.BRIGHTNESS
@ -88,8 +82,3 @@ class LutronCasetaLight(LutronCasetaDevice, LightEntity):
def is_on(self):
"""Return true if device is on."""
return self._device["current_state"] > 0
async def async_update(self):
"""Call when forcing a refresh of the device."""
self._device = self._smartbridge.get_device_by_id(self.device_id)
_LOGGER.debug(self._device)

View File

@ -19,16 +19,10 @@ async def async_setup_entry(
Adds scenes from the Caseta bridge associated with the config_entry as
scene entities.
"""
entities = []
data = hass.data[CASETA_DOMAIN][config_entry.entry_id]
bridge = data[BRIDGE_LEAP]
scenes = bridge.get_scenes()
for scene in scenes:
entity = LutronCasetaScene(scenes[scene], bridge)
entities.append(entity)
async_add_entities(entities, True)
async_add_entities(LutronCasetaScene(scenes[scene], bridge) for scene in scenes)
class LutronCasetaScene(Scene):

View File

@ -1,16 +1,13 @@
"""Support for Lutron Caseta switches."""
import logging
from homeassistant.components.switch import DOMAIN, SwitchEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import LutronCasetaDevice
from . import LutronCasetaDeviceUpdatableEntity
from .const import BRIDGE_DEVICE, BRIDGE_LEAP, DOMAIN as CASETA_DOMAIN
_LOGGER = logging.getLogger(__name__)
async def async_setup_entry(
hass: HomeAssistant,
@ -22,21 +19,17 @@ async def async_setup_entry(
Adds switches from the Caseta bridge associated with the config_entry as
switch entities.
"""
entities = []
data = hass.data[CASETA_DOMAIN][config_entry.entry_id]
bridge = data[BRIDGE_LEAP]
bridge_device = data[BRIDGE_DEVICE]
switch_devices = bridge.get_devices_by_domain(DOMAIN)
for switch_device in switch_devices:
entity = LutronCasetaLight(switch_device, bridge, bridge_device)
entities.append(entity)
async_add_entities(entities, True)
return True
async_add_entities(
LutronCasetaLight(switch_device, bridge, bridge_device)
for switch_device in switch_devices
)
class LutronCasetaLight(LutronCasetaDevice, SwitchEntity):
class LutronCasetaLight(LutronCasetaDeviceUpdatableEntity, SwitchEntity):
"""Representation of a Lutron Caseta switch."""
async def async_turn_on(self, **kwargs):
@ -51,8 +44,3 @@ class LutronCasetaLight(LutronCasetaDevice, SwitchEntity):
def is_on(self):
"""Return true if device is on."""
return self._device["current_state"] > 0
async def async_update(self):
"""Update when forcing a refresh of the device."""
self._device = self._smartbridge.get_device_by_id(self.device_id)
_LOGGER.debug(self._device)