python-kasa 0.6.0.1__tar.gz → 0.6.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (159) hide show
  1. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/CHANGELOG.md +63 -0
  2. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/PKG-INFO +35 -13
  3. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/README.md +34 -12
  4. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/__init__.py +2 -2
  5. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/aestransport.py +137 -67
  6. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/cli.py +42 -23
  7. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/device_factory.py +4 -3
  8. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/deviceconfig.py +1 -0
  9. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/discover.py +28 -11
  10. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/exceptions.py +3 -0
  11. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/httpclient.py +20 -5
  12. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/iotprotocol.py +7 -13
  13. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/json.py +5 -1
  14. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/klaptransport.py +41 -30
  15. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/modules/emeter.py +1 -1
  16. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/protocol.py +34 -14
  17. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/smartbulb.py +2 -2
  18. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/smartdevice.py +7 -3
  19. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/smartdimmer.py +2 -2
  20. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/smartlightstrip.py +2 -2
  21. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/smartplug.py +2 -2
  22. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/smartprotocol.py +7 -13
  23. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/smartstrip.py +2 -2
  24. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tapo/tapodevice.py +19 -4
  25. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tapo/tapoplug.py +2 -2
  26. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/conftest.py +23 -7
  27. python_kasa-0.6.1/kasa/tests/fixtures/HS300(US)_2.0_1.0.12.json +90 -0
  28. python_kasa-0.6.1/kasa/tests/fixtures/KL125(US)_4.0_1.0.5.json +93 -0
  29. python_kasa-0.6.1/kasa/tests/fixtures/KL430(US)_2.0_1.0.11.json +59 -0
  30. python_kasa-0.6.1/kasa/tests/fixtures/KP115(US)_1.0_1.0.21.json +42 -0
  31. python_kasa-0.6.1/kasa/tests/fixtures/smart/EP25(US)_2.6_1.0.2.json +414 -0
  32. python_kasa-0.6.1/kasa/tests/fixtures/smart/L510E(US)_3.0_1.0.5.json +295 -0
  33. python_kasa-0.6.1/kasa/tests/fixtures/smart/L510E(US)_3.0_1.1.2.json +267 -0
  34. python_kasa-0.6.1/kasa/tests/fixtures/smart/L530E(US)_2.0_1.1.0.json +439 -0
  35. python_kasa-0.6.1/kasa/tests/fixtures/smart/L900-10(US)_1.0_1.0.11.json +428 -0
  36. python_kasa-0.6.1/kasa/tests/fixtures/smart/L920-5(US)_1.0_1.1.3.json +415 -0
  37. python_kasa-0.6.1/kasa/tests/fixtures/smart/L930-5(US)_1.0_1.1.2.json +429 -0
  38. python_kasa-0.6.1/kasa/tests/fixtures/smart/P100_1.0.0_1.3.7.json +197 -0
  39. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/fixtures/smart/P125M(US)_1.0_1.1.0.json +11 -11
  40. python_kasa-0.6.1/kasa/tests/fixtures/smart/P135(US)_1.0_1.0.5.json +317 -0
  41. python_kasa-0.6.1/kasa/tests/fixtures/smart/S500D(US)_1.0_1.0.5.json +317 -0
  42. python_kasa-0.6.1/kasa/tests/fixtures/smart/S505(US)_1.0_1.0.2.json +309 -0
  43. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/newfakes.py +3 -0
  44. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/test_aestransport.py +90 -12
  45. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/test_cli.py +49 -2
  46. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/test_device_factory.py +2 -0
  47. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/test_discovery.py +119 -2
  48. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/test_httpclient.py +2 -2
  49. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/test_klapprotocol.py +8 -5
  50. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/test_protocol.py +2 -2
  51. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/pyproject.toml +10 -3
  52. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/LICENSE +0 -0
  53. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/credentials.py +0 -0
  54. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/device_type.py +0 -0
  55. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/effects.py +0 -0
  56. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/emeterstatus.py +0 -0
  57. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/modules/__init__.py +0 -0
  58. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/modules/ambientlight.py +0 -0
  59. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/modules/antitheft.py +0 -0
  60. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/modules/cloud.py +0 -0
  61. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/modules/countdown.py +0 -0
  62. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/modules/module.py +0 -0
  63. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/modules/motion.py +0 -0
  64. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/modules/rulemodule.py +0 -0
  65. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/modules/schedule.py +0 -0
  66. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/modules/time.py +0 -0
  67. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/modules/usage.py +0 -0
  68. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/py.typed +0 -0
  69. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tapo/__init__.py +0 -0
  70. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tapo/tapobulb.py +0 -0
  71. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/__init__.py +0 -0
  72. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/fixtures/EP10(US)_1.0_1.0.2.json +0 -0
  73. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/fixtures/EP40(US)_1.0_1.0.2.json +0 -0
  74. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/fixtures/ES20M(US)_1.0_1.0.8.json +0 -0
  75. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/fixtures/HS100(UK)_1.0_1.2.6.json +0 -0
  76. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/fixtures/HS100(UK)_4.1_1.1.0.json +0 -0
  77. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/fixtures/HS100(US)_1.0_1.2.5.json +0 -0
  78. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/fixtures/HS100(US)_2.0_1.5.6.json +0 -0
  79. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/fixtures/HS103(US)_1.0_1.5.7.json +0 -0
  80. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/fixtures/HS103(US)_2.1_1.1.2.json +0 -0
  81. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/fixtures/HS103(US)_2.1_1.1.4.json +0 -0
  82. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/fixtures/HS105(US)_1.0_1.5.6.json +0 -0
  83. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/fixtures/HS105(US)_1.0_mocked.json +0 -0
  84. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/fixtures/HS107(US)_1.0_1.0.8.json +0 -0
  85. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/fixtures/HS110(EU)_1.0_1.2.5.json +0 -0
  86. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/fixtures/HS110(EU)_2.0_mocked.json +0 -0
  87. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/fixtures/HS110(EU)_4.0_1.0.4.json +0 -0
  88. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/fixtures/HS110(US)_1.0_mocked.json +0 -0
  89. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/fixtures/HS200(US)_1.0_mocked.json +0 -0
  90. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/fixtures/HS200(US)_2.0_1.5.7.json +0 -0
  91. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/fixtures/HS200(US)_5.0_1.0.2.json +0 -0
  92. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/fixtures/HS210(US)_1.0_1.5.8.json +0 -0
  93. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/fixtures/HS220(US)_1.0_1.5.7.json +0 -0
  94. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/fixtures/HS220(US)_1.0_mocked.json +0 -0
  95. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/fixtures/HS220(US)_2.0_1.0.3.json +0 -0
  96. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/fixtures/HS300(US)_1.0_1.0.10.json +0 -0
  97. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/fixtures/HS300(US)_2.0_1.0.3.json +0 -0
  98. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/fixtures/KL110(US)_1.0_1.8.11.json +0 -0
  99. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/fixtures/KL120(US)_1.0_1.8.6.json +0 -0
  100. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/fixtures/KL125(US)_1.20_1.0.5.json +0 -0
  101. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/fixtures/KL125(US)_2.0_1.0.7.json +0 -0
  102. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/fixtures/KL130(EU)_1.0_1.8.8.json +0 -0
  103. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/fixtures/KL130(US)_1.0_1.8.11.json +0 -0
  104. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/fixtures/KL135(US)_1.0_1.0.6.json +0 -0
  105. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/fixtures/KL400L5(US)_1.0_1.0.5.json +0 -0
  106. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/fixtures/KL400L5(US)_1.0_1.0.8.json +0 -0
  107. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/fixtures/KL420L5(US)_1.0_1.0.2.json +0 -0
  108. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/fixtures/KL430(UN)_2.0_1.0.8.json +0 -0
  109. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/fixtures/KL430(US)_1.0_1.0.10.json +0 -0
  110. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/fixtures/KL430(US)_2.0_1.0.8.json +0 -0
  111. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/fixtures/KL430(US)_2.0_1.0.9.json +0 -0
  112. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/fixtures/KL50(US)_1.0_1.1.13.json +0 -0
  113. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/fixtures/KL60(UN)_1.0_1.1.4.json +0 -0
  114. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/fixtures/KL60(US)_1.0_1.1.13.json +0 -0
  115. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/fixtures/KP100(US)_3.0_1.0.1.json +0 -0
  116. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/fixtures/KP105(UK)_1.0_1.0.5.json +0 -0
  117. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/fixtures/KP105(UK)_1.0_1.0.7.json +0 -0
  118. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/fixtures/KP115(EU)_1.0_1.0.16.json +0 -0
  119. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/fixtures/KP115(US)_1.0_1.0.17.json +0 -0
  120. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/fixtures/KP125(US)_1.0_1.0.6.json +0 -0
  121. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/fixtures/KP200(US)_3.0_1.0.3.json +0 -0
  122. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/fixtures/KP303(UK)_1.0_1.0.3.json +0 -0
  123. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/fixtures/KP303(US)_2.0_1.0.3.json +0 -0
  124. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/fixtures/KP400(US)_1.0_1.0.10.json +0 -0
  125. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/fixtures/KP400(US)_2.0_1.0.6.json +0 -0
  126. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/fixtures/KP401(US)_1.0_1.0.0.json +0 -0
  127. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/fixtures/KP405(US)_1.0_1.0.5.json +0 -0
  128. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/fixtures/KS200M(US)_1.0_1.0.8.json +0 -0
  129. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/fixtures/KS220M(US)_1.0_1.0.4.json +0 -0
  130. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/fixtures/KS230(US)_1.0_1.0.14.json +0 -0
  131. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/fixtures/LB100(US)_1.0_mocked.json +0 -0
  132. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/fixtures/LB110(US)_1.0_1.8.11.json +0 -0
  133. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/fixtures/LB120(US)_1.0_mocked.json +0 -0
  134. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/fixtures/LB130(US)_1.0_mocked.json +0 -0
  135. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/fixtures/smart/EP25(US)_2.6_1.0.1.json +0 -0
  136. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/fixtures/smart/KP125M(US)_1.0_1.1.3.json +0 -0
  137. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/fixtures/smart/KS205(US)_1.0_1.0.2.json +0 -0
  138. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/fixtures/smart/KS225(US)_1.0_1.0.2.json +0 -0
  139. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/fixtures/smart/L510B(EU)_3.0_1.0.5.json +0 -0
  140. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/fixtures/smart/L530E(EU)_3.0_1.0.6.json +0 -0
  141. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/fixtures/smart/L530E(EU)_3.0_1.1.0.json +0 -0
  142. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/fixtures/smart/L900-10(EU)_1.0_1.0.17.json +0 -0
  143. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/fixtures/smart/L900-5(EU)_1.0_1.0.17.json +0 -0
  144. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/fixtures/smart/L900-5(EU)_1.0_1.1.0.json +0 -0
  145. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/fixtures/smart/L920-5(US)_1.0_1.1.0.json +0 -0
  146. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/fixtures/smart/P110(EU)_1.0_1.2.3.json +0 -0
  147. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/fixtures/smart/P110(UK)_1.0_1.3.0.json +0 -0
  148. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/test_bulb.py +0 -0
  149. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/test_device_type.py +0 -0
  150. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/test_deviceconfig.py +0 -0
  151. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/test_dimmer.py +0 -0
  152. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/test_emeter.py +0 -0
  153. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/test_lightstrip.py +0 -0
  154. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/test_plug.py +0 -0
  155. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/test_readme_examples.py +0 -0
  156. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/test_smartdevice.py +0 -0
  157. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/test_smartprotocol.py +0 -0
  158. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/test_strip.py +0 -0
  159. {python_kasa-0.6.0.1 → python_kasa-0.6.1}/kasa/tests/test_usage.py +0 -0
