Skip to content

Commit

Permalink
Tests for the Chuangmi IR controller (#184)
Browse files Browse the repository at this point in the history
  • Loading branch information
syssi authored Jan 28, 2018
1 parent ff11aed commit e861c3a
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 7 deletions.
22 changes: 15 additions & 7 deletions miio/chuangmi_ir.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class ChuangmiIrException(DeviceException):
class ChuangmiIr(Device):
"""Main class representing Chuangmi IR Remote Controller."""

def learn(self, key: int):
def learn(self, key: int=1):
"""Learn an infrared command.
:param int key: Storage slot, must be between 1 and 1000000"""
Expand All @@ -22,21 +22,29 @@ def learn(self, key: int):
raise ChuangmiIrException("Invalid storage slot.")
return self.send("miIO.ir_learn", {'key': str(key)})

def read(self, key: int):
def read(self, key: int=1):
"""Read a learned command.
FIXME what is the return value? Examples needed.
Positive response (chuangmi.ir.v2):
{'key': '1', 'code': 'Z6WPAasBAAA3BQAA4AwJAEA....AAABAAEBAQAAAQAA=='}
Negative response (chuangmi.ir.v2):
{'error': {'code': -5002, 'message': 'no code for this key'}, 'id': 5}
Negative response (chuangmi.ir.v2):
{'error': {'code': -5003, 'message': 'learn timeout'}, 'id': 17}
:param int key: Slot to read from"""

if key < 1 or key > 1000000:
raise ChuangmiIrException("Invalid storage slot.")
return self.send("miIO.ir_read", {'key': str(key)})

def play(self, command: str, frequency: int):
def play(self, command: str, frequency: int=38400):
"""Play a captured command.
:param str command: Command to execute
:param int frequence: Execution frequency"""
if frequency is None:
frequency = 38400
:param int frequency: Execution frequency"""
return self.send("miIO.ir_play",
{'freq': frequency, 'code': command})

Expand Down
78 changes: 78 additions & 0 deletions miio/tests/test_chuangmi_ir.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
from unittest import TestCase
from miio import ChuangmiIr
from miio.chuangmi_ir import ChuangmiIrException
from .dummies import DummyDevice
import pytest
import base64

PROSONIC_POWER_ON = 'Z6VPAAUCAABgAgAAxQYAAOUIAACUEQAAqyIAADSeAABwdQEAAAAAAAA' \
'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABFEBAQEBAQEBAgICAgIC' \
'AgEBAgECAQEBAQIBAgECAgICBgNXA1cDUA'

PROSONIC_POWER_ON_PROTON = '0000006C00220002015B00AD001600160016001600160016' \
'001600160016001600160016001600160016001600160041' \
'001600410016004100160041001600410016004100160041' \
'001600160016001600160041001600160016004100160016' \
'001600160016001600160016001600410016001600160041' \
'001600160016004100160041001600410016004100160623' \
'015B005700160E6E'


class DummyChuangmiIr(DummyDevice, ChuangmiIr):
def __init__(self, *args, **kwargs):
self.state = {}
self.return_values = {
'miIO.ir_learn': lambda x: True,
'miIO.ir_read': lambda x: True,
'miIO.ir_play': self._ir_play_input_validation,
}
super().__init__(args, kwargs)

@staticmethod
def _ir_play_input_validation(payload):
try:
base64.b64decode(payload['code'])
return True
except TypeError:
return False


@pytest.fixture(scope="class")
def chuangmiir(request):
request.cls.device = DummyChuangmiIr()
# TODO add ability to test on a real device


@pytest.mark.usefixtures("chuangmiir")
class TestChuangmiIr(TestCase):
def test_learn(self):
assert self.device.learn() is True
assert self.device.learn(30) is True

with pytest.raises(ChuangmiIrException):
self.device.learn(-1)

with pytest.raises(ChuangmiIrException):
self.device.learn(1000001)

def test_read(self):
assert self.device.read() is True
assert self.device.read(30) is True

with pytest.raises(ChuangmiIrException):
self.device.read(-1)

with pytest.raises(ChuangmiIrException):
self.device.read(1000001)

def test_play(self):
assert self.device.play(PROSONIC_POWER_ON) is True
assert self.device.play(PROSONIC_POWER_ON, 19200) is True

def test_play_pronto(self):
assert self.device.play_pronto(PROSONIC_POWER_ON_PROTON) is True
assert self.device.play_pronto(PROSONIC_POWER_ON_PROTON, 0) is True
assert self.device.play_pronto(PROSONIC_POWER_ON_PROTON, 1) is True

with pytest.raises(ChuangmiIrException):
self.device.play_pronto(PROSONIC_POWER_ON_PROTON, -1)

0 comments on commit e861c3a

Please sign in to comment.