Skip to content

Commit 776555f

Browse files
committed
Add pre-commit
1 parent c057b35 commit 776555f

File tree

14 files changed

+669
-416
lines changed

14 files changed

+669
-416
lines changed

.pre-commit-config.yaml

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
repos:
2+
- repo: https://github.com/pre-commit/pre-commit-hooks
3+
rev: v4.5.0
4+
hooks:
5+
- id: debug-statements
6+
7+
- repo: https://github.com/psf/black
8+
rev: 23.10.1
9+
hooks:
10+
- id: black
11+
12+
- repo: https://github.com/PyCQA/flake8
13+
rev: 6.1.0
14+
hooks:
15+
- id: flake8
16+
entry: pflake8
17+
additional_dependencies:
18+
- pyproject-flake8==6.1.0
19+
- flake8-bugbear==23.1.20
20+
- flake8-comprehensions==3.10.1
21+
- flake8_2020==1.7.0
22+
- mccabe==0.7.0
23+
- pycodestyle==2.11.1
24+
- pyflakes==3.1.0
25+
26+
- repo: https://github.com/PyCQA/isort
27+
rev: 5.12.0
28+
hooks:
29+
- id: isort
30+
31+
- repo: https://github.com/pre-commit/mirrors-mypy
32+
rev: v1.6.1
33+
hooks:
34+
- id: mypy
35+
additional_dependencies:
36+
- zigpy
37+
- types-setuptools
38+
39+
- repo: https://github.com/asottile/pyupgrade
40+
rev: v3.15.0
41+
hooks:
42+
- id: pyupgrade
43+
44+
- repo: https://github.com/fsouza/autoflake8
45+
rev: v0.4.1
46+
hooks:
47+
- id: autoflake8

tests/test_api.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
import asyncio
2-
import sys
2+
from unittest.mock import AsyncMock, MagicMock, patch, sentinel
3+
34
import pytest
45
import serial
56
import serial_asyncio
6-
from unittest.mock import AsyncMock, MagicMock, patch, sentinel
77

8+
from zigpy_zigate import api as zigate_api
89
import zigpy_zigate.config as config
910
import zigpy_zigate.uart
10-
from zigpy_zigate import api as zigate_api
1111

1212
DEVICE_CONFIG = config.SCHEMA_DEVICE({config.CONF_DEVICE_PATH: "/dev/null"})
1313

1414

15-
1615
@pytest.fixture
1716
def api():
1817
api = zigate_api.ZiGate(DEVICE_CONFIG)
@@ -60,7 +59,7 @@ async def test_api_new(conn_mck):
6059
@patch.object(zigate_api.ZiGate, "set_raw_mode", new_callable=AsyncMock)
6160
@pytest.mark.parametrize(
6261
"port",
63-
('/dev/null', 'pizigate:/dev/ttyAMA0'),
62+
("/dev/null", "pizigate:/dev/ttyAMA0"),
6463
)
6564
async def test_probe_success(mock_raw_mode, port, monkeypatch):
6665
"""Test device probing."""
@@ -69,6 +68,7 @@ async def mock_conn(loop, protocol_factory, **kwargs):
6968
protocol = protocol_factory()
7069
loop.call_soon(protocol.connection_made, None)
7170
return None, protocol
71+
7272
monkeypatch.setattr(serial_asyncio, "create_serial_connection", mock_conn)
7373
DEVICE_CONFIG = zigpy_zigate.config.SCHEMA_DEVICE(
7474
{zigpy_zigate.config.CONF_DEVICE_PATH: port}

tests/test_application.py

Lines changed: 71 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
from unittest.mock import AsyncMock, MagicMock, patch, sentinel, call
1+
import logging
2+
from unittest.mock import AsyncMock, MagicMock, call, patch
23

34
import pytest
4-
import logging
5-
import zigpy.types as zigpy_t
65
import zigpy.exceptions
6+
import zigpy.types as zigpy_t
77

88
import zigpy_zigate.api
9-
import zigpy_zigate.types as t
109
import zigpy_zigate.config as config
10+
import zigpy_zigate.types as t
1111
import zigpy_zigate.zigbee.application
1212

1313
APP_CONFIG = zigpy_zigate.zigbee.application.ControllerApplication.SCHEMA(
@@ -16,7 +16,7 @@
1616
config.CONF_DATABASE: None,
1717
}
1818
)
19-
FAKE_FIRMWARE_VERSION = '3.1z'
19+
FAKE_FIRMWARE_VERSION = "3.1z"
2020

