123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242 |
- 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
- 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={
- "rawAdvData": b"c\xc0X\x00\x11\x04",
- "data": {
- "calibration": True,
- "battery": 88,
- "inMotion": in_motion,
- "position": position,
- "lightLevel": 1,
- "deviceChain": 1,
- },
- "isEncrypted": False,
- "model": "c",
- "modelFriendlyName": "Curtain",
- "modelName": SwitchbotModel.CURTAIN,
- },
- device=ble_device,
- rssi=-80,
- active=True,
- )
- @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)
- )
- assert curtain_device.is_opening() is False
- assert curtain_device.is_closing() is 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)
- )
- assert curtain_device.is_opening() is True
- assert curtain_device.is_closing() is 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)
- )
- assert curtain_device.is_opening() is False
- assert curtain_device.is_closing() is True
- @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)
- )
- assert curtain_device.is_opening() is False
- assert curtain_device.is_closing() is 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)
- )
- assert curtain_device.is_opening() is False
- assert curtain_device.is_closing() is False
- @pytest.mark.asyncio
- @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)
- )
- basic_info = bytes([0, 0, 0, 0, 0, 0, 100, 0])
- async def custom_implementation():
- return basic_info
- curtain_device._get_basic_info = Mock(side_effect=custom_implementation)
- 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)])
- 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)
- )
- 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()
- assert curtain_device.is_opening() is True
- assert curtain_device.is_closing() is False
- @pytest.mark.asyncio
- @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)
- )
- 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()
- assert curtain_device.is_opening() is False
- assert curtain_device.is_closing() is True
- @pytest.mark.asyncio
- @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)
- )
- 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])
- 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)])
- 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)
- )
- 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])
- await curtain_device.get_basic_info()
- assert curtain_device.is_opening() is False
- assert curtain_device.is_closing() is False
|