@@ -76,7 +76,7 @@ def _sb_uuid(comms_type: str = "service") -> UUID | str:
76
76
WRITE_CHAR_UUID = _sb_uuid (comms_type = "tx" )
77
77
78
78
79
- class SwitchbotDevice :
79
+ class SwitchbotBaseDevice :
80
80
"""Base Representation of a Switchbot Device."""
81
81
82
82
def __init__ (
@@ -349,6 +349,12 @@ def get_address(self) -> str:
349
349
def _get_adv_value (self , key : str ) -> Any :
350
350
"""Return value from advertisement data."""
351
351
if self ._override_adv_data and key in self ._override_adv_data :
352
+ _LOGGER .debug (
353
+ "%s: Using override value for %s: %s" ,
354
+ self .name ,
355
+ key ,
356
+ self ._override_adv_data [key ],
357
+ )
352
358
return self ._override_adv_data [key ]
353
359
if not self ._sb_adv_data :
354
360
return None
@@ -362,9 +368,6 @@ def update_from_advertisement(self, advertisement: SwitchBotAdvertisement) -> No
362
368
"""Update device data from advertisement."""
363
369
# Only accept advertisements if the data is not missing
364
370
# if we already have an advertisement with data
365
- if advertisement .data .get ("data" ) or not self ._sb_adv_data .data .get ("data" ):
366
- self ._sb_adv_data = advertisement
367
- self ._override_adv_data = None
368
371
if self ._device and ble_device_has_changed (self ._device , advertisement .device ):
369
372
self ._cached_services = None
370
373
self ._device = advertisement .device
@@ -432,9 +435,51 @@ def _check_command_result(
432
435
)
433
436
return result [index ] in values
434
437
438
+ def _set_advertisement_data (self , advertisement : SwitchBotAdvertisement ) -> None :
439
+ """Set advertisement data."""
440
+ if advertisement .data .get ("data" ) or not self ._sb_adv_data .data .get ("data" ):
441
+ self ._sb_adv_data = advertisement
442
+ self ._override_adv_data = None
443
+
444
+
445
+ class SwitchbotDevice (SwitchbotBaseDevice ):
446
+ """Base Representation of a Switchbot Device.
447
+
448
+ This base class consumes the advertisement data during connection. If the device
449
+ sends stale advertisement data while connected, use
450
+ SwitchbotDeviceOverrideStateDuringConnection instead.
451
+ """
452
+
453
+ def update_from_advertisement (self , advertisement : SwitchBotAdvertisement ) -> None :
454
+ """Update device data from advertisement."""
455
+ super ().update_from_advertisement (advertisement )
456
+ self ._set_advertisement_data (advertisement )
457
+
458
+
459
+ class SwitchbotDeviceOverrideStateDuringConnection (SwitchbotBaseDevice ):
460
+ """Base Representation of a Switchbot Device.
461
+
462
+ This base class ignores the advertisement data during connection and uses the
463
+ data from the device instead.
464
+ """
465
+
466
+ def update_from_advertisement (self , advertisement : SwitchBotAdvertisement ) -> None :
467
+ super ().update_from_advertisement (advertisement )
468
+ if self ._client and self ._client .is_connected :
469
+ # We do not consume the advertisement data if we are connected
470
+ # to the device. This is because the advertisement data is not
471
+ # updated when the device is connected for some devices.
472
+ _LOGGER .debug ("%s: Ignore advertisement data during connection" , self .name )
473
+ return
474
+ self ._set_advertisement_data (advertisement )
475
+
435
476
436
477
class SwitchbotSequenceDevice (SwitchbotDevice ):
437
- """A Switchbot sequence device."""
478
+ """A Switchbot sequence device.
479
+
480
+ This class must not use SwitchbotDeviceOverrideStateDuringConnection because
481
+ it needs to know when the sequence_number has changed.
482
+ """
438
483
439
484
def update_from_advertisement (self , advertisement : SwitchBotAdvertisement ) -> None :
440
485
"""Update device data from advertisement."""
0 commit comments