Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco committed Jan 11, 2024
1 parent a05d368 commit 36b920f
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 42 deletions.
9 changes: 7 additions & 2 deletions tests/test_adv_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,9 @@ def test_parse_advertisement_data_curtain3():
"""Test parse_advertisement_data for curtain 3."""
ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
adv_data = generate_advertisement_data(
manufacturer_data={2409: b"\xaa\xbb\xcc\xdd\xee\xff\xf7\x07\x00\x11\x04\x00\x49"},
manufacturer_data={
2409: b"\xaa\xbb\xcc\xdd\xee\xff\xf7\x07\x00\x11\x04\x00\x49"
},
service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"{\xc0\x49\x00\x11\x04"},
rssi=-80,
)
Expand Down Expand Up @@ -391,7 +393,9 @@ def test_parse_advertisement_data_curtain3_passive():
"""Test parse_advertisement_data for curtain passive."""
ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
adv_data = generate_advertisement_data(
manufacturer_data={2409: b"\xaa\xbb\xcc\xdd\xee\xff\xf7\x07\x00\x11\x04\x00\x49"},
manufacturer_data={
2409: b"\xaa\xbb\xcc\xdd\xee\xff\xf7\x07\x00\x11\x04\x00\x49"
},
service_data={},
rssi=-80,
)
Expand Down Expand Up @@ -1364,6 +1368,7 @@ def test_parsing_lock_passive():
active=False,
)


def test_parsing_lock_active_old_firmware():
"""Test parsing lock with active data. Old firmware."""
ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
Expand Down
125 changes: 85 additions & 40 deletions tests/test_curtain.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
import pytest

from typing import Any
from unittest.mock import Mock

import pytest
from bleak.backends.device import BLEDevice

from switchbot import SwitchBotAdvertisement, SwitchbotModel
from switchbot.devices import curtain

from test_adv_parser import generate_ble_device

from unittest.mock import Mock
from .test_adv_parser import generate_ble_device


def set_advertisement_data(ble_device: BLEDevice, in_motion: bool, position: int):
"""Set advertisement data with defaults."""

