@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
@@ -37,14 +37,13 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
37
37
  };
38
38
  Object.defineProperty(exports, "__esModule", { value: true });
39
39
  const TuyaDevice_1 = __importStar(require("../cloud/device/TuyaDevice"));
40
- const TuyaOpenAPI_1 = __importDefault(require("../cloud/api/TuyaOpenAPI"));
41
- const TuyaDeviceManager_1 = __importDefault(require("../cloud/device/TuyaDeviceManager"));
40
+ const TuyaDeviceManager_1 = __importDefault(require("../shared/TuyaDeviceManager"));
42
41
  const TuyaDiscovery_1 = __importDefault(require("./TuyaDiscovery"));
43
42
  const LocalDevice_1 = __importDefault(require("./LocalDevice"));
44
- const Logger_1 = require("../shared/util/Logger");
45
43
  const ConfigHash_1 = require("../shared/util/ConfigHash");
46
44
  const ZigbeeGatewayDetection_1 = require("./ZigbeeGatewayDetection");
47
45
  const DynamicChildDiscovery_1 = require("./DynamicChildDiscovery");
46
+ const config_1 = require("../config");
48
47
  /**
49
48
  * Default DP-to-code mapping used when the user hasn't supplied one.
50
49
  * Keys are Tuya standard instruction codes; values are typical DP numbers.
@@ -92,41 +91,105 @@ function buildDpToCodeMap(dpMapping) {
92
91
  */
