@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
package/dist/platform.js CHANGED
@@ -1,37 +1,4 @@
1
1
  "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || (function () {
19
- var ownKeys = function(o) {
20
- ownKeys = Object.getOwnPropertyNames || function (o) {
21
- var ar = [];
22
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
- return ar;
24
- };
25
- return ownKeys(o);
26
- };
27
- return function (mod) {
28
- if (mod && mod.__esModule) return mod;
29
- var result = {};
30
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
- __setModuleDefault(result, mod);
32
- return result;
33
- };
34
- })();
35
2
  var __importDefault = (this && this.__importDefault) || function (mod) {
36
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
37
4
  };
@@ -40,18 +7,23 @@ exports.TuyaPlatform = void 0;
40
7
  const jsonschema_1 = require("jsonschema");
41
8
  const path_1 = __importDefault(require("path"));
42
9
  const fs_1 = __importDefault(require("fs"));
43
- const TuyaDeviceManager_1 = __importDefault(require("./cloud/device/TuyaDeviceManager"));
10
+ const uuid_1 = require("uuid");
11
+ // Cloud imports (from src/cloud/)
12
+ const TuyaDevice_1 = __importDefault(require("./cloud/device/TuyaDevice"));
44
13
  const TuyaCustomDeviceManager_1 = __importDefault(require("./cloud/device/TuyaCustomDeviceManager"));
45
14
  const TuyaHomeDeviceManager_1 = __importDefault(require("./cloud/device/TuyaHomeDeviceManager"));
46
- const TuyaOpenAPI_1 = __importStar(require("./cloud/api/TuyaOpenAPI"));
15
+ const TuyaOpenAPI_1 = __importDefault(require("./cloud/api/TuyaOpenAPI"));
47
16
  // Local imports (from src/local/)
48
17
  const LocalDeviceManager_1 = __importDefault(require("./local/LocalDeviceManager"));
49
18
  // Shared imports (from src/shared/)
50
- const AccessoryFactory_1 = __importDefault(require("./shared/accessories/AccessoryFactory"));
19
+ const TuyaDeviceManager_1 = __importDefault(require("./shared/TuyaDeviceManager"));
20
+ const ConfigMigrator_1 = __importDefault(require("./shared/util/ConfigMigrator"));
21
+ const AccessoryFactory_1 = __importDefault(require("./shared/accessory/AccessoryFactory"));
51
22
  const util_1 = require("./shared/util/util");
52
- const ConfigHash_1 = require("./shared/util/ConfigHash");
53
23
  const settings_1 = require("./settings");
54
24
  const config_1 = require("./config");
25
+ const Logger_1 = require("./shared/util/Logger");
26
+ const TuyaHybridDeviceManager_1 = __importDefault(require("./shared/TuyaHybridDeviceManager"));
55
27
  /**
56
28
  * TuyaPlatform — unified Homebridge platform supporting both Tuya Cloud (REST/MQTT)
57
29
  * and Tuya Local (direct LAN TCP) device communication.
@@ -62,9 +34,10 @@ const config_1 = require("./config");
62
34
  * "both" — cloud + local simultaneously
63
35
  */
