1
mirror of https://github.com/home-assistant/core synced 2024-08-02 23:40:32 +02:00
ha-core/homeassistant/components/zwave/switch.py
epenet f456f68dff
Add switch setup type hints [s-z] (#63305)
Co-authored-by: epenet <epenet@users.noreply.github.com>
2022-01-03 16:44:20 +01:00

65 lines
2.0 KiB
Python

"""Support for Z-Wave switches."""
import time
from homeassistant.components.switch import DOMAIN, SwitchEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import ZWaveDeviceEntity, workaround
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""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)
def get_device(values, **kwargs):
"""Create zwave entity device."""
return ZwaveSwitch(values)
class ZwaveSwitch(ZWaveDeviceEntity, SwitchEntity):
"""Representation of a Z-Wave switch."""
def __init__(self, values):
"""Initialize the Z-Wave switch device."""
ZWaveDeviceEntity.__init__(self, values, DOMAIN)
self.refresh_on_update = (
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
if self.refresh_on_update and time.perf_counter() - self.last_update > 30:
self.last_update = time.perf_counter()
self.node.request_state()
@property
def is_on(self):
"""Return true if device is on."""
return self._state
def turn_on(self, **kwargs):
"""Turn the device on."""
self.node.set_switch(self.values.primary.value_id, True)
def turn_off(self, **kwargs):
"""Turn the device off."""
self.node.set_switch(self.values.primary.value_id, False)