@homebridge-plugins/homebridge-tuya 3.0.0-beta.3 → 3.0.0-beta.30

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 (321) hide show
  1. package/README.md +364 -142
  2. package/config.schema.json +1299 -894
  3. package/dist/cloud/api/TuyaOpenAPI.js +108 -60
  4. package/dist/cloud/api/TuyaOpenAPI.js.map +1 -1
  5. package/dist/cloud/api/TuyaOpenMQ.js +45 -22
  6. package/dist/cloud/api/TuyaOpenMQ.js.map +1 -1
  7. package/dist/cloud/device/{TuyaDeviceManager.js → TuyaCloudDeviceManager.js} +146 -96
  8. package/dist/cloud/device/TuyaCloudDeviceManager.js.map +1 -0
  9. package/dist/cloud/device/TuyaCustomDeviceManager.js +75 -5
  10. package/dist/cloud/device/TuyaCustomDeviceManager.js.map +1 -1
  11. package/dist/cloud/device/TuyaDevice.js.map +1 -1
  12. package/dist/cloud/device/TuyaHomeDeviceManager.js +81 -13
  13. package/dist/cloud/device/TuyaHomeDeviceManager.js.map +1 -1
  14. package/dist/config.js +26 -5
  15. package/dist/config.js.map +1 -1
  16. package/dist/local/DynamicChildDiscovery.js +2 -1
  17. package/dist/local/DynamicChildDiscovery.js.map +1 -1
  18. package/dist/local/LocalDevice.js +108 -11
  19. package/dist/local/LocalDevice.js.map +1 -1
  20. package/dist/local/LocalDeviceManager.js +362 -177
  21. package/dist/local/LocalDeviceManager.js.map +1 -1
  22. package/dist/local/TuyaDiscovery.js +17 -8
  23. package/dist/local/TuyaDiscovery.js.map +1 -1
  24. package/dist/local/ZigbeeGatewayDetection.js +7 -4
  25. package/dist/local/ZigbeeGatewayDetection.js.map +1 -1
  26. package/dist/local/protocol/ProtocolUtilities.js +61 -27
  27. package/dist/local/protocol/ProtocolUtilities.js.map +1 -1
  28. package/dist/local/protocol/ProtocolV31V32.js +61 -11
  29. package/dist/local/protocol/ProtocolV31V32.js.map +1 -1
  30. package/dist/local/protocol/ProtocolV33.js +4 -2
  31. package/dist/local/protocol/ProtocolV33.js.map +1 -1
  32. package/dist/local/protocol/ProtocolV34.js +10 -5
  33. package/dist/local/protocol/ProtocolV34.js.map +1 -1
  34. package/dist/local/protocol/ProtocolV35.js +17 -6
  35. package/dist/local/protocol/ProtocolV35.js.map +1 -1
  36. package/dist/platform.js +263 -497
  37. package/dist/platform.js.map +1 -1
  38. package/dist/settings.js +0 -2
  39. package/dist/settings.js.map +1 -1
  40. package/dist/shared/TuyaDeviceManager.js +147 -0
  41. package/dist/shared/TuyaDeviceManager.js.map +1 -0
  42. package/dist/shared/TuyaHybridDeviceManager.js +219 -0
  43. package/dist/shared/TuyaHybridDeviceManager.js.map +1 -0
  44. package/dist/shared/accessory/AccessoryFactory.js +359 -0
  45. package/dist/shared/accessory/AccessoryFactory.js.map +1 -0
  46. package/dist/shared/{accessories → accessory}/AirConditionerAccessory.js +129 -23
  47. package/dist/shared/accessory/AirConditionerAccessory.js.map +1 -0
  48. package/dist/shared/{accessories → accessory}/AirPurifierAccessory.js.map +1 -1
  49. package/dist/shared/accessory/AirQualitySensorAccessory.js.map +1 -0
  50. package/dist/shared/{accessories → accessory}/BaseAccessory.js +47 -97
  51. package/dist/shared/accessory/BaseAccessory.js.map +1 -0
  52. package/dist/shared/{accessories → accessory}/BlindsAccessory.js.map +1 -1
  53. package/dist/shared/{accessories → accessory}/CameraAccessory.js +9 -3
  54. package/dist/shared/accessory/CameraAccessory.js.map +1 -0
  55. package/dist/shared/{accessories → accessory}/CarbonDioxideSensorAccessory.js.map +1 -1
  56. package/dist/shared/{accessories → accessory}/CarbonMonoxideSensorAccessory.js.map +1 -1
  57. package/dist/shared/accessory/CatToiletAccessory.js +130 -0
  58. package/dist/shared/accessory/CatToiletAccessory.js.map +1 -0
  59. package/dist/shared/{accessories → accessory}/ContactSensorAccessory.js.map +1 -1
  60. package/dist/shared/{accessories → accessory}/DehumidifierAccessory.js.map +1 -1
  61. package/dist/shared/{accessories → accessory}/DiffuserAccessory.js.map +1 -1
  62. package/dist/shared/{accessories → accessory}/DimmerAccessory.js.map +1 -1
  63. package/dist/shared/{accessories → accessory}/DoorbellAccessory.js.map +1 -1
  64. package/dist/shared/{accessories → accessory}/ExtractionHoodAccessory.js.map +1 -1
  65. package/dist/shared/{accessories → accessory}/FanAccessory.js +16 -4
  66. package/dist/shared/accessory/FanAccessory.js.map +1 -0
  67. package/dist/shared/{accessories → accessory}/GarageDoorAccessory.js +19 -2
  68. package/dist/shared/accessory/GarageDoorAccessory.js.map +1 -0
  69. package/dist/shared/{accessories → accessory}/HeaterAccessory.js +3 -0
  70. package/dist/shared/accessory/HeaterAccessory.js.map +1 -0
  71. package/dist/shared/{accessories → accessory}/HeaterAccessory_old.js.map +1 -1
  72. package/dist/shared/accessory/HumanPresenceSensorAccessory.js.map +1 -0
  73. package/dist/shared/{accessories → accessory}/HumidifierAccessory.js.map +1 -1
  74. package/dist/shared/accessory/IRAdapter.js +169 -0
  75. package/dist/shared/accessory/IRAdapter.js.map +1 -0
  76. package/dist/shared/{accessories → accessory}/IRAirConditionerAccessory.js +2 -2
  77. package/dist/shared/accessory/IRAirConditionerAccessory.js.map +1 -0
  78. package/dist/shared/accessory/IRControlHubAccessory.js.map +1 -0
  79. package/dist/shared/accessory/IRControlHubSubAccessory.js.map +1 -0
  80. package/dist/shared/accessory/IRFanAccessory.js +109 -0
  81. package/dist/shared/accessory/IRFanAccessory.js.map +1 -0
  82. package/dist/shared/accessory/IRGenericAccessory.js.map +1 -0
  83. package/dist/shared/{accessories → accessory}/LeakSensorAccessory.js.map +1 -1
  84. package/dist/shared/{accessories → accessory}/LightAccessory.js.map +1 -1
  85. package/dist/shared/{accessories → accessory}/LightSensorAccessory.js.map +1 -1
  86. package/dist/shared/{accessories → accessory}/LocationWeatherAccessory.js +9 -36
  87. package/dist/shared/accessory/LocationWeatherAccessory.js.map +1 -0
  88. package/dist/shared/{accessories → accessory}/LockAccessory.js +3 -3
  89. package/dist/shared/{accessories → accessory}/LockAccessory.js.map +1 -1
  90. package/dist/shared/accessory/MotionSensorAccessory.js.map +1 -0
  91. package/dist/shared/accessory/OutletAccessory.js.map +1 -0
  92. package/dist/shared/{accessories → accessory}/PetFeederAccessory.js.map +1 -1
  93. package/dist/shared/accessory/SaunaAccessory.js +134 -0
  94. package/dist/shared/accessory/SaunaAccessory.js.map +1 -0
  95. package/dist/shared/{accessories → accessory}/SceneAccessory.js +1 -1
  96. package/dist/shared/accessory/SceneAccessory.js.map +1 -0
  97. package/dist/shared/accessory/SceneSwitchAccessory.js.map +1 -0
  98. package/dist/shared/{accessories → accessory}/SecuritySystemAccessory.js +4 -4
  99. package/dist/shared/accessory/SecuritySystemAccessory.js.map +1 -0
  100. package/dist/shared/{accessories → accessory}/SmokeSensorAccessory.js.map +1 -1
  101. package/dist/shared/{accessories → accessory}/SwitchAccessory.js +1 -45
  102. package/dist/shared/accessory/SwitchAccessory.js.map +1 -0
  103. package/dist/shared/accessory/TemperatureHumiditySensorAccessory.js.map +1 -0
  104. package/dist/shared/{accessories → accessory}/ThermostatAccessory.js.map +1 -1
  105. package/dist/shared/{accessories → accessory}/TowerRackAccessory.js.map +1 -1
  106. package/dist/shared/{accessories → accessory}/ValveAccessory.js.map +1 -1
  107. package/dist/shared/{accessories → accessory}/VibrationSensorAccessory.js.map +1 -1
  108. package/dist/shared/accessory/WeatherStationAccessory.js.map +1 -0
  109. package/dist/shared/accessory/WetBulbGlobeTemperatureAccessory.js.map +1 -0
  110. package/dist/shared/{accessories → accessory}/WhiteNoiseLightAccessory.js +1 -0
  111. package/dist/shared/accessory/WhiteNoiseLightAccessory.js.map +1 -0
  112. package/dist/shared/accessory/WindowAccessory.js.map +1 -0
  113. package/dist/shared/{accessories → accessory}/WindowCoveringAccessory.js.map +1 -1
  114. package/dist/shared/{accessories → accessory}/WirelessSwitchAccessory.js.map +1 -1
  115. package/dist/shared/accessory/characteristic/Active.js.map +1 -0
  116. package/dist/shared/accessory/characteristic/AirQuality.js.map +1 -0
  117. package/dist/shared/{accessories → accessory}/characteristic/CurrentRelativeHumidity.js +3 -0
  118. package/dist/shared/accessory/characteristic/CurrentRelativeHumidity.js.map +1 -0
  119. package/dist/shared/accessory/characteristic/CurrentTemperature.js.map +1 -0
  120. package/dist/shared/accessory/characteristic/CurrentWeather.js.map +1 -0
  121. package/dist/shared/accessory/characteristic/CurrentWeatherByOpenMeteo.js.map +1 -0
  122. package/dist/shared/{accessories → accessory}/characteristic/CurrentWetBulbGlobeTemperature.js +3 -2
  123. package/dist/shared/accessory/characteristic/CurrentWetBulbGlobeTemperature.js.map +1 -0
  124. package/dist/shared/{accessories → accessory}/characteristic/EnergyUsage.js +0 -5
  125. package/dist/shared/accessory/characteristic/EnergyUsage.js.map +1 -0
  126. package/dist/shared/accessory/characteristic/EnumOn.js +48 -0
  127. package/dist/shared/accessory/characteristic/EnumOn.js.map +1 -0
  128. package/dist/shared/accessory/characteristic/ExtraCharacteristicAdapter.js +197 -0
  129. package/dist/shared/accessory/characteristic/ExtraCharacteristicAdapter.js.map +1 -0
  130. package/dist/shared/{accessories → accessory}/characteristic/Light.js +62 -12
  131. package/dist/shared/accessory/characteristic/Light.js.map +1 -0
  132. package/dist/shared/accessory/characteristic/LightSensor.js.map +1 -0
  133. package/dist/shared/{accessories → accessory}/characteristic/LockPhysicalControls.js +3 -0
  134. package/dist/shared/accessory/characteristic/LockPhysicalControls.js.map +1 -0
  135. package/dist/shared/accessory/characteristic/MotionDetected.js.map +1 -0
  136. package/dist/shared/accessory/characteristic/Name.js.map +1 -0
  137. package/dist/shared/accessory/characteristic/OccupancyDetected.js.map +1 -0
  138. package/dist/shared/{accessories → accessory}/characteristic/On.js +3 -2
  139. package/dist/shared/accessory/characteristic/On.js.map +1 -0
  140. package/dist/shared/accessory/characteristic/OutletInUse.js.map +1 -0
  141. package/dist/shared/accessory/characteristic/ProgrammableSwitchEvent.js.map +1 -0
  142. package/dist/shared/accessory/characteristic/RawOn.js +93 -0
  143. package/dist/shared/accessory/characteristic/RawOn.js.map +1 -0
  144. package/dist/shared/{accessories → accessory}/characteristic/RelativeHumidityDehumidifierThreshold.js +3 -0
  145. package/dist/shared/accessory/characteristic/RelativeHumidityDehumidifierThreshold.js.map +1 -0
  146. package/dist/shared/accessory/characteristic/RotationSpeed.js +157 -0
  147. package/dist/shared/accessory/characteristic/RotationSpeed.js.map +1 -0
  148. package/dist/shared/accessory/characteristic/SecuritySystemState.js.map +1 -0
  149. package/dist/shared/{accessories → accessory}/characteristic/SwingMode.js +3 -0
  150. package/dist/shared/accessory/characteristic/SwingMode.js.map +1 -0
  151. package/dist/shared/accessory/characteristic/TargetTemperature.js.map +1 -0
  152. package/dist/shared/{accessories → accessory}/characteristic/TemperatureDisplayUnits.js +9 -2
  153. package/dist/shared/accessory/characteristic/TemperatureDisplayUnits.js.map +1 -0
  154. package/dist/shared/util/ConfigHash.js +2 -2
  155. package/dist/shared/util/ConfigHash.js.map +1 -1
  156. package/dist/shared/util/ConfigMigrator.js +222 -0
  157. package/dist/shared/util/ConfigMigrator.js.map +1 -0
  158. package/dist/shared/util/FfmpegStreamingProcess.js +62 -0
  159. package/dist/shared/util/FfmpegStreamingProcess.js.map +1 -1
  160. package/dist/shared/util/InfraredTool.js +0 -2
  161. package/dist/shared/util/InfraredTool.js.map +1 -1
  162. package/dist/shared/util/Logger.js +85 -10
  163. package/dist/shared/util/Logger.js.map +1 -1
  164. package/dist/shared/util/TuyaStreamDelegate.js +201 -99
  165. package/dist/shared/util/TuyaStreamDelegate.js.map +1 -1
  166. package/dist/shared/util/color.js +15 -4
  167. package/dist/shared/util/color.js.map +1 -1
  168. package/dist/shared/util/util.js +91 -5
  169. package/dist/shared/util/util.js.map +1 -1
  170. package/docs/ADVANCED_OPTIONS.md +623 -0
  171. package/docs/CONFIG_GUIDE.md +616 -0
  172. package/docs/Get-Local-Keys-for-Your-Devices.md +246 -0
  173. package/{SUPPORTED_DEVICES.md → docs/SUPPORTED_DEVICES.md} +27 -5
  174. package/eslint.config.mjs +0 -1
  175. package/{jest.config.js → jest.config.ts} +5 -2
  176. package/package.json +14 -11
  177. package/test/ConfigMigrator.test.ts +1060 -0
  178. package/test/FanAccessory.test.ts +458 -0
  179. package/test/Light.test.ts +424 -0
  180. package/test/Logger.test.ts +482 -0
  181. package/test/ProtocolV31V32.test.ts +42 -0
  182. package/test/accessoryFactory.data.test.ts +176 -0
  183. package/test/accessoryFactory.test.ts +65 -35
  184. package/test/airConditioner.swing.test.ts +226 -0
  185. package/test/airConditioner.test.ts +770 -0
  186. package/test/color.test.ts +52 -0
  187. package/test/config.test.ts +17 -13
  188. package/test/custom.test.ts +2 -2
  189. package/test/errorHandling.test.ts +12 -12
  190. package/test/fanAccessory.test.ts +458 -0
  191. package/test/home.test.ts +1 -1
  192. package/test/hybridDeviceManager.test.ts +808 -0
  193. package/test/infraredTools.test.ts +67 -0
  194. package/test/light.test.ts +424 -0
  195. package/test/localDevice.test.ts +31 -53
  196. package/test/localDeviceManager.behavior.test.ts +211 -37
  197. package/test/localDeviceManager.test.ts +124 -41
  198. package/test/mqttMessages.test.ts +40 -16
  199. package/test/platform.test.ts +380 -814
  200. package/test/platform.validation.local.test.ts +90 -120
  201. package/test/protocol.test.ts +27 -12
  202. package/test/protocol35Frames.test.ts +204 -0
  203. package/test/rotationSpeed.test.ts +150 -0
  204. package/test/tuyaCustomDeviceManager.test.ts +111 -17
  205. package/test/tuyaDeviceManager.test.ts +655 -645
  206. package/test/tuyaDiscovery.test.ts +21 -33
  207. package/test/tuyaHomeDeviceManager.test.ts +114 -19
  208. package/test/tuyaOpenAPI.test.ts +25 -44
  209. package/test/tuyaOpenMQ.test.ts +15 -23
  210. package/test/util.test.ts +107 -7
  211. package/test/zigbee/LocalDevice.zigbee.test.ts +14 -18
  212. package/test/zigbee/LocalDeviceManager.cloud-discovery.test.ts +19 -20
  213. package/test/zigbee/LocalDeviceManager.zigbee.reconnect.test.ts +19 -20
  214. package/test/zigbee/LocalDeviceManager.zigbee.test.ts +73 -35
  215. package/test/zigbee/ZigbeeGatewayDetection.test.ts +2 -2
  216. package/ADVANCED_OPTIONS.md +0 -267
  217. package/dist/cloud/device/TuyaDeviceManager.js.map +0 -1
  218. package/dist/local/CloudLocalReconciliation.js +0 -180
  219. package/dist/local/CloudLocalReconciliation.js.map +0 -1
  220. package/dist/shared/accessories/AccessoryFactory.js +0 -309
  221. package/dist/shared/accessories/AccessoryFactory.js.map +0 -1
  222. package/dist/shared/accessories/AirConditionerAccessory.js.map +0 -1
  223. package/dist/shared/accessories/AirQualitySensorAccessory.js.map +0 -1
  224. package/dist/shared/accessories/BaseAccessory.js.map +0 -1
  225. package/dist/shared/accessories/CameraAccessory.js.map +0 -1
  226. package/dist/shared/accessories/FanAccessory.js.map +0 -1
  227. package/dist/shared/accessories/GarageDoorAccessory.js.map +0 -1
  228. package/dist/shared/accessories/HeaterAccessory.js.map +0 -1
  229. package/dist/shared/accessories/HumanPresenceSensorAccessory.js.map +0 -1
  230. package/dist/shared/accessories/IRAirConditionerAccessory.js.map +0 -1
  231. package/dist/shared/accessories/IRControlHubAccessory.js.map +0 -1
  232. package/dist/shared/accessories/IRControlHubSubAccessory.js.map +0 -1
  233. package/dist/shared/accessories/IRGenericAccessory.js.map +0 -1
  234. package/dist/shared/accessories/LocationWeatherAccessory.js.map +0 -1
  235. package/dist/shared/accessories/MotionSensorAccessory.js.map +0 -1
  236. package/dist/shared/accessories/OutletAccessory.js.map +0 -1
  237. package/dist/shared/accessories/SceneAccessory.js.map +0 -1
  238. package/dist/shared/accessories/SceneSwitchAccessory.js.map +0 -1
  239. package/dist/shared/accessories/SecuritySystemAccessory.js.map +0 -1
  240. package/dist/shared/accessories/SwitchAccessory.js.map +0 -1
  241. package/dist/shared/accessories/TemperatureHumiditySensorAccessory.js.map +0 -1
  242. package/dist/shared/accessories/WeatherStationAccessory.js.map +0 -1
  243. package/dist/shared/accessories/WetBulbGlobeTemperatureAccessory.js.map +0 -1
  244. package/dist/shared/accessories/WhiteNoiseLightAccessory.js.map +0 -1
  245. package/dist/shared/accessories/WindowAccessory.js.map +0 -1
  246. package/dist/shared/accessories/characteristic/Active.js.map +0 -1
  247. package/dist/shared/accessories/characteristic/AirQuality.js.map +0 -1
  248. package/dist/shared/accessories/characteristic/CurrentRelativeHumidity.js.map +0 -1
  249. package/dist/shared/accessories/characteristic/CurrentTemperature.js.map +0 -1
  250. package/dist/shared/accessories/characteristic/CurrentWeather.js.map +0 -1
  251. package/dist/shared/accessories/characteristic/CurrentWeatherByOpenMeteo.js.map +0 -1
  252. package/dist/shared/accessories/characteristic/CurrentWetBulbGlobeTemperature.js.map +0 -1
  253. package/dist/shared/accessories/characteristic/EnergyUsage.js.map +0 -1
  254. package/dist/shared/accessories/characteristic/Light.js.map +0 -1
  255. package/dist/shared/accessories/characteristic/LightSensor.js.map +0 -1
  256. package/dist/shared/accessories/characteristic/LockPhysicalControls.js.map +0 -1
  257. package/dist/shared/accessories/characteristic/MotionDetected.js.map +0 -1
  258. package/dist/shared/accessories/characteristic/Name.js.map +0 -1
  259. package/dist/shared/accessories/characteristic/OccupancyDetected.js.map +0 -1
  260. package/dist/shared/accessories/characteristic/On.js.map +0 -1
  261. package/dist/shared/accessories/characteristic/OutletInUse.js.map +0 -1
  262. package/dist/shared/accessories/characteristic/ProgrammableSwitchEvent.js.map +0 -1
  263. package/dist/shared/accessories/characteristic/RelativeHumidityDehumidifierThreshold.js.map +0 -1
  264. package/dist/shared/accessories/characteristic/RotationSpeed.js +0 -78
  265. package/dist/shared/accessories/characteristic/RotationSpeed.js.map +0 -1
  266. package/dist/shared/accessories/characteristic/SecuritySystemState.js.map +0 -1
  267. package/dist/shared/accessories/characteristic/SwingMode.js.map +0 -1
  268. package/dist/shared/accessories/characteristic/TargetTemperature.js.map +0 -1
  269. package/dist/shared/accessories/characteristic/TemperatureDisplayUnits.js.map +0 -1
  270. package/test/zigbee/CloudLocalReconciliation.test.ts +0 -284
  271. package/tuya-local-configs/dev-1.hash +0 -1
  272. package/tuya-local-configs/device1.hash +0 -1
  273. package/wiki/Get-Local-Keys-for-Your-Devices.md +0 -85
  274. /package/dist/shared/{accessories → accessory}/AirPurifierAccessory.js +0 -0
  275. /package/dist/shared/{accessories → accessory}/AirQualitySensorAccessory.js +0 -0
  276. /package/dist/shared/{accessories → accessory}/BlindsAccessory.js +0 -0
  277. /package/dist/shared/{accessories → accessory}/CarbonDioxideSensorAccessory.js +0 -0
  278. /package/dist/shared/{accessories → accessory}/CarbonMonoxideSensorAccessory.js +0 -0
  279. /package/dist/shared/{accessories → accessory}/ContactSensorAccessory.js +0 -0
  280. /package/dist/shared/{accessories → accessory}/DehumidifierAccessory.js +0 -0
  281. /package/dist/shared/{accessories → accessory}/DiffuserAccessory.js +0 -0
  282. /package/dist/shared/{accessories → accessory}/DimmerAccessory.js +0 -0
  283. /package/dist/shared/{accessories → accessory}/DoorbellAccessory.js +0 -0
  284. /package/dist/shared/{accessories → accessory}/ExtractionHoodAccessory.js +0 -0
  285. /package/dist/shared/{accessories → accessory}/HeaterAccessory_old.js +0 -0
  286. /package/dist/shared/{accessories → accessory}/HumanPresenceSensorAccessory.js +0 -0
  287. /package/dist/shared/{accessories → accessory}/HumidifierAccessory.js +0 -0
  288. /package/dist/shared/{accessories → accessory}/IRControlHubAccessory.js +0 -0
  289. /package/dist/shared/{accessories → accessory}/IRControlHubSubAccessory.js +0 -0
  290. /package/dist/shared/{accessories → accessory}/IRGenericAccessory.js +0 -0
  291. /package/dist/shared/{accessories → accessory}/LeakSensorAccessory.js +0 -0
  292. /package/dist/shared/{accessories → accessory}/LightAccessory.js +0 -0
  293. /package/dist/shared/{accessories → accessory}/LightSensorAccessory.js +0 -0
  294. /package/dist/shared/{accessories → accessory}/MotionSensorAccessory.js +0 -0
  295. /package/dist/shared/{accessories → accessory}/OutletAccessory.js +0 -0
  296. /package/dist/shared/{accessories → accessory}/PetFeederAccessory.js +0 -0
  297. /package/dist/shared/{accessories → accessory}/SceneSwitchAccessory.js +0 -0
  298. /package/dist/shared/{accessories → accessory}/SmokeSensorAccessory.js +0 -0
  299. /package/dist/shared/{accessories → accessory}/TemperatureHumiditySensorAccessory.js +0 -0
  300. /package/dist/shared/{accessories → accessory}/ThermostatAccessory.js +0 -0
  301. /package/dist/shared/{accessories → accessory}/TowerRackAccessory.js +0 -0
  302. /package/dist/shared/{accessories → accessory}/ValveAccessory.js +0 -0
  303. /package/dist/shared/{accessories → accessory}/VibrationSensorAccessory.js +0 -0
  304. /package/dist/shared/{accessories → accessory}/WeatherStationAccessory.js +0 -0
  305. /package/dist/shared/{accessories → accessory}/WetBulbGlobeTemperatureAccessory.js +0 -0
  306. /package/dist/shared/{accessories → accessory}/WindowAccessory.js +0 -0
  307. /package/dist/shared/{accessories → accessory}/WindowCoveringAccessory.js +0 -0
  308. /package/dist/shared/{accessories → accessory}/WirelessSwitchAccessory.js +0 -0
  309. /package/dist/shared/{accessories → accessory}/characteristic/Active.js +0 -0
  310. /package/dist/shared/{accessories → accessory}/characteristic/AirQuality.js +0 -0
  311. /package/dist/shared/{accessories → accessory}/characteristic/CurrentTemperature.js +0 -0
  312. /package/dist/shared/{accessories → accessory}/characteristic/CurrentWeather.js +0 -0
  313. /package/dist/shared/{accessories → accessory}/characteristic/CurrentWeatherByOpenMeteo.js +0 -0
  314. /package/dist/shared/{accessories → accessory}/characteristic/LightSensor.js +0 -0
  315. /package/dist/shared/{accessories → accessory}/characteristic/MotionDetected.js +0 -0
  316. /package/dist/shared/{accessories → accessory}/characteristic/Name.js +0 -0
  317. /package/dist/shared/{accessories → accessory}/characteristic/OccupancyDetected.js +0 -0
  318. /package/dist/shared/{accessories → accessory}/characteristic/OutletInUse.js +0 -0
  319. /package/dist/shared/{accessories → accessory}/characteristic/ProgrammableSwitchEvent.js +0 -0
  320. /package/dist/shared/{accessories → accessory}/characteristic/SecuritySystemState.js +0 -0
  321. /package/dist/shared/{accessories → accessory}/characteristic/TargetTemperature.js +0 -0
