From 69af6f3405391f7264e28cd2b6e882e54df8fc31 Mon Sep 17 00:00:00 2001 From: David Mulcahey Date: Sun, 13 Oct 2024 15:01:44 -0400 Subject: [PATCH] fix mypy errors --- examples/client_test.py | 24 +++++-------------- tests/common.py | 3 +++ tests/test_client_controller.py | 18 ++++++-------- tests/test_server_client.py | 4 ++-- zhaws/server/__main__.py | 3 +-- zhaws/server/platforms/__init__.py | 2 +- .../platforms/alarm_control_panel/api.py | 2 +- zhaws/server/websocket/server.py | 6 +++-- 8 files changed, 25 insertions(+), 37 deletions(-) diff --git a/examples/client_test.py b/examples/client_test.py index 1a3301a..e8700ac 100644 --- a/examples/client_test.py +++ b/examples/client_test.py @@ -28,19 +28,7 @@ async def main() -> None: async with Controller("ws://localhost:8001/") as controller: await controller.clients.listen() - await controller.network.start_network( - { - "radio_type": "ezsp", - "device": { - "path": "/dev/cu.GoControl_zigbee\u0011", - "flow_control": "software", - "baudrate": 57600, - }, - "database_path": "./zigbee.db", - "enable_quirks": True, - "message_id": 1, - } - ) + await controller.network.start_network() await controller.load_devices() await controller.load_groups() @@ -94,7 +82,7 @@ async def main() -> None: await asyncio.sleep(3) except Exception as err: - _LOGGER.exception(exc_info=err) + _LOGGER.exception("Exception testing lights", exc_info=err) if test_switches: try: @@ -116,7 +104,7 @@ async def main() -> None: await asyncio.sleep(3) except Exception as err: - _LOGGER.exception(exc_info=err) + _LOGGER.exception("Exception testing switches", exc_info=err) if test_alarm_control_panel: try: @@ -130,7 +118,7 @@ async def main() -> None: await asyncio.sleep(3) except Exception as err: - _LOGGER.exception(exc_info=err) + _LOGGER.exception("Exception testing alarm control panel", exc_info=err) if test_locks: try: @@ -146,7 +134,7 @@ async def main() -> None: await asyncio.sleep(3) except Exception as err: - _LOGGER.exception(exc_info=err) + _LOGGER.exception("Exception testing locks", exc_info=err) if test_buttons: try: @@ -158,7 +146,7 @@ async def main() -> None: await asyncio.sleep(3) except Exception as err: - _LOGGER.exception(exc_info=err) + _LOGGER.exception("Exception testing buttons", exc_info=err) """TODO turn this into an example for how to create a group with the client await controller.groups_helper.create_group( diff --git a/tests/common.py b/tests/common.py index e90e22b..c0f29aa 100644 --- a/tests/common.py +++ b/tests/common.py @@ -11,6 +11,8 @@ import zigpy.zcl import zigpy.zcl.foundation as zcl_f +from zha.application.discovery import Platform +from zha.zigbee import Device, Group from zhaws.client.model.types import BasePlatformEntity from zhaws.client.proxy import DeviceProxy from zhaws.server.websocket.server import Server @@ -203,6 +205,7 @@ def find_entity_id( for entity_id in entities: if qualifier in entity_id: return entity_id + return None else: return entities[0] diff --git a/tests/test_client_controller.py b/tests/test_client_controller.py index 3f8e9b3..62370fd 100644 --- a/tests/test_client_controller.py +++ b/tests/test_client_controller.py @@ -102,7 +102,7 @@ def get_group_entity( for entity in group_proxy.group_model.entities.values() } - return entities.get(entity_id) # type: ignore + return entities.get(entity_id) @pytest.fixture @@ -141,7 +141,7 @@ async def test_controller_devices( client_device: Optional[DeviceProxy] = controller.devices.get(zha_device.ieee) assert client_device is not None - entity: SwitchEntity = get_entity(client_device, entity_id) # type: ignore + entity: SwitchEntity = get_entity(client_device, entity_id) assert entity is not None assert isinstance(entity, SwitchEntity) @@ -176,7 +176,7 @@ async def test_controller_devices( assert len(controller.devices) == 1 # we removed and joined the device again so lets get the entity again - client_device: Optional[DeviceProxy] = controller.devices.get(zha_device.ieee) + client_device = controller.devices.get(zha_device.ieee) assert client_device is not None entity: SwitchEntity = get_entity(client_device, entity_id) # type: ignore assert entity is not None @@ -330,14 +330,14 @@ async def test_controller_groups( assert client_device1 is not None entity_id1 = find_entity_id(Platform.SWITCH, device_switch_1) assert entity_id1 is not None - entity1: SwitchEntity = get_entity(client_device1, entity_id1) # type: ignore + entity1: SwitchEntity = get_entity(client_device1, entity_id1) assert entity1 is not None client_device2: Optional[DeviceProxy] = controller.devices.get(device_switch_2.ieee) assert client_device2 is not None entity_id2 = find_entity_id(Platform.SWITCH, device_switch_2) assert entity_id2 is not None - entity2: SwitchEntity = get_entity(client_device2, entity_id2) # type: ignore + entity2: SwitchEntity = get_entity(client_device2, entity_id2) assert entity2 is not None response: GroupModel = await controller.groups_helper.create_group( @@ -351,9 +351,7 @@ async def test_controller_groups( assert client_device2.device_model.ieee in response.members # test remove member from group from controller - response: GroupModel = await controller.groups_helper.remove_group_members( - response, [entity2] - ) + response = await controller.groups_helper.remove_group_members(response, [entity2]) await server.block_till_done() assert len(controller.groups) == 2 assert response.id in controller.groups @@ -362,9 +360,7 @@ async def test_controller_groups( assert client_device2.device_model.ieee not in response.members # test add member to group from controller - response: GroupModel = await controller.groups_helper.add_group_members( - response, [entity2] - ) + response = await controller.groups_helper.add_group_members(response, [entity2]) await server.block_till_done() assert len(controller.groups) == 2 assert response.id in controller.groups diff --git a/tests/test_server_client.py b/tests/test_server_client.py index 9bdfddb..481d9bb 100644 --- a/tests/test_server_client.py +++ b/tests/test_server_client.py @@ -27,11 +27,11 @@ async def test_server_client_connect_disconnect( assert client._listen_task is not None # The listen task is automatically stopped when we disconnect - assert client._listen_task is None # type: ignore + assert client._listen_task is None assert "not connected" in repr(client) assert not client.connected - assert not server.is_serving # type: ignore + assert not server.is_serving assert server._ws_server is None diff --git a/zhaws/server/__main__.py b/zhaws/server/__main__.py index c63f17f..68514c6 100644 --- a/zhaws/server/__main__.py +++ b/zhaws/server/__main__.py @@ -16,8 +16,7 @@ async def main(config_path: str | None = None) -> None: """Run the websocket server.""" if config_path is None: - _LOGGER.info("No config file provided, using default configuration") - configuration = ServerConfiguration() + raise ValueError("config_path must be provided") else: _LOGGER.info("Loading configuration from %s", config_path) path = Path(config_path) diff --git a/zhaws/server/platforms/__init__.py b/zhaws/server/platforms/__init__.py index ffd39f8..611d30c 100644 --- a/zhaws/server/platforms/__init__.py +++ b/zhaws/server/platforms/__init__.py @@ -13,5 +13,5 @@ class PlatformEntityCommand(WebSocketCommand): """Base class for platform entity commands.""" ieee: Union[EUI64, None] - group_id: Union[int, None] + group_id: Union[int, None] = None unique_id: str diff --git a/zhaws/server/platforms/alarm_control_panel/api.py b/zhaws/server/platforms/alarm_control_panel/api.py index 9203250..f3ef1a2 100644 --- a/zhaws/server/platforms/alarm_control_panel/api.py +++ b/zhaws/server/platforms/alarm_control_panel/api.py @@ -90,7 +90,7 @@ class TriggerAlarmCommand(PlatformEntityCommand): command: Literal[APICommands.ALARM_CONTROL_PANEL_TRIGGER] = ( APICommands.ALARM_CONTROL_PANEL_TRIGGER ) - code: Union[str, None] + code: Union[str, None] = None @decorators.websocket_command(TriggerAlarmCommand) diff --git a/zhaws/server/websocket/server.py b/zhaws/server/websocket/server.py index bc81f52..ca6942b 100644 --- a/zhaws/server/websocket/server.py +++ b/zhaws/server/websocket/server.py @@ -170,10 +170,12 @@ async def block_till_done(self) -> None: async def _await_and_log_pending(self, pending: Iterable[Awaitable[Any]]) -> None: """Await and log tasks that take a long time.""" - # pylint: disable=no-self-use wait_time = 0 while pending: - _, pending = await asyncio.wait(pending, timeout=BLOCK_LOG_TIMEOUT) + _, pending = await asyncio.wait( + [asyncio.ensure_future(task) for task in pending], + timeout=BLOCK_LOG_TIMEOUT, + ) if not pending: return wait_time += BLOCK_LOG_TIMEOUT