-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
restructure, add entity APIs back and remove duplicate models
- Loading branch information
Showing
64 changed files
with
4,990 additions
and
1,973 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
tests/data/serialization_data/centralite-3320-l-extended-device-info.json
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
"""Websocket tests modules.""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
"""Test zhaws binary sensor.""" | ||
|
||
from collections.abc import Awaitable, Callable | ||
from typing import Optional | ||
|
||
import pytest | ||
import zigpy.profiles.zha | ||
from zigpy.zcl.clusters import general, measurement, security | ||
|
||
from zha.application.discovery import Platform | ||
from zha.application.platforms.model import BasePlatformEntity, BinarySensorEntity | ||
from zha.websocket.client.controller import Controller | ||
from zha.websocket.client.proxy import DeviceProxy | ||
from zha.websocket.server.gateway import WebSocketGateway as Server | ||
|
||
from ..common import ( | ||
SIG_EP_INPUT, | ||
SIG_EP_OUTPUT, | ||
SIG_EP_PROFILE, | ||
SIG_EP_TYPE, | ||
create_mock_zigpy_device, | ||
join_zigpy_device, | ||
send_attributes_report, | ||
update_attribute_cache, | ||
) | ||
|
||
|
||
def find_entity( | ||
device_proxy: DeviceProxy, platform: Platform | ||
) -> Optional[BasePlatformEntity]: | ||
"""Find an entity for the specified platform on the given device.""" | ||
for entity in device_proxy.device_model.entities.values(): | ||
if entity.platform == platform: | ||
return entity | ||
return None | ||
|
||
|
||
DEVICE_IAS = { | ||
1: { | ||
SIG_EP_PROFILE: zigpy.profiles.zha.PROFILE_ID, | ||
SIG_EP_TYPE: zigpy.profiles.zha.DeviceType.IAS_ZONE, | ||
SIG_EP_INPUT: [security.IasZone.cluster_id], | ||
SIG_EP_OUTPUT: [], | ||
} | ||
} | ||
|
||
|
||
DEVICE_OCCUPANCY = { | ||
1: { | ||
SIG_EP_PROFILE: zigpy.profiles.zha.PROFILE_ID, | ||
SIG_EP_TYPE: zigpy.profiles.zha.DeviceType.OCCUPANCY_SENSOR, | ||
SIG_EP_INPUT: [measurement.OccupancySensing.cluster_id], | ||
SIG_EP_OUTPUT: [], | ||
} | ||
} | ||
|
||
|
||
async def async_test_binary_sensor_on_off( | ||
server: Server, cluster: general.OnOff, entity: BinarySensorEntity | ||
) -> None: | ||
"""Test getting on and off messages for binary sensors.""" | ||
# binary sensor on | ||
await send_attributes_report(server, cluster, {1: 0, 0: 1, 2: 2}) | ||
assert entity.state.state is True | ||
|
||
# binary sensor off | ||
await send_attributes_report(server, cluster, {1: 1, 0: 0, 2: 2}) | ||
assert entity.state.state is False | ||
|
||
|
||
async def async_test_iaszone_on_off( | ||
server: Server, cluster: security.IasZone, entity: BinarySensorEntity | ||
) -> None: | ||
"""Test getting on and off messages for iaszone binary sensors.""" | ||
# binary sensor on | ||
cluster.listener_event("cluster_command", 1, 0, [1]) | ||
await server.async_block_till_done() | ||
assert entity.state.state is True | ||
|
||
# binary sensor off | ||
cluster.listener_event("cluster_command", 1, 0, [0]) | ||
await server.async_block_till_done() | ||
assert entity.state.state is False | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"device, on_off_test, cluster_name, reporting", | ||
[ | ||
(DEVICE_IAS, async_test_iaszone_on_off, "ias_zone", (0,)), | ||
(DEVICE_OCCUPANCY, async_test_binary_sensor_on_off, "occupancy", (1,)), | ||
], | ||
) | ||
async def test_binary_sensor( | ||
connected_client_and_server: tuple[Controller, Server], | ||
device: dict, | ||
on_off_test: Callable[..., Awaitable[None]], | ||
cluster_name: str, | ||
reporting: tuple, | ||
) -> None: | ||
"""Test ZHA binary_sensor platform.""" | ||
controller, server = connected_client_and_server | ||
zigpy_device = create_mock_zigpy_device(server, device) | ||
zhaws_device = await join_zigpy_device(server, zigpy_device) | ||
|
||
await server.async_block_till_done() | ||
|
||
client_device: Optional[DeviceProxy] = controller.devices.get(zhaws_device.ieee) | ||
assert client_device is not None | ||
entity: BinarySensorEntity = find_entity(client_device, Platform.BINARY_SENSOR) # type: ignore | ||
assert entity is not None | ||
assert isinstance(entity, BinarySensorEntity) | ||
assert entity.state.state is False | ||
|
||
# test getting messages that trigger and reset the sensors | ||
cluster = getattr(zigpy_device.endpoints[1], cluster_name) | ||
await on_off_test(server, cluster, entity) | ||
|
||
# test refresh | ||
if cluster_name == "ias_zone": | ||
cluster.PLUGGED_ATTR_READS = {"zone_status": 0} | ||
update_attribute_cache(cluster) | ||
await controller.entities.refresh_state(entity) | ||
await server.async_block_till_done() | ||
assert entity.state.state is False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
"""Test ZHA button.""" | ||
|
||
from typing import Optional | ||
from unittest.mock import patch | ||
|
||
from zigpy.const import SIG_EP_PROFILE | ||
from zigpy.profiles import zha | ||
from zigpy.zcl.clusters import general, security | ||
import zigpy.zcl.foundation as zcl_f | ||
|
||
from zha.application.discovery import Platform | ||
from zha.application.platforms.model import BasePlatformEntity, ButtonEntity | ||
from zha.websocket.client.controller import Controller | ||
from zha.websocket.client.proxy import DeviceProxy | ||
from zha.websocket.server.gateway import WebSocketGateway as Server | ||
|
||
from ..common import ( | ||
SIG_EP_INPUT, | ||
SIG_EP_OUTPUT, | ||
SIG_EP_TYPE, | ||
create_mock_zigpy_device, | ||
join_zigpy_device, | ||
mock_coro, | ||
) | ||
|
||
|
||
def find_entity( | ||
device_proxy: DeviceProxy, platform: Platform | ||
) -> Optional[BasePlatformEntity]: | ||
"""Find an entity for the specified platform on the given device.""" | ||
for entity in device_proxy.device_model.entities.values(): | ||
if entity.platform == platform: | ||
return entity | ||
return None | ||
|
||
|
||
async def test_button( | ||
connected_client_and_server: tuple[Controller, Server], | ||
) -> None: | ||
"""Test zha button platform.""" | ||
controller, server = connected_client_and_server | ||
zigpy_device = create_mock_zigpy_device( | ||
server, | ||
{ | ||
1: { | ||
SIG_EP_INPUT: [ | ||
general.Basic.cluster_id, | ||
general.Identify.cluster_id, | ||
security.IasZone.cluster_id, | ||
], | ||
SIG_EP_OUTPUT: [], | ||
SIG_EP_TYPE: zha.DeviceType.IAS_ZONE, | ||
SIG_EP_PROFILE: zha.PROFILE_ID, | ||
} | ||
}, | ||
) | ||
zhaws_device = await join_zigpy_device(server, zigpy_device) | ||
cluster = zigpy_device.endpoints[1].identify | ||
|
||
assert cluster is not None | ||
client_device: Optional[DeviceProxy] = controller.devices.get(zhaws_device.ieee) | ||
assert client_device is not None | ||
entity: ButtonEntity = find_entity(client_device, Platform.BUTTON) # type: ignore | ||
assert entity is not None | ||
assert isinstance(entity, ButtonEntity) | ||
|
||
with patch( | ||
"zigpy.zcl.Cluster.request", | ||
return_value=mock_coro([0x00, zcl_f.Status.SUCCESS]), | ||
): | ||
await controller.buttons.press(entity) | ||
await server.async_block_till_done() | ||
assert len(cluster.request.mock_calls) == 1 | ||
assert cluster.request.call_args[0][0] is False | ||
assert cluster.request.call_args[0][1] == 0 | ||
assert cluster.request.call_args[0][3] == 5 # duration in seconds |
Oops, something went wrong.