return SwitchBotAdvertisement(
address="aa:bb:cc:dd:ee:ff",
data={
Expand All @@ -37,71 +35,102 @@ def set_advertisement_data(ble_device: BLEDevice, in_motion: bool, position: int
active=True,
)

@pytest.mark.parametrize("reverse_mode", [(True),(False)])

@pytest.mark.parametrize("reverse_mode", [(True), (False)])
def test_device_passive_not_in_motion(reverse_mode):
"""Test passive not in motion advertisement."""
ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
curtain_device = curtain.SwitchbotCurtain(ble_device, reverse_mode=reverse_mode)
curtain_device.update_from_advertisement(set_advertisement_data(ble_device, False, 0))
curtain_device.update_from_advertisement(
set_advertisement_data(ble_device, False, 0)
)

assert curtain_device.is_opening() is False
assert curtain_device.is_closing() is False

@pytest.mark.parametrize("reverse_mode", [(True),(False)])

@pytest.mark.parametrize("reverse_mode", [(True), (False)])
def test_device_passive_opening(reverse_mode):
"""Test passive opening advertisement."""
ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
curtain_device = curtain.SwitchbotCurtain(ble_device, reverse_mode=reverse_mode)
curtain_device.update_from_advertisement(set_advertisement_data(ble_device, True, 0))
curtain_device.update_from_advertisement(set_advertisement_data(ble_device, True, 10))
curtain_device.update_from_advertisement(
set_advertisement_data(ble_device, True, 0)
)
curtain_device.update_from_advertisement(
set_advertisement_data(ble_device, True, 10)
)

assert curtain_device.is_opening() is True
assert curtain_device.is_closing() is False

@pytest.mark.parametrize("reverse_mode", [(True),(False)])

@pytest.mark.parametrize("reverse_mode", [(True), (False)])
def test_device_passive_closing(reverse_mode):
"""Test passive closing advertisement."""
ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
curtain_device = curtain.SwitchbotCurtain(ble_device, reverse_mode=reverse_mode)
curtain_device.update_from_advertisement(set_advertisement_data(ble_device, True, 100))
curtain_device.update_from_advertisement(set_advertisement_data(ble_device, True, 90))
curtain_device.update_from_advertisement(
set_advertisement_data(ble_device, True, 100)
)
curtain_device.update_from_advertisement(
set_advertisement_data(ble_device, True, 90)
)

assert curtain_device.is_opening() is False
assert curtain_device.is_closing() is True

@pytest.mark.parametrize("reverse_mode", [(True),(False)])

@pytest.mark.parametrize("reverse_mode", [(True), (False)])
def test_device_passive_opening_then_stop(reverse_mode):
"""Test passive stopped after opening advertisement."""
ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
curtain_device = curtain.SwitchbotCurtain(ble_device, reverse_mode=reverse_mode)
curtain_device.update_from_advertisement(set_advertisement_data(ble_device, True, 0))
curtain_device.update_from_advertisement(set_advertisement_data(ble_device, True, 10))
curtain_device.update_from_advertisement(set_advertisement_data(ble_device, False, 10))
curtain_device.update_from_advertisement(
set_advertisement_data(ble_device, True, 0)
)
curtain_device.update_from_advertisement(
set_advertisement_data(ble_device, True, 10)
)
curtain_device.update_from_advertisement(
set_advertisement_data(ble_device, False, 10)
)

assert curtain_device.is_opening() is False
assert curtain_device.is_closing() is False

@pytest.mark.parametrize("reverse_mode", [(True),(False)])

@pytest.mark.parametrize("reverse_mode", [(True), (False)])
def test_device_passive_closing_then_stop(reverse_mode):
"""Test passive stopped after closing advertisement."""
ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
curtain_device = curtain.SwitchbotCurtain(ble_device, reverse_mode=reverse_mode)
curtain_device.update_from_advertisement(set_advertisement_data(ble_device, True, 100))
curtain_device.update_from_advertisement(set_advertisement_data(ble_device, True, 90))
curtain_device.update_from_advertisement(set_advertisement_data(ble_device, False, 90))
curtain_device.update_from_advertisement(
set_advertisement_data(ble_device, True, 100)
)
curtain_device.update_from_advertisement(
set_advertisement_data(ble_device, True, 90)
)
curtain_device.update_from_advertisement(
set_advertisement_data(ble_device, False, 90)
)

assert curtain_device.is_opening() is False
assert curtain_device.is_closing() is False


@pytest.mark.asyncio
@pytest.mark.parametrize("reverse_mode", [(True),(False)])
@pytest.mark.parametrize("reverse_mode", [(True), (False)])
async def test_device_active_not_in_motion(reverse_mode):
"""Test active not in motion."""
ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
curtain_device = curtain.SwitchbotCurtain(ble_device, reverse_mode=reverse_mode)
curtain_device.update_from_advertisement(set_advertisement_data(ble_device, False, 0))
curtain_device.update_from_advertisement(
set_advertisement_data(ble_device, False, 0)
)

basic_info = bytes([0, 0, 0, 0, 0, 0, 100, 0])

basic_info = bytes([0,0,0,0,0,0,100,0])
async def custom_implementation():
return basic_info

Expand All @@ -112,15 +141,19 @@ async def custom_implementation():
assert curtain_device.is_opening() is False
assert curtain_device.is_closing() is False


@pytest.mark.asyncio
@pytest.mark.parametrize("reverse_mode", [(True),(False)])
@pytest.mark.parametrize("reverse_mode", [(True), (False)])
async def test_device_active_opening(reverse_mode):
"""Test active opening."""
ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
curtain_device = curtain.SwitchbotCurtain(ble_device, reverse_mode=reverse_mode)
curtain_device.update_from_advertisement(set_advertisement_data(ble_device, True, 0))
curtain_device.update_from_advertisement(
set_advertisement_data(ble_device, True, 0)
)

basic_info = bytes([0, 0, 0, 0, 0, 67, 10, 0])

basic_info = bytes([0,0,0,0,0,67,10,0])
async def custom_implementation():
return basic_info

Expand All @@ -131,15 +164,19 @@ async def custom_implementation():
assert curtain_device.is_opening() is True
assert curtain_device.is_closing() is False


@pytest.mark.asyncio
@pytest.mark.parametrize("reverse_mode", [(True),(False)])
@pytest.mark.parametrize("reverse_mode", [(True), (False)])
async def test_device_active_closing(reverse_mode):
"""Test active closing."""
ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
curtain_device = curtain.SwitchbotCurtain(ble_device, reverse_mode=reverse_mode)
curtain_device.update_from_advertisement(set_advertisement_data(ble_device, True, 100))
curtain_device.update_from_advertisement(
set_advertisement_data(ble_device, True, 100)
)

basic_info = bytes([0, 0, 0, 0, 0, 67, 90, 0])

basic_info = bytes([0,0,0,0,0,67,90,0])
async def custom_implementation():
return basic_info

Expand All @@ -150,48 +187,56 @@ async def custom_implementation():
assert curtain_device.is_opening() is False
assert curtain_device.is_closing() is True


@pytest.mark.asyncio
@pytest.mark.parametrize("reverse_mode", [(True),(False)])
@pytest.mark.parametrize("reverse_mode", [(True), (False)])
async def test_device_active_opening_then_stop(reverse_mode):
"""Test active stopped after opening."""
ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
curtain_device = curtain.SwitchbotCurtain(ble_device, reverse_mode=reverse_mode)
curtain_device.update_from_advertisement(set_advertisement_data(ble_device, True, 0))
curtain_device.update_from_advertisement(
set_advertisement_data(ble_device, True, 0)
)

basic_info = bytes([0, 0, 0, 0, 0, 67, 10, 0])

basic_info = bytes([0,0,0,0,0,67,10,0])
async def custom_implementation():
return basic_info

curtain_device._get_basic_info = Mock(side_effect=custom_implementation)

await curtain_device.get_basic_info()

basic_info = bytes([0,0,0,0,0,0,10,0])
basic_info = bytes([0, 0, 0, 0, 0, 0, 10, 0])

await curtain_device.get_basic_info()

assert curtain_device.is_opening() is False
assert curtain_device.is_closing() is False


@pytest.mark.asyncio
@pytest.mark.parametrize("reverse_mode", [(True),(False)])
@pytest.mark.parametrize("reverse_mode", [(True), (False)])
async def test_device_active_closing_then_stop(reverse_mode):
"""Test active stopped after closing."""
ble_device = generate_ble_device("aa:bb:cc:dd:ee:ff", "any")
curtain_device = curtain.SwitchbotCurtain(ble_device, reverse_mode=reverse_mode)
curtain_device.update_from_advertisement(set_advertisement_data(ble_device, True, 100))
curtain_device.update_from_advertisement(
set_advertisement_data(ble_device, True, 100)
)

basic_info = bytes([0, 0, 0, 0, 0, 67, 90, 0])

basic_info = bytes([0,0,0,0,0,67,90,0])
async def custom_implementation():
return basic_info

curtain_device._get_basic_info = Mock(side_effect=custom_implementation)

await curtain_device.get_basic_info()

basic_info = bytes([0,0,0,0,0,0,90,0])
basic_info = bytes([0, 0, 0, 0, 0, 0, 90, 0])

await curtain_device.get_basic_info()

assert curtain_device.is_opening() is False
assert curtain_device.is_closing() is False
assert curtain_device.is_closing() is False

0 comments on commit 36b920f

Please sign in to comment.