2021-01-22 09:51:39 -07:00
|
|
|
"""Support for Z-Wave cover devices."""
|
2021-03-18 15:08:35 +01:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-10-28 22:30:34 +02:00
|
|
|
from typing import Any, cast
|
2021-01-22 09:51:39 -07:00
|
|
|
|
|
|
|
from zwave_js_server.client import Client as ZwaveClient
|
2021-09-29 20:21:53 -04:00
|
|
|
from zwave_js_server.const import TARGET_STATE_PROPERTY, TARGET_VALUE_PROPERTY
|
2021-09-06 22:37:12 +02:00
|
|
|
from zwave_js_server.const.command_class.barrier_operator import BarrierState
|
2021-09-29 20:21:53 -04:00
|
|
|
from zwave_js_server.const.command_class.multilevel_switch import (
|
|
|
|
COVER_ON_PROPERTY,
|
|
|
|
COVER_OPEN_PROPERTY,
|
|
|
|
COVER_UP_PROPERTY,
|
|
|
|
)
|
2022-05-24 23:52:07 +02:00
|
|
|
from zwave_js_server.model.driver import Driver
|
2021-02-14 04:24:29 -08:00
|
|
|
from zwave_js_server.model.value import Value as ZwaveValue
|
2021-01-22 09:51:39 -07:00
|
|
|
|
|
|
|
from homeassistant.components.cover import (
|
|
|
|
ATTR_POSITION,
|
2021-10-28 22:30:34 +02:00
|
|
|
ATTR_TILT_POSITION,
|
2021-01-22 09:51:39 -07:00
|
|
|
DOMAIN as COVER_DOMAIN,
|
2021-12-19 06:28:09 -05:00
|
|
|
CoverDeviceClass,
|
2021-01-22 09:51:39 -07:00
|
|
|
CoverEntity,
|
2022-04-07 08:21:31 +02:00
|
|
|
CoverEntityFeature,
|
2021-01-22 09:51:39 -07:00
|
|
|
)
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import HomeAssistant, callback
|
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
2021-04-29 11:28:14 +01:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2021-01-22 09:51:39 -07:00
|
|
|
|
2021-07-13 04:31:49 -04:00
|
|
|
from .const import DATA_CLIENT, DOMAIN
|
2021-01-22 09:51:39 -07:00
|
|
|
from .discovery import ZwaveDiscoveryInfo
|
2021-10-28 22:30:34 +02:00
|
|
|
from .discovery_data_template import CoverTiltDataTemplate
|
2021-01-22 09:51:39 -07:00
|
|
|
from .entity import ZWaveBaseEntity
|
|
|
|
|
2022-05-11 23:51:10 -04:00
|
|
|
PARALLEL_UPDATES = 0
|
|
|
|
|
2021-01-22 09:51:39 -07:00
|
|
|
|
|
|
|
async def async_setup_entry(
|
2021-04-29 11:28:14 +01:00
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
2021-01-22 09:51:39 -07:00
|
|
|
) -> None:
|
|
|
|
"""Set up Z-Wave Cover from Config Entry."""
|
|
|
|
client: ZwaveClient = hass.data[DOMAIN][config_entry.entry_id][DATA_CLIENT]
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def async_add_cover(info: ZwaveDiscoveryInfo) -> None:
|
|
|
|
"""Add Z-Wave cover."""
|
2022-05-24 23:52:07 +02:00
|
|
|
driver = client.driver
|
|
|
|
assert driver is not None # Driver is ready before platforms are loaded.
|
2021-03-18 15:08:35 +01:00
|
|
|
entities: list[ZWaveBaseEntity] = []
|
2021-02-14 04:24:29 -08:00
|
|
|
if info.platform_hint == "motorized_barrier":
|
2022-05-24 23:52:07 +02:00
|
|
|
entities.append(ZwaveMotorizedBarrier(config_entry, driver, info))
|
2023-05-02 06:18:19 -04:00
|
|
|
elif info.platform_hint and info.platform_hint.endswith("tilt"):
|
2022-05-24 23:52:07 +02:00
|
|
|
entities.append(ZWaveTiltCover(config_entry, driver, info))
|
2021-02-14 04:24:29 -08:00
|
|
|
else:
|
2022-05-24 23:52:07 +02:00
|
|
|
entities.append(ZWaveCover(config_entry, driver, info))
|
2021-01-22 09:51:39 -07:00
|
|
|
async_add_entities(entities)
|
|
|
|
|
2021-07-13 04:31:49 -04:00
|
|
|
config_entry.async_on_unload(
|
2021-01-22 09:51:39 -07:00
|
|
|
async_dispatcher_connect(
|
|
|
|
hass,
|
|
|
|
f"{DOMAIN}_{config_entry.entry_id}_add_{COVER_DOMAIN}",
|
|
|
|
async_add_cover,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def percent_to_zwave_position(value: int) -> int:
|
|
|
|
"""Convert position in 0-100 scale to 0-99 scale.
|
|
|
|
|
|
|
|
`value` -- (int) Position byte value from 0-100.
|
|
|
|
"""
|
|
|
|
if value > 0:
|
|
|
|
return max(1, round((value / 100) * 99))
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
2021-10-28 22:30:34 +02:00
|
|
|
def percent_to_zwave_tilt(value: int) -> int:
|
|
|
|
"""Convert position in 0-100 scale to 0-99 scale.
|
|
|
|
|
|
|
|
`value` -- (int) Position byte value from 0-100.
|
|
|
|
"""
|
|
|
|
if value > 0:
|
|
|
|
return round((value / 100) * 99)
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
|
|
def zwave_tilt_to_percent(value: int) -> int:
|
|
|
|
"""Convert 0-99 scale to position in 0-100 scale.
|
|
|
|
|
|
|
|
`value` -- (int) Position byte value from 0-99.
|
|
|
|
"""
|
|
|
|
if value > 0:
|
|
|
|
return round((value / 99) * 100)
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
2021-01-22 09:51:39 -07:00
|
|
|
class ZWaveCover(ZWaveBaseEntity, CoverEntity):
|
|
|
|
"""Representation of a Z-Wave Cover device."""
|
|
|
|
|
2023-05-02 06:18:19 -04:00
|
|
|
_attr_supported_features = (
|
|
|
|
CoverEntityFeature.OPEN
|
|
|
|
| CoverEntityFeature.CLOSE
|
|
|
|
| CoverEntityFeature.SET_POSITION
|
|
|
|
)
|
|
|
|
|
2021-06-01 04:26:22 -04:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
config_entry: ConfigEntry,
|
2022-05-24 23:52:07 +02:00
|
|
|
driver: Driver,
|
2021-06-01 04:26:22 -04:00
|
|
|
info: ZwaveDiscoveryInfo,
|
|
|
|
) -> None:
|
|
|
|
"""Initialize a ZWaveCover entity."""
|
2022-05-24 23:52:07 +02:00
|
|
|
super().__init__(config_entry, driver, info)
|
2021-06-01 04:26:22 -04:00
|
|
|
|
2023-05-02 06:18:19 -04:00
|
|
|
self._stop_cover_value = (
|
|
|
|
self.get_zwave_value(COVER_OPEN_PROPERTY)
|
|
|
|
or self.get_zwave_value(COVER_UP_PROPERTY)
|
|
|
|
or self.get_zwave_value(COVER_ON_PROPERTY)
|
|
|
|
)
|
|
|
|
|
|
|
|
if self._stop_cover_value:
|
|
|
|
self._attr_supported_features |= CoverEntityFeature.STOP
|
|
|
|
|
2021-06-01 04:26:22 -04:00
|
|
|
# Entity class attributes
|
2021-12-19 06:28:09 -05:00
|
|
|
self._attr_device_class = CoverDeviceClass.WINDOW
|
2023-05-02 06:18:19 -04:00
|
|
|
if self.info.platform_hint and self.info.platform_hint.startswith("shutter"):
|
2021-12-19 06:28:09 -05:00
|
|
|
self._attr_device_class = CoverDeviceClass.SHUTTER
|
2023-05-02 06:18:19 -04:00
|
|
|
if self.info.platform_hint and self.info.platform_hint.startswith("blind"):
|
2021-12-19 06:28:09 -05:00
|
|
|
self._attr_device_class = CoverDeviceClass.BLIND
|
2021-05-16 09:26:16 +03:00
|
|
|
|
2021-01-22 09:51:39 -07:00
|
|
|
@property
|
2021-03-18 15:08:35 +01:00
|
|
|
def is_closed(self) -> bool | None:
|
2021-01-22 09:51:39 -07:00
|
|
|
"""Return true if cover is closed."""
|
2021-02-06 14:02:03 +01:00
|
|
|
if self.info.primary_value.value is None:
|
|
|
|
# guard missing value
|
|
|
|
return None
|
2021-01-22 09:51:39 -07:00
|
|
|
return bool(self.info.primary_value.value == 0)
|
|
|
|
|
|
|
|
@property
|
2021-03-18 15:08:35 +01:00
|
|
|
def current_cover_position(self) -> int | None:
|
2021-01-22 09:51:39 -07:00
|
|
|
"""Return the current position of cover where 0 means closed and 100 is fully open."""
|
2021-02-06 14:02:03 +01:00
|
|
|
if self.info.primary_value.value is None:
|
|
|
|
# guard missing value
|
|
|
|
return None
|
2022-11-21 01:29:55 +01:00
|
|
|
return round((cast(int, self.info.primary_value.value) / 99) * 100)
|
2021-01-22 09:51:39 -07:00
|
|
|
|
|
|
|
async def async_set_cover_position(self, **kwargs: Any) -> None:
|
|
|
|
"""Move the cover to a specific position."""
|
2021-09-29 20:21:53 -04:00
|
|
|
target_value = self.get_zwave_value(TARGET_VALUE_PROPERTY)
|
2022-10-18 04:06:29 -04:00
|
|
|
assert target_value is not None
|
2021-01-22 09:51:39 -07:00
|
|
|
await self.info.node.async_set_value(
|
|
|
|
target_value, percent_to_zwave_position(kwargs[ATTR_POSITION])
|
|
|
|
)
|
|
|
|
|
|
|
|
async def async_open_cover(self, **kwargs: Any) -> None:
|
|
|
|
"""Open the cover."""
|
2021-09-29 20:21:53 -04:00
|
|
|
target_value = self.get_zwave_value(TARGET_VALUE_PROPERTY)
|
2022-10-18 04:06:29 -04:00
|
|
|
assert target_value is not None
|
2021-02-05 02:48:47 -07:00
|
|
|
await self.info.node.async_set_value(target_value, 99)
|
2021-01-22 09:51:39 -07:00
|
|
|
|
|
|
|
async def async_close_cover(self, **kwargs: Any) -> None:
|
|
|
|
"""Close cover."""
|
2021-09-29 20:21:53 -04:00
|
|
|
target_value = self.get_zwave_value(TARGET_VALUE_PROPERTY)
|
2022-10-18 04:06:29 -04:00
|
|
|
assert target_value is not None
|
2021-02-05 02:48:47 -07:00
|
|
|
await self.info.node.async_set_value(target_value, 0)
|
2021-02-01 08:46:36 -07:00
|
|
|
|
|
|
|
async def async_stop_cover(self, **kwargs: Any) -> None:
|
|
|
|
"""Stop cover."""
|
2023-05-02 06:18:19 -04:00
|
|
|
assert self._stop_cover_value
|
|
|
|
# Stop the cover, will stop regardless of the actual direction of travel.
|
|
|
|
await self.info.node.async_set_value(self._stop_cover_value, False)
|
2021-02-14 04:24:29 -08:00
|
|
|
|
|
|
|
|
2021-10-28 22:30:34 +02:00
|
|
|
class ZWaveTiltCover(ZWaveCover):
|
2023-05-02 06:18:19 -04:00
|
|
|
"""Representation of a Z-Wave cover device with tilt."""
|
2021-10-28 22:30:34 +02:00
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
config_entry: ConfigEntry,
|
2022-05-24 23:52:07 +02:00
|
|
|
driver: Driver,
|
2021-10-28 22:30:34 +02:00
|
|
|
info: ZwaveDiscoveryInfo,
|
|
|
|
) -> None:
|
|
|
|
"""Initialize a ZWaveCover entity."""
|
2022-05-24 23:52:07 +02:00
|
|
|
super().__init__(config_entry, driver, info)
|
2023-05-02 06:18:19 -04:00
|
|
|
|
|
|
|
self._current_tilt_value = cast(
|
2021-10-28 22:30:34 +02:00
|
|
|
CoverTiltDataTemplate, self.info.platform_data_template
|
2023-05-02 06:18:19 -04:00
|
|
|
).current_tilt_value(self.info.platform_data)
|
|
|
|
|
|
|
|
self._attr_supported_features |= (
|
|
|
|
CoverEntityFeature.OPEN_TILT
|
|
|
|
| CoverEntityFeature.CLOSE_TILT
|
|
|
|
| CoverEntityFeature.SET_TILT_POSITION
|
2021-10-28 22:30:34 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def current_cover_tilt_position(self) -> int | None:
|
|
|
|
"""Return current position of cover tilt.
|
|
|
|
|
|
|
|
None is unknown, 0 is closed, 100 is fully open.
|
|
|
|
"""
|
2023-05-02 06:18:19 -04:00
|
|
|
value = self._current_tilt_value
|
2022-05-25 01:23:34 +02:00
|
|
|
if value is None or value.value is None:
|
|
|
|
return None
|
|
|
|
return zwave_tilt_to_percent(int(value.value))
|
2021-10-28 22:30:34 +02:00
|
|
|
|
|
|
|
async def async_set_cover_tilt_position(self, **kwargs: Any) -> None:
|
|
|
|
"""Move the cover tilt to a specific position."""
|
2023-05-02 06:18:19 -04:00
|
|
|
assert self._current_tilt_value
|
|
|
|
await self.info.node.async_set_value(
|
|
|
|
self._current_tilt_value,
|
|
|
|
percent_to_zwave_tilt(kwargs[ATTR_TILT_POSITION]),
|
|
|
|
)
|
2021-10-28 22:30:34 +02:00
|
|
|
|
|
|
|
async def async_open_cover_tilt(self, **kwargs: Any) -> None:
|
|
|
|
"""Open the cover tilt."""
|
|
|
|
await self.async_set_cover_tilt_position(tilt_position=100)
|
|
|
|
|
|
|
|
async def async_close_cover_tilt(self, **kwargs: Any) -> None:
|
|
|
|
"""Close the cover tilt."""
|
|
|
|
await self.async_set_cover_tilt_position(tilt_position=0)
|
|
|
|
|
|
|
|
|
2021-02-14 04:24:29 -08:00
|
|
|
class ZwaveMotorizedBarrier(ZWaveBaseEntity, CoverEntity):
|
|
|
|
"""Representation of a Z-Wave motorized barrier device."""
|
|
|
|
|
2022-04-07 08:21:31 +02:00
|
|
|
_attr_supported_features = CoverEntityFeature.OPEN | CoverEntityFeature.CLOSE
|
2021-12-19 06:28:09 -05:00
|
|
|
_attr_device_class = CoverDeviceClass.GARAGE
|
2021-06-01 04:26:22 -04:00
|
|
|
|
2021-02-14 04:24:29 -08:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
config_entry: ConfigEntry,
|
2022-05-24 23:52:07 +02:00
|
|
|
driver: Driver,
|
2021-02-14 04:24:29 -08:00
|
|
|
info: ZwaveDiscoveryInfo,
|
|
|
|
) -> None:
|
|
|
|
"""Initialize a ZwaveMotorizedBarrier entity."""
|
2022-05-24 23:52:07 +02:00
|
|
|
super().__init__(config_entry, driver, info)
|
2022-05-25 01:23:34 +02:00
|
|
|
# TARGET_STATE_PROPERTY is required in the discovery schema.
|
|
|
|
self._target_state = cast(
|
|
|
|
ZwaveValue,
|
|
|
|
self.get_zwave_value(TARGET_STATE_PROPERTY, add_to_watched_value_ids=False),
|
2021-02-14 04:24:29 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
@property
|
2021-03-18 15:08:35 +01:00
|
|
|
def is_opening(self) -> bool | None:
|
2021-02-14 04:24:29 -08:00
|
|
|
"""Return if the cover is opening or not."""
|
|
|
|
if self.info.primary_value.value is None:
|
|
|
|
return None
|
2021-07-16 23:54:11 -07:00
|
|
|
return bool(self.info.primary_value.value == BarrierState.OPENING)
|
2021-02-14 04:24:29 -08:00
|
|
|
|
|
|
|
@property
|
2021-03-18 15:08:35 +01:00
|
|
|
def is_closing(self) -> bool | None:
|
2021-02-14 04:24:29 -08:00
|
|
|
"""Return if the cover is closing or not."""
|
|
|
|
if self.info.primary_value.value is None:
|
|
|
|
return None
|
2021-07-16 23:54:11 -07:00
|
|
|
return bool(self.info.primary_value.value == BarrierState.CLOSING)
|
2021-02-14 04:24:29 -08:00
|
|
|
|
|
|
|
@property
|
2021-03-18 15:08:35 +01:00
|
|
|
def is_closed(self) -> bool | None:
|
2021-02-14 04:24:29 -08:00
|
|
|
"""Return if the cover is closed or not."""
|
|
|
|
if self.info.primary_value.value is None:
|
|
|
|
return None
|
|
|
|
# If a barrier is in the stopped state, the only way to proceed is by
|
|
|
|
# issuing an open cover command. Return None in this case which
|
|
|
|
# produces an unknown state and allows it to be resolved with an open
|
|
|
|
# command.
|
2021-07-16 23:54:11 -07:00
|
|
|
if self.info.primary_value.value == BarrierState.STOPPED:
|
2021-02-14 04:24:29 -08:00
|
|
|
return None
|
|
|
|
|
2021-07-16 23:54:11 -07:00
|
|
|
return bool(self.info.primary_value.value == BarrierState.CLOSED)
|
2021-02-14 04:24:29 -08:00
|
|
|
|
|
|
|
async def async_open_cover(self, **kwargs: Any) -> None:
|
|
|
|
"""Open the garage door."""
|
2021-07-16 23:54:11 -07:00
|
|
|
await self.info.node.async_set_value(self._target_state, BarrierState.OPEN)
|
2021-02-14 04:24:29 -08:00
|
|
|
|
|
|
|
async def async_close_cover(self, **kwargs: Any) -> None:
|
|
|
|
"""Close the garage door."""
|
2021-07-16 23:54:11 -07:00
|
|
|
await self.info.node.async_set_value(self._target_state, BarrierState.CLOSED)
|