@@ -1,50 +1,90 @@
1
1
  import { beforeEach, describe, expect, jest, test } from '@jest/globals';
2
2
  import { TuyaPlatform } from '../src/platform';
3
-
4
- jest.mock('../src/shared/util/Logger', () => ({
5
- __esModule: true,
6
- default: class Logger {
7
- log() {}
8
- info() {}
9
- warn() {}
10
- error() {}
11
- },
12
- PrefixLogger: class PrefixLogger {
13
- constructor(public log: unknown, public name: string, public debug: boolean) {}
14
- },
15
- }));
3
+ import { initLogger } from '../src/shared/util/Logger';
4
+ import { Logger } from 'homebridge';
5
+
6
+ // Mock Logger
7
+ const mockLog: Logger = {
8
+ debug: jest.fn(),
9
+ info: jest.fn(),
10
+ warn: jest.fn(),
11
+ error: jest.fn(),
12
+ log: jest.fn(),
13
+ success: jest.fn(),
14
+ } as unknown as Logger;
16
15
 
17
16
  jest.mock('../src/cloud/api/TuyaOpenAPI', () => {
18
17
  return class MockTuyaOpenAPI {
19
- log = console;
20
- };
21
- });
18
+ log: Console;
22
19
 
23
- jest.mock('../src/cloud/device/TuyaDeviceManager', () => {
24
- class MockTuyaDeviceManager {
25
- static readonly Events = {
26
- DEVICE_ADD: 'DEVICE_ADD',
27
- DEVICE_INFO_UPDATE: 'DEVICE_INFO_UPDATE',
28
- DEVICE_STATUS_UPDATE: 'DEVICE_STATUS_UPDATE',
29
- DEVICE_DELETE: 'DEVICE_DELETE',
30
- };
31
- }
32
- return MockTuyaDeviceManager;
20
+ constructor() {
21
+ this.log = console;
22
+ }
23
+ static getDefaultEndpoint = jest.fn().mockReturnValue([1]);
24
+ };
33
25
  });
