hass-core/homeassistant/components/lutron/switch.py
Dima Zavin 6bf35232b9 Don't force a query to the main lutron repeater on update (#25939)
We only want to force a query if we don't have any previous state.
Otherwise, we should be tracking the state via continuous status
updates.

For lights (not switches) the extra query was also superfluous since
it was already querying on startup.

We should probably have a timeout on that so at some point we'll
requery in case remote end disconnected/rebooted, etc. Leaving for
another PR.
2019-08-17 23:50:15 +02:00

52 lines
1.5 KiB
Python

"""Support for Lutron switches."""
import logging
from homeassistant.components.switch import SwitchDevice
from . import LUTRON_CONTROLLER, LUTRON_DEVICES, LutronDevice
_LOGGER = logging.getLogger(__name__)
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the Lutron switches."""
devs = []
for (area_name, device) in hass.data[LUTRON_DEVICES]["switch"]:
dev = LutronSwitch(area_name, device, hass.data[LUTRON_CONTROLLER])
devs.append(dev)
add_entities(devs, True)
class LutronSwitch(LutronDevice, SwitchDevice):
"""Representation of a Lutron Switch."""
def __init__(self, area_name, lutron_device, controller):
"""Initialize the switch."""
self._prev_state = None
super().__init__(area_name, lutron_device, controller)
def turn_on(self, **kwargs):
"""Turn the switch on."""
self._lutron_device.level = 100
def turn_off(self, **kwargs):
"""Turn the switch off."""
self._lutron_device.level = 0
@property
def device_state_attributes(self):
"""Return the state attributes."""
attr = {}
attr["lutron_integration_id"] = self._lutron_device.id
return attr
@property
def is_on(self):
"""Return true if device is on."""
return self._lutron_device.last_level() > 0
def update(self):
"""Call when forcing a refresh of the device."""
if self._prev_state is None:
self._prev_state = self._lutron_device.level > 0