Update Lutron in callback (#108779)
* Update Lutron in callback * Update Lutron in callback * Remove abstractmethod * Don't do IO in constructor * Split fetching and setting
This commit is contained in:
parent
0c9a30ab69
commit
114bf0da34
6 changed files with 50 additions and 64 deletions
|
@ -52,13 +52,11 @@ class LutronOccupancySensor(LutronDevice, BinarySensorEntity):
|
||||||
_lutron_device: OccupancyGroup
|
_lutron_device: OccupancyGroup
|
||||||
_attr_device_class = BinarySensorDeviceClass.OCCUPANCY
|
_attr_device_class = BinarySensorDeviceClass.OCCUPANCY
|
||||||
|
|
||||||
@property
|
|
||||||
def is_on(self) -> bool:
|
|
||||||
"""Return true if the binary sensor is on."""
|
|
||||||
# Error cases will end up treated as unoccupied.
|
|
||||||
return self._lutron_device.state == OccupancyGroup.State.OCCUPIED
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def extra_state_attributes(self) -> Mapping[str, Any] | None:
|
def extra_state_attributes(self) -> Mapping[str, Any] | None:
|
||||||
"""Return the state attributes."""
|
"""Return the state attributes."""
|
||||||
return {"lutron_integration_id": self._lutron_device.id}
|
return {"lutron_integration_id": self._lutron_device.id}
|
||||||
|
|
||||||
|
def _update_attrs(self) -> None:
|
||||||
|
"""Update the state attributes."""
|
||||||
|
self._attr_is_on = self._lutron_device.state == OccupancyGroup.State.OCCUPIED
|
||||||
|
|
|
@ -53,16 +53,6 @@ class LutronCover(LutronDevice, CoverEntity):
|
||||||
_lutron_device: Output
|
_lutron_device: Output
|
||||||
_attr_name = None
|
_attr_name = None
|
||||||
|
|
||||||
@property
|
|
||||||
def is_closed(self) -> bool:
|
|
||||||
"""Return if the cover is closed."""
|
|
||||||
return self._lutron_device.last_level() < 1
|
|
||||||
|
|
||||||
@property
|
|
||||||
def current_cover_position(self) -> int:
|
|
||||||
"""Return the current position of cover."""
|
|
||||||
return self._lutron_device.last_level()
|
|
||||||
|
|
||||||
def close_cover(self, **kwargs: Any) -> None:
|
def close_cover(self, **kwargs: Any) -> None:
|
||||||
"""Close the cover."""
|
"""Close the cover."""
|
||||||
self._lutron_device.level = 0
|
self._lutron_device.level = 0
|
||||||
|
@ -77,10 +67,15 @@ class LutronCover(LutronDevice, CoverEntity):
|
||||||
position = kwargs[ATTR_POSITION]
|
position = kwargs[ATTR_POSITION]
|
||||||
self._lutron_device.level = position
|
self._lutron_device.level = position
|
||||||
|
|
||||||
def update(self) -> None:
|
def _request_state(self) -> None:
|
||||||
"""Call when forcing a refresh of the device."""
|
"""Request the state from the device."""
|
||||||
# Reading the property (rather than last_level()) fetches value
|
self._lutron_device.level # pylint: disable=pointless-statement
|
||||||
level = self._lutron_device.level
|
|
||||||
|
def _update_attrs(self) -> None:
|
||||||
|
"""Update the state attributes."""
|
||||||
|
level = self._lutron_device.last_level()
|
||||||
|
self._attr_is_closed = level < 1
|
||||||
|
self._attr_current_cover_position = level
|
||||||
_LOGGER.debug("Lutron ID: %d updated to %f", self._lutron_device.id, level)
|
_LOGGER.debug("Lutron ID: %d updated to %f", self._lutron_device.id, level)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
|
|
@ -27,10 +27,17 @@ class LutronBaseEntity(Entity):
|
||||||
"""Register callbacks."""
|
"""Register callbacks."""
|
||||||
self._lutron_device.subscribe(self._update_callback, None)
|
self._lutron_device.subscribe(self._update_callback, None)
|
||||||
|
|
||||||
|
def _request_state(self) -> None:
|
||||||
|
"""Request the state."""
|
||||||
|
|
||||||
|
def _update_attrs(self) -> None:
|
||||||
|
"""Update the entity's attributes."""
|
||||||
|
|
||||||
def _update_callback(
|
def _update_callback(
|
||||||
self, _device: LutronEntity, _context: None, _event: LutronEvent, _params: dict
|
self, _device: LutronEntity, _context: None, _event: LutronEvent, _params: dict
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Run when invoked by pylutron when the device state changes."""
|
"""Run when invoked by pylutron when the device state changes."""
|
||||||
|
self._update_attrs()
|
||||||
self.schedule_update_ha_state()
|
self.schedule_update_ha_state()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
@ -41,6 +48,11 @@ class LutronBaseEntity(Entity):
|
||||||
return None
|
return None
|
||||||
return f"{self._controller.guid}_{self._lutron_device.uuid}"
|
return f"{self._controller.guid}_{self._lutron_device.uuid}"
|
||||||
|
|
||||||
|
def update(self) -> None:
|
||||||
|
"""Update the entity's state."""
|
||||||
|
self._request_state()
|
||||||
|
self._update_attrs()
|
||||||
|
|
||||||
|
|
||||||
class LutronDevice(LutronBaseEntity):
|
class LutronDevice(LutronBaseEntity):
|
||||||
"""Representation of a Lutron device entity."""
|
"""Representation of a Lutron device entity."""
|
||||||
|
|
|
@ -54,14 +54,6 @@ class LutronLight(LutronDevice, LightEntity):
|
||||||
_prev_brightness: int | None = None
|
_prev_brightness: int | None = None
|
||||||
_attr_name = None
|
_attr_name = None
|
||||||
|
|
||||||
@property
|
|
||||||
def brightness(self) -> int:
|
|
||||||
"""Return the brightness of the light."""
|
|
||||||
new_brightness = to_hass_level(self._lutron_device.last_level())
|
|
||||||
if new_brightness != 0:
|
|
||||||
self._prev_brightness = new_brightness
|
|
||||||
return new_brightness
|
|
||||||
|
|
||||||
def turn_on(self, **kwargs: Any) -> None:
|
def turn_on(self, **kwargs: Any) -> None:
|
||||||
"""Turn the light on."""
|
"""Turn the light on."""
|
||||||
if ATTR_BRIGHTNESS in kwargs and self._lutron_device.is_dimmable:
|
if ATTR_BRIGHTNESS in kwargs and self._lutron_device.is_dimmable:
|
||||||
|
@ -82,12 +74,15 @@ class LutronLight(LutronDevice, LightEntity):
|
||||||
"""Return the state attributes."""
|
"""Return the state attributes."""
|
||||||
return {"lutron_integration_id": self._lutron_device.id}
|
return {"lutron_integration_id": self._lutron_device.id}
|
||||||
|
|
||||||
@property
|
def _request_state(self) -> None:
|
||||||
def is_on(self) -> bool:
|
"""Request the state from the device."""
|
||||||
"""Return true if device is on."""
|
self._lutron_device.level # pylint: disable=pointless-statement
|
||||||
return self._lutron_device.last_level() > 0
|
|
||||||
|
|
||||||
def update(self) -> None:
|
def _update_attrs(self) -> None:
|
||||||
"""Call when forcing a refresh of the device."""
|
"""Update the state attributes."""
|
||||||
if self._prev_brightness is None:
|
level = self._lutron_device.last_level()
|
||||||
self._prev_brightness = to_hass_level(self._lutron_device.level)
|
self._attr_is_on = level > 0
|
||||||
|
hass_level = to_hass_level(level)
|
||||||
|
self._attr_brightness = hass_level
|
||||||
|
if self._prev_brightness is None or hass_level != 0:
|
||||||
|
self._prev_brightness = hass_level
|
||||||
|
|
|
@ -27,11 +27,8 @@ async def async_setup_entry(
|
||||||
entry_data: LutronData = hass.data[DOMAIN][config_entry.entry_id]
|
entry_data: LutronData = hass.data[DOMAIN][config_entry.entry_id]
|
||||||
|
|
||||||
async_add_entities(
|
async_add_entities(
|
||||||
[
|
LutronScene(area_name, keypad, device, entry_data.client)
|
||||||
LutronScene(area_name, keypad, device, entry_data.client)
|
for area_name, keypad, device, led in entry_data.scenes
|
||||||
for area_name, keypad, device, led in entry_data.scenes
|
|
||||||
],
|
|
||||||
True,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -44,13 +44,6 @@ class LutronSwitch(LutronDevice, SwitchEntity):
|
||||||
|
|
||||||
_lutron_device: Output
|
_lutron_device: Output
|
||||||
|
|
||||||
def __init__(
|
|
||||||
self, area_name: str, lutron_device: Output, controller: Lutron
|
|
||||||
) -> None:
|
|
||||||
"""Initialize the switch."""
|
|
||||||
self._prev_state = None
|
|
||||||
super().__init__(area_name, lutron_device, controller)
|
|
||||||
|
|
||||||
def turn_on(self, **kwargs: Any) -> None:
|
def turn_on(self, **kwargs: Any) -> None:
|
||||||
"""Turn the switch on."""
|
"""Turn the switch on."""
|
||||||
self._lutron_device.level = 100
|
self._lutron_device.level = 100
|
||||||
|
@ -64,15 +57,13 @@ class LutronSwitch(LutronDevice, SwitchEntity):
|
||||||
"""Return the state attributes."""
|
"""Return the state attributes."""
|
||||||
return {"lutron_integration_id": self._lutron_device.id}
|
return {"lutron_integration_id": self._lutron_device.id}
|
||||||
|
|
||||||
@property
|
def _request_state(self) -> None:
|
||||||
def is_on(self) -> bool:
|
"""Request the state from the device."""
|
||||||
"""Return true if device is on."""
|
self._lutron_device.level # pylint: disable=pointless-statement
|
||||||
return self._lutron_device.last_level() > 0
|
|
||||||
|
|
||||||
def update(self) -> None:
|
def _update_attrs(self) -> None:
|
||||||
"""Call when forcing a refresh of the device."""
|
"""Update the state attributes."""
|
||||||
if self._prev_state is None:
|
self._attr_is_on = self._lutron_device.last_level() > 0
|
||||||
self._prev_state = self._lutron_device.level > 0
|
|
||||||
|
|
||||||
|
|
||||||
class LutronLed(LutronKeypad, SwitchEntity):
|
class LutronLed(LutronKeypad, SwitchEntity):
|
||||||
|
@ -110,12 +101,10 @@ class LutronLed(LutronKeypad, SwitchEntity):
|
||||||
"led": self._lutron_device.name,
|
"led": self._lutron_device.name,
|
||||||
}
|
}
|
||||||
|
|
||||||
@property
|
def _request_state(self) -> None:
|
||||||
def is_on(self) -> bool:
|
"""Request the state from the device."""
|
||||||
"""Return true if device is on."""
|
|
||||||
return self._lutron_device.last_state
|
|
||||||
|
|
||||||
def update(self) -> None:
|
|
||||||
"""Call when forcing a refresh of the device."""
|
|
||||||
# The following property getter actually triggers an update in Lutron
|
|
||||||
self._lutron_device.state # pylint: disable=pointless-statement
|
self._lutron_device.state # pylint: disable=pointless-statement
|
||||||
|
|
||||||
|
def _update_attrs(self) -> None:
|
||||||
|
"""Update the state attributes."""
|
||||||
|
self._attr_is_on = self._lutron_device.last_state
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue