Open and close tilt for Fibaro devices in zwave_js (#58435)

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
Paul Frank 2021-10-28 22:30:34 +02:00 committed by GitHub
parent 2b7fe06b16
commit 3705f2f7f1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 1363 additions and 6 deletions

View file

@ -1,8 +1,9 @@
"""Support for Z-Wave cover devices."""
from __future__ import annotations
import asyncio
import logging
from typing import Any
from typing import Any, cast
from zwave_js_server.client import Client as ZwaveClient
from zwave_js_server.const import TARGET_STATE_PROPERTY, TARGET_VALUE_PROPERTY
@ -19,13 +20,19 @@ from zwave_js_server.model.value import Value as ZwaveValue
from homeassistant.components.cover import (
ATTR_POSITION,
ATTR_TILT_POSITION,
DEVICE_CLASS_BLIND,
DEVICE_CLASS_GARAGE,
DEVICE_CLASS_SHUTTER,
DEVICE_CLASS_WINDOW,
DOMAIN as COVER_DOMAIN,
SUPPORT_CLOSE,
SUPPORT_CLOSE_TILT,
SUPPORT_OPEN,
SUPPORT_OPEN_TILT,
SUPPORT_SET_POSITION,
SUPPORT_SET_TILT_POSITION,
SUPPORT_STOP,
CoverEntity,
)
from homeassistant.config_entries import ConfigEntry
@ -35,6 +42,7 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import DATA_CLIENT, DOMAIN
from .discovery import ZwaveDiscoveryInfo
from .discovery_data_template import CoverTiltDataTemplate
from .entity import ZWaveBaseEntity
LOGGER = logging.getLogger(__name__)
@ -54,6 +62,8 @@ async def async_setup_entry(
entities: list[ZWaveBaseEntity] = []
if info.platform_hint == "motorized_barrier":
entities.append(ZwaveMotorizedBarrier(config_entry, client, info))
elif info.platform_hint == "window_shutter_tilt":
entities.append(ZWaveTiltCover(config_entry, client, info))
else:
entities.append(ZWaveCover(config_entry, client, info))
async_add_entities(entities)
@ -77,6 +87,26 @@ def percent_to_zwave_position(value: int) -> int:
return 0
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
class ZWaveCover(ZWaveBaseEntity, CoverEntity):
"""Representation of a Z-Wave Cover device."""
@ -91,7 +121,7 @@ class ZWaveCover(ZWaveBaseEntity, CoverEntity):
# Entity class attributes
self._attr_device_class = DEVICE_CLASS_WINDOW
if self.info.platform_hint == "window_shutter":
if self.info.platform_hint in ("window_shutter", "window_shutter_tilt"):
self._attr_device_class = DEVICE_CLASS_SHUTTER
if self.info.platform_hint == "window_blind":
self._attr_device_class = DEVICE_CLASS_BLIND
@ -150,6 +180,64 @@ class ZWaveCover(ZWaveBaseEntity, CoverEntity):
await self.info.node.async_set_value(close_value, False)
class ZWaveTiltCover(ZWaveCover):
"""Representation of a Fibaro Z-Wave cover device."""
_attr_supported_features = (
SUPPORT_OPEN
| SUPPORT_CLOSE
| SUPPORT_STOP
| SUPPORT_SET_POSITION
| SUPPORT_OPEN_TILT
| SUPPORT_CLOSE_TILT
| SUPPORT_SET_TILT_POSITION
)
def __init__(
self,
config_entry: ConfigEntry,
client: ZwaveClient,
info: ZwaveDiscoveryInfo,
) -> None:
"""Initialize a ZWaveCover entity."""
super().__init__(config_entry, client, info)
self.data_template = cast(
CoverTiltDataTemplate, self.info.platform_data_template
)
@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.
"""
value = self.data_template.current_tilt_value(self.info.platform_data)
return zwave_tilt_to_percent(value.value) if value else None
async def async_set_cover_tilt_position(self, **kwargs: Any) -> None:
"""Move the cover tilt to a specific position."""
tilt_value = self.data_template.current_tilt_value(self.info.platform_data)
if tilt_value:
await self.info.node.async_set_value(
tilt_value,
percent_to_zwave_tilt(kwargs[ATTR_TILT_POSITION]),
)
# The following 2 lines are a workaround for this issue:
# https://github.com/zwave-js/node-zwave-js/issues/3611
# As soon as the issue is fixed, and minimum server schema is bumped
# the 2 lines should be removed.
await asyncio.sleep(2.5)
await self.info.node.async_refresh_cc_values(tilt_value.command_class)
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)
class ZwaveMotorizedBarrier(ZWaveBaseEntity, CoverEntity):
"""Representation of a Z-Wave motorized barrier device."""

View file

@ -44,6 +44,7 @@ from homeassistant.helpers.device_registry import DeviceEntry
from .const import LOGGER
from .discovery_data_template import (
BaseDiscoverySchemaDataTemplate,
CoverTiltDataTemplate,
DynamicCurrentTempClimateDataTemplate,
NumericSensorDataTemplate,
ZwaveValueID,
@ -258,14 +259,29 @@ DISCOVERY_SCHEMAS = [
type={"number"},
),
),
# Fibaro Shutter Fibaro FGS222
# Fibaro Shutter Fibaro FGR222
ZWaveDiscoverySchema(
platform="cover",
hint="window_shutter",
hint="window_shutter_tilt",
manufacturer_id={0x010F},
product_id={0x1000},
product_type={0x0302},
product_id={0x1000, 0x1001},
product_type={0x0301, 0x0302},
primary_value=SWITCH_MULTILEVEL_CURRENT_VALUE_SCHEMA,
data_template=CoverTiltDataTemplate(
tilt_value_id=ZwaveValueID(
"fibaro",
CommandClass.MANUFACTURER_PROPRIETARY,
endpoint=0,
property_key="venetianBlindsTilt",
)
),
required_values=[
ZWaveValueDiscoverySchema(
command_class={CommandClass.MANUFACTURER_PROPRIETARY},
property={"fibaro"},
property_key={"venetianBlindsTilt"},
)
],
),
# Qubino flush shutter
ZWaveDiscoverySchema(

View file

@ -226,3 +226,28 @@ class NumericSensorDataTemplate(BaseDiscoverySchemaDataTemplate):
return key
return None
@dataclass
class TiltValueMix:
"""Mixin data class for the tilt_value."""
tilt_value_id: ZwaveValueID
@dataclass
class CoverTiltDataTemplate(BaseDiscoverySchemaDataTemplate, TiltValueMix):
"""Tilt data template class for Z-Wave Cover entities."""
def resolve_data(self, value: ZwaveValue) -> dict[str, Any]:
"""Resolve helper class data for a discovered value."""
return {"tilt_value": self._get_value_from_id(value.node, self.tilt_value_id)}
def values_to_watch(self, resolved_data: dict[str, Any]) -> Iterable[ZwaveValue]:
"""Return list of all ZwaveValues resolved by helper that should be watched."""
return [resolved_data["tilt_value"]]
@staticmethod
def current_tilt_value(resolved_data: dict[str, Any]) -> ZwaveValue | None:
"""Get current tilt ZwaveValue from resolved data."""
return resolved_data["tilt_value"]