34
26
 
35
27
  jest.mock('../src/cloud/device/TuyaCustomDeviceManager', () => {
36
- return class MockTuyaCustomDeviceManager {};
28
+ return class MockTuyaCustomDeviceManager {
29
+ pullDevices: ReturnType<typeof jest.fn>;
30
+ updateInfraredRemotes: ReturnType<typeof jest.fn>;
31
+ getDeviceSchemaConfig: ReturnType<typeof jest.fn>;
32
+ devices: unknown[];
33
+ api: typeof mockAPI;
34
+ on: ReturnType<typeof jest.fn>;
35
+
36
+ constructor() {
37
+ this.pullDevices = jest.fn().mockReturnValue([]);
38
+ this.updateInfraredRemotes = jest.fn();
39
+ this.getDeviceSchemaConfig = jest.fn();
40
+ this.devices = [];
41
+ this.api = mockAPI;
42
+ this.on = jest.fn();
43
+ }
44
+ };
37
45
  });
38
46
 
39
47
  jest.mock('../src/cloud/device/TuyaHomeDeviceManager', () => {
40
- return class MockTuyaHomeDeviceManager {};
48
+ return class MockTuyaHomeDeviceManager {
49
+ pullDevices: ReturnType<typeof jest.fn>;
50
+ updateInfraredRemotes: ReturnType<typeof jest.fn>;
51
+ getDeviceSchemaConfig: ReturnType<typeof jest.fn>;
52
+ devices: unknown[];
53
+ api: typeof mockAPI;
54
+ on: ReturnType<typeof jest.fn>;
55
+
56
+ constructor() {
57
+ this.pullDevices = jest.fn().mockReturnValue([]);
58
+ this.updateInfraredRemotes = jest.fn();
59
+ this.getDeviceSchemaConfig = jest.fn();
60
+ this.devices = [];
61
+ this.api = mockAPI;
62
+ this.on = jest.fn();
63
+ }
64
+ };
41
65
  });
