|
16 | 16 | log.setLevel(level=logging.INFO) |
17 | 17 | log.setLevel(level=logging.DEBUG) # Debug hack! |
18 | 18 |
|
| 19 | +import base64 |
| 20 | + |
19 | 21 | import tinytuya |
| 22 | +from tinytuya.Contrib.RFRemoteControlDevice import RFRemoteControlDevice |
20 | 23 |
|
21 | 24 | LOCAL_KEY = '0123456789abcdef' |
22 | 25 |
|
@@ -255,5 +258,59 @@ def test_not_a_bulb(self): |
255 | 258 | self.assertDictEqual(result_payload, expected_payload) |
256 | 259 |
|
257 | 260 |
|
| 261 | +def build_mock_rf(): |
| 262 | + d = RFRemoteControlDevice('DEVICE_ID_HERE', 'IP_ADDRESS_HERE', LOCAL_KEY, control_type=1) |
| 263 | + d.set_version(3.3) |
| 264 | + d.set_value = MagicMock() |
| 265 | + return d |
| 266 | + |
| 267 | + |
| 268 | +class TestRFRemoteControlDevice(unittest.TestCase): |
| 269 | + def test_rf_decode_button_returns_dict(self): |
| 270 | + # Bug 1: rf_decode_button was missing the () call on base64.b64decode, |
| 271 | + # causing it to always return None instead of the decoded JSON dict. |
| 272 | + sample = {"study_feq": "433", "ver": "2"} |
| 273 | + encoded = base64.b64encode(json.dumps(sample).encode()).decode() |
| 274 | + |
| 275 | + result = RFRemoteControlDevice.rf_decode_button(encoded) |
| 276 | + |
| 277 | + self.assertIsNotNone(result, "rf_decode_button returned None — function was not called") |
| 278 | + self.assertDictEqual(result, sample) |
| 279 | + |
| 280 | + def test_rf_send_button_payload_structure(self): |
| 281 | + # Bug 2: send_command('rfstudy_send', ...) was building the wrong payload: |
| 282 | + # - used 'study_feq' (string) instead of 'feq' (int) |
| 283 | + # - omitted 'mode' and 'rate' fields |
| 284 | + # - omitted 'ver' inside each key dict |
| 285 | + # Bug 3: rf_send_button was forwarding study_feq from the decoded button into |
| 286 | + # feq, but feq must always be 0 so the device uses the frequency embedded in |
| 287 | + # the code. Passing the actual frequency value selects a different chip path. |
| 288 | + d = build_mock_rf() |
| 289 | + |
| 290 | + # Use a button that has study_feq set to a non-zero value to confirm it is |
| 291 | + # NOT forwarded into the payload's feq field. |
| 292 | + button_data = {"study_feq": "433", "ver": "2"} |
| 293 | + base64_code = base64.b64encode(json.dumps(button_data).encode()).decode() |
| 294 | + |
| 295 | + d.rf_send_button(base64_code) |
| 296 | + |
| 297 | + call_args = d.set_value.call_args |
| 298 | + dp = call_args[0][0] |
| 299 | + payload = json.loads(call_args[0][1]) |
| 300 | + |
| 301 | + self.assertEqual(dp, RFRemoteControlDevice.DP_SEND_IR) |
| 302 | + self.assertEqual(payload['control'], 'rfstudy_send') |
| 303 | + |
| 304 | + self.assertIn('feq', payload, "payload missing 'feq' (was 'study_feq')") |
| 305 | + self.assertNotIn('study_feq', payload, "payload must not contain 'study_feq' for rfstudy_send") |
| 306 | + self.assertIsInstance(payload['feq'], int, "'feq' must be int, not string") |
| 307 | + self.assertEqual(payload['feq'], 0, "feq must be 0 so the device uses the frequency embedded in the code") |
| 308 | + self.assertIn('mode', payload, "payload missing 'mode'") |
| 309 | + self.assertIn('rate', payload, "payload missing 'rate'") |
| 310 | + |
| 311 | + self.assertIn('key1', payload) |
| 312 | + self.assertIn('ver', payload['key1'], "key1 missing 'ver'") |
| 313 | + |
| 314 | + |
258 | 315 | if __name__ == '__main__': |
259 | 316 | unittest.main() |
0 commit comments