homebridge-kasa-python 2.7.0-beta.23 → 2.7.0-beta.24

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.
@@ -16,9 +16,10 @@ UNSUPPORTED_TYPES = {
16
16
  DeviceType.Unknown.value,
17
17
  }
18
18
 
19
+ credentials: Optional[Credentials] = None
19
20
  device_cache: Dict[str, Device] = {}
20
21
  device_locks: Dict[str, asyncio.Lock] = {}
21
- device_configs: Dict[str, DeviceConfig] = {}
22
+ device_configs: Dict[str, dict[Any, Any]] = {}
22
23
 
23
24
  def serialize_child(child: Device) -> Dict[str, Any]:
24
25
  print(f"Serializing child device {child.alias}")
@@ -92,6 +93,7 @@ async def discover_devices(
92
93
  additional_broadcasts: Optional[List[str]] = None,
93
94
  manual_devices: Optional[List[str]] = None
94
95
  ) -> Dict[str, Any]:
96
+ global credentials
95
97
  devices = {}
96
98
  devices_to_remove = []
97
99
  broadcasts = ["255.255.255.255"] + (additional_broadcasts or [])
@@ -202,6 +204,8 @@ async def create_device_info(host: str, device: Device):
202
204
  print("Creating device info for host: ", host)
203
205
  device_info = custom_serializer(device)
204
206
  device_configs[host] = device.config.to_dict()
207
+ if not device_configs.get(host):
208
+ device_configs[host] = device.config.to_dict()
205
209
  all_device_info = {
206
210
  "sys_info": device_info["sys_info"],
207
211
  "feature_info": device_info["feature_info"],
@@ -210,14 +214,20 @@ async def create_device_info(host: str, device: Device):
210
214
 
211
215
  async def get_sys_info(host: str) -> Dict[str, Any]:
212
216
  print("Getting sys_info for host: ", host)
213
- device_config = device_configs.get(host)
217
+ device_config_dict = device_configs.get(host)
218
+ if device_config_dict:
219
+ print("Device config found in cache")
220
+ device_config = DeviceConfig.from_dict(device_config_dict)
221
+ else:
222
+ print("Device config not found in cache, recreating...")
223
+ device_config = await recreate_device_config(host)
214
224
  lock = device_locks.get(host, asyncio.Lock())
215
225
  async with lock:
216
226
  device = await get_or_connect_device(host, device_config)
217
227
  try:
218
228
  await device.update()
219
229
  except Exception as e:
220
- print(f"Action failed: {e}, reconnecting...")
230
+ print(f"GetSysInfo failed: {e}, reconnecting...")
221
231
  device = await reconnect_device(host, device_config)
222
232
  device_info = custom_serializer(device)
223
233
  return {"sys_info": device_info["sys_info"]}
@@ -230,7 +240,13 @@ async def control_device(
230
240
  child_num: Optional[int] = None
231
241
  ) -> Dict[str, Any]:
232
242
  print(f"Controlling device at host: {host}")
233
- device_config = device_configs.get(host)
243
+ device_config_dict = device_configs.get(host)
244
+ if device_config_dict:
245
+ print("Device config found in cache")
246
+ device_config = DeviceConfig.from_dict(device_config_dict)
247
+ else:
248
+ print("Device config not found in cache, recreating...")
249
+ device_config = await recreate_device_config(host)
234
250
  lock = device_locks.get(host, asyncio.Lock())
235
251
  async with lock:
236
252
  device = await get_or_connect_device(host, device_config)
@@ -245,18 +261,31 @@ async def get_or_connect_device(host: str, device_config: DeviceConfig) -> Devic
245
261
  device = device_cache.get(host)
246
262
  if not device:
247
263
  print(f"Device not in cache, connecting to device at host: {host}")
248
- device = await Device.connect(config=Device.Config.from_dict(device_config))
264
+ device = await Device.connect(config=device_config)
249
265
  device_cache[host] = device
266
+ else:
267
+ print(f"Device found in cache: {device.alias}")
250
268
  return device
251
269
 
252
270
  async def reconnect_device(host: str, device_config: DeviceConfig) -> Device:
253
271
  device = device_cache.pop(host, None)
254
272
  if device:
255
273
  await device.disconnect()
256
- device = await Device.connect(config=Device.Config.from_dict(device_config))
274
+ device = await Device.connect(config=device_config)
257
275
  device_cache[host] = device
258
276
  return device
259
277
 
278
+ async def recreate_device_config(host: str) -> DeviceConfig:
279
+ global credentials
280
+ device = await Discover.discover_single(host=host, credentials=credentials)
281
+ await device.update()
282
+ device_configs[host] = device.config.to_dict()
283
+ if not device_configs.get(host):
284
+ device_configs[host] = device.config.to_dict()
285
+ device_config = DeviceConfig.from_dict(device_configs[host])
286
+ await device.disconnect()
287
+ return device_config
288
+
260
289
  async def perform_device_action(device: Device, feature: str, action: str, value: Any, child_num: Optional[int] = None) -> Dict[str, Any]:
261
290
  target = device.children[child_num] if child_num is not None else device
262
291
  light = target.modules.get(Module.Light)
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "displayName": "Homebridge Kasa Python",
3
3
  "name": "homebridge-kasa-python",
4
- "version": "2.7.0-beta.23",
4
+ "version": "2.7.0-beta.24",
5
5
  "description": "Plugin that uses Python-Kasa API to communicate with Kasa Devices.",
6
6
  "license": "MIT",
7
7
  "type": "module",