93
92
  class LocalDeviceManager extends TuyaDeviceManager_1.default {
94
93
  // ──────────────────────────────────────────────────────────────────────────
95
- constructor(localConfig, log, persistPath) {
96
- // Create a minimal dummy API so the parent initialises without connecting
97
- const dummyLog = new Logger_1.PrefixLogger(log, 'LocalDummy', false);
98
- const dummyApi = new TuyaOpenAPI_1.default('', '', '', dummyLog);
99
- super(dummyApi, false);
100
- this.localDevices = [];
94
+ constructor(localConfig, debugMode = false) {
95
+ super(debugMode);
101
96
  this.localConnections = new Map();
102
97
  this.discoveryTimeout = null;
103
98
  this.rediscoveryTimeout = null;
104
99
  this.isDiscoveryActive = false;
105
100
  this.discoveredInCurrentPhase = new Set();
106
- // Maps deviceId → effective dpMapping (code → dp)
107
101
  this.dpMaps = new Map();
108
- // Maps deviceId → reverse dpMapping (dp → code)
109
102
  this.reverseDpMaps = new Map();
110
- // Maps discovered gwId → IP (kept even after TuyaDevice created)
111
103
  this.discoveredIPs = new Map();
112
- // Maps discovered gwId → version
113
104
  this.discoveredVersions = new Map();
114
- // Pending local response watches to cancel superseded commands
115
105
  this.pendingLocalResponseWatchers = new Map();
116
- // Tracks which devices had config changes
117
- this.configChanged = new Map();
118
- // ── Zigbee gateway/child support ──────────────────────────────────────────
119
- /** Parent gateway ID → GatewayRelationship (derived from config at init). */
120
106
  this.gatewayRelationships = new Map();
121
- /** Parent gateway device ID → its active LocalDevice connection. */
122
107
  this.gatewayConnections = new Map();
123
- // Stop the MQ that the parent started
124
- this.mq.stop();
125
- this.parentLog = log;
126
- this.log = new Logger_1.PrefixLogger(log, 'LocalDeviceManager', false);
127
108
  this.config = localConfig;
128
- this.discovery = new TuyaDiscovery_1.default(log);
129
- this.configHash = new ConfigHash_1.ConfigHash(persistPath || '', 'tuya-local-configs');
109
+ this.discovery = new TuyaDiscovery_1.default();
110
+ }
111
+ async pullDevices() {
112
+ await this.initLocalDevices();
113
+ this.connectAllDevices();
114
+ const devices = [...this.devices];
115
+ this.log.info(`Registered ${devices.length} local device(s).`);
116
+ return devices;
117
+ }
118
+ createDeviceConfigHash(device) {
119
+ const cfg = this.config.devices?.find(config => config.tuyaDeviceId === device.id);
120
+ if (!cfg) {
121
+ return '';
122
+ }
123
+ const effectiveSwitchCount = this._getEffectiveSwitchCount(cfg, device);
124
+ // Check if config has changed since last run
125
+ // Hash the config fields that affect device behavior
126
+ const configToHash = {
127
+ tuyaDeviceId: cfg.tuyaDeviceId,
128
+ name: cfg.name,
129
+ tuyaKey: cfg.tuyaKey,
130
+ dpMapping: this._getDPMap(cfg.tuyaDeviceId),
131
+ switchCount: effectiveSwitchCount,
132
+ outletCount: effectiveSwitchCount,
133
+ protocolVersion: cfg.protocolVersion,
134
+ category: cfg.category,
135
+ };
136
+ return ConfigHash_1.ConfigHash.computeHash(configToHash);
137
+ }
138
+ configDevice(device) {
139
+ const deviceConfig = this.getDeviceConfig(device);
140
+ if (deviceConfig?.category) {
141
+ this.log.warn('Override %o category from %o to %o', device.name, device.category, deviceConfig.category);
142
+ device.category = deviceConfig.category;
143
+ }
144
+ if (deviceConfig?.unbridged) {
145
+ this.log.warn('Unbridge %o category %o', device.name, device.category);
146
+ device.unbridged = deviceConfig.unbridged;
147
+ }
148
+ }
149
+ getDeviceSchemaConfig(device, dpCode) {
150
+ const schema = this.getDeviceConfig(device)?.schema;
151
+ if (!schema || !dpCode) {
152
+ return undefined;
153
+ }
154
+ // ignore case - allow both the device code and its (optional) rename
155
+ const target = dpCode.toString().toLowerCase();
156
+ return schema.find(s => {
157
+ const code = s.code?.toString().toLowerCase();
158
+ const newCode = s.newCode?.toString().toLowerCase();
159
+ return code === target || newCode === target;
160
+ });
161
+ }
162
+ enableGarageDoorUseContactSensorForState(device) {
163
+ return this.getDeviceConfig(device)?.garageDoorUseContactSensorForState === true;
164
+ }
165
+ async retrieveDeviceRTSP(deviceID) {
166
+ const cameraConfig = this.config.cameras?.find(cameraConfig => cameraConfig.deviceId === deviceID);
167
+ if (cameraConfig) {
168
+ const rtspUrl = cameraConfig.rtspUrl;
169
+ if (rtspUrl.includes('@') || !cameraConfig.username) {
170
+ return rtspUrl;
171
+ }
172
+ else {
173
+ return `rtsp://${cameraConfig.username}:${cameraConfig.password}@${rtspUrl.substring('rtsp://'.length)}`;
174
+ }
175
+ }
176
+ return '';
177
+ }
178
+ getDeviceConfig(device) {
179
+ if (!this.config) {
180
+ return undefined;
181
+ }
182
+ // Find matching override, respecting source filtering
183
+ const matches = this.config.deviceOverrides?.filter(config => {
184
+ const sourceMatch = [config_1.TuyaPluginMode.local, config_1.TuyaPluginMode.both].includes(config.configFor);
185
+ const idMatch = config.id === device.id || config.id === device.uuid ||
186
+ config.id === device.product_id || config.id === 'global';
187
+ return sourceMatch && idMatch;
188
+ }) ?? [];
189
+ // Return device-specific config, then product, then global
190
+ return matches.find(config => config.id === device.id || config.id === device.uuid) ||
191
+ matches.find(config => config.id === device.product_id) ||
192
+ matches.find(config => config.id === 'global');
130
193
  }
131
194
  /**
132
195
  * Initialise discovery and register manually configured devices.
@@ -134,9 +197,10 @@ class LocalDeviceManager extends TuyaDeviceManager_1.default {
134
197
  */
135
198
  async initLocalDevices() {
136
199
  // ── Zigbee: detect parent-child relationships before registering devices ──
137
- if (this.config.devices && this.config.devices.length > 0) {
200
+ const zigbeeDevices = this.config.devices?.filter(ZigbeeGatewayDetection_1.ZigbeeGatewayDetection.isZigbeeDevice);
201
+ if (zigbeeDevices && zigbeeDevices.length > 0) {
138
202
  try {
139
- this.gatewayRelationships = ZigbeeGatewayDetection_1.ZigbeeGatewayDetection.detectFromDevices(this.config.devices);
203
+ this.gatewayRelationships = ZigbeeGatewayDetection_1.ZigbeeGatewayDetection.detectFromDevices(zigbeeDevices);
140
204
  if (this.gatewayRelationships.size > 0) {
141
205
  for (const [parentId, rel] of this.gatewayRelationships) {
142
206
  this.log.info(`Zigbee gateway detected: ${parentId} has ${rel.children.length} sub-device(s): ` +
@@ -157,10 +221,13 @@ class LocalDeviceManager extends TuyaDeviceManager_1.default {
157
221
  if (ZigbeeGatewayDetection_1.ZigbeeGatewayDetection.isChild(cfg)) {
158
222
  continue;
159
223
  }
160
- this._registerDeviceConfig(cfg);
224
+ const device = this._registerDeviceConfig(cfg);
225
+ if (!!device) {
226
+ this._syncSubDevices(device);
227
+ }
161
228
  }
162
229
  // Validate devices have keys if auto-discovery is disabled
163
- if (this.config.autoDiscoverDevices === false) {
230
+ if (!this.config.autoDiscoverDevices) {
164
231
  for (const cfg of this.config.devices) {
165
232
  if (!cfg.tuyaKey && !ZigbeeGatewayDetection_1.ZigbeeGatewayDetection.isChild(cfg)) {
166
233
  const deviceName = cfg.name || cfg.tuyaDeviceId;
@@ -172,7 +239,7 @@ class LocalDeviceManager extends TuyaDeviceManager_1.default {
172
239
  }
173
240
  // Start UDP discovery unless explicitly disabled
174
241
  if (this.config.autoDiscoverDevices !== false) {
175
- this.discovery.on('discover', (result) => {
242
+ this.discovery.on(TuyaDiscovery_1.default.Events.DEVICE_DISCOVER, (result) => {
176
243
  this._onDiscovered(result);
177
244
  });
178
245
  this._startDiscoveryPhase();
@@ -185,6 +252,59 @@ class LocalDeviceManager extends TuyaDeviceManager_1.default {
185
252
  }
186
253
  }
187
254
  }
255
+ /** sync connection info for sub devices */
256
+ _syncSubDevices(device) {
257
+ // We assume cases where only the parent device has settings and the sub‑device has none, as well as the opposite situation.
258
+ // or maybe a possibility that some cases may still be undetected.
259
+ if (device.sub) {
260
+ const parentDevice = this.devices.find(d => d.id === device.parent_id);
261
+ if (parentDevice) {
262
+ this._syncConnectionInfo(parentDevice, device);
263
+ }
264
+ else {
265
+ const dummyParentDevice = this._buildDiscoveredDevice({
266
+ id: device.parent_id,
267
+ ip: '',
268
+ version: '',
269
+ });
270
+ this._syncConnectionInfo(dummyParentDevice, device);
271
+ this.devices.push(dummyParentDevice);
272
+ }
273
+ }
274
+ else {
275
+ const subDevices = this.devices.filter(d => d.sub && d.parent_id === device.id);
276
+ subDevices.forEach(subDevice => {
277
+ this._syncConnectionInfo(device, subDevice);
278
+ });
279
+ // else no subDevices
280
+ }
281
+ }
282
+ /**
283
+ * parentDevice and subDevice must have same localIp, localKey, localVersion.
284
+ * priority: value exists > value not exists > subDevice > parentDevice
285
+ */
286
+ _syncConnectionInfo(parentDevice, subDevice) {
287
+ const connectionProps = ['localIp', 'localKey', 'localVersion'];
288
+ for (const prop of connectionProps) {
289
+ const parentValue = parentDevice[prop];
290
+ const subValue = subDevice[prop];
291
+ const resolvedValue = this._resolveConnectionInfoValue(parentValue, subValue);
292
+ if (resolvedValue === undefined) {
293
+ continue;
294
+ }
295
+ parentDevice[prop] = resolvedValue;
296
+ subDevice[prop] = resolvedValue;
297
+ }
298
+ }
299
+ _resolveConnectionInfoValue(parentValue, subValue) {
300
+ if (subValue !== undefined && subValue !== null && subValue !== '') {
301
+ return subValue;
302
+ }
303
+ if (parentValue !== undefined && parentValue !== null && parentValue !== '') {
304
+ return parentValue;
305
+ }
306
+ return undefined;
307
+ }
188
308
  /** Stop discovery and disconnect all local TCP connections. */
189
309
  stopLocalDevices() {
190
310
  if (this.discoveryTimeout) {
@@ -207,9 +327,24 @@ class LocalDeviceManager extends TuyaDeviceManager_1.default {
207
327
  }
208
328
  this.gatewayConnections.clear();
209
329
  }
210
- /** Returns ALL devices (both local shadow entries from this manager). */
211
330
  getDevice(deviceID) {
212
- return this.localDevices.find(d => d.id === deviceID);
331
+ const device = this.devices.find(d => d.id === deviceID);
332
+ if (device) {
333
+ // local devices have synthetic schemas based on config, so enrich the device schema from config on retrieval
334
+ // Merge config schema with device schema, prioritizing config
335
+ device.schema.forEach(s => {
336
+ const configSchema = this.getDeviceConfig(device)?.schema?.find(cs => cs.code === s.code);
337
+ if (configSchema) {
338
+ Object.assign(s, configSchema);
339
+ }
340
+ });
341
+ this.getDeviceConfig(device)?.schema?.forEach(cs => {
342
+ if (!device.schema.some(s => s.code === cs.code)) {
343
+ device.schema.push(cs);
344
+ }
345
+ });
346
+ }
347
+ return device;
213
348
  }
214
349
  /**
215
350
  * Send commands to a local device.
@@ -217,10 +352,10 @@ class LocalDeviceManager extends TuyaDeviceManager_1.default {
217
352
  * all existing accessory handlers work without modification.
218
353
  */
219
354
  async sendCommands(deviceID, commands) {
220
- const dpMap = this.dpMaps.get(deviceID);
355
+ const dpMap = this._getDPMap(deviceID);
221
356
  if (!dpMap) {
222
357
  this.log.warn(`No dpMapping for local device ${deviceID}`);
223
- return;
358
+ return false;
224
359
  }
225
360
  const dps = {};
226
361
  for (const { code, value } of commands) {
@@ -232,10 +367,10 @@ class LocalDeviceManager extends TuyaDeviceManager_1.default {
232
367
  dps[String(dp)] = value;
233
368
  }
234
369
  if (Object.keys(dps).length === 0) {
235
- return;
370
+ return false;
236
371
  }
237
372
  // Log the command for user visibility
238
- const device = this.localDevices.find(d => d.id === deviceID);
373
+ const device = this.getDevice(deviceID);
239
374
  const deviceName = device?.name || deviceID;
240
375
  const commandStr = commands.map(c => `${c.code}=${c.value}`).join(', ');
241
376
  this.log.info(`[${deviceName}] Sending command (local): ${commandStr}`);
@@ -251,7 +386,7 @@ class LocalDeviceManager extends TuyaDeviceManager_1.default {
251
386
  if (!conn) {
252
387
  conn = this._createConnection(deviceID);
253
388
  if (!conn) {
254
- return;
389
+ return false;
255
390
  }
256
391
  }
257
392
  const expectedDps = new Set(Object.keys(dps));
@@ -312,12 +447,13 @@ class LocalDeviceManager extends TuyaDeviceManager_1.default {
312
447
  return true;
313
448
  }
314
449
  try {
315
- await responsePromise;
316
- return true;
450
+ return await responsePromise;
317
451
  }
318
452
  catch (error) {
319
453
  this.log.warn(`Local command timeout or error for ${deviceName}: ${error instanceof Error ? error.message : error}`);
320
- throw error;
454
+ // To prevent forcing the caller to implement try‑catch handling, this function will not throw error.
455
+ // throw error;
456
+ return false;
321
457
  }
322
458
  }
323
459
  _cancelSupersededLocalResponseWatchers(deviceID, expectedDps) {
@@ -349,52 +485,11 @@ class LocalDeviceManager extends TuyaDeviceManager_1.default {
349
485
  this.pendingLocalResponseWatchers.delete(deviceID);
350
486
  }
351
487
  }
352
- handleCloudStatusUpdate(deviceID, statusUpdates) {
353
- const device = this.localDevices.find(d => d.id === deviceID);
354
- if (!device) {
355
- return;
356
- }
357
- const dpMap = this.dpMaps.get(deviceID) ?? {};
358
- const statusDps = new Set();
359
- for (const status of statusUpdates) {
360
- const dp = dpMap[status.code];
361
- statusDps.add(dp !== undefined ? String(dp) : String(status.code));
362
- const existing = device.status.find(s => s.code === status.code);
363
- if (existing) {
364
- existing.value = status.value;
365
- }
366
- else {
367
- device.status.push({ code: status.code, value: status.value });
368
- }
369
- }
370
- const watchers = this.pendingLocalResponseWatchers.get(deviceID);
371
- if (!watchers || watchers.length === 0) {
372
- return;
373
- }
374
- const remaining = [];
375
- for (const watcher of watchers) {
376
- let matched = false;
377
- for (const dp of watcher.expectedDps) {
378
- if (statusDps.has(dp)) {
379
- matched = true;
380
- break;
381
- }
382
- }
383
- if (matched) {
384
- watcher.cleanup();
385
- watcher.resolve(true);
386
- }
387
- else {
388
- remaining.push(watcher);
389
- }
390
- }
391
- if (remaining.length > 0) {
392
- this.pendingLocalResponseWatchers.set(deviceID, remaining);
393
- }
394
- else {
395
- this.pendingLocalResponseWatchers.delete(deviceID);
396
- }
397
- this.emit(TuyaDeviceManager_1.default.Events.DEVICE_STATUS_UPDATE, device, statusUpdates);
488
+ /**
489
+ * Logic to suppress unnecessary discovery processes.
490
+ */
491
+ static isTargetDevice(device) {
492
+ return !device.isVirtualDevice() || !device.sub;
398
493
  }
399
494
  // ── Private helpers ────────────────────────────────────────────────────────
400
495
  /**
@@ -407,7 +502,7 @@ class LocalDeviceManager extends TuyaDeviceManager_1.default {
407
502
  }
408
503
  this.isDiscoveryActive = true;
409
504
  this.discoveredInCurrentPhase.clear();
410
- this.log.info('Starting discovery phase');
505
+ this.log.debug('Starting discovery phase');
411
506
  this.discovery.start();
412
507
  // Stop after configurable timeout (config is in seconds, default 60s)
413
508
  // Convert to milliseconds
@@ -420,16 +515,16 @@ class LocalDeviceManager extends TuyaDeviceManager_1.default {
420
515
  this.discovery.stop();
421
516
  // Report discovery results for clarity
422
517
  if (this.discoveredInCurrentPhase.size > 0) {
423
- this.log.info(`Ended discovery phase – rediscovered ${this.discoveredInCurrentPhase.size} device(s)`);
518
+ this.log.debug(`Ended discovery phase – rediscovered ${this.discoveredInCurrentPhase.size} device(s)`);
424
519
  }
425
520
  else {
426
521
  // No devices found; they may be using cached IPs from previous discovery
427
- const knownDevices = this.localDevices.filter(d => d.localKey);
522
+ const knownDevices = this.devices.filter(d => d.localKey);
428
523
  if (knownDevices.length > 0) {
429
- this.log.info(`Ended discovery phase – no new devices found (${knownDevices.length} configured device(s) using cached IP)`);
524
+ this.log.debug(`Ended discovery phase – no new devices found (${knownDevices.length} configured device(s) using cached IP)`);
430
525
  }
431
526
  else {
432
- this.log.info('Ended discovery phase – no devices found');
527
+ this.log.debug('Ended discovery phase – no devices found');
433
528
  }
434
529
  }
435
530
  this.discoveryTimeout = null;
@@ -457,35 +552,19 @@ class LocalDeviceManager extends TuyaDeviceManager_1.default {
457
552
  if (!cfg.tuyaDeviceId) {
458
553
  const name = cfg.name ?? 'unknown';
459
554
  this.log.warn(`Skipping invalid local config entry for ${name}: missing tuyaDeviceId`);
460
- return;
461
- }
462
- const effectiveMap = Object.assign({}, DEFAULT_DP_MAP, cfg.dpMapping ?? {});
463
- this.dpMaps.set(cfg.tuyaDeviceId, effectiveMap);
464
- this.reverseDpMaps.set(cfg.tuyaDeviceId, buildDpToCodeMap(effectiveMap));
465
- const existing = this.localDevices.find(d => d.id === cfg.tuyaDeviceId);
466
- // Check if config has changed since last run
467
- // Hash the config fields that affect device behavior
468
- const configToHash = {
469
- tuyaDeviceId: cfg.tuyaDeviceId,
470
- name: cfg.name,
471
- tuyaKey: cfg.tuyaKey,
472
- dpMapping: cfg.dpMapping,
473
- switchCount: cfg.switchCount,
474
- outletCount: cfg.outletCount,
475
- protocolVersion: cfg.protocolVersion,
476
- category: cfg.category,
477
- };
478
- const { changed: configChanged } = this.configHash.hasConfigChanged(cfg.tuyaDeviceId, configToHash);
479
- this.configChanged.set(cfg.tuyaDeviceId, configChanged);
480
- if (configChanged && existing) {
481
- this.log.info(`Device ${cfg.tuyaDeviceId}: config changed, rebuilding schema`);
555
+ return undefined;
482
556
  }
483
- if (!existing) {
484
- const device = this._buildTuyaDevice(cfg, configChanged);
485
- this.localDevices.push(device);
486
- this.devices.push(device); // keep parent's devices array in sync
487
- this.emit(TuyaDeviceManager_1.default.Events.DEVICE_ADD, device);
557
+ const existing = this.getDevice(cfg.tuyaDeviceId);
558
+ if (existing) {
559
+ return existing;
488
560
  }
561
+ const effectiveMap = this._createDPMap(cfg);
562
+ this._setDPMap(cfg.tuyaDeviceId, cfg.parentDeviceId, ZigbeeGatewayDetection_1.ZigbeeGatewayDetection.isZigbeeDevice(cfg), effectiveMap);
563
+ const device = this._buildTuyaDevice(cfg);
564
+ this.configDevice(device);
565
+ this.devices.push(device);
566
+ this.emit(TuyaDeviceManager_1.default.Events.DEVICE_ADD, device);
567
+ return device;
489
568
  }
490
569
  /**
491
570
  * Set up auto-detection listener to count switches from device status updates.
@@ -494,7 +573,7 @@ class LocalDeviceManager extends TuyaDeviceManager_1.default {
494
573
  */
495
574
  _setupAutoDetectionListener(deviceId, device, conn) {
496
575
  const cfg = this.config.devices?.find(d => d.tuyaDeviceId === deviceId);
497
- if (!cfg || (cfg.switchCount || cfg.outletCount)) {
576
+ if (!cfg || this._getConfiguredSwitchCount(cfg) !== undefined) {
498
577
  return; // Already has explicit count
499
578
  }
500
579
  this.log.debug(`[AutoDetect] Setting up auto-detection listener for ${deviceId}`);
@@ -503,8 +582,7 @@ class LocalDeviceManager extends TuyaDeviceManager_1.default {
503
582
  const onStatusChange = (changes) => {
504
583
  this.log.debug(`[AutoDetect] Status change received for ${deviceId}: ${JSON.stringify(Object.keys(changes))}`);
505
584
  // Collect switch numbers from incoming changes
506
- const dpMap = this.dpMaps.get(deviceId) ?? DEFAULT_DP_MAP;
507
- const reverseMap = buildDpToCodeMap(dpMap);
585
+ const reverseMap = this.reverseDpMaps.get(deviceId) ?? {};
508
586
  for (const dpStr of Object.keys(changes)) {
509
587
  const dpNum = parseInt(dpStr, 10);
510
588
  const code = reverseMap[dpNum];
@@ -527,28 +605,12 @@ class LocalDeviceManager extends TuyaDeviceManager_1.default {
527
605
  this.log.info(`[AutoDetect] Auto-detected ${detectedCount} switch(es) for "${cfg.name}" (${deviceId}): ` +
528
606
  `switches=[${switchList}]`);
529
607
  // Update device schema with detected switch count
530
- device.schema = this._buildSyntheticSchema(this.dpMaps.get(deviceId) ?? DEFAULT_DP_MAP, detectedCount);
531
- device.switchCount = detectedCount;
608
+ device.schema = this._buildSyntheticSchema(device, this._getDPMap(deviceId), detectedCount);
609
+ device.switchCount = this._getEffectiveSwitchCount(cfg) ?? detectedCount;
532
610
  // Auto-detection is now complete
533
611
  device.isAutoDetecting = false;
534
- // Recalculate config hash since the schema has changed
535
- // This ensures SwitchAccessory knows to rebuild services
536
- const updatedConfigToHash = {
537
- tuyaDeviceId: cfg.tuyaDeviceId,
538
- name: cfg.name,
539
- tuyaKey: cfg.tuyaKey,
540
- dpMapping: cfg.dpMapping,
541
- switchCount: detectedCount,
542
- outletCount: cfg.outletCount,
543
- protocolVersion: cfg.protocolVersion,
544
- category: cfg.category,
545
- };
546
- const { changed: schemaChanged } = this.configHash.hasConfigChanged(deviceId, updatedConfigToHash);
547
- if (schemaChanged) {
548
- this.log.debug('[AutoDetect] Schema changed after auto-detection, marking config as changed');
549
- device.configChanged = true;
550
- this.configChanged.set(deviceId, true);
551
- }
612
+ // update config
613
+ cfg.switchCount = device.switchCount;
552
614
  // Notify the platform so configureServices() is re-run and switch_2+ services are added.
553
615
  this.emit(TuyaDeviceManager_1.default.Events.DEVICE_INFO_UPDATE, device, { schemaUpdated: true });
554
616
  }
@@ -568,7 +630,7 @@ class LocalDeviceManager extends TuyaDeviceManager_1.default {
568
630
  this.discoveredVersions.set(result.id, result.version);
569
631
  this.discoveredInCurrentPhase.add(result.id);
570
632
  // If we already have a TuyaDevice for this ID (from manual config), update its IP
571
- const existing = this.localDevices.find(d => d.id === result.id);
633
+ const existing = this.getDevice(result.id);
572
634
  if (existing) {
573
635
  this.log.info(`Device ${result.id} (v${result.version}) rediscovered @ ${result.ip} ` +
574
636
  '– updating connection');
@@ -594,14 +656,14 @@ class LocalDeviceManager extends TuyaDeviceManager_1.default {
594
656
  // New device discovered via UDP – create a placeholder TuyaDevice
595
657
  // (it won't be controllable until the user provides a tuyaKey via config)
596
658
  const device = this._buildDiscoveredDevice(result);
597
- this.localDevices.push(device);
659
+ this.configDevice(device);
598
660
  this.devices.push(device);
599
661
  const productInfo = result.productKey ? ` productKey=${result.productKey}` : '';
600
662
  this.log.info(`Local device discovered: ${result.id} @ ${result.ip} (v${result.version}${productInfo}) ` +
601
663
  '– add to config with tuyaKey to control');
602
664
  this.emit(TuyaDeviceManager_1.default.Events.DEVICE_ADD, device);
603
665
  }
604
- _buildTuyaDevice(cfg, configChanged = false) {
666
+ _buildTuyaDevice(cfg) {
605
667
  const device = new TuyaDevice_1.default({});
606
668
  const deviceId = cfg.tuyaDeviceId ?? '';
607
669
  device.id = deviceId;
@@ -622,11 +684,15 @@ class LocalDeviceManager extends TuyaDeviceManager_1.default {
622
684
  device.active_time = 0;
623
685
  device.update_time = 0;
624
686
  device.status = [];
625
- const switchCount = cfg.switchCount ?? cfg.outletCount ?? 1;
626
- const autoDetectNote = !cfg.switchCount && !cfg.outletCount && cfg.tuyaKey ? ' (auto-detecting on connect)' : '';
627
- const isAutoDetecting = !!(cfg.tuyaKey && !cfg.switchCount && !cfg.outletCount);
687
+ device.parent_id = cfg.parentDeviceId;
688
+ device.remote_keys = cfg.remote_keys;
689
+ device.sub = !ZigbeeGatewayDetection_1.ZigbeeGatewayDetection.isChild(cfg) && !!cfg.parentDeviceId;
690
+ const configuredSwitchCount = this._getConfiguredSwitchCount(cfg);
691
+ const switchCount = configuredSwitchCount ?? 1;
692
+ const autoDetectNote = configuredSwitchCount === undefined && cfg.tuyaKey ? ' (auto-detecting on connect)' : '';
693
+ const isAutoDetecting = !!(cfg.tuyaKey && configuredSwitchCount === undefined);
628
694
  this.log.info(`Building synthetic schema for ${cfg.name} with switchCount=${switchCount}${autoDetectNote}`);
629
- device.schema = this._buildSyntheticSchema(Object.assign({}, DEFAULT_DP_MAP, cfg.dpMapping ?? {}), switchCount);
695
+ device.schema = this._buildSyntheticSchema(device, this._getDPMap(device.id), switchCount);
630
696
  this.log.info(`Device "${cfg.name}" schema includes ${device.schema.length} codes: ${device.schema.map(s => s.code).join(', ')}`);
631
697
  // Store local-specific metadata directly on the device object for later use
632
698
  device.localKey = cfg.tuyaKey;
@@ -638,8 +704,7 @@ class LocalDeviceManager extends TuyaDeviceManager_1.default {
638
704
  device.isLocal = true;
639
705
  device.switchCount = switchCount;
640
706
  device.isAutoDetecting = isAutoDetecting;
641
- // Track whether config changed (used by SwitchAccessory to decide whether to remove cached extras)
642
- device.configChanged = configChanged;
707
+ this.log.debug(`_buildTuyaDevice local device:${JSON.stringify(device)}`);
643
708
  return device;
644
709
  }
645
710
  _buildDiscoveredDevice(result) {
@@ -665,9 +730,10 @@ class LocalDeviceManager extends TuyaDeviceManager_1.default {
665
730
  device.schema = [];
666
731
  device.isLocal = true;
667
732
  device.localVersion = result.version;
733
+ this.log.info(`_buildDiscoveredDevice local device:${JSON.stringify(device)}`);
668
734
  return device;
669
735
  }
670
- _buildSyntheticSchema(dpMapping, switchCount = 1) {
736
+ _buildSyntheticSchema(device, dpMapping, switchCount = 1) {
671
737
  // If switchCount is limited, filter switch-related entries to only the needed ones
672
738
  let filteredMapping = dpMapping;
673
739
  if (switchCount > 0) {
@@ -676,11 +742,6 @@ class LocalDeviceManager extends TuyaDeviceManager_1.default {
676
742
  filteredMapping = {};
677
743
  const switchCodes = [];
678
744
  for (const [code, dp] of Object.entries(dpMapping)) {
679
- // Keep non-switch DPs (brightness, temp, color, etc.)
680
- if (!code.startsWith('switch')) {
681
- filteredMapping[code] = dp;
682
- }
683
- // For switches, only include switch_1..switch_N
684
745
  const switchMatch = code.match(/^switch_(\d+)$/);
685
746
  if (switchMatch) {
686
747
  const switchNum = parseInt(switchMatch[1], 10);
@@ -688,51 +749,93 @@ class LocalDeviceManager extends TuyaDeviceManager_1.default {
688
749
  filteredMapping[code] = dp;
689
750
  switchCodes.push(code);
690
751
  }
752
+ continue;
691
753
  }
754
+ // Non-numbered switch codes such as switch_fan, switch_led,
755
+ // switch_horizontal, etc. are preserved.
756
+ filteredMapping[code] = dp;
692
757
  }
693
758
  if (switchCodes.length < Object.keys(dpMapping).filter(k => k.startsWith('switch')).length) {
694
759
  this.log.debug(`Filtered switches: kept [${switchCodes.join(', ')}], excluded others`);
695
760
  }
696
761
  }
697
- return Object.keys(filteredMapping).map(code => ({
762
+ this.log.debug(`local device schema codes:${JSON.stringify(filteredMapping)}`);
763
+ const schemas = Object.keys(filteredMapping).map(code => ({
698
764
  code,
699
765
  mode: TuyaDevice_1.TuyaDeviceSchemaMode.READ_WRITE,
700
766
  type: TuyaDevice_1.TuyaDeviceSchemaType.Boolean, // best-effort default; handlers may override
701
767
  property: {},
702
768
  }));
769
+ // apply overrides. The synthetic schemas are keyed on the (possibly renamed)
770
+ // dp code, so match on newCode when a rename is set, otherwise on the device code.
771
+ this.getDeviceConfig(device)?.schema?.forEach(s => {
772
+ if (!s.code) {
773
+ return;
774
+ }
775
+ const targetCode = s.newCode?.trim() || s.code;
776
+ if (!targetCode) {
777
+ return;
778
+ }
779
+ const schema = schemas.find(t => t.code === targetCode);
780
+ if (!!schema) {
781
+ schema.type = s.type ?? schema.type;
782
+ schema.property = s.property ?? schema.property;
783
+ }
784
+ else {
785
+ schemas.push({
786
+ code: targetCode,
787
+ mode: TuyaDevice_1.TuyaDeviceSchemaMode.READ_WRITE,
788
+ type: s.type ?? TuyaDevice_1.TuyaDeviceSchemaType.Boolean,
789
+ property: s.property ?? {},
790
+ });
791
+ }
792
+ });
793
+ return schemas;
703
794
  }
704
795
  _createConnection(deviceID) {
705
- const device = this.localDevices.find(d => d.id === deviceID);
796
+ let device = this.getDevice(deviceID);
706
797
  if (!device) {
707
798
  return undefined;
708
799
  }
709
- const localKey = device.localKey;
800
+ // Since the sub‑device’s actual entity is the parent device, no separate connection is established for the sub‑device.
801
+ if (device.sub) {
802
+ const parentDevice = this.getDevice(device.parent_id);
803
+ if (parentDevice) {
804
+ device = parentDevice;
805
+ }
806
+ else {
807
+ this.log.warn(`parent device not found. parentDeviceID: ${device.parent_id}, subDeviceID: ${deviceID}.`);
808
+ this.log.warn('sub device will be used as parent device.');
809
+ }
810
+ }
811
+ const connectDeviceID = device.id;
812
+ let localKey = device.localKey;
710
813
  if (!localKey) {
711
- this.log.warn(`No tuyaKey configured for local device ${deviceID} — cannot connect.`);
814
+ this.log.warn(`No tuyaKey configured for local device ${connectDeviceID} — cannot connect.`);
712
815
  return undefined;
713
816
  }
714
- const ip = device.ip || device.localIp;
817
+ const ip = device.localIp ?? device.ip;
715
818
  if (!ip) {
716
- this.log.warn(`No IP known for local device ${deviceID} — cannot connect.`);
819
+ this.log.warn(`No IP known for local device ${connectDeviceID} — cannot connect.`);
717
820
  return undefined;
718
821
  }
719
822
  const version = device.localVersion
720
- ?? this.discoveredVersions.get(deviceID)
823
+ ?? this.discoveredVersions.get(connectDeviceID)
721
824
  ?? '3.3';
722
825
  const conn = new LocalDevice_1.default({
723
- id: deviceID,
826
+ id: connectDeviceID,
724
827
  key: Buffer.from(localKey, 'utf8'),
725
828
  ip,
726
829
  version,
727
830
  name: device.name,
728
- }, this.parentLog);
831
+ });
729
832
  conn.on('connect', () => {
730
833
  device.online = true;
731
834
  this.emit(TuyaDeviceManager_1.default.Events.DEVICE_INFO_UPDATE, device, { online: true });
732
835
  // ── Zigbee: when a gateway connects, set up its children ──
733
- if (this.gatewayRelationships.has(deviceID)) {
734
- this.gatewayConnections.set(deviceID, conn);
735
- this._setupZigbeeChildren(deviceID, conn);
836
+ if (this.gatewayRelationships.has(connectDeviceID)) {
837
+ this.gatewayConnections.set(connectDeviceID, conn);
838
+ this._setupZigbeeChildren(connectDeviceID, conn);
736
839
  }
737
840
  });
738
841
  conn.on('disconnect', () => {
@@ -740,7 +843,7 @@ class LocalDeviceManager extends TuyaDeviceManager_1.default {
740
843
  this.emit(TuyaDeviceManager_1.default.Events.DEVICE_INFO_UPDATE, device, { online: false });
741
844
  });
742
845
  conn.on('change', (changes) => {
743
- const reverseMap = this.reverseDpMaps.get(deviceID) ?? {};
846
+ const reverseMap = this.reverseDpMaps.get(connectDeviceID) ?? {};
744
847
  const statusUpdates = [];
745
848
  for (const [dp, val] of Object.entries(changes)) {
746
849
  const code = reverseMap[parseInt(dp, 10)] ?? dp;
@@ -759,12 +862,12 @@ class LocalDeviceManager extends TuyaDeviceManager_1.default {
759
862
  }
760
863
  });
761
864
  conn.on('error', (err) => {
762
- this.log.warn(`Error for local device ${deviceID}: ${err.message}`);
865
+ this.log.warn(`Error for local device ${connectDeviceID}: ${err.message}`);
763
866
  });
764
867
  // Set up auto-detection listener BEFORE connecting to avoid race conditions
765
- this._setupAutoDetectionListener(deviceID, device, conn);
868
+ this._setupAutoDetectionListener(connectDeviceID, device, conn);
766
869
  conn.connect();
767
- this.localConnections.set(deviceID, conn);
870
+ this.localConnections.set(connectDeviceID, conn);
768
871
  return conn;
769
872
  }
770
873
  /**
@@ -797,7 +900,7 @@ class LocalDeviceManager extends TuyaDeviceManager_1.default {
797
900
  * Preserves the child's state and event listeners while updating the parent reference.
798
901
  */
799
902
  _reconnectZigbeeChild(childDeviceId, cid, childConn, newParentConn) {
800
- const childTuyaDevice = this.localDevices.find(d => d.id === childDeviceId);
903
+ const childTuyaDevice = this.getDevice(childDeviceId);
801
904
  if (!childTuyaDevice) {
802
905
  this.log.warn(`Zigbee child ${childDeviceId}: TuyaDevice not found during reconnect`);
803
906
  return;
@@ -837,10 +940,10 @@ class LocalDeviceManager extends TuyaDeviceManager_1.default {
837
940
  this.log.debug(`Zigbee child ${childDeviceId}: using per-child category override`);
838
941
  }
839
942
  // Register TuyaDevice if not already present
840
- if (!this.localDevices.find(d => d.id === childDeviceId)) {
943
+ if (!this.getDevice(childDeviceId)) {
841
944
  this._registerDeviceConfig(effectiveChildCfg);
842
945
  }
843
- const childTuyaDevice = this.localDevices.find(d => d.id === childDeviceId);
946
+ const childTuyaDevice = this.getDevice(childDeviceId);
844
947
  if (!childTuyaDevice) {
845
948
  this.log.warn(`Could not build TuyaDevice for Zigbee child ${childDeviceId}`);
846
949
  return;
@@ -852,7 +955,7 @@ class LocalDeviceManager extends TuyaDeviceManager_1.default {
852
955
  ip: parentConn['context'].ip,
853
956
  version: parentConn['context'].version,
854
957
  name: cfg.name ?? childDeviceId,
855
- }, this.parentLog);
958
+ });
856
959
  // Link parent ↔ child
857
960
  childConn.parentDevice = parentConn;
858
961
  childConn.childId = cid;
@@ -958,7 +1061,7 @@ class LocalDeviceManager extends TuyaDeviceManager_1.default {
958
1061
  }
959
1062
  /** Connect all configured local devices eagerly (called after init). */
960
1063
  connectAllDevices() {
961
- for (const device of this.localDevices) {
1064
+ for (const device of this.devices) {
962
1065
  const localKey = device.localKey;
963
1066
  if (!localKey) {
964
1067
  continue; // Skip if no local key configured
@@ -976,6 +1079,88 @@ class LocalDeviceManager extends TuyaDeviceManager_1.default {
976
1079
  }
977
1080
  }
978
1081
  }
1082
+ _getConfiguredSwitchCount(cfg) {
1083
+ if (!cfg) {
1084
+ return undefined;
1085
+ }
1086
+ if (cfg.switchCount !== undefined) {
1087
+ return cfg.switchCount;
1088
+ }
1089
+ if (cfg.outletCount !== undefined) {
1090
+ return cfg.outletCount;
1091
+ }
1092
+ return undefined;
1093
+ }
1094
+ _getEffectiveSwitchCount(cfg, device) {
1095
+ const configuredSwitchCount = this._getConfiguredSwitchCount(cfg);
1096
+ if (configuredSwitchCount !== undefined) {
1097
+ return configuredSwitchCount;
1098
+ }
1099
+ const deviceCount = device ? device.switchCount
1100
+ ?? device.outletCount
1101
+ : undefined;
1102
+ return deviceCount;
1103
+ }
1104
+ _createDPMap(cfg) {
1105
+ const effectiveMap = Object.assign({}, cfg.dpMapping, Object.fromEntries(cfg.dpMappingArray?.map(item => [item.dpCode, item.dpID]) ?? []));
1106
+ if (Object.keys(effectiveMap).length === 0) {
1107
+ Object.assign(effectiveMap, DEFAULT_DP_MAP);
1108
+ }
1109
+ // override dpcodes
1110
+ for (const [dpCode, dpID] of Object.entries(effectiveMap)) {
1111
+ const schemaConfig = this.getDeviceSchemaConfig({ id: cfg.tuyaDeviceId }, dpCode);
1112
+ if (!schemaConfig?.newCode) {
1113
+ continue;
1114
+ }
1115
+ // Only treat newCode as a rename when it actually differs from the device code.
1116
+ // Renaming to the same key would set-then-delete it, dropping the dp↔code mapping.
1117
+ if (schemaConfig.newCode && schemaConfig.newCode !== dpCode) {
1118
+ effectiveMap[schemaConfig.newCode] = dpID;
1119
+ delete effectiveMap[dpCode];
1120
+ }
1121
+ const targetCode = schemaConfig.newCode.trim();
1122
+ if (!targetCode || targetCode === dpCode) {
1123
+ continue;
1124
+ }
1125
+ effectiveMap[targetCode] = dpID;
1126
+ delete effectiveMap[dpCode];
1127
+ }
1128
+ return effectiveMap;
1129
+ }
1130
+ _setDPMap(deviceID, parentDeviceId, isZigbeeDevice, dpMap) {
1131
+ // Because sub‑devices have no actual entity, they are registered as part of the parent device’s information.
1132
+ // Since there is only one real device,
1133
+ // configuring each sub‑device individually is unnecessary—and even if you did, the settings would end up identical.
1134
+ // If any inconsistencies occur, the values are merged, with later definitions taking precedence.
1135
+ const isSubDevice = !isZigbeeDevice && !!parentDeviceId;
1136
+ if (isSubDevice) {
1137
+ const parentDPMap = this._getDPMap(parentDeviceId);
1138
+ const mergedDPMap = { ...parentDPMap, ...dpMap };
1139
+ this.dpMaps.set(parentDeviceId, mergedDPMap);
1140
+ this.reverseDpMaps.set(parentDeviceId, buildDpToCodeMap(mergedDPMap));
1141
+ }
1142
+ else {
1143
+ this.dpMaps.set(deviceID, dpMap);
1144
+ this.reverseDpMaps.set(deviceID, buildDpToCodeMap(dpMap));
1145
+ }
1146
+ }
1147
+ _getDPMap(deviceID) {
1148
+ let localConfig = this.config.devices?.find(config => config.tuyaDeviceId === deviceID);
1149
+ if (!localConfig) {
1150
+ // ZigbeeChildDevice or HubSubDevice
1151
+ localConfig = this.config.devices?.find(config => config.parentDeviceId === deviceID);
1152
+ }
1153
+ if (!localConfig) {
1154
+ // A Local device almost never operates without configuration, but this is just in case.
1155
+ return DEFAULT_DP_MAP;
1156
+ }
1157
+ if (!ZigbeeGatewayDetection_1.ZigbeeGatewayDetection.isChild(localConfig) && localConfig.parentDeviceId) {
1158
+ return this.dpMaps.get(localConfig.parentDeviceId) ?? DEFAULT_DP_MAP;
1159
+ }
1160
+ else {
1161
+ return this.dpMaps.get(deviceID) ?? DEFAULT_DP_MAP;
1162
+ }
1163
+ }
979
1164
  }
980
1165
  exports.default = LocalDeviceManager;
981
1166
  //# sourceMappingURL=LocalDeviceManager.js.map