1
mirror of https://github.com/home-assistant/core synced 2024-07-30 21:18:57 +02:00
ha-core/homeassistant/components/zwave_js/switch.py

139 lines
4.6 KiB
Python

"""Representation of Z-Wave switches."""
from __future__ import annotations
import logging
from typing import Any
from zwave_js_server.client import Client as ZwaveClient
from zwave_js_server.const import TARGET_VALUE_PROPERTY
from zwave_js_server.const.command_class.barrier_operator import (
BarrierEventSignalingSubsystemState,
)
from homeassistant.components.switch import DOMAIN as SWITCH_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 .const import DATA_CLIENT, DOMAIN
from .discovery import ZwaveDiscoveryInfo
from .entity import ZWaveBaseEntity
LOGGER = logging.getLogger(__name__)
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up Z-Wave sensor from config entry."""
client: ZwaveClient = hass.data[DOMAIN][config_entry.entry_id][DATA_CLIENT]
@callback
def async_add_switch(info: ZwaveDiscoveryInfo) -> None:
"""Add Z-Wave Switch."""
entities: list[ZWaveBaseEntity] = []
if info.platform_hint == "barrier_event_signaling_state":
entities.append(
ZWaveBarrierEventSignalingSwitch(config_entry, client, info)
)
else:
entities.append(ZWaveSwitch(config_entry, client, info))
async_add_entities(entities)
config_entry.async_on_unload(
async_dispatcher_connect(
hass,
f"{DOMAIN}_{config_entry.entry_id}_add_{SWITCH_DOMAIN}",
async_add_switch,
)
)
class ZWaveSwitch(ZWaveBaseEntity, SwitchEntity):
"""Representation of a Z-Wave switch."""
def __init__(
self, config_entry: ConfigEntry, client: ZwaveClient, info: ZwaveDiscoveryInfo
) -> None:
"""Initialize the switch."""
super().__init__(config_entry, client, info)
self._target_value = self.get_zwave_value(TARGET_VALUE_PROPERTY)
@property
def is_on(self) -> bool | None: # type: ignore
"""Return a boolean for the state of the switch."""
if self.info.primary_value.value is None:
# guard missing value
return None
return bool(self.info.primary_value.value)
async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn the switch on."""
if self._target_value is not None:
await self.info.node.async_set_value(self._target_value, True)
async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn the switch off."""
if self._target_value is not None:
await self.info.node.async_set_value(self._target_value, False)
class ZWaveBarrierEventSignalingSwitch(ZWaveBaseEntity, SwitchEntity):
"""This switch is used to turn on or off a barrier device's event signaling subsystem."""
def __init__(
self,
config_entry: ConfigEntry,
client: ZwaveClient,
info: ZwaveDiscoveryInfo,
) -> None:
"""Initialize a ZWaveBarrierEventSignalingSwitch entity."""
super().__init__(config_entry, client, info)
self._state: bool | None = None
self._update_state()
# Entity class attributes
self._attr_name = self.generate_name(include_value_name=True)
@callback
def on_value_update(self) -> None:
"""Call when a watched value is added or updated."""
self._update_state()
@property
def is_on(self) -> bool | None: # type: ignore
"""Return a boolean for the state of the switch."""
return self._state
async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn the switch on."""
await self.info.node.async_set_value(
self.info.primary_value, BarrierEventSignalingSubsystemState.ON
)
# this value is not refreshed, so assume success
self._state = True
self.async_write_ha_state()
async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn the switch off."""
await self.info.node.async_set_value(
self.info.primary_value, BarrierEventSignalingSubsystemState.OFF
)
# this value is not refreshed, so assume success
self._state = False
self.async_write_ha_state()
@callback
def _update_state(self) -> None:
self._state = None
if self.info.primary_value.value is not None:
self._state = (
self.info.primary_value.value == BarrierEventSignalingSubsystemState.ON
)