64
36
  class TuyaPlatform {
37
+ static { this.PLATFORM_UUID = '8CC2405F-4DB0-4586-B32F-A38CC156164D'; }
65
38
  validate() {
66
39
  // Local-only mode does not need cloud options
67
- if (this.mode === 'local') {
40
+ if (this.mode === config_1.TuyaPluginMode.local) {
68
41
  if (!this.platformConfig.local) {
69
42
  this.log.error('mode is "local" but no "local" config block found.');
70
43
  return false;
@@ -72,41 +45,42 @@ class TuyaPlatform {
72
45
  return true;
73
46
  }
74
47
  // Both mode requires a local block in addition to cloud options
75
- if (this.mode === 'both' && !this.platformConfig.local) {
48
+ if (this.mode === config_1.TuyaPluginMode.both && !this.platformConfig.local) {
76
49
  this.log.error('mode is "both" but no "local" config block found.');
77
50
  return false;
78
51
  }
79
52
  // Cloud or "both" mode requires cloud options
80
53
  let result;
81
- if (!this.options) {
82
- this.log.error('Not configured — "options" block is required for cloud mode, exit.');
54
+ if (!this.platformConfig.cloud) {
55
+ this.log.error('Not configured — "cloud" block is required for cloud mode, exit.');
83
56
  return false;
84
57
  }
85
- else if (this.options.projectType === '1') {
86
- result = new jsonschema_1.Validator().validate(this.options, config_1.customOptionsSchema);
58
+ else if (this.platformConfig.cloud.projectType === '1') {
59
+ result = new jsonschema_1.Validator().validate(this.platformConfig.cloud, config_1.customOptionsSchema);
87
60
  }
88
- else if (this.options.projectType === '2') {
89
- result = new jsonschema_1.Validator().validate(this.options, config_1.homeOptionsSchema);
61
+ else if (this.platformConfig.cloud.projectType === '2') {
62
+ result = new jsonschema_1.Validator().validate(this.platformConfig.cloud, config_1.homeOptionsSchema);
90
63
  }
91
64
  else {
92
- this.log.error(`Unsupported projectType: ${this.options['projectType']}, exit.`);
65
+ this.log.error(`Unsupported projectType: ${this.platformConfig.cloud['projectType']}, exit.`);
93
66
  return false;
94
67
  }
95
68
  result.errors.forEach(error => this.log.error(error.stack));
96
69
  if (result.errors.length > 0) {
97
70
  return false;
98
71
  }
99
- if (!this.validateDeviceOverrides() || !this.validateSchema()) {
72
+ if (!this.validateDeviceOverrides(this.platformConfig.cloud.deviceOverrides)
73
+ || !this.validateSchema(this.platformConfig.cloud.deviceOverrides)) {
100
74
  return false;
101
75
  }
102
76
  return true;
103
77
  }
104
- validateDeviceOverrides() {
105
- if (!this.options.deviceOverrides) {
78
+ validateDeviceOverrides(deviceOverrides) {
79
+ if (!deviceOverrides) {
106
80
  return true;
107
81
  }
108
82
  const idMap = new Map();
109
- for (const item of this.options.deviceOverrides) {
83
+ for (const item of deviceOverrides) {
110
84
  if (idMap.has(item.id)) {
111
85
  idMap.get(item.id)?.push(item);
112
86
  }
@@ -122,11 +96,11 @@ class TuyaPlatform {
122
96
  }
123
97
  return true;
124
98
  }
125
- validateSchema() {
126
- if (!this.options.deviceOverrides) {
99
+ validateSchema(deviceOverrides) {
100
+ if (!deviceOverrides) {
127
101
  return true;
128
102
  }
129
- for (const deviceOverride of this.options.deviceOverrides) {
103
+ for (const deviceOverride of deviceOverrides) {
130
104
  if (!deviceOverride.schema) {
131
105
  continue;
132
106
  }
@@ -154,29 +128,48 @@ class TuyaPlatform {
154
128
  this.api = api;
155
129
  this.Service = this.api.hap.Service;
156
130
  this.Characteristic = this.api.hap.Characteristic;
157
- /** Cast config to our typed shape for easy access. */
131
+ this.platformAccessories = new Map();
132
+ this.platformID = (0, uuid_1.v5)(this.config.name ?? 'TuyaPlatform', TuyaPlatform.PLATFORM_UUID);
158
133
  this.platformConfig = this.config;
159
- /** Cloud credentials block (options.projectType, accessId, etc.) */
160
- this.options = this.config.options;
161
- /** Active communication mode. Defaults to "cloud" for backward compatibility. */
162
- this.mode = this.config.mode ?? 'cloud';
163
- // this is used to track restored cached accessories
164
- this.cachedAccessories = [];
165
- /** All active accessory handler instances. */
134
+ this.mode = this.platformConfig.mode ?? config_1.TuyaPluginMode.cloud;
166
135
  this.accessoryHandlers = [];
136
+ this.debug = false;
137
+ (0, Logger_1.initLogger)(log);
138
+ this.platformConfig = new ConfigMigrator_1.default(!!this.platformConfig.common?.debug).migrate(config);
139
+ this.debug = this.platformConfig.common?.debug ?? false;
140
+ this.debugLevel = this.platformConfig.common?.debugLevel ?? undefined;
141
+ this.mode = this.platformConfig.mode;
167
142
  if (!this.validate()) {
168
143
  return;
169
144
  }
145
+ // Error/Exception handling.
146
+ const formatError = (e) => {
147
+ if (e instanceof Error) {
148
+ return `${e.message}\n${e.stack}`;
149
+ }
150
+ return JSON.stringify(e);
151
+ };
152
+ process.on('uncaughtException', (err) => {
153
+ this.log.error('Uncaught exception: %o', formatError(err));
154
+ process.exit(1);
155
+ });
156
+ process.on('unhandledRejection', (reason) => {
157
+ this.log.error('Unhandled rejection: %o', formatError(reason));
158
+ process.exit(1);
159
+ });
170
160
  this.log.debug('Finished initializing platform');
171
161
  // When this event is fired it means Homebridge has restored all cached accessories from disk.
172
162
  // Dynamic Platform plugins should only register new accessories after this event was fired,
173
163
  // in order to ensure they weren't added to homebridge already. This event can also be used
174
164
  // to start discovery of new accessories.
175
- this.api.on('didFinishLaunching', async () => {
165
+ this.api.on("didFinishLaunching" /* APIEvent.DID_FINISH_LAUNCHING */, async () => {
176
166
  this.log.debug('Executed didFinishLaunching callback');
177
167
  // run the method to discover / register your devices as accessories
178
168
  await this.initDevices();
179
169
  });
170
+ this.api.on("shutdown" /* APIEvent.SHUTDOWN */, async () => {
171
+ this.log.debug('Executed shutdown callback');
172
+ });
180
173
  }
181
174
  /**
182
175
  * This function is invoked when homebridge restores cached accessories from disk at startup.
@@ -184,474 +177,161 @@ class TuyaPlatform {
184
177
  */
185
178
  configureAccessory(accessory) {
186
179
  this.log.info('Loading accessory from cache:', accessory.displayName);
187
- AccessoryFactory_1.default.configAccessory(this, accessory);
188
180
  // add the restored accessory to the accessories cache so we can track if it has already been registered
189
- this.cachedAccessories.push(accessory);
181
+ this.platformAccessories.set(accessory.UUID, accessory);
190
182
  }
191
- /**
192
- * This is an example method showing how to register discovered accessories.
193
- * Accessories must only be registered once, previously created accessories
194
- * must not be registered again to prevent "duplicate UUID" errors.
195
- */
196
- async initDevices() {
197
- const cloudEnabled = this.mode === 'cloud' || this.mode === 'both';
198
- const localEnabled = this.mode === 'local' || this.mode === 'both';
199
- // For "both" mode, initialize cloud FIRST to fetch device details for enrichment
200
- let cloudDevices;
201
- if (cloudEnabled && this.options && this.mode === 'both') {
202
- this.log.info('[Cloud] Initializing cloud device manager for hybrid mode enrichment…');
203
- if (this.options.projectType === '1') {
204
- cloudDevices = await this.initCustomProject();
205
- }
206
- else if (this.options.projectType === '2') {
207
- cloudDevices = await this.initHomeProject();
208
- }
183
+ getPersistPath() {
184
+ const user = this.api.user;
185
+ if (typeof user.persistPath === 'function') {
186
+ return user.persistPath();
209
187
  }
210
- // ── Local devices ────────────────────────────────────────────────────────
211
- if (localEnabled && this.platformConfig.local) {
212
- // If "both" mode and we successfully got cloud devices, enrich local config
213
- if (this.mode === 'both' && cloudDevices && this.deviceManager) {
214
- this.log.info('[Local] Enriching local config with cloud device details…');
215
- await this.enrichLocalConfigFromCloud(cloudDevices);
216
- }
217
- this.log.info('[Local] Initialising local device manager…');
218
- this.localDeviceManager = new LocalDeviceManager_1.default(this.platformConfig.local, this.log, this.api.user.storagePath());
219
- await this.localDeviceManager.initLocalDevices();
220
- this.localDeviceManager.connectAllDevices();
221
- this.localDeviceManager.on(TuyaDeviceManager_1.default.Events.DEVICE_ADD, (device) => this.addAccessory(device, 'local'));
222
- this.localDeviceManager.on(TuyaDeviceManager_1.default.Events.DEVICE_INFO_UPDATE, this.updateAccessoryInfo.bind(this));
223
- this.localDeviceManager.on(TuyaDeviceManager_1.default.Events.DEVICE_STATUS_UPDATE, this.updateAccessoryStatus.bind(this));
224
- this.localDeviceManager.on(TuyaDeviceManager_1.default.Events.DEVICE_DELETE, this.removeAccessory.bind(this));
225
- const localDevices = [...this.localDeviceManager.devices.values()];
226
- this.log.info(`[Local] Registered ${localDevices.length} local device(s).`);
227
- for (const device of localDevices) {
228
- this.addAccessory(device, 'local');
229
- }
188
+ if (typeof user.storagePath === 'function') {
189
+ return user.storagePath();
230
190
  }
231
- // ── Cloud devices (if not already initialized in "both" mode) ──────────────
232
- if (cloudEnabled && this.options && this.mode !== 'both') {
233
- let devices;
234
- if (this.options.projectType === '1') {
235
- devices = await this.initCustomProject();
236
- }
237
- else if (this.options.projectType === '2') {
238
- devices = await this.initHomeProject();
239
- }
240
- else {
241
- this.log.warn(`Unsupported projectType: ${this.options['projectType']}.`);
242
- }
243
- if (devices && this.deviceManager) {
244
- // Initialize config hash tracker for cloud devices
245
- this.configHash = new ConfigHash_1.ConfigHash(this.api.user.storagePath(), 'tuya-cloud-configs');
246
- // Apply device config overrides
247
- for (const device of devices) {
248
- const deviceConfig = this.getDeviceConfig(device, 'cloud');
249
- if (deviceConfig?.category) {
250
- this.log.warn('Override %o category from %o to %o', device.name, device.category, deviceConfig.category);
251
- device.category = deviceConfig.category;
252
- }
253
- if (deviceConfig?.unbridged) {
254
- this.log.warn('Unbridge %o category %o', device.name, device.category);
255
- device.unbridged = deviceConfig.unbridged;
256
- }
257
- // Check if config has changed since last run
258
- // Hash the device override fields that affect accessory structure
259
- const configToHash = {
260
- deviceId: device.id,
261
- customCategory: deviceConfig?.category,
262
- unbridged: deviceConfig?.unbridged ?? false,
263
- schemaOverrides: deviceConfig?.schema ? JSON.stringify(deviceConfig.schema) : undefined,
264
- adaptiveLighting: deviceConfig?.adaptiveLighting ?? false,
265
- };
266
- const { changed: configChanged } = this.configHash.hasConfigChanged(device.id, configToHash);
267
- device.configChanged = configChanged;
268
- if (configChanged) {
269
- this.log.info(`[Cloud] Device config changed for "${device.name}" (${device.id}), will rebuild services`);
270
- }
271
- }
272
- await this.deviceManager.updateInfraredRemotes(devices);
273
- this.log.info(`[Cloud] Got ${devices.length} device(s) and scene(s).`);
274
- const file = path_1.default.join(this.api.user.persistPath(), `TuyaDeviceList.${this.deviceManager.api.tokenInfo.uid}.json`);
275
- this.log.info('Device list saved at %s', file);
276
- if (!fs_1.default.existsSync(this.api.user.persistPath())) {
277
- await fs_1.default.promises.mkdir(this.api.user.persistPath());
278
- }
279
- await fs_1.default.promises.writeFile(file, JSON.stringify(devices, null, 2));
280
- for (const device of devices) {
281
- this.addAccessory(device, 'cloud');
282
- }
283
- this.deviceManager.on(TuyaDeviceManager_1.default.Events.DEVICE_ADD, (device) => this.addAccessory(device, 'cloud'));
284
- this.deviceManager.on(TuyaDeviceManager_1.default.Events.DEVICE_INFO_UPDATE, this.updateAccessoryInfo.bind(this));
285
- this.deviceManager.on(TuyaDeviceManager_1.default.Events.DEVICE_STATUS_UPDATE, this.updateAccessoryStatus.bind(this));
286
- this.deviceManager.on(TuyaDeviceManager_1.default.Events.DEVICE_DELETE, this.removeAccessory.bind(this));
287
- }
288
- }
289
- else if (cloudEnabled && this.mode === 'both' && cloudDevices && this.deviceManager) {
290
- // "both" mode: finish setting up cloud devices that were already initialized
291
- this.configHash = new ConfigHash_1.ConfigHash(this.api.user.storagePath(), 'tuya-cloud-configs');
292
- // Apply device config overrides
293
- for (const device of cloudDevices) {
294
- const deviceConfig = this.getDeviceConfig(device, 'cloud');
295
- if (deviceConfig?.category) {
296
- this.log.warn('Override %o category from %o to %o', device.name, device.category, deviceConfig.category);
297
- device.category = deviceConfig.category;
298
- }
299
- if (deviceConfig?.unbridged) {
300
- this.log.warn('Unbridge %o category %o', device.name, device.category);
301
- device.unbridged = deviceConfig.unbridged;
302
- }
303
- // Check if config has changed since last run
304
- const configToHash = {
305
- deviceId: device.id,
306
- customCategory: deviceConfig?.category,
307
- unbridged: deviceConfig?.unbridged ?? false,
308
- schemaOverrides: deviceConfig?.schema ? JSON.stringify(deviceConfig.schema) : undefined,
309
- adaptiveLighting: deviceConfig?.adaptiveLighting ?? false,
310
- };
311
- const { changed: configChanged } = this.configHash.hasConfigChanged(device.id, configToHash);
312
- device.configChanged = configChanged;
313
- if (configChanged) {
314
- this.log.info(`[Cloud] Device config changed for "${device.name}" (${device.id}), will rebuild services`);
315
- }
316
- }
317
- await this.deviceManager.updateInfraredRemotes(cloudDevices);
318
- this.log.info(`[Cloud] Got ${cloudDevices.length} device(s) and scene(s).`);
319
- const file = path_1.default.join(this.api.user.persistPath(), `TuyaDeviceList.${this.deviceManager.api.tokenInfo.uid}.json`);
320
- this.log.info('Device list saved at %s', file);
321
- if (!fs_1.default.existsSync(this.api.user.persistPath())) {
322
- await fs_1.default.promises.mkdir(this.api.user.persistPath());
323
- }
324
- await fs_1.default.promises.writeFile(file, JSON.stringify(cloudDevices, null, 2));
325
- for (const device of cloudDevices) {
326
- this.addAccessory(device, 'cloud');
327
- }
328
- this.deviceManager.on(TuyaDeviceManager_1.default.Events.DEVICE_ADD, (device) => this.addAccessory(device, 'cloud'));
329
- this.deviceManager.on(TuyaDeviceManager_1.default.Events.DEVICE_INFO_UPDATE, this.updateAccessoryInfo.bind(this));
330
- this.deviceManager.on(TuyaDeviceManager_1.default.Events.DEVICE_STATUS_UPDATE, this.updateAccessoryStatus.bind(this));
331
- if (this.localDeviceManager) {
332
- this.deviceManager.on(TuyaDeviceManager_1.default.Events.DEVICE_STATUS_UPDATE, (device, status) => {
333
- this.localDeviceManager?.handleCloudStatusUpdate(device.id, status);
334
- });
335
- }
336
- this.deviceManager.on(TuyaDeviceManager_1.default.Events.DEVICE_DELETE, this.removeAccessory.bind(this));
191
+ return path_1.default.join(process.cwd(), '.homebridge');
192
+ }
193
+ async saveDeviceList() {
194
+ if (!this.deviceManager) {
195
+ return;
337
196
  }
338
- // Remove stale cached accessories not claimed by any device
339
- for (const cachedAccessory of this.cachedAccessories) {
340
- this.log.warn('Removing unused accessory from cache:', cachedAccessory.displayName);
341
- this.api.unregisterPlatformAccessories(settings_1.PLUGIN_NAME, settings_1.PLATFORM_NAME, [cachedAccessory]);
197
+ if (this.tid !== undefined) {
198
+ clearTimeout(this.tid);
199
+ this.tid = undefined;
342
200
  }
343
- this.cachedAccessories = [];
344
- }
345
- getDeviceConfig(device, source) {
346
- if (!this.options.deviceOverrides) {
347
- return undefined;
348
- }
349
- // Find matching override, respecting source filtering
350
- // Since deviceOverrides are in the cloud config, they default to cloud-only
351
- const effectiveSource = source || 'cloud';
352
- const matches = this.options.deviceOverrides.filter(config => {
353
- const sourceMatch = config.source
354
- ? (config.source === 'both' || config.source === effectiveSource) // explicit source
355
- : effectiveSource === 'cloud'; // default: cloud-only
356
- const idMatch = config.id === device.id || config.id === device.uuid ||
357
- config.id === device.product_id || config.id === 'global';
358
- return sourceMatch && idMatch;
359
- });
360
- // Return device-specific config, then product, then global
361
- return matches.find(config => config.id === device.id || config.id === device.uuid) ||
362
- matches.find(config => config.id === device.product_id) ||
363
- matches.find(config => config.id === 'global');
364
- }
365
- getDeviceSchemaConfig(device, code, source) {
366
- const deviceConfig = this.getDeviceConfig(device, source);
367
- if (!deviceConfig || !deviceConfig.schema) {
368
- return undefined;
369
- }
370
- // migrate old config
371
- deviceConfig.schema.forEach(item => {
372
- if (item['oldCode']) {
373
- item.newCode = item.code;
374
- item.code = item['oldCode'];
375
- item['oldCode'] = undefined;
376
- }
377
- });
378
- // ignore case - allow both old (code) and migrated (newCode) names
379
- const schemaConfig = deviceConfig.schema.find(item => {
380
- if (!code) {
381
- return false;
201
+ this.tid = setTimeout(async () => {
202
+ const persistPath = this.getPersistPath();
203
+ const file = path_1.default.join(persistPath, `TuyaDeviceList.${this.platformID}-${this.mode}.json`);
204
+ this.log.info('Device list saved at %s', file);
205
+ if (!fs_1.default.existsSync(persistPath)) {
206
+ await fs_1.default.promises.mkdir(persistPath, { recursive: true });
382
207
  }
383
- const target = code.toString().toLowerCase();
384
- const legacyCode = item.code?.toString().toLowerCase();
385
- const migratedCode = item.newCode?.toString().toLowerCase();
386
- return legacyCode === target || migratedCode === target;
387
- });
388
- if (!schemaConfig) {
389
- return undefined;
390
- }
391
- return schemaConfig;
208
+ const devices = [...this.platformAccessories.values()].map(accessory => this.deviceManager.getDevice(accessory.context.deviceID));
209
+ await fs_1.default.promises.writeFile(file, JSON.stringify(devices, null, 2));
210
+ this.tid = undefined;
211
+ }, 5000);
392
212
  }
393
213
  /**
394
- * Enrich local device config with cloud device details (local_key, ip, etc.)
395
- * Called during "both" mode initialization to populate local credentials from cloud API
214
+ * This is an example method showing how to register discovered accessories.
215
+ * Accessories must only be registered once, previously created accessories
216
+ * must not be registered again to prevent "duplicate UUID" errors.
396
217
  */
397
- async enrichLocalConfigFromCloud(devices) {
398
- if (!this.deviceManager || !this.platformConfig.local || !this.platformConfig.local.devices) {
399
- return;
400
- }
401
- // Create a map of cloud devices by ID for quick lookup
402
- const cloudDeviceMap = new Map(devices.map(d => [d.id, d]));
403
- for (const device of devices) {
404
- try {
405
- // Skip if already manually configured in local section
406
- const existingConfig = this.platformConfig.local.devices.find(cfg => cfg.tuyaDeviceId === device.id || cfg.tuyaDeviceId === device.uuid);
407
- if (existingConfig && existingConfig.tuyaKey) {
408
- this.log.debug(`[Hybrid] Device ${device.name} (${device.id}) already has manual local config, skipping cloud enrichment`);
409
- continue;
410
- }
411
- // Fetch device details from cloud API (includes local_key)
412
- const detailRes = await this.deviceManager.getDeviceDetails(device.id);
413
- if (!detailRes.success || !detailRes.result) {
414
- this.log.debug(`[Hybrid] Could not fetch device details for ${device.name} (${device.id}) from cloud API`);
415
- continue;
416
- }
417
- const details = detailRes.result;
418
- const deviceId = device.id || device.uuid;
419
- const localKey = details.local_key || details.localKey;
420
- const ip = details.ip || details.address;
421
- if (!deviceId) {
422
- this.log.warn(`[Hybrid] Skipping enrichment for ${device.name} because cloud device ID is missing`);
423
- continue;
424
- }
425
- if (!localKey) {
426
- this.log.debug(`[Hybrid] No local_key available for ${device.name} (${deviceId}), will use cloud-only`);
427
- continue;
428
- }
429
- if (existingConfig) {
430
- // Update existing manual config with cloud-provided local_key and optional IP
431
- existingConfig.tuyaKey = localKey;
432
- if (ip) {
433
- existingConfig.ip = ip;
434
- }
435
- this.log.info(`[Hybrid] Enriched local config for ${device.name} (${deviceId}) with cloud-provided local_key and ip`);
436
- }
437
- else {
438
- // Create new local device config entry from cloud device details
439
- const localDeviceConfig = {
440
- tuyaDeviceId: deviceId,
441
- tuyaKey: localKey,
442
- name: device.name || `LocalDevice-${deviceId.slice(0, 8)}`,
443
- category: device.category,
444
- };
445
- if (ip) {
446
- localDeviceConfig.ip = ip;
447
- }
448
- if (device.product_id) {
449
- localDeviceConfig.productId = device.product_id;
450
- }
451
- if (!this.platformConfig.local.devices) {
452
- this.platformConfig.local.devices = [];
453
- }
454
- this.platformConfig.local.devices.push(localDeviceConfig);
455
- this.log.info(`[Hybrid] Added cloud device ${device.name} (${deviceId}) to local config with local_key`);
456
- }
457
- }
458
- catch (error) {
459
- const msg = error instanceof Error ? error.message : String(error);
460
- this.log.warn(`[Hybrid] Error enriching device ${device.name} (${device.id}): ${msg}`);
461
- // Continue with next device - this is non-fatal
462
- }
218
+ async initDevices() {
219
+ let deviceManager;
220
+ if (this.mode === config_1.TuyaPluginMode.cloud) {
221
+ deviceManager = await this.initCloudMode(this.platformConfig.cloud);
463
222
  }
464
- }
465
- async initCustomProject() {
466
- if (this.options.projectType !== '1') {
467
- return undefined;
468
- }
469
- const DEFAULT_USER = 'homebridge';
470
- const DEFAULT_PASS = 'homebridge';
471
- let res;
472
- const { endpoint, accessId, accessKey, debug, debugLevel } = this.options;
473
- const debugMode = debug && ((debugLevel ?? '').length > 0 ? debugLevel?.includes('api') : true);
474
- const api = new TuyaOpenAPI_1.default(endpoint, accessId, accessKey, this.log, 'en', debugMode);
475
- const deviceManager = new TuyaCustomDeviceManager_1.default(api, debugMode);
476
- this.log.info('Get token.');
477
- res = await api.getToken();
478
- if (res.success === false) {
479
- this.log.error(`Get token failed. code=${res.code}, msg=${res.msg}`);
480
- return undefined;
481
- }
482
- this.log.info(`Search default user "${DEFAULT_USER}"`);
483
- res = await api.customGetUserInfo(DEFAULT_USER);
484
- if (res.success === false) {
485
- this.log.error(`Search user failed. code=${res.code}, msg=${res.msg}`);
486
- return undefined;
487
- }
488
- if (!res.result.user_name) {
489
- this.log.info(`Default user "${DEFAULT_USER}" not exist.`);
490
- this.log.info(`Creating default user "${DEFAULT_USER}".`);
491
- res = await api.customCreateUser(DEFAULT_USER, DEFAULT_PASS);
492
- if (res.success === false) {
493
- this.log.error(`Create default user failed. code=${res.code}, msg=${res.msg}`);
494
- return undefined;
495
- }
223
+ else if (this.mode === config_1.TuyaPluginMode.local) {
224
+ deviceManager = await this.initLocalMode(this.platformConfig.local);
496
225
  }
497
- else {
498
- this.log.info(`Default user "${DEFAULT_USER}" exists.`);
499
- }
500
- const uid = res.result.user_id;
501
- this.log.info('Fetching asset list.');
502
- res = await deviceManager.getAssetList();
503
- if (res.success === false) {
504
- this.log.error(`Fetching asset list failed. code=${res.code}, msg=${res.msg}`);
505
- return undefined;
506
- }
507
- const assetIDList = [];
508
- for (const { asset_id, asset_name } of res.result.list) {
509
- this.log.info(`Got asset_id=${asset_id}, asset_name=${asset_name}`);
510
- assetIDList.push(asset_id);
511
- }
512
- if (assetIDList.length === 0) {
513
- this.log.warn('Asset list is empty. exit.');
514
- return undefined;
515
- }
516
- this.log.info('Authorize asset list.');
517
- res = await deviceManager.authorizeAssetList(uid, assetIDList, true);
518
- if (res.success === false) {
519
- this.log.error(`Authorize asset list failed. code=${res.code}, msg=${res.msg}`);
520
- return undefined;
521
- }
522
- this.log.info(`Log in with user "${DEFAULT_USER}".`);
523
- res = await api.customLogin(DEFAULT_USER, DEFAULT_USER);
524
- if (res.success === false) {
525
- this.log.error(`Login failed. code=${res.code}, msg=${res.msg}`);
526
- if (res.code && TuyaOpenAPI_1.LOGIN_ERROR_MESSAGES[res.code]) {
527
- this.log.error(TuyaOpenAPI_1.LOGIN_ERROR_MESSAGES[res.code]);
528
- }
529
- return undefined;
226
+ else if (this.mode === config_1.TuyaPluginMode.both) {
227
+ deviceManager = await this.initBothMode(this.platformConfig.cloud, this.platformConfig.local);
530
228
  }
531
- this.log.info('Start MQTT connection.');
532
- deviceManager.mq.start();
533
- this.log.info('Fetching device list.');
534
- deviceManager.ownerIDs = assetIDList;
535
- const devices = await deviceManager.updateDevices(assetIDList);
536
229
  this.deviceManager = deviceManager;
537
- return devices;
538
- }
539
- async initHomeProject() {
540
- if (this.options.projectType !== '2') {
541
- return undefined;
542
- }
543
- let res;
544
- const { accessId, accessKey, countryCode, username, password, appSchema, endpoint, debug, debugLevel } = this.options;
545
- const debugMode = debug && ((debugLevel ?? '').length > 0 ? debugLevel?.includes('api') : true);
546
- const api = new TuyaOpenAPI_1.default((endpoint && endpoint.length > 0) ? endpoint : TuyaOpenAPI_1.default.getDefaultEndpoint(countryCode), accessId, accessKey, this.log, 'en', debugMode);
547
- const deviceManager = new TuyaHomeDeviceManager_1.default(api, debugMode);
548
- this.log.info('Log in to Tuya Cloud.');
549
- res = await api.homeLogin(countryCode, username, password, appSchema);
550
- if (res.success === false) {
551
- this.log.error(`Login failed. code=${res.code}, msg=${res.msg}`);
552
- if (res.code && TuyaOpenAPI_1.LOGIN_ERROR_MESSAGES[res.code]) {
553
- this.log.error(TuyaOpenAPI_1.LOGIN_ERROR_MESSAGES[res.code]);
554
- }
555
- return undefined;
556
- }
557
- this.log.info('Start MQTT connection.');
558
- deviceManager.mq.start();
559
- this.log.info('Fetching home list.');
560
- res = await deviceManager.getHomeList();
561
- if (res.success === false) {
562
- this.log.error(`Fetching home list failed. code=${res.code}, msg=${res.msg}`);
563
- return undefined;
564
- }
565
- const homeIDList = [];
566
- for (const { home_id, name } of res.result) {
567
- this.log.info(`Got home_id=${home_id}, name=${name}`);
568
- if (this.options.homeWhitelist) {
569
- if (this.options.homeWhitelist.includes(home_id)) {
570
- this.log.info(`Found home_id=${home_id} in whitelist; including devices from this home.`);
571
- homeIDList.push(home_id);
572
- }
573
- else {
574
- this.log.info(`Did not find home_id=${home_id} in whitelist; excluding devices from this home.`);
575
- }
576
- }
577
- else {
578
- homeIDList.push(home_id);
579
- }
580
- }
581
- if (homeIDList.length === 0) {
582
- this.log.warn('Home list is empty.');
583
- }
584
- this.log.info('Fetching device list.');
585
- deviceManager.ownerIDs = homeIDList.map(homeID => homeID.toString());
586
- const devices = await deviceManager.updateDevices(homeIDList);
587
- this.log.info('Fetching scene list.');
588
- for (const homeID of homeIDList) {
589
- const scenes = await deviceManager.getSceneList(homeID);
590
- for (const scene of scenes) {
591
- this.log.info(`Got scene_id=${scene.id}, name=${scene.name}`);
592
- }
593
- devices.push(...scenes);
230
+ const devices = await this.deviceManager.pullDevices();
231
+ devices.forEach(device => this.log.debug(`${device.id}: ${device.name}`));
232
+ for (const device of devices) {
233
+ this.addAccessory(device);
234
+ }
235
+ // add RTSP camera accessories that are not present in either Cloud or Local (non-Tuya devices).
236
+ this.platformConfig.local?.cameras?.
237
+ filter(cconfig => !devices.map(device => device.id).includes(cconfig.deviceId)).forEach(cconfig => {
238
+ this.log.info(`adding non-Tuya device: ${cconfig.deviceName}`);
239
+ const device = this.deviceManager.createRTSPCameraDevice(cconfig);
240
+ this.deviceManager.devices.push(device);
241
+ this.addAccessory(device);
242
+ });
243
+ deviceManager.on(TuyaDeviceManager_1.default.Events.DEVICE_ADD, this.addAccessory.bind(this));
244
+ deviceManager.on(TuyaDeviceManager_1.default.Events.DEVICE_INFO_UPDATE, this.updateAccessoryInfo.bind(this));
245
+ deviceManager.on(TuyaDeviceManager_1.default.Events.DEVICE_STATUS_UPDATE, this.updateAccessoryStatus.bind(this));
246
+ deviceManager.on(TuyaDeviceManager_1.default.Events.DEVICE_DELETE, this.removeAccessory.bind(this));
247
+ if (this.mode !== config_1.TuyaPluginMode.cloud) {
248
+ // When running in local mode, a short delay is added before detecting the change.
249
+ setTimeout(this.finishDeviceInitializing.bind(this), (this.platformConfig.local?.discoverTimeout ?? 5) * 1000);
594
250
  }
595
- this.deviceManager = deviceManager;
596
- if (this.options.generateWeatherAccessory) {
597
- const targetDevice = devices.find(device => device.lat && device.lon);
598
- if (targetDevice) {
599
- devices.push(this.createWeatherDevice(targetDevice, res.result));
600
- }
251
+ else {
252
+ this.finishDeviceInitializing();
601
253
  }
602
- return devices;
603
254
  }
604
- addAccessory(device, source) {
605
- // Apply device override config before checking if hidden
606
- const deviceConfig = this.getDeviceConfig(device, source);
607
- if (deviceConfig?.category) {
608
- this.log.warn('Override %o category from %o to %o', device.name, device.category, deviceConfig.category);
609
- device.category = deviceConfig.category;
255
+ async initCloudMode(cloudConfig) {
256
+ this.log.info('[Cloud] Initializing cloud device manager…');
257
+ let deviceManager;
258
+ if (cloudConfig.projectType === '1') {
259
+ deviceManager = await this.initCustomProject(cloudConfig);
610
260
  }
611
- if (deviceConfig?.unbridged) {
612
- this.log.warn('Unbridge %o category %o', device.name, device.category);
613
- device.unbridged = deviceConfig.unbridged;
261
+ else if (cloudConfig.projectType === '2') {
262
+ deviceManager = await this.initHomeProject(cloudConfig);
614
263
  }
264
+ return deviceManager;
265
+ }
266
+ async initLocalMode(localConfig) {
267
+ this.log.info('[Local] Initializing local device manager…');
268
+ return new LocalDeviceManager_1.default(localConfig, !!this.debug);
269
+ }
270
+ async initBothMode(cloudConfig, localConfig) {
271
+ this.log.debug('[Hybrid] Initializing...');
272
+ const { accessId, accessKey, endpoint, forceIPv4 } = cloudConfig;
273
+ const debugMode = !!this.debugLevel ? this.debugLevel.includes('api') : this.debug;
274
+ const _endpoint = (endpoint && endpoint.length > 0) ? endpoint : TuyaOpenAPI_1.default.getDefaultEndpoint(cloudConfig['countryCode']);
275
+ const api = new TuyaOpenAPI_1.default(_endpoint, accessId, accessKey, 'en', debugMode, forceIPv4);
276
+ return new TuyaHybridDeviceManager_1.default(api, cloudConfig, localConfig, !!this.debug);
277
+ }
278
+ async initCustomProject(config) {
279
+ const { endpoint, accessId, accessKey, forceIPv4 } = config;
280
+ const debugMode = !!this.debugLevel ? this.debugLevel.includes('api') : this.debug;
281
+ const api = new TuyaOpenAPI_1.default(endpoint, accessId, accessKey, 'en', debugMode, forceIPv4);
282
+ return new TuyaCustomDeviceManager_1.default(api, config, debugMode);
283
+ }
284
+ async initHomeProject(config) {
285
+ const { accessId, accessKey, countryCode, endpoint, forceIPv4 } = config;
286
+ const debugMode = !!this.debugLevel ? this.debugLevel.includes('api') : this.debug;
287
+ const _endpoint = (endpoint && endpoint.length > 0) ? endpoint : TuyaOpenAPI_1.default.getDefaultEndpoint(countryCode);
288
+ const api = new TuyaOpenAPI_1.default(_endpoint, accessId, accessKey, 'en', debugMode, forceIPv4);
289
+ return new TuyaHomeDeviceManager_1.default(api, config, debugMode);
290
+ }
291
+ addAccessory(device) {
615
292
  if (device.category === 'hidden') {
616
293
  this.log.info('Hide Accessory:', device.name);
617
- return;
294
+ return null;
618
295
  }
619
296
  const uuid = this.api.hap.uuid.generate(device.id);
620
- const existingAccessory = this.cachedAccessories.find(accessory => accessory.UUID === uuid);
297
+ const existingAccessory = this.platformAccessories.get(uuid);
621
298
  if (existingAccessory && !device.unbridged) {
622
- this.log.info('Restoring existing accessory from cache:', existingAccessory.displayName);
623
- // Update context
624
- if (!existingAccessory.context || !existingAccessory.context.deviceID) {
625
- this.log.info('Update accessory context:', existingAccessory.displayName);
626
- existingAccessory.context.deviceID = device.id;
627
- this.api.updatePlatformAccessories([existingAccessory]);
628
- }
299
+ this.log.debug('Restoring existing accessory from cache:', existingAccessory.displayName);
629
300
  // create the accessory handler for the restored accessory
630
301
  const handler = AccessoryFactory_1.default.createAccessory(this, existingAccessory, device);
631
302
  this.accessoryHandlers.push(handler);
632
- const index = this.cachedAccessories.indexOf(existingAccessory);
633
- if (index >= 0) {
634
- this.cachedAccessories.splice(index, 1);
635
- }
303
+ // Always apply the user settings to ensure that any changes made after the cache was created are properly reflected.
304
+ AccessoryFactory_1.default.configAccessory(this, existingAccessory);
305
+ this.syncDeviceAndAccessory(device, existingAccessory);
306
+ this.saveDeviceList();
307
+ return existingAccessory;
636
308
  }
637
309
  else {
638
310
  // the accessory does not yet exist, so we need to create it
639
- this.log.info('Adding new accessory:', device.name);
311
+ this.log.debug('Adding new accessory:', device.name);
640
312
  // create a new accessory (sanitize name to conform to HAP rules)
641
313
  const safeName = (0, util_1.sanitizeName)(device.name) ?? (device.id || 'Tuya Device');
642
314
  const accessory = new this.api.platformAccessory(safeName, uuid);
643
315
  accessory.context.deviceID = device.id;
316
+ // Initialize config hash tracker for device
317
+ const currentHash = this.deviceManager.createDeviceConfigHash(device);
318
+ accessory.context.deviceConfigHash = currentHash;
644
319
  // create the accessory handler for the newly create accessory
645
320
  const handler = AccessoryFactory_1.default.createAccessory(this, accessory, device);
646
321
  this.accessoryHandlers.push(handler);
322
+ AccessoryFactory_1.default.configAccessory(this, accessory);
647
323
  // link the accessory to your platform
648
324
  if (device.unbridged) {
325
+ this.log.success(`(unbridged) publish accessory: ${accessory.displayName}`);
649
326
  this.api.publishExternalAccessories(settings_1.PLUGIN_NAME, [accessory]);
650
327
  }
651
328
  else {
329
+ this.log.success(`publish accessory: ${accessory.displayName}`);
652
330
  this.api.registerPlatformAccessories(settings_1.PLUGIN_NAME, settings_1.PLATFORM_NAME, [accessory]);
653
331
  }
654
- AccessoryFactory_1.default.configAccessory(this, accessory);
332
+ this.platformAccessories.set(accessory.UUID, accessory);
333
+ this.saveDeviceList();
334
+ return accessory;
655
335
  }
656
336
  }
657
337
  updateAccessoryInfo(device, info) {
@@ -659,8 +339,11 @@ class TuyaPlatform {
659
339
  if (!handler) {
660
340
  return;
661
341
  }
662
- // this.log.debug('onDeviceInfoUpdate devId = %s, status = %o}', device.id, info);
663
- handler.onDeviceInfoUpdate(info);
342
+ const accessory = this.platformAccessories.get(this.api.hap.uuid.generate(device.id));
343
+ handler.onDeviceInfoUpdate(info).then(() => {
344
+ this.syncDeviceAndAccessory(device, accessory);
345
+ });
346
+ this.saveDeviceList();
664
347
  }
665
348
  updateAccessoryStatus(device, status) {
666
349
  const handler = this.getAccessoryHandler(device.id);
@@ -669,6 +352,7 @@ class TuyaPlatform {
669
352
  }
670
353
  // this.log.debug('onDeviceStatusUpdate devId = %s, status = %o}', device.id, status);
671
354
  handler.onDeviceStatusUpdate(status);
355
+ // Status updates occur frequently, so the saveDeviceList process is not performed.
672
356
  }
673
357
  removeAccessory(deviceID) {
674
358
  const handler = this.getAccessoryHandler(deviceID);
@@ -679,21 +363,103 @@ class TuyaPlatform {
679
363
  if (index >= 0) {
680
364
  this.accessoryHandlers.splice(index, 1);
681
365
  }
366
+ this.saveDeviceList();
682
367
  this.api.unregisterPlatformAccessories(settings_1.PLUGIN_NAME, settings_1.PLATFORM_NAME, [handler.accessory]);
683
368
  this.log.info('Removing existing accessory from cache:', handler.accessory.displayName);
369
+ this.platformAccessories.delete(handler.accessory.UUID);
370
+ }
371
+ async finishDeviceInitializing() {
372
+ // Remove stale cached accessories not claimed by any device
373
+ for (const [uuid, accessory] of this.platformAccessories) {
374
+ if (this.deviceManager.getDevice(accessory.context.deviceID) === undefined) {
375
+ this.log.warn('Removing unused accessory from cache:', accessory.displayName);
376
+ this.api.unregisterPlatformAccessories(settings_1.PLUGIN_NAME, settings_1.PLATFORM_NAME, [accessory]);
377
+ this.platformAccessories.delete(uuid);
378
+ }
379
+ }
684
380
  }
685
381
  getAccessoryHandler(deviceID) {
686
382
  return this.accessoryHandlers.find(handler => handler.device?.id === deviceID);
687
383
  }
688
- createWeatherDevice(device, result) {
689
- const key = `weather-${device.owner_id}`;
690
- const uuid = this.api.hap.uuid.generate(key);
691
- this.log.info(`add weather device:${key}`);
692
- const virtualDevice = this.deviceManager.createVirtualDevice(device, uuid);
693
- virtualDevice.product_id = 'virtual-product-id-weather';
694
- virtualDevice.category = 'wsdcg';
695
- virtualDevice.name = `Weather(${result.find(home => home.home_id === device.owner_id)?.name})`;
696
- return virtualDevice;
384
+ /**
385
+ * When device information is updated due to changes in the plugin settings,
386
+ * the updated data must be reflected in the accessory cache.
387
+ * @param device
388
+ * @param accessory
389
+ */
390
+ async syncDeviceAndAccessory(device, accessory) {
391
+ this.log.debug(`sync device to accessory cache. device: ${device.name}, accessory cache: ${accessory.displayName}`);
392
+ // A code value used to detect changes in the plugin settings.
393
+ const currentHash = this.deviceManager.createDeviceConfigHash(device);
394
+ if (currentHash !== accessory.context.deviceConfigHash) {
395
+ this.log.info(`Device config changed for "${device.name}" (${device.id}), will rebuild services`);
396
+ accessory.context.deviceConfigHash = currentHash;
397
+ // To reflect configuration changes without discarding the existing
398
+ // cache instance as much as possible, the latest configuration data is created as a separate accessory.
399
+ const cloneDevice = Object.assign(new TuyaDevice_1.default({}), (0, util_1.deepClone)(device));
400
+ this.deviceManager.devices.push(cloneDevice);
401
+ cloneDevice.id = 'dummyID' + cloneDevice.id;
402
+ cloneDevice.name = 'dummy';
403
+ let cloudConfig = this.platformConfig.cloud?.deviceOverrides?.find(i => i.id === device.id);
404
+ let localConfig = this.platformConfig.local?.devices?.find(i => i.tuyaDeviceId === device.id);
405
+ let serviceConfig = this.platformConfig.cloud?.serviceInformationOverrides?.find(i => i.device_id === device.id);
406
+ if (cloudConfig) {
407
+ cloudConfig = (0, util_1.deepClone)(cloudConfig);
408
+ cloudConfig.id = cloneDevice.id;
409
+ this.platformConfig.cloud.deviceOverrides.push(cloudConfig);
410
+ }
411
+ if (localConfig) {
412
+ localConfig = (0, util_1.deepClone)(localConfig);
413
+ localConfig.tuyaDeviceId = cloneDevice.id;
414
+ this.platformConfig.local.devices.push(localConfig);
415
+ }
416
+ if (serviceConfig) {
417
+ serviceConfig = (0, util_1.deepClone)(serviceConfig);
418
+ serviceConfig.device_id = cloneDevice.id;
419
+ this.platformConfig.cloud.serviceInformationOverrides.push(serviceConfig);
420
+ }
421
+ const freshAccessory = this.addAccessory(cloneDevice);
422
+ // The dummy item should be removed immediately.
423
+ this.removeAccessory(cloneDevice.id);
424
+ this.deviceManager.devices.splice(this.deviceManager.devices.indexOf(cloneDevice), 1);
425
+ if (cloudConfig) {
426
+ this.platformConfig.cloud.deviceOverrides.splice(this.platformConfig.cloud.deviceOverrides.indexOf(cloudConfig), 1);
427
+ }
428
+ if (localConfig) {
429
+ localConfig.tuyaDeviceId = cloneDevice.id;
430
+ this.platformConfig.local.devices.splice(this.platformConfig.local.devices.indexOf(localConfig), 1);
431
+ }
432
+ if (serviceConfig) {
433
+ serviceConfig.device_id = cloudConfig.id;
434
+ this.platformConfig.cloud.serviceInformationOverrides.splice(this.platformConfig.cloud.serviceInformationOverrides.indexOf(serviceConfig), 1);
435
+ }
436
+ if (freshAccessory === null) {
437
+ // hidden
438
+ this.removeAccessory(device.id);
439
+ return;
440
+ }
441
+ const newServices = freshAccessory.services;
442
+ const oldServices = accessory.services;
443
+ for (const oldService of oldServices) {
444
+ const existsInNew = newServices.some(newService => newService.UUID === oldService.UUID);
445
+ if (!existsInNew) {
446
+ accessory.removeService(oldService);
447
+ continue;
448
+ }
449
+ const newService = newServices.find(s => s.UUID === oldService.UUID);
450
+ if (!newService) {
451
+ continue;
452
+ }
453
+ for (const oldChar of oldService.characteristics) {
454
+ const existsCharInNew = newService.characteristics.some(newChar => newChar.UUID === oldChar.UUID);
455
+ if (!existsCharInNew) {
456
+ oldService.removeCharacteristic(oldChar);
457
+ }
458
+ }
459
+ }
460
+ }
461
+ this.log.success(`(update) publish accessory: ${accessory.displayName}`);
462
+ this.api.updatePlatformAccessories([accessory]);
697
463
  }
698
464
  }
699
465
  exports.TuyaPlatform = TuyaPlatform;