Air Conditioning Companion: Rewrite a captured command before replay#317
Conversation
miio/airconditioningcompanion.py
Outdated
| default_output=format_output("Sending the supplied infrared command") | ||
| ) | ||
| def send_ir_code(self, command: str): | ||
| def send_ir_code(self, model: str, code: str): |
There was a problem hiding this comment.
I'm not a fan of working on a hex string. I think it would be better to convert both model and code strings to bytearrays at the beginning and work on them. Then at the end, convert it back to a hex string.
miio/airconditioningcompanion.py
Outdated
| code[26:28] + code[28:32] + '27' | ||
|
|
||
| checksum = sum([int(command[i:i + 2], 16) for i in range(0, len(command), 2)]) | ||
| checksum = "{:02X}".format(checksum % 256) |
There was a problem hiding this comment.
checksum & 0xFF produces the same value as checksum % 256, but should be much faster (bit-wise operation vs arithmetic one).
|
|
||
| def test_send_ir_code(self): | ||
| assert self.device.send_ir_code('0000000') is True | ||
| assert self.device.send_ir_code(bytes.fromhex('010500978022222102'), bytes.fromhex('00')) is True |
There was a problem hiding this comment.
line too long (105 > 100 characters)
miio/airconditioningcompanion.py
Outdated
| slot = bytes([121 + slot]) | ||
|
|
||
| # FE + 0487 + 00007145 + 9470 + 1FFF + 7F + FF + 06 + 0042 + 27 + 4E + 0025002D008500AC01... | ||
| command = code[0:1] + model[2:8] + b'\x94\x70\x1F\xFF' + slot + b'\xFF' + code[13:16] + b'\x27' |
There was a problem hiding this comment.
line too long (103 > 100 characters)
|
|
||
| def test_send_ir_code(self): | ||
| assert self.device.send_ir_code('0000000') is True | ||
| assert self.device.send_ir_code(bytes.fromhex('010500978022222102'), bytes.fromhex('00')) is True |
There was a problem hiding this comment.
line too long (105 > 100 characters)
miio/airconditioningcompanion.py
Outdated
| slot = bytes([121 + slot]) | ||
|
|
||
| # FE + 0487 + 00007145 + 9470 + 1FFF + 7F + FF + 06 + 0042 + 27 + 4E + 0025002D008500AC01... | ||
| command = code[0:1] + model[2:8] + b'\x94\x70\x1F\xFF' + slot + b'\xFF' + code[13:16] + b'\x27' |
There was a problem hiding this comment.
line too long (103 > 100 characters)
| assert self.state().load_power == 2 | ||
| assert self.state().air_condition_model == '010500978022222102' | ||
| assert self.state().air_condition_model == \ | ||
| bytes.fromhex('010500978022222102') |
There was a problem hiding this comment.
continuation line over-indented for visual indent
miio/airconditioningcompanion.py
Outdated
|
|
||
| # FE + 0487 + 00007145 + 9470 + 1FFF + 7F + FF + 06 + 0042 + 27 + 4E + 0025002D008500AC01... | ||
| command = code[0:1] + model[2:8] + b'\x94\x70\x1F\xFF' + \ | ||
| slot + b'\xFF' + code[13:16] + b'\x27' |
There was a problem hiding this comment.
continuation line over-indented for visual indent
| def test_send_ir_code(self): | ||
| assert self.device.send_ir_code('0000000') is True | ||
| assert self.device.send_ir_code(bytes.fromhex('010500978022222102'), | ||
| bytes.fromhex('00')) is True |
There was a problem hiding this comment.
continuation line under-indented for visual indent
|
@yawor The bytearray variant is a bit hard to read now. I'm doing it correct? |
miio/airconditioningcompanion.py
Outdated
| default_output=format_output("Sending the supplied infrared command") | ||
| ) | ||
| def send_ir_code(self, model: str, code: str, slot: int=0): | ||
| def send_ir_code(self, model: bytes, code: bytes, slot: int=0): |
There was a problem hiding this comment.
I've been thinking about keeping strings here as args but converting them to bytearray/bytes right away. Having bytes as arg type for command line is probably not very good idea :). Maybe there should be two methods? Main one taking bytes like this one, but the @command should be applied to a wrapper method, which takes hex strings, converts them and calls the bytes one.
There was a problem hiding this comment.
Optionally (maybe even better solution) would be to add converting functions to click arguments definition to convert hex strings from command line to bytes.
|
@syssi seems to be OK. I would only do something about click args, because it'll probably be hard to enter raw bytes on command line. Unless click is smart enough to do this conversion automatically for bytes type. |
16bb6e4 to
c3ce22c
Compare
| @@ -1,4 +1,7 @@ | |||
| import base64 | |||
miio/airconditioningcompanion.py
Outdated
|
|
||
| try: | ||
| code = bytes.fromhex(code) | ||
| except: |
miio/airconditioningcompanion.py
Outdated
| """ | ||
| try: | ||
| model = bytes.fromhex(model) | ||
| except: |
| else: | ||
| return d | ||
|
|
||
| with open(os.path.join(os.path.dirname(__file__), |
There was a problem hiding this comment.
expected 2 blank lines after class or function definition, found 1
cp. syssi/xiaomi_airconditioningcompanion#5