@@ -1,5 +1,67 @@
1
1
  # Changelog
2
2
 
3
+ ## [0.6.1](https://github.com/python-kasa/python-kasa/tree/0.6.1) (2024-01-25)
4
+
5
+ [Full Changelog](https://github.com/python-kasa/python-kasa/compare/0.6.0.1...0.6.1)
6
+
7
+ Release highlights:
8
+ * Support for tapo wall switches
9
+ * Support for unprovisioned devices
10
+ * Performance and stability improvements
11
+
12
+ **Implemented enhancements:**
13
+
14
+ - Add support for tapo wall switches \(S500D\) [\#704](https://github.com/python-kasa/python-kasa/pull/704) (@bdraco)
15
+ - Add new cli command 'command' to execute arbitrary commands [\#692](https://github.com/python-kasa/python-kasa/pull/692) (@rytilahti)
16
+ - Allow raw-command and wifi without update [\#688](https://github.com/python-kasa/python-kasa/pull/688) (@rytilahti)
17
+ - Generate AES KeyPair lazily [\#687](https://github.com/python-kasa/python-kasa/pull/687) (@sdb9696)
18
+ - Add reboot and factory\_reset to tapodevice [\#686](https://github.com/python-kasa/python-kasa/pull/686) (@rytilahti)
19
+ - Try default tapo credentials for klap and aes [\#685](https://github.com/python-kasa/python-kasa/pull/685) (@sdb9696)
20
+ - Sleep between discovery packets [\#656](https://github.com/python-kasa/python-kasa/pull/656) (@sdb9696)
21
+
22
+ **Fixed bugs:**
23
+
24
+ - Do not crash on missing geolocation [\#701](https://github.com/python-kasa/python-kasa/pull/701) (@rytilahti)
25
+ - Fix P100 error getting conn closed when trying default login after login failure [\#690](https://github.com/python-kasa/python-kasa/pull/690) (@sdb9696)
26
+
27
+ **Documentation updates:**
28
+
29
+ - Add protocol and transport documentation [\#663](https://github.com/python-kasa/python-kasa/pull/663) (@sdb9696)
30
+ - Document authenticated provisioning [\#634](https://github.com/python-kasa/python-kasa/pull/634) (@rytilahti)
31
+
32
+ **Closed issues:**
33
+
34
+ - Consider handshake as still valid on ServerDisconnectedError [\#676](https://github.com/python-kasa/python-kasa/issues/676)
35
+ - AES Transport creates the key even if the device is offline [\#675](https://github.com/python-kasa/python-kasa/issues/675)
36
+ - how to provision new Tapo plug devices? [\#565](https://github.com/python-kasa/python-kasa/issues/565)
37
+ - Space out discovery requests [\#229](https://github.com/python-kasa/python-kasa/issues/229)
38
+
39
+ **Merged pull requests:**
40
+
41
+ - Add additional L900-10 fixture [\#707](https://github.com/python-kasa/python-kasa/pull/707) (@bdraco)
42
+ - Replace rich formatting stripper [\#706](https://github.com/python-kasa/python-kasa/pull/706) (@bdraco)
43
+ - Add support for the S500 [\#705](https://github.com/python-kasa/python-kasa/pull/705) (@bdraco)
44
+ - Fix overly greedy \_strip\_rich\_formatting [\#703](https://github.com/python-kasa/python-kasa/pull/703) (@bdraco)
45
+ - Ensure login token is only sent if aes state is ESTABLISHED [\#702](https://github.com/python-kasa/python-kasa/pull/702) (@bdraco)
46
+ - Update readme fixture checker and readme [\#699](https://github.com/python-kasa/python-kasa/pull/699) (@rytilahti)
47
+ - Fix test\_klapprotocol test duration [\#698](https://github.com/python-kasa/python-kasa/pull/698) (@sdb9696)
48
+ - Renew the handshake session 20 minutes before we think it will expire [\#697](https://github.com/python-kasa/python-kasa/pull/697) (@bdraco)
49
+ - Add --batch-size hint to timeout errors in dump\_devinfo [\#696](https://github.com/python-kasa/python-kasa/pull/696) (@sdb9696)
50
+ - Add L930-5 fixture [\#694](https://github.com/python-kasa/python-kasa/pull/694) (@bdraco)
51
+ - Add fixtures for L510E [\#693](https://github.com/python-kasa/python-kasa/pull/693) (@bdraco)
52
+ - Refactor aestransport to use a state enum [\#691](https://github.com/python-kasa/python-kasa/pull/691) (@bdraco)
53
+ - Update transport close/reset behaviour [\#689](https://github.com/python-kasa/python-kasa/pull/689) (@sdb9696)
54
+ - Check README for supported models [\#684](https://github.com/python-kasa/python-kasa/pull/684) (@rytilahti)
55
+ - Add P100 test fixture [\#683](https://github.com/python-kasa/python-kasa/pull/683) (@bdraco)
56
+ - Make dump\_devinfo request batch size configurable [\#681](https://github.com/python-kasa/python-kasa/pull/681) (@sdb9696)
57
+ - Add updated L920 fixture [\#680](https://github.com/python-kasa/python-kasa/pull/680) (@bdraco)
58
+ - Update fixtures from test devices [\#679](https://github.com/python-kasa/python-kasa/pull/679) (@bdraco)
59
+ - Show discovery data for state with verbose [\#678](https://github.com/python-kasa/python-kasa/pull/678) (@rytilahti)
60
+ - Add L530E\(US\) fixture [\#674](https://github.com/python-kasa/python-kasa/pull/674) (@bdraco)
61
+ - Add P135 fixture [\#673](https://github.com/python-kasa/python-kasa/pull/673) (@bdraco)
62
+ - Rename base TPLinkProtocol to BaseProtocol [\#669](https://github.com/python-kasa/python-kasa/pull/669) (@sdb9696)
63
+ - Add 1003 \(TRANSPORT\_UNKNOWN\_CREDENTIALS\_ERROR\) [\#667](https://github.com/python-kasa/python-kasa/pull/667) (@rytilahti)
64
+
3
65
  ## [0.6.0.1](https://github.com/python-kasa/python-kasa/tree/0.6.0.1) (2024-01-21)
4
66
 
5
67
  [Full Changelog](https://github.com/python-kasa/python-kasa/compare/0.6.0...0.6.0.1)
@@ -17,6 +79,7 @@ A patch release to improve the protocol handling.
17
79
 
18
80
  **Merged pull requests:**
19
81
 
82
+ - Release 0.6.0.1 [\#666](https://github.com/python-kasa/python-kasa/pull/666) (@rytilahti)
20
83
  - Add l900-5 1.1.0 fixture [\#664](https://github.com/python-kasa/python-kasa/pull/664) (@rytilahti)
21
84
  - Add fixtures with new MAC mask [\#661](https://github.com/python-kasa/python-kasa/pull/661) (@sdb9696)
22
85
  - Make close behaviour consistent across new protocols and transports [\#660](https://github.com/python-kasa/python-kasa/pull/660) (@sdb9696)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: python-kasa
3
- Version: 0.6.0.1
3
+ Version: 0.6.1
4
4
  Summary: Python API for TP-Link Kasa Smarthome devices
5
5
  Home-page: https://github.com/python-kasa/python-kasa
6
6
  License: GPL-3.0-or-later
@@ -250,7 +250,7 @@ Note, that this works currently only on kasa-branded devices which use port 9999
250
250
  In principle, most kasa-branded devices that are locally controllable using the official Kasa mobile app work with this library.
251
251
 
252
252
  The following lists the devices that have been manually verified to work.
253
- **If your device is unlisted but working, please open a pull request to update the list and add a fixture file (use `devtools/dump_devinfo.py` to generate one).**
253
+ **If your device is unlisted but working, please open a pull request to update the list and add a fixture file (use `python -m devtools.dump_devinfo` to generate one).**
254
254
 
255
255
  ### Plugs
256
256
 
@@ -263,10 +263,10 @@ The following lists the devices that have been manually verified to work.
263
263
  * KP105
264
264
  * KP115
265
265
  * KP125
266
- * KP125M [See note below](#tapo-and-newer-kasa-branded-devices)
266
+ * KP125M [See note below](#newer-kasa-branded-devices)
267
267
  * KP401
268
268
  * EP10
269
- * EP25 [See note below](#tapo-and-newer-kasa-branded-devices)
269
+ * EP25 [See note below](#newer-kasa-branded-devices)
270
270
 
271
271
  ### Power Strips
272
272
 
@@ -308,18 +308,37 @@ The following lists the devices that have been manually verified to work.
308
308
  * KL420L5
309
309
  * KL430
310
310
 
311
- ### Tapo and newer Kasa branded devices
311
+ ### Tapo branded devices
312
312
 
313
313
  The library has recently added a limited supported for devices that carry Tapo branding.
314
314
 
315
315
  At the moment, the following devices have been confirmed to work:
316
316
 
317
- * Tapo P110 (plug)
318
- * Tapo L530E (bulb)
319
- * Tapo L900-5 (led strip)
320
- * Tapo L900-10 (led strip)
321
- * Kasa KS205 (Wifi/Matter Wall Switch)
322
- * Kasa KS225 (Wifi/Matter Wall Dimmer Switch)
317
+ #### Plugs
318
+
319
+ * Tapo P110
320
+ * Tapo P125M
321
+ * Tapo P135 (dimming not yet supported)
322
+
323
+ #### Bulbs
324
+
325
+ * Tapo L510B
326
+ * Tapo L510E
327
+ * Tapo L530E
328
+
329
+ #### Light strips
330
+
331
+ * Tapo L900-5
332
+ * Tapo L900-10
333
+ * Tapo L920-5
334
+ * Tapo L930-5
335
+
336
+ #### Wall switches
337
+
338
+ * Tapo S500D
339
+ * Tapo S505
340
+
341
+ ### Newer Kasa branded devices
323
342
 
324
343
  Some newer hardware versions of Kasa branded devices are now using the same protocol as
325
344
  Tapo branded devices. Support for these devices is currently limited as per TAPO branded
@@ -327,8 +346,11 @@ devices:
327
346
 
328
347
  * Kasa EP25 (plug) hw_version 2.6
329
348
  * Kasa KP125M (plug)
349
+ * Kasa KS205 (Wifi/Matter Wall Switch)
350
+ * Kasa KS225 (Wifi/Matter Wall Dimmer Switch)
351
+
330
352
 
331
- **If your device is unlisted but working, please open a pull request to update the list and add a fixture file (use `devtools/dump_devinfo.py` to generate one).**
353
+ **If your device is unlisted but working, please open a pull request to update the list and add a fixture file (use `python -m devtools.dump_devinfo` to generate one).**
332
354
 
333
355
  ## Resources
334
356
 
@@ -355,7 +377,7 @@ use it directly you should expect it could break in future releases until this s
355
377
  Other TAPO libraries are:
356
378
 
357
379
  * [PyTapo - Python library for communication with Tapo Cameras](https://github.com/JurajNyiri/pytapo)
358
- * [Tapo P100 (Tapo P105/P100 plugs, Tapo L510E bulbs)](https://github.com/fishbigger/TapoP100)
380
+ * [Tapo P100 (Tapo plugs, Tapo bulbs)](https://github.com/fishbigger/TapoP100)
359
381
  * [Home Assistant integration](https://github.com/fishbigger/HomeAssistant-Tapo-P100-Control)
360
382
  * [plugp100, another tapo library](https://github.com/petretiandrea/plugp100)
361
383
  * [Home Assistant integration](https://github.com/petretiandrea/home-assistant-tapo-p100)
@@ -215,7 +215,7 @@ Note, that this works currently only on kasa-branded devices which use port 9999
215
215
  In principle, most kasa-branded devices that are locally controllable using the official Kasa mobile app work with this library.
216
216
 
217
217
  The following lists the devices that have been manually verified to work.
218
- **If your device is unlisted but working, please open a pull request to update the list and add a fixture file (use `devtools/dump_devinfo.py` to generate one).**
218
+ **If your device is unlisted but working, please open a pull request to update the list and add a fixture file (use `python -m devtools.dump_devinfo` to generate one).**
219
219
 
220
220
  ### Plugs
221
221
 
@@ -228,10 +228,10 @@ The following lists the devices that have been manually verified to work.
228
228
  * KP105
229
229
  * KP115
230
230
  * KP125
231
- * KP125M [See note below](#tapo-and-newer-kasa-branded-devices)
231
+ * KP125M [See note below](#newer-kasa-branded-devices)
232
232
  * KP401
233
233
  * EP10
234
- * EP25 [See note below](#tapo-and-newer-kasa-branded-devices)
234
+ * EP25 [See note below](#newer-kasa-branded-devices)
235
235
 
236
236
  ### Power Strips
237
237
 
@@ -273,18 +273,37 @@ The following lists the devices that have been manually verified to work.
273
273
  * KL420L5
274
274
  * KL430
275
275
 
276
- ### Tapo and newer Kasa branded devices
276
+ ### Tapo branded devices
277
277
 
278
278
  The library has recently added a limited supported for devices that carry Tapo branding.
279
279
 
280
280
  At the moment, the following devices have been confirmed to work:
281
281
 
282
- * Tapo P110 (plug)
283
- * Tapo L530E (bulb)
284
- * Tapo L900-5 (led strip)
285
- * Tapo L900-10 (led strip)
286
- * Kasa KS205 (Wifi/Matter Wall Switch)
287
- * Kasa KS225 (Wifi/Matter Wall Dimmer Switch)
282
+ #### Plugs
283
+
284
+ * Tapo P110
285
+ * Tapo P125M
286
+ * Tapo P135 (dimming not yet supported)
287
+
288
+ #### Bulbs
289
+
290
+ * Tapo L510B
291
+ * Tapo L510E
292
+ * Tapo L530E
293
+
294
+ #### Light strips
295
+
296
+ * Tapo L900-5
297
+ * Tapo L900-10
298
+ * Tapo L920-5
299
+ * Tapo L930-5
300
+
301
+ #### Wall switches
302
+
303
+ * Tapo S500D
304
+ * Tapo S505
305
+
306
+ ### Newer Kasa branded devices
288
307
 
289
308
  Some newer hardware versions of Kasa branded devices are now using the same protocol as
290
309
  Tapo branded devices. Support for these devices is currently limited as per TAPO branded
@@ -292,8 +311,11 @@ devices:
292
311
 
293
312
  * Kasa EP25 (plug) hw_version 2.6
294
313
  * Kasa KP125M (plug)
314
+ * Kasa KS205 (Wifi/Matter Wall Switch)
315
+ * Kasa KS225 (Wifi/Matter Wall Dimmer Switch)
316
+
295
317
 
296
- **If your device is unlisted but working, please open a pull request to update the list and add a fixture file (use `devtools/dump_devinfo.py` to generate one).**
318
+ **If your device is unlisted but working, please open a pull request to update the list and add a fixture file (use `python -m devtools.dump_devinfo` to generate one).**
297
319
 
298
320
  ## Resources
299
321
 
@@ -320,7 +342,7 @@ use it directly you should expect it could break in future releases until this s
320
342
  Other TAPO libraries are:
321
343
 
322
344
  * [PyTapo - Python library for communication with Tapo Cameras](https://github.com/JurajNyiri/pytapo)
323
- * [Tapo P100 (Tapo P105/P100 plugs, Tapo L510E bulbs)](https://github.com/fishbigger/TapoP100)
345
+ * [Tapo P100 (Tapo plugs, Tapo bulbs)](https://github.com/fishbigger/TapoP100)
324
346
  * [Home Assistant integration](https://github.com/fishbigger/HomeAssistant-Tapo-P100-Control)
325
347
  * [plugp100, another tapo library](https://github.com/petretiandrea/plugp100)
326
348
  * [Home Assistant integration](https://github.com/petretiandrea/home-assistant-tapo-p100)
@@ -29,7 +29,7 @@ from kasa.exceptions import (
29
29
  UnsupportedDeviceException,
30
30
  )
31
31
  from kasa.iotprotocol import IotProtocol
32
- from kasa.protocol import TPLinkProtocol, TPLinkSmartHomeProtocol
32
+ from kasa.protocol import BaseProtocol, TPLinkSmartHomeProtocol
33
33
  from kasa.smartbulb import SmartBulb, SmartBulbPreset, TurnOnBehavior, TurnOnBehaviors
34
34
  from kasa.smartdevice import DeviceType, SmartDevice
35
35
  from kasa.smartdimmer import SmartDimmer
@@ -44,7 +44,7 @@ __version__ = version("python-kasa")
44
44
  __all__ = [
45
45
  "Discover",
46
46
  "TPLinkSmartHomeProtocol",
47
- "TPLinkProtocol",
47
+ "BaseProtocol",
48
48
  "IotProtocol",
49
49
  "SmartProtocol",
50
50
  "SmartBulb",
@@ -8,7 +8,8 @@ import base64
8
8
  import hashlib
9
9
  import logging
10
10
  import time
11
- from typing import Dict, Optional, cast
11
+ from enum import Enum, auto
12
+ from typing import TYPE_CHECKING, Any, AsyncGenerator, Dict, Optional, Tuple, cast
12
13
 
13
14
  from cryptography.hazmat.primitives import padding, serialization
14
15
  from cryptography.hazmat.primitives.asymmetric import padding as asymmetric_padding
@@ -30,17 +31,29 @@ from .exceptions import (
30
31
  from .httpclient import HttpClient
31
32
  from .json import dumps as json_dumps
32
33
  from .json import loads as json_loads
33
- from .protocol import BaseTransport
34
+ from .protocol import DEFAULT_CREDENTIALS, BaseTransport, get_default_credentials
34
35
 
35
36
  _LOGGER = logging.getLogger(__name__)
36
37
 
37
38
 
39
+ ONE_DAY_SECONDS = 86400
40
+ SESSION_EXPIRE_BUFFER_SECONDS = 60 * 20
41
+
42
+
38
43
  def _sha1(payload: bytes) -> str:
39
44
  sha1_algo = hashlib.sha1() # noqa: S324
40
45
  sha1_algo.update(payload)
41
46
  return sha1_algo.hexdigest()
42
47
 
43
48
 
49
+ class TransportState(Enum):
50
+ """Enum for AES state."""
51
+
52
+ HANDSHAKE_REQUIRED = auto() # Handshake needed
53
+ LOGIN_REQUIRED = auto() # Login needed
54
+ ESTABLISHED = auto() # Ready to send requests
55
+
56
+
44
57
  class AesTransport(BaseTransport):
45
58
  """Implementation of the AES encryption protocol.
46
59
 
@@ -50,11 +63,14 @@ class AesTransport(BaseTransport):
50
63
 
51
64
  DEFAULT_PORT: int = 80
52
65
  SESSION_COOKIE_NAME = "TP_SESSIONID"
66
+ TIMEOUT_COOKIE_NAME = "TIMEOUT"
53
67
  COMMON_HEADERS = {
54
68
  "Content-Type": "application/json",
55
69
  "requestByApp": "true",
56
70
  "Accept": "application/json",
57
71
  }
72
+ CONTENT_LENGTH = "Content-Length"
73
+ KEY_PAIR_CONTENT_LENGTH = 314
58
74
 
59
75
  def __init__(
60
76
  self,
@@ -69,27 +85,29 @@ class AesTransport(BaseTransport):
69
85
  ) and not self._credentials_hash:
70
86
  self._credentials = Credentials()
71
87
  if self._credentials:
72
- self._login_params = self._get_login_params()
88
+ self._login_params = self._get_login_params(self._credentials)
73
89
  else:
74
90
  self._login_params = json_loads(
75
91
  base64.b64decode(self._credentials_hash.encode()).decode() # type: ignore[union-attr]
76
92
  )
77
-
93
+ self._default_credentials: Optional[Credentials] = None
78
94
  self._http_client: HttpClient = HttpClient(config)
79
95
 
80
- self._handshake_done = False
96
+ self._state = TransportState.HANDSHAKE_REQUIRED
81
97
 
82
98
  self._encryption_session: Optional[AesEncyptionSession] = None
83
99
  self._session_expire_at: Optional[float] = None
84
100
 
85
101
  self._session_cookie: Optional[Dict[str, str]] = None
86
102
 
87
- self._login_token = None
103
+ self._login_token: Optional[str] = None
104
+
105
+ self._key_pair: Optional[KeyPair] = None
88
106
 
89
107
  _LOGGER.debug("Created AES transport for %s", self._host)
90
108
 
91
109
  @property
92
- def default_port(self):
110
+ def default_port(self) -> int:
93
111
  """Default port for the transport."""
94
112
  return self.DEFAULT_PORT
95
113
 
@@ -98,29 +116,25 @@ class AesTransport(BaseTransport):
98
116
  """The hashed credentials used by the transport."""
99
117
  return base64.b64encode(json_dumps(self._login_params).encode()).decode()
100
118
 
101
- def _get_login_params(self):
119
+ def _get_login_params(self, credentials: Credentials) -> Dict[str, str]:
102
120
  """Get the login parameters based on the login_version."""
103
- un, pw = self.hash_credentials(self._login_version == 2)
121
+ un, pw = self.hash_credentials(self._login_version == 2, credentials)
104
122
  password_field_name = "password2" if self._login_version == 2 else "password"
105
123
  return {password_field_name: pw, "username": un}
106
124
 
107
- def hash_credentials(self, login_v2):
125
+ @staticmethod
126
+ def hash_credentials(login_v2: bool, credentials: Credentials) -> Tuple[str, str]:
108
127
  """Hash the credentials."""
128
+ un = base64.b64encode(_sha1(credentials.username.encode()).encode()).decode()
109
129
  if login_v2:
110
- un = base64.b64encode(
111
- _sha1(self._credentials.username.encode()).encode()
112
- ).decode()
113
130
  pw = base64.b64encode(
114
- _sha1(self._credentials.password.encode()).encode()
131
+ _sha1(credentials.password.encode()).encode()
115
132
  ).decode()
116
133
  else:
117
- un = base64.b64encode(
118
- _sha1(self._credentials.username.encode()).encode()
119
- ).decode()
120
- pw = base64.b64encode(self._credentials.password.encode()).decode()
134
+ pw = base64.b64encode(credentials.password.encode()).decode()
121
135
  return un, pw
122
136
 
123
- def _handle_response_error_code(self, resp_dict: dict, msg: str):
137
+ def _handle_response_error_code(self, resp_dict: Any, msg: str) -> None:
124
138
  error_code = SmartErrorCode(resp_dict.get("error_code")) # type: ignore[arg-type]
125
139
  if error_code == SmartErrorCode.SUCCESS:
126
140
  return
@@ -130,15 +144,14 @@ class AesTransport(BaseTransport):
130
144
  if error_code in SMART_RETRYABLE_ERRORS:
131
145
  raise RetryableException(msg, error_code=error_code)
132
146
  if error_code in SMART_AUTHENTICATION_ERRORS:
133
- self._handshake_done = False
134
- self._login_token = None
147
+ self._state = TransportState.HANDSHAKE_REQUIRED
135
148
  raise AuthenticationException(msg, error_code=error_code)
136
149
  raise SmartDeviceException(msg, error_code=error_code)
137
150
 
138
- async def send_secure_passthrough(self, request: str):
151
+ async def send_secure_passthrough(self, request: str) -> Dict[str, Any]:
139
152
  """Send encrypted message as passthrough."""
140
153
  url = f"http://{self._host}/app"
141
- if self._login_token:
154
+ if self._state is TransportState.ESTABLISHED and self._login_token:
142
155
  url += f"?token={self._login_token}"
143
156
 
144
157
  encrypted_payload = self._encryption_session.encrypt(request.encode()) # type: ignore
@@ -160,23 +173,50 @@ class AesTransport(BaseTransport):
160
173
  + f"status code {status_code} to passthrough"
161
174
  )
162
175
 
163
- resp_dict = cast(Dict, resp_dict)
164
176
  self._handle_response_error_code(
165
177
  resp_dict, "Error sending secure_passthrough message"
166
178
  )
167
179
 
168
- response = self._encryption_session.decrypt( # type: ignore
169
- resp_dict["result"]["response"].encode()
170
- )
171
- resp_dict = json_loads(response)
172
- return resp_dict
180
+ if TYPE_CHECKING:
181
+ resp_dict = cast(Dict[str, Any], resp_dict)
182
+ assert self._encryption_session is not None
183
+
184
+ raw_response: str = resp_dict["result"]["response"]
185
+ response = self._encryption_session.decrypt(raw_response.encode())
186
+ return json_loads(response) # type: ignore[return-value]
173
187
 
174
188
  async def perform_login(self):
175
189
  """Login to the device."""
176
- self._login_token = None
190
+ try:
191
+ await self.try_login(self._login_params)
192
+ except AuthenticationException as aex:
193
+ try:
194
+ if aex.error_code is not SmartErrorCode.LOGIN_ERROR:
195
+ raise aex
196
+ if self._default_credentials is None:
197
+ self._default_credentials = get_default_credentials(
198
+ DEFAULT_CREDENTIALS["TAPO"]
199
+ )
200
+ await self.perform_handshake()
201
+ await self.try_login(self._get_login_params(self._default_credentials))
202
+ _LOGGER.debug(
203
+ "%s: logged in with default credentials",
204
+ self._host,
205
+ )
206
+ except AuthenticationException:
207
+ raise
208
+ except Exception as ex:
209
+ raise AuthenticationException(
210
+ "Unable to login and trying default "
211
+ + "login raised another exception: %s",
212
+ ex,
213
+ ) from ex
214
+
215
+ async def try_login(self, login_params: Dict[str, Any]) -> None:
216
+ """Try to login with supplied login_params."""
177
217
  login_request = {
178
218
  "method": "login_device",
179
- "params": self._login_params,
219
+ "params": login_params,
180
220
  "request_time_milis": round(time.time() * 1000),
181
221
  }
182
222
  request = json_dumps(login_request)
@@ -184,39 +224,52 @@ class AesTransport(BaseTransport):
184
224
  resp_dict = await self.send_secure_passthrough(request)
185
225
  self._handle_response_error_code(resp_dict, "Error logging in")
186
226
  self._login_token = resp_dict["result"]["token"]
227
+ self._state = TransportState.ESTABLISHED
187
228
 
188
- async def perform_handshake(self):
189
- """Perform the handshake."""
190
- _LOGGER.debug("Will perform handshaking...")
191
- _LOGGER.debug("Generating keypair")
192
-
193
- self._handshake_done = False
194
- self._session_expire_at = None
195
- self._session_cookie = None
196
-
197
- url = f"http://{self._host}/app"
198
- key_pair = KeyPair.create_key_pair()
229
+ async def _generate_key_pair_payload(self) -> AsyncGenerator:
230
+ """Generate the request body and return an ascyn_generator.
199
231
 
232
+ This prevents the key pair being generated unless a connection
233
+ can be made to the device.
234
+ """
235
+ _LOGGER.debug("Generating keypair")
236
+ self._key_pair = KeyPair.create_key_pair()
200
237
  pub_key = (
201
238
  "-----BEGIN PUBLIC KEY-----\n"
202
- + key_pair.get_public_key()
239
+ + self._key_pair.get_public_key() # type: ignore[union-attr]
203
240
  + "\n-----END PUBLIC KEY-----\n"
204
241
  )
205
242
  handshake_params = {"key": pub_key}
206
243
  _LOGGER.debug(f"Handshake params: {handshake_params}")
207
-
208
244
  request_body = {"method": "handshake", "params": handshake_params}
209
-
210
245
  _LOGGER.debug(f"Request {request_body}")
246
+ yield json_dumps(request_body).encode()
211
247
 
212
- status_code, resp_dict = await self._http_client.post(
248
+ async def perform_handshake(self) -> None:
249
+ """Perform the handshake."""
250
+ _LOGGER.debug("Will perform handshaking...")
251
+
252
+ self._key_pair = None
253
+ self._login_token = None
254
+ self._session_expire_at = None
255
+ self._session_cookie = None
256
+
257
+ url = f"http://{self._host}/app"
258
+ # Device needs the content length or it will response with 500
259
+ headers = {
260
+ **self.COMMON_HEADERS,
261
+ self.CONTENT_LENGTH: str(self.KEY_PAIR_CONTENT_LENGTH),
262
+ }
263
+ http_client = self._http_client
264
+
265
+ status_code, resp_dict = await http_client.post(
213
266
  url,
214
- json=request_body,
215
- headers=self.COMMON_HEADERS,
267
+ json=self._generate_key_pair_payload(),
268
+ headers=headers,
216
269
  cookies_dict=self._session_cookie,
217
270
  )
218
271
 
219
- _LOGGER.debug(f"Device responded with: {resp_dict}")
272
+ _LOGGER.debug("Device responded with: %s", resp_dict)
220
273
 
221
274
  if status_code != 200:
222
275
  raise SmartDeviceException(
@@ -226,25 +279,32 @@ class AesTransport(BaseTransport):
226
279
 
227
280
  self._handle_response_error_code(resp_dict, "Unable to complete handshake")
228
281
 
282
+ if TYPE_CHECKING:
283
+ resp_dict = cast(Dict[str, Any], resp_dict)
284
+
229
285
  handshake_key = resp_dict["result"]["key"]
230
286
 
231
287
  if (
232
- cookie := self._http_client.get_cookie( # type: ignore
233
- self.SESSION_COOKIE_NAME
234
- )
288
+ cookie := http_client.get_cookie(self.SESSION_COOKIE_NAME) # type: ignore
235
289
  ) or (
236
- cookie := self._http_client.get_cookie( # type: ignore
237
- "SESSIONID"
238
- )
290
+ cookie := http_client.get_cookie("SESSIONID") # type: ignore
239
291
  ):
240
292
  self._session_cookie = {self.SESSION_COOKIE_NAME: cookie}
241
293
 
242
- self._session_expire_at = time.time() + 86400
294
+ timeout = int(
295
+ http_client.get_cookie(self.TIMEOUT_COOKIE_NAME) or ONE_DAY_SECONDS
296
+ )
297
+ # There is a 24 hour timeout on the session cookie
298
+ # but the clock on the device is not always accurate
299
+ # so we set the expiry to 24 hours from now minus a buffer
300
+ self._session_expire_at = time.time() + timeout - SESSION_EXPIRE_BUFFER_SECONDS
301
+ if TYPE_CHECKING:
302
+ assert self._key_pair is not None
243
303
  self._encryption_session = AesEncyptionSession.create_from_keypair(
244
- handshake_key, key_pair
304
+ handshake_key, self._key_pair
245
305
  )
246
306
 
247
- self._handshake_done = True
307
+ self._state = TransportState.LOGIN_REQUIRED
248
308
 
249
309
  _LOGGER.debug("Handshake with %s complete", self._host)
250
310
 
@@ -255,22 +315,32 @@ class AesTransport(BaseTransport):
255
315
  or self._session_expire_at - time.time() <= 0
256
316
  )
257
317
 
258
- async def send(self, request: str):
318
+ async def send(self, request: str) -> Dict[str, Any]:
259
319
  """Send the request."""
260
- if not self._handshake_done or self._handshake_session_expired():
320
+ if (
321
+ self._state is TransportState.HANDSHAKE_REQUIRED
322
+ or self._handshake_session_expired()
323
+ ):
261
324
  await self.perform_handshake()
262
- if not self._login_token:
263
- await self.perform_login()
325
+ if self._state is not TransportState.ESTABLISHED:
326
+ try:
327
+ await self.perform_login()
328
+ # After a login failure handshake needs to
329
+ # be redone or a 9999 error is received.
330
+ except AuthenticationException as ex:
331
+ self._state = TransportState.HANDSHAKE_REQUIRED
332
+ raise ex
264
333
 
265
334
  return await self.send_secure_passthrough(request)
266
335
 
267
336
  async def close(self) -> None:
268
- """Mark the handshake and login as not done.
337
+ """Close the http client and reset internal state."""
338
+ await self.reset()
339
+ await self._http_client.close()
269
340
 
270
- Since we likely lost the connection.
271
- """
272
- self._handshake_done = False
273
- self._login_token = None
341
+ async def reset(self) -> None:
342
+ """Reset internal handshake and login state."""
343
+ self._state = TransportState.HANDSHAKE_REQUIRED
274
344
 
275
345
 
276
346
  class AesEncyptionSession: