core/homeassistant/components/zwave/switch.py

62 lines
1.9 KiB
Python
Raw Normal View History

"""Support for Z-Wave switches."""
import logging
import time
2016-02-19 05:27:50 +00:00
from homeassistant.components.switch import DOMAIN, SwitchDevice
from homeassistant.core import callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
2019-07-31 19:25:30 +00:00
from . import ZWaveDeviceEntity, workaround
2015-11-07 14:52:36 +00:00
_LOGGER = logging.getLogger(__name__)
2015-11-07 14:52:36 +00:00
async def async_setup_entry(hass, config_entry, async_add_entities):
"""Set up Z-Wave Switch from Config Entry."""
2019-07-31 19:25:30 +00:00
@callback
def async_add_switch(switch):
"""Add Z-Wave Switch."""
async_add_entities([switch])
2019-07-31 19:25:30 +00:00
async_dispatcher_connect(hass, "zwave_new_switch", async_add_switch)
def get_device(values, **kwargs):
"""Create zwave entity device."""
return ZwaveSwitch(values)
2015-11-07 14:52:36 +00:00
2019-03-06 01:17:58 +00:00
class ZwaveSwitch(ZWaveDeviceEntity, SwitchDevice):
2016-03-08 12:35:39 +00:00
"""Representation of a Z-Wave switch."""
def __init__(self, values):
2016-03-08 12:35:39 +00:00
"""Initialize the Z-Wave switch device."""
2019-03-06 01:17:58 +00:00
ZWaveDeviceEntity.__init__(self, values, DOMAIN)
self.refresh_on_update = (
2019-07-31 19:25:30 +00:00
workaround.get_device_mapping(values.primary)
== workaround.WORKAROUND_REFRESH_NODE_ON_UPDATE
)
self.last_update = time.perf_counter()
self._state = self.values.primary.data
def update_properties(self):
"""Handle data changes for node values."""
self._state = self.values.primary.data
2019-07-31 19:25:30 +00:00
if self.refresh_on_update and time.perf_counter() - self.last_update > 30:
self.last_update = time.perf_counter()
self.node.request_state()
2015-11-07 14:52:36 +00:00
@property
def is_on(self):
2016-03-08 12:35:39 +00:00
"""Return true if device is on."""
return self._state
2015-11-07 14:52:36 +00:00
def turn_on(self, **kwargs):
2016-03-08 12:35:39 +00:00
"""Turn the device on."""
self.node.set_switch(self.values.primary.value_id, True)
2015-11-07 14:52:36 +00:00
def turn_off(self, **kwargs):
2016-03-08 12:35:39 +00:00
"""Turn the device off."""
self.node.set_switch(self.values.primary.value_id, False)