42
66
 
43
67
  jest.mock('../src/local/LocalDeviceManager', () => {
44
- return class MockLocalDeviceManager {};
68
+ return class MockLocalDeviceManager {
69
+ pullDevices: ReturnType<typeof jest.fn>;
70
+ updateInfraredRemotes: ReturnType<typeof jest.fn>;
71
+ getDeviceSchemaConfig: ReturnType<typeof jest.fn>;
72
+ devices: unknown[];
73
+ api: typeof mockAPI;
74
+ on: ReturnType<typeof jest.fn>;
75
+
76
+ constructor() {
77
+ this.pullDevices = jest.fn().mockReturnValue([]);
78
+ this.updateInfraredRemotes = jest.fn();
79
+ this.getDeviceSchemaConfig = jest.fn();
80
+ this.devices = [];
81
+ this.api = mockAPI;
82
+ this.on = jest.fn();
83
+ }
84
+ };
45
85
  });
46
86
 
47
- jest.mock('../src/shared/accessories/AccessoryFactory', () => ({
87
+ jest.mock('../src/shared/accessory/AccessoryFactory', () => ({
48
88
  __esModule: true,
49
89
  default: {
50
90
  configAccessory: jest.fn(),
@@ -57,6 +97,13 @@ jest.mock('../src/shared/util/util', () => ({
57
97
  retry: jest.fn(async (fn: () => Promise<unknown>) => fn()),
58
98
  }));
59
99
 
100
+ jest.mock('../src/shared/util/ConfigMigrator', () => ({
101
+ __esModule: true,
102
+ default: class ConfigMigrator {
103
+ migrate(arg:any) { return arg; }
104
+ }
105
+ }));
106
+
60
107
  const mockAPI = {
61
108
  hap: {
62
109
  Service: {},
@@ -69,19 +116,13 @@ const mockAPI = {
69
116
  user: {
70
117
  persistPath: () => '/tmp',
71
118
  },
119
+ tokenInfo: {}
72
120
  } as any;
73
121
 
74
122
  describe('TuyaPlatform validation around cloud/local modes', () => {
75
- let log: any;
76
-
77
123
  beforeEach(() => {
78
- log = {
79
- debug: jest.fn(),
80
- info: jest.fn(),
81
- warn: jest.fn(),
82
- error: jest.fn(),
83
- };
84
124
  jest.clearAllMocks();
125
+ initLogger(mockLog);
85
126
  });
86
127
 
87
128
  test('rejects mode both when local block is missing', () => {
@@ -89,7 +130,7 @@ describe('TuyaPlatform validation around cloud/local modes', () => {
89
130
  platform: 'TuyaPlatform',
90
131
  name: 'Tuya',
91
132
  mode: 'both',
92
- options: {
133
+ cloud: {
93
134
  projectType: '2',
94
135
  accessId: 'id',
95
136
  accessKey: 'key',
@@ -102,9 +143,9 @@ describe('TuyaPlatform validation around cloud/local modes', () => {
102
143
  },
103
144
  };
104
145
 
105
- const platform = new TuyaPlatform(log, config, mockAPI);
146
+ const platform = new TuyaPlatform(mockLog, config, mockAPI);
106
147
  expect(platform.validate()).toBe(false);
107
- expect(log.error).toHaveBeenCalledWith('mode is "both" but no "local" config block found.');
148
+ expect(mockLog.error).toHaveBeenCalledWith('mode is "both" but no "local" config block found.');
108
149
  });
109
150
 
110
151
  test('rejects mode both when cloud options are missing', () => {
@@ -115,9 +156,9 @@ describe('TuyaPlatform validation around cloud/local modes', () => {
115
156
  local: { devices: [] },
116
157
  };
117
158
 
118
- const platform = new TuyaPlatform(log, config, mockAPI);
159
+ const platform = new TuyaPlatform(mockLog, config, mockAPI);
119
160
  expect(platform.validate()).toBe(false);
120
- expect(log.error).toHaveBeenCalledWith('Not configured — "options" block is required for cloud mode, exit.');
161
+ expect(mockLog.error).toHaveBeenCalledWith('Not configured — "cloud" block is required for cloud mode, exit.');
121
162
  });
122
163
 
123
164
  test('detects duplicate device override ids', () => {
@@ -125,7 +166,7 @@ describe('TuyaPlatform validation around cloud/local modes', () => {
125
166
  platform: 'TuyaPlatform',
126
167
  name: 'Tuya',
127
168
  mode: 'cloud',
128
- options: {
169
+ cloud: {
129
170
  projectType: '2',
130
171
  accessId: 'id',
131
172
  accessKey: 'key',
@@ -142,8 +183,8 @@ describe('TuyaPlatform validation around cloud/local modes', () => {
142
183
  },
143
184
  };
144
185
 
145
- const platform = new TuyaPlatform(log, config, mockAPI);
146
- expect(platform.validateDeviceOverrides()).toBe(false);
186
+ const platform = new TuyaPlatform(mockLog, config, mockAPI);
187
+ expect(platform.validateDeviceOverrides(config.cloud.deviceOverrides)).toBe(false);
147
188
  });
148
189
 
149
190
  test('detects duplicate schema codes inside one device override', () => {
@@ -151,7 +192,7 @@ describe('TuyaPlatform validation around cloud/local modes', () => {
151
192
  platform: 'TuyaPlatform',
152
193
  name: 'Tuya',
153
194
  mode: 'cloud',
154
- options: {
195
+ cloud: {
155
196
  projectType: '2',
156
197
  accessId: 'id',
157
198
  accessKey: 'key',
@@ -173,78 +214,7 @@ describe('TuyaPlatform validation around cloud/local modes', () => {
173
214
  },
174
215
  };
175
216
 
176
- const platform = new TuyaPlatform(log, config, mockAPI);
177
- expect(platform.validateSchema()).toBe(false);
178
- });
179
-
180
- test('resolves device config precedence: device, then product, then global', () => {
181
- const config = {
182
- platform: 'TuyaPlatform',
183
- name: 'Tuya',
184
- mode: 'cloud',
185
- options: {
186
- projectType: '2',
187
- accessId: 'id',
188
- accessKey: 'key',
189
- countryCode: 1,
190
- username: 'user@example.com',
191
- password: 'secret',
192
- appSchema: 'tuyaSmart',
193
- generateWeatherAccessory: false,
194
- weatherAPI: '',
195
- deviceOverrides: [
196
- { id: 'global', category: 'global-cat' },
197
- { id: 'prod-1', category: 'product-cat' },
198
- { id: 'device-1', category: 'device-cat' },
199
- ],
200
- },
201
- };
202
-
203
- const platform = new TuyaPlatform(log, config, mockAPI);
204
-
205
- const exact = platform.getDeviceConfig({ id: 'device-1', uuid: 'uuid-1', product_id: 'prod-1' } as any);
206
- const byProduct = platform.getDeviceConfig({ id: 'device-2', uuid: 'uuid-2', product_id: 'prod-1' } as any);
207
- const fallback = platform.getDeviceConfig({ id: 'device-3', uuid: 'uuid-3', product_id: 'prod-3' } as any);
208
-
209
- expect(exact?.category).toBe('device-cat');
210
- expect(byProduct?.category).toBe('product-cat');
211
- expect(fallback?.category).toBe('global-cat');
212
- });
213
-
214
- test('finds schema config case-insensitively and migrates oldCode', () => {
215
- const config = {
216
- platform: 'TuyaPlatform',
217
- name: 'Tuya',
218
- mode: 'cloud',
219
- options: {
220
- projectType: '2',
221
- accessId: 'id',
222
- accessKey: 'key',
223
- countryCode: 1,
224
- username: 'user@example.com',
225
- password: 'secret',
226
- appSchema: 'tuyaSmart',
227
- generateWeatherAccessory: false,
228
- weatherAPI: '',
229
- deviceOverrides: [
230
- {
231
- id: 'device-1',
232
- schema: [
233
- { oldCode: 'switch_1', code: 'new_switch_1', hidden: true },
234
- ],
235
- },
236
- ],
237
- },
238
- };
239
-
240
- const platform = new TuyaPlatform(log, config, mockAPI);
241
- const device = { id: 'device-1', uuid: 'device-1', product_id: 'prod-1' } as any;
242
-
243
- const result = platform.getDeviceSchemaConfig(device, 'SWITCH_1');
244
-
245
- expect(result).toBeDefined();
246
- expect(result?.code).toBe('switch_1');
247
- expect((result as any).newCode).toBe('new_switch_1');
248
- expect(result?.hidden).toBe(true);
217
+ const platform = new TuyaPlatform(mockLog, config, mockAPI);
218
+ expect(platform.validateSchema(config.cloud.deviceOverrides)).toBe(false);
249
219
  });
250
220
  });
@@ -53,6 +53,9 @@ describe('ProtocolFactory', () => {
53
53
 
54
54
  describe('Protocol Base Interface', () => {
55
55
  let protocol: ReturnType<typeof ProtocolFactory.createProtocol>;
56
+ // v3.5 encrypts every frame, so encoding always needs a key: the session key once
57
+ // the handshake is done, the device key before that.
58
+ const deviceKey = Buffer.from('0123456789abcdef');
56
59
 
57
60
  beforeEach(() => {
58
61
  protocol = ProtocolFactory.createProtocol('3.5');
@@ -64,7 +67,7 @@ describe('Protocol Base Interface', () => {
64
67
  const data = Buffer.from([0xaa, 0xbb, 0xcc]);
65
68
  const seqNo = 1;
66
69
 
67
- const frame = protocol.encodeFrame(cmd, data, seqNo);
70
+ const frame = protocol.encodeFrame(cmd, data, seqNo, undefined, deviceKey);
68
71
 
69
72
  expect(frame).toBeInstanceOf(Buffer);
70
73
  expect(frame.length).toBeGreaterThan(0);
@@ -87,7 +90,7 @@ describe('Protocol Base Interface', () => {
87
90
  const data = Buffer.alloc(0);
88
91
  const seqNo = 1;
89
92
 
90
- const frame = protocol.encodeFrame(cmd, data, seqNo);
93
+ const frame = protocol.encodeFrame(cmd, data, seqNo, undefined, deviceKey);
91
94
 
92
95
  expect(frame).toBeInstanceOf(Buffer);
93
96
  expect(frame.length).toBeGreaterThan(0);
@@ -101,7 +104,7 @@ describe('Protocol Base Interface', () => {
101
104
  const data = Buffer.from([0xaa, 0xbb]);
102
105
  const seqNo = 1;
103
106
 
104
- const frame = protocol.encodeFrame(cmd, data, seqNo);
107
+ const frame = protocol.encodeFrame(cmd, data, seqNo, undefined, deviceKey);
105
108
  const isComplete = protocol.isFrameComplete(frame);
106
109
 
107
110
  expect(typeof isComplete).toBe('boolean');
@@ -129,7 +132,7 @@ describe('Protocol Base Interface', () => {
129
132
  const data = Buffer.from([0xaa, 0xbb]);
130
133
  const seqNo = 1;
131
134
 
132
- const encodedFrame = protocol.encodeFrame(cmd, data, seqNo);
135
+ const encodedFrame = protocol.encodeFrame(cmd, data, seqNo, undefined, deviceKey);
133
136
  const result = protocol.extractFrame(encodedFrame);
134
137
 
135
138
  if (result) {
@@ -249,12 +252,13 @@ describe('packMessage6699 / unpackMessage6699 round-trips', () => {
249
252
  test('GCM frame round-trip preserves seqno, cmd, and payload', () => {
250
253
  const plaintext = Buffer.from('{"dps":{"1":true}}');
251
254
  const frame = packMessage6699(42, 13, plaintext, key);
252
- const msg = unpackMessage6699(frame, key);
255
+ // This is a client→device frame, which carries no retcode, so unpack must be
256
+ // told not to strip one. Frames coming from a device always carry it.
257
+ const msg = unpackMessage6699(frame, key, true);
253
258
  expect(msg).not.toBeNull();
254
259
  expect(msg!.seqno).toBe(42);
255
260
  expect(msg!.cmd).toBe(13);
256
261
  expect(msg!.hmacOk).toBe(true);
257
- // Payload starts with '{' (not 0x00 0x00), so retcode stripping does not apply
258
262
  expect(msg!.payload).toEqual(plaintext);
259
263
  });
260
264
 
@@ -270,18 +274,29 @@ describe('packMessage6699 / unpackMessage6699 round-trips', () => {
270
274
  const frame = packMessage6699(1, 16, plaintext, key);
271
275
  // header=18, iv=12, ciphertext=37, tag=16, suffix=4 → total 87 bytes
272
276
  expect(frame.length).toBe(18 + 12 + 37 + 16 + 4);
273
- // length field at offset 14 should be 12+37+16+4 = 69
274
- expect(frame.readUInt32BE(14)).toBe(12 + 37 + 16 + 4);
277
+ // length field at offset 14 should be 12+37+16 = 65: it covers IV + ciphertext +
278
+ // tag and NOT the suffix (TinyTuya message_helper.pack_message)
279
+ expect(frame.readUInt32BE(14)).toBe(12 + 37 + 16);
275
280
  });
276
281
  });
277
282
 
278
283
  describe('ProtocolV35 encode + decode round-trips', () => {
279
284
  const key = Buffer.from('0123456789abcdef');
280
285
  const proto = new ProtocolV35();
286
+ // Frames sent by a device carry a 4-byte retcode ahead of the payload, which
287
+ // decodeFrame strips. Prepend one when building a frame that stands in for a
288
+ // device reply, otherwise the first four payload bytes are lost.
289
+ const RETCODE = Buffer.alloc(4);
290
+ const asDeviceFrame = (cmd: number, payload: Buffer, seq = 1, versionHeader = false): Buffer => {
291
+ const body = versionHeader
292
+ ? Buffer.concat([Buffer.from('3.5' + '\x00'.repeat(12), 'latin1'), payload])
293
+ : payload;
294
+ return packMessage6699(seq, cmd, Buffer.concat([RETCODE, body]), key);
295
+ };
281
296
 
282
297
  test('data command (cmd 13) with session key: round-trip strips version header', () => {
283
298
  const payload = Buffer.from('{"protocol":5,"t":1234,"data":{"dps":{"1":true}}}');
284
- const frame = proto.encodeFrame(13, payload, 1, key);
299
+ const frame = asDeviceFrame(13, payload, 1, true);
285
300
  const decoded = proto.decodeFrame(frame, key, key);
286
301
  expect(decoded).not.toBeNull();
287
302
  expect(decoded!.cmd).toBe(13);
@@ -290,7 +305,7 @@ describe('ProtocolV35 encode + decode round-trips', () => {
290
305
 
291
306
  test('key exchange cmd 3 (NO_VERSION_HEADER_CMDS): no version header added or stripped', () => {
292
307
  const nonce = Buffer.from('0123456789abcdef'); // 16 bytes
293
- const frame = proto.encodeFrame(3, nonce, 1, key);
308
+ const frame = asDeviceFrame(3, nonce);
294
309
  const decoded = proto.decodeFrame(frame, key, key);
295
310
  expect(decoded).not.toBeNull();
296
311
  expect(decoded!.cmd).toBe(3);
@@ -299,8 +314,8 @@ describe('ProtocolV35 encode + decode round-trips', () => {
299
314
 
300
315
  test('decodeFrame falls back to deviceKey when sessionKey is absent (pre-exchange)', () => {
301
316
  const nonce = Buffer.from('0123456789abcdef');
302
- // Encode with key (simulating device using deviceKey before exchange)
303
- const frame = proto.encodeFrame(3, nonce, 1, key);
317
+ // Simulates the device replying with the device key, before a session exists
318
+ const frame = asDeviceFrame(3, nonce, 1);
304
319
  // Decode without passing sessionKey - should use deviceKey as fallback
305
320
  const decoded = proto.decodeFrame(frame, key); // sessionKey omitted
306
321
  expect(decoded).not.toBeNull();
@@ -0,0 +1,204 @@
1
+ import { describe, expect, test } from '@jest/globals';
2
+
3
+ /**
4
+ * Wire-format tests for the v3.5 (0x6699) frame layout.
5
+ *
6
+ * The golden vectors below were produced by TinyTuya (message_helper.pack_message
7
+ * with prefix 0x6699), the reference implementation these frames follow. They are
8
+ * reproducible with:
9
+ *
10
+ * from tinytuya.core import header as H
11
+ * from tinytuya.core.message_helper import TuyaMessage, pack_message
12
+ * msg = TuyaMessage(seq, cmd, None, payload, 0, True, H.PREFIX_6699_VALUE, b'0123456789ab')
13
+ * pack_message(msg, hmac_key=key).hex()
14
+ *
15
+ * The IV is pinned to '0123456789ab' so the bytes are deterministic. At runtime the
16
+ * IV is random; it travels inside the frame, so any unique value is wire-compatible.
17
+ */
18
+
19
+ // Pin the 12-byte GCM nonce only while `pinnedIv` is set, so the golden vectors can
20
+ // be compared byte for byte while every other use of randomBytes stays real.
21
+ let pinnedIv: Buffer | null = null;
22
+
23
+ jest.mock('crypto', () => {
24
+ const actual = jest.requireActual<typeof import('crypto')>('crypto');
25
+ return {
26
+ ...actual,
27
+ randomBytes: (size: number): Buffer =>
28
+ (size === 12 && pinnedIv ? Buffer.from(pinnedIv) : actual.randomBytes(size)),
29
+ };
30
+ });
31
+
32
+ // eslint-disable-next-line @typescript-eslint/no-require-imports
33
+ const {
34
+ packMessage6699,
35
+ unpackMessage6699,
36
+ isFrameComplete,
37
+ extractFrame,
38
+ HEADER_SIZE_6699,
39
+ PREFIX_6699,
40
+ SUFFIX_6699,
41
+ } = require('../src/local/protocol/ProtocolUtilities');
42
+ // eslint-disable-next-line @typescript-eslint/no-require-imports
43
+ const { ProtocolV35 } = require('../src/local/protocol/ProtocolV35');
44
+
45
+ const DEVICE_KEY = Buffer.from('0123456789abcdef', 'latin1');
46
+ const SESSION_KEY = Buffer.from('00112233445566778899aabbccddeeff', 'hex');
47
+ const FIXED_IV = Buffer.from('0123456789ab', 'latin1');
48
+ const NONCE = Buffer.from('aabbccddeeff00112233445566778899', 'hex');
49
+
50
+ const GOLDEN = {
51
+ keyExchangeStart:
52
+ '00006699000000000001000000030000002c303132333435363738396162c8f9c126c09f2d98213033b1'
53
+ + 'e2ad2b6a4ea62b6ce07e9a059e670d5dca806da600009966',
54
+ dpQueryNew:
55
+ '00006699000000000003000000100000003a303132333435363738396162790702f77ca154ac87f222ad'
56
+ + 'ba96e03e078b5fa743be4093905c8d4673cd0055f1c00304a7f59f502fb819f58afa00009966',
57
+ heartbeat:
58
+ '00006699000000000005000000090000001c3031323334353637383961628f7981ee45e7bc696425ac42'
59
+ + '39b34ee000009966',
60
+ };
61
+
62
+ function withFixedIv<T>(fn: () => T): T {
63
+ pinnedIv = FIXED_IV;
64
+ try {
65
+ return fn();
66
+ } finally {
67
+ pinnedIv = null;
68
+ }
69
+ }
70
+
71
+ describe('v3.5 0x6699 frame format', () => {
72
+ describe('length field', () => {
73
+ test('counts IV + ciphertext + tag, excluding the 4-byte suffix', () => {
74
+ const plaintext = Buffer.from('{"dps":{"1":true}}');
75
+ const frame = packMessage6699(1, 13, plaintext, SESSION_KEY);
76
+
77
+ const declared = frame.readUInt32BE(14);
78
+ expect(declared).toBe(12 + plaintext.length + 16);
79
+ // whole frame = header + declared length + suffix
80
+ expect(frame.length).toBe(HEADER_SIZE_6699 + declared + 4);
81
+ expect(frame.readUInt32BE(0)).toBe(PREFIX_6699);
82
+ expect(frame.readUInt32BE(frame.length - 4)).toBe(SUFFIX_6699);
83
+ });
84
+
85
+ test('round-trips through unpackMessage6699', () => {
86
+ // A device→client frame carries a 4-byte retcode ahead of the payload.
87
+ const body = Buffer.from('{"dps":{"1":true,"2":250}}');
88
+ const withRetcode = Buffer.concat([Buffer.alloc(4), body]);
89
+ const frame = packMessage6699(7, 16, withRetcode, SESSION_KEY);
90
+
91
+ const msg = unpackMessage6699(frame, SESSION_KEY);
92
+ expect(msg).not.toBeNull();
93
+ expect(msg.seqno).toBe(7);
94
+ expect(msg.cmd).toBe(16);
95
+ expect(msg.hmacOk).toBe(true);
96
+ expect(msg.payload.toString()).toBe(body.toString());
97
+ });
98
+
99
+ test('returns null when the frame is truncated', () => {
100
+ const frame = packMessage6699(1, 9, Buffer.alloc(4), SESSION_KEY);
101
+ expect(unpackMessage6699(frame.subarray(0, frame.length - 1), SESSION_KEY)).toBeNull();
102
+ });
103
+
104
+ test('returns null when the auth tag does not verify', () => {
105
+ const frame = packMessage6699(1, 16, Buffer.alloc(8), SESSION_KEY);
106
+ const tampered = Buffer.from(frame);
107
+ tampered[HEADER_SIZE_6699 + 12] ^= 0xff; // flip a ciphertext bit
108
+ expect(unpackMessage6699(tampered, SESSION_KEY)).toBeNull();
109
+ });
110
+ });
111
+
112
+ describe('frame boundaries', () => {
113
+ test('isFrameComplete requires the suffix as well', () => {
114
+ const frame = packMessage6699(1, 16, Buffer.alloc(16), SESSION_KEY);
115
+ expect(isFrameComplete(frame)).toBe(true);
116
+ expect(isFrameComplete(frame.subarray(0, frame.length - 1))).toBe(false);
117
+ });
118
+
119
+ test('extractFrame consumes the whole frame, leaving nothing behind', () => {
120
+ const frame = packMessage6699(2, 16, Buffer.alloc(16), SESSION_KEY);
121
+ const extracted = extractFrame(frame);
122
+
123
+ expect(extracted).not.toBeNull();
124
+ expect(extracted.frame.length).toBe(frame.length);
125
+ // A short read here leaves suffix bytes in the buffer and desyncs every
126
+ // following frame in the stream.
127
+ expect(extracted.remaining.length).toBe(0);
128
+ });
129
+
130
+ test('extractFrame splits two concatenated frames cleanly', () => {
131
+ const first = packMessage6699(1, 16, Buffer.alloc(8), SESSION_KEY);
132
+ const second = packMessage6699(2, 9, Buffer.alloc(4), SESSION_KEY);
133
+ const stream = Buffer.concat([first, second]);
134
+
135
+ const a = extractFrame(stream);
136
+ expect(a.frame).toEqual(first);
137
+ const b = extractFrame(a.remaining);
138
+ expect(b.frame).toEqual(second);
139
+ expect(b.remaining.length).toBe(0);
140
+ });
141
+ });
142
+
143
+ describe('GCM nonce', () => {
144
+ test('differs between two frames with identical content', () => {
145
+ const plaintext = Buffer.from('same');
146
+ const a = packMessage6699(1, 16, plaintext, SESSION_KEY);
147
+ const b = packMessage6699(1, 16, plaintext, SESSION_KEY);
148
+
149
+ const ivA = a.subarray(HEADER_SIZE_6699, HEADER_SIZE_6699 + 12);
150
+ const ivB = b.subarray(HEADER_SIZE_6699, HEADER_SIZE_6699 + 12);
151
+ expect(ivA.equals(ivB)).toBe(false);
152
+ });
153
+ });
154
+
155
+ describe('matches TinyTuya byte for byte', () => {
156
+ test('SESS_KEY_NEG_START (cmd 3) keyed with the device key', () => {
157
+ const frame = withFixedIv(() => new ProtocolV35().encodeFrame(3, NONCE, 1, undefined, DEVICE_KEY));
158
+ expect(frame.toString('hex')).toBe(GOLDEN.keyExchangeStart);
159
+ });
160
+
161
+ test('DP_QUERY_NEW (cmd 16) keyed with the session key', () => {
162
+ const payload = Buffer.from('{"gwId":"test","devId":"test"}');
163
+ const frame = withFixedIv(() => new ProtocolV35().encodeFrame(16, payload, 3, SESSION_KEY, DEVICE_KEY));
164
+ expect(frame.toString('hex')).toBe(GOLDEN.dpQueryNew);
165
+ });
166
+
167
+ test('HEART_BEAT (cmd 9)', () => {
168
+ const frame = withFixedIv(() => new ProtocolV35().encodeFrame(9, Buffer.alloc(0), 5, SESSION_KEY, DEVICE_KEY));
169
+ expect(frame.toString('hex')).toBe(GOLDEN.heartbeat);
170
+ });
171
+
172
+ test('decodes a TinyTuya-produced frame', () => {
173
+ // The other direction: a frame generated by TinyTuya must be readable here,
174
+ // including the length field that excludes the suffix.
175
+ const frame = Buffer.from(GOLDEN.dpQueryNew, 'hex');
176
+ expect(isFrameComplete(frame)).toBe(true);
177
+
178
+ const msg = unpackMessage6699(frame, SESSION_KEY);
179
+ expect(msg).not.toBeNull();
180
+ expect(msg.cmd).toBe(16);
181
+ expect(msg.hmacOk).toBe(true);
182
+ });
183
+ });
184
+
185
+ describe('key exchange', () => {
186
+ test('uses the 0x6699 format before a session key exists', () => {
187
+ const frame = new ProtocolV35().encodeFrame(3, NONCE, 1, undefined, DEVICE_KEY);
188
+ // A 0x55AA frame here is dropped by the device without any reply.
189
+ expect(frame.readUInt32BE(0)).toBe(PREFIX_6699);
190
+ expect(unpackMessage6699(frame, DEVICE_KEY)).not.toBeNull();
191
+ });
192
+
193
+ test('throws when no device key is available', () => {
194
+ expect(() => new ProtocolV35().encodeFrame(3, NONCE, 1, undefined, undefined)).toThrow();
195
+ });
196
+
197
+ test('does not prepend the version header to key-exchange commands', () => {
198
+ const frame = new ProtocolV35().encodeFrame(3, NONCE, 1, undefined, DEVICE_KEY);
199
+ const msg = unpackMessage6699(frame, DEVICE_KEY);
200
+ // unpack strips the 4-byte retcode, so compare against the nonce minus those bytes
201
+ expect(msg.payload).toEqual(NONCE.subarray(4));
202
+ });
203
+ });
204
+ });