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"]

View File

@ -356,6 +356,12 @@ def aeotec_nano_shutter_state_fixture():
return json.loads(load_fixture("zwave_js/cover_aeotec_nano_shutter_state.json"))
@pytest.fixture(name="fibaro_fgr222_shutter_state", scope="session")
def fibaro_fgr222_shutter_state_fixture():
"""Load the Fibaro FGR222 node state fixture data."""
return json.loads(load_fixture("zwave_js/cover_fibaro_fgr222_state.json"))
@pytest.fixture(name="aeon_smart_switch_6_state", scope="session")
def aeon_smart_switch_6_state_fixture():
"""Load the AEON Labs (ZW096) Smart Switch 6 node state fixture data."""
@ -743,6 +749,14 @@ def aeotec_nano_shutter_cover_fixture(client, aeotec_nano_shutter_state):
return node
@pytest.fixture(name="fibaro_fgr222_shutter")
def fibaro_fgr222_shutter_cover_fixture(client, fibaro_fgr222_shutter_state):
"""Mock a Fibaro FGR222 Shutter node."""
node = Node(client, copy.deepcopy(fibaro_fgr222_shutter_state))
client.driver.controller.nodes[node.node_id] = node
return node
@pytest.fixture(name="aeon_smart_switch_6")
def aeon_smart_switch_6_fixture(client, aeon_smart_switch_6_state):
"""Mock an AEON Labs (ZW096) Smart Switch 6 node."""

View File

@ -3,6 +3,7 @@ from zwave_js_server.event import Event
from homeassistant.components.cover import (
ATTR_CURRENT_POSITION,
ATTR_CURRENT_TILT_POSITION,
DEVICE_CLASS_BLIND,
DEVICE_CLASS_GARAGE,
DEVICE_CLASS_SHUTTER,
@ -25,6 +26,7 @@ GDC_COVER_ENTITY = "cover.aeon_labs_garage_door_controller_gen5"
BLIND_COVER_ENTITY = "cover.window_blind_controller"
SHUTTER_COVER_ENTITY = "cover.flush_shutter"
AEOTEC_SHUTTER_COVER_ENTITY = "cover.nano_shutter_v_3"
FIBARO_SHUTTER_COVER_ENTITY = "cover.fgr_222_test_cover"
async def test_window_cover(hass, client, chain_actuator_zws12, integration):
@ -307,6 +309,85 @@ async def test_window_cover(hass, client, chain_actuator_zws12, integration):
assert state.state == "closed"
async def test_fibaro_FGR222_shutter_cover(
hass, client, fibaro_fgr222_shutter, integration
):
"""Test tilt function of the Fibaro Shutter devices."""
state = hass.states.get(FIBARO_SHUTTER_COVER_ENTITY)
assert state
assert state.attributes[ATTR_DEVICE_CLASS] == DEVICE_CLASS_SHUTTER
assert state.state == "open"
assert state.attributes[ATTR_CURRENT_TILT_POSITION] == 0
# Test opening tilts
await hass.services.async_call(
"cover",
"open_cover_tilt",
{"entity_id": FIBARO_SHUTTER_COVER_ENTITY},
blocking=True,
)
assert len(client.async_send_command.call_args_list) == 1
args = client.async_send_command.call_args[0][0]
assert args["command"] == "node.set_value"
assert args["nodeId"] == 42
assert args["valueId"] == {
"endpoint": 0,
"commandClass": 145,
"commandClassName": "Manufacturer Proprietary",
"property": "fibaro",
"propertyKey": "venetianBlindsTilt",
"propertyName": "fibaro",
"propertyKeyName": "venetianBlindsTilt",
"ccVersion": 0,
"metadata": {
"type": "number",
"readable": True,
"writeable": True,
"label": "Venetian blinds tilt",
"min": 0,
"max": 99,
},
"value": 0,
}
assert args["value"] == 99
client.async_send_command.reset_mock()
# Test closing tilts
await hass.services.async_call(
"cover",
"close_cover_tilt",
{"entity_id": FIBARO_SHUTTER_COVER_ENTITY},
blocking=True,
)
assert len(client.async_send_command.call_args_list) == 1
args = client.async_send_command.call_args[0][0]
assert args["command"] == "node.set_value"
assert args["nodeId"] == 42
assert args["valueId"] == {
"endpoint": 0,
"commandClass": 145,
"commandClassName": "Manufacturer Proprietary",
"property": "fibaro",
"propertyKey": "venetianBlindsTilt",
"propertyName": "fibaro",
"propertyKeyName": "venetianBlindsTilt",
"ccVersion": 0,
"metadata": {
"type": "number",
"readable": True,
"writeable": True,
"label": "Venetian blinds tilt",
"min": 0,
"max": 99,
},
"value": 0,
}
assert args["value"] == 0
async def test_aeotec_nano_shutter_cover(
hass, client, aeotec_nano_shutter, integration
):

File diff suppressed because it is too large Load Diff