2019-02-13 20:21:14 +00:00
|
|
|
"""Support for Z-Wave switches."""
|
2017-01-02 17:55:56 +00:00
|
|
|
import logging
|
2017-03-05 20:55:52 +00:00
|
|
|
import time
|
2018-10-16 12:58:25 +00:00
|
|
|
from homeassistant.core import callback
|
2016-02-19 05:27:50 +00:00
|
|
|
from homeassistant.components.switch import DOMAIN, SwitchDevice
|
2018-10-16 12:58:25 +00:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
2019-03-06 01:17:58 +00:00
|
|
|
from . import (
|
|
|
|
ZWaveDeviceEntity,
|
|
|
|
workaround,
|
|
|
|
)
|
2015-11-07 14:52:36 +00:00
|
|
|
|
2017-01-02 17:55:56 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2015-11-07 14:52:36 +00:00
|
|
|
|
2018-10-16 12:58:25 +00:00
|
|
|
async def async_setup_platform(hass, config, async_add_entities,
|
|
|
|
discovery_info=None):
|
|
|
|
"""Old method of setting up Z-Wave switches."""
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
|
|
|
"""Set up Z-Wave Switch from Config Entry."""
|
|
|
|
@callback
|
|
|
|
def async_add_switch(switch):
|
|
|
|
"""Add Z-Wave Switch."""
|
|
|
|
async_add_entities([switch])
|
|
|
|
|
|
|
|
async_dispatcher_connect(hass, 'zwave_new_switch', async_add_switch)
|
|
|
|
|
|
|
|
|
2017-03-14 23:55:33 +00:00
|
|
|
def get_device(values, **kwargs):
|
2017-02-23 21:06:28 +00:00
|
|
|
"""Create zwave entity device."""
|
2017-03-14 23:55:33 +00:00
|
|
|
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."""
|
|
|
|
|
2017-03-14 23:55:33 +00:00
|
|
|
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)
|
2017-03-14 23:55:33 +00:00
|
|
|
self.refresh_on_update = (
|
2019-03-06 01:17:58 +00:00
|
|
|
workaround.get_device_mapping(values.primary) ==
|
|
|
|
workaround.WORKAROUND_REFRESH_NODE_ON_UPDATE)
|
2017-03-05 20:55:52 +00:00
|
|
|
self.last_update = time.perf_counter()
|
2017-03-14 23:55:33 +00:00
|
|
|
self._state = self.values.primary.data
|
2017-02-18 07:56:05 +00:00
|
|
|
|
|
|
|
def update_properties(self):
|
2017-05-02 20:47:20 +00:00
|
|
|
"""Handle data changes for node values."""
|
2017-03-14 23:55:33 +00:00
|
|
|
self._state = self.values.primary.data
|
2017-03-05 20:55:52 +00:00
|
|
|
if self.refresh_on_update and \
|
|
|
|
time.perf_counter() - self.last_update > 30:
|
|
|
|
self.last_update = time.perf_counter()
|
2017-03-14 23:55:33 +00:00
|
|
|
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."""
|
2017-02-18 07:56:05 +00:00
|
|
|
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."""
|
2017-03-14 23:55:33 +00:00
|
|
|
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."""
|
2017-03-14 23:55:33 +00:00
|
|
|
self.node.set_switch(self.values.primary.value_id, False)
|