2121

2222
@pytest.fixture
@@ -41,7 +41,7 @@ def test_zigpy_ieee(app):
4141

4242
def test_model_detection(app):
4343
device = zigpy_zigate.zigbee.application.ZiGateDevice(app, 0, 0)
44-
assert device.model == 'ZiGate USB-TTL {}'.format(FAKE_FIRMWARE_VERSION)
44+
assert device.model == "ZiGate USB-TTL {}".format(FAKE_FIRMWARE_VERSION)
4545

4646

4747
@pytest.mark.asyncio
@@ -52,16 +52,17 @@ async def test_form_network_success(app):
5252
app._api.reset = AsyncMock()
5353

5454
async def mock_start_network():
55-
return [[0x00, 0x1234, 0x0123456789abcdef], 0]
55+
return [[0x00, 0x1234, 0x0123456789ABCDEF], 0]
56+
5657
app._api.start_network = mock_start_network
5758

5859
async def mock_get_network_state():
5960
return [
6061
[
6162
0x0000,
62-
t.EUI64([0xef, 0xcd, 0xab, 0x89, 0x67, 0x45, 0x23, 0x01]),
63+
t.EUI64([0xEF, 0xCD, 0xAB, 0x89, 0x67, 0x45, 0x23, 0x01]),
6364
0x1234,
64-
0x1234abcdef012345,
65+
0x1234ABCDEF012345,
6566
0x11,
6667
],
6768
0,
@@ -88,11 +89,15 @@ async def test_form_network_failed(app):
8889
app._api.set_channel = AsyncMock()
8990
app._api.set_extended_panid = AsyncMock()
9091
app._api.reset = AsyncMock()
92+
9193
async def mock_start_network():
9294
return [[0x06], 0]
95+
9396
app._api.start_network = mock_start_network
97+
9498
async def mock_get_network_state():
95-
return [[0xffff, 0x0123456789abcdef, 0x1234, 0, 0x11], 0]
99+
return [[0xFFFF, 0x0123456789ABCDEF, 0x1234, 0, 0x11], 0]
100+
96101
app._api.get_network_state = mock_get_network_state
97102

98103
with pytest.raises(zigpy.exceptions.FormationFailure):
@@ -139,10 +144,10 @@ async def test_disconnect_multiple(app):
139144

140145
@pytest.mark.asyncio
141146
@patch("zigpy_zigate.zigbee.application.ZiGate.new")
142-
@pytest.mark.parametrize("version_rsp, expected_version", [
143-
[((261, 798), 0), "3.1e"],
144-
[((5, 801), 0), "3.21"]
145-
])
147+
@pytest.mark.parametrize(
148+
"version_rsp, expected_version",
149+
[[((261, 798), 0), "3.1e"], [((5, 801), 0), "3.21"]],
150+
)
146151
async def test_startup_connect(zigate_new, app, version_rsp, expected_version):
147152
api = zigate_new.return_value
148153
api.version.return_value = version_rsp
@@ -153,15 +158,37 @@ async def test_startup_connect(zigate_new, app, version_rsp, expected_version):
153158

154159

155160
@pytest.mark.asyncio
156-
@pytest.mark.parametrize("version, addr_mode", [
157-
["3.1z", t.AddressMode.NWK_NO_ACK],
158-
["3.1d", t.AddressMode.NWK],
159-
])
161+
@pytest.mark.parametrize(
162+
"version, addr_mode",
163+
[
164+
["3.1z", t.AddressMode.NWK_NO_ACK],
165+
["3.1d", t.AddressMode.NWK],
166+
],
167+
)
160168
async def test_send_unicast_request(app, version, addr_mode):
161-
packet = zigpy_t.ZigbeePacket(src=zigpy_t.AddrModeAddress(addr_mode=zigpy_t.AddrMode.NWK, address=0x0000), src_ep=1, dst=zigpy_t.AddrModeAddress(addr_mode=zigpy_t.AddrMode.NWK, address=0xFA5D), dst_ep=1, source_route=None, extended_timeout=False, tsn=20, profile_id=260, cluster_id=6, data=zigpy_t.SerializableBytes(b'\x01\x14\x00'), tx_options=zigpy_t.TransmitOptions.NONE, radius=0, non_member_radius=0, lqi=None, rssi=None)
169+
packet = zigpy_t.ZigbeePacket(
170+
src=zigpy_t.AddrModeAddress(addr_mode=zigpy_t.AddrMode.NWK, address=0x0000),
171+
src_ep=1,
172+
dst=zigpy_t.AddrModeAddress(addr_mode=zigpy_t.AddrMode.NWK, address=0xFA5D),
173+
dst_ep=1,
174+
source_route=None,
175+
extended_timeout=False,
176+
tsn=20,
177+
profile_id=260,
178+
cluster_id=6,
179+
data=zigpy_t.SerializableBytes(b"\x01\x14\x00"),
180+
tx_options=zigpy_t.TransmitOptions.NONE,
181+
radius=0,
182+
non_member_radius=0,
183+
lqi=None,
184+
rssi=None,
185+
)
162186

163187
app.version = version
164-
app._api.raw_aps_data_request.return_value = ([t.Status.Success, 163, 1328, b'\x00\x00'], 0)
188+
app._api.raw_aps_data_request.return_value = (
189+
[t.Status.Success, 163, 1328, b"\x00\x00"],
190+
0,
191+
)
165192
await app.send_packet(packet)
166193

167194
# The packet was sent with ACKs, even though zigpy didn't ask for it
@@ -172,9 +199,28 @@ async def test_send_unicast_request(app, version, addr_mode):
172199

173200
@pytest.mark.asyncio
174201
async def test_send_group_request(app):
175-
packet = zigpy_t.ZigbeePacket(src=None, src_ep=1, dst=zigpy_t.AddrModeAddress(addr_mode=zigpy_t.AddrMode.Group, address=0x0002), dst_ep=None, source_route=None, extended_timeout=False, tsn=21, profile_id=260, cluster_id=6, data=zigpy_t.SerializableBytes(b'\x01\x15\x00'), tx_options=zigpy_t.TransmitOptions.NONE, radius=0, non_member_radius=3, lqi=None, rssi=None)
202+
packet = zigpy_t.ZigbeePacket(
203+
src=None,
204+
src_ep=1,
205+
dst=zigpy_t.AddrModeAddress(addr_mode=zigpy_t.AddrMode.Group, address=0x0002),
206+
dst_ep=None,
207+
source_route=None,
208+
extended_timeout=False,
209+
tsn=21,
210+
profile_id=260,
211+
cluster_id=6,
212+
data=zigpy_t.SerializableBytes(b"\x01\x15\x00"),
213+
tx_options=zigpy_t.TransmitOptions.NONE,
214+
radius=0,
215+
non_member_radius=3,
216+
lqi=None,
217+
rssi=None,
218+
)
176219

177-
app._api.raw_aps_data_request.return_value = ([t.Status.Success, 0, 1328, b'\x01\xea\x00\x00'], 0)
220+
app._api.raw_aps_data_request.return_value = (
221+
[t.Status.Success, 0, 1328, b"\x01\xea\x00\x00"],
222+
0,
223+
)
178224
await app.send_packet(packet)
179225

180226
app._api.raw_aps_data_request.assert_called_once()
@@ -183,7 +229,9 @@ async def test_send_group_request(app):
183229
@pytest.mark.asyncio
184230
async def test_energy_scanning(app, caplog):
185231
with caplog.at_level(logging.WARNING):
186-
scan_results = await app.energy_scan(channels=zigpy_t.Channels.ALL_CHANNELS, duration_exp=2, count=5)
232+
scan_results = await app.energy_scan(
233+
channels=zigpy_t.Channels.ALL_CHANNELS, duration_exp=2, count=5
234+
)
187235

188236
assert scan_results == {c: 0 for c in zigpy_t.Channels.ALL_CHANNELS}
189237

0 commit comments

Comments
 (0)