@@ -76,7 +76,7 @@ def _sb_uuid(comms_type: str = "service") -> UUID | str:
7676WRITE_CHAR_UUID = _sb_uuid (comms_type = "tx" )
7777
7878
79- class SwitchbotDevice :
79+ class SwitchbotBaseDevice :
8080 """Base Representation of a Switchbot Device."""
8181
8282 def __init__ (
@@ -349,6 +349,12 @@ def get_address(self) -> str:
349349 def _get_adv_value (self , key : str ) -> Any :
350350 """Return value from advertisement data."""
351351 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+ )
352358 return self ._override_adv_data [key ]
353359 if not self ._sb_adv_data :
354360 return None
@@ -362,9 +368,6 @@ def update_from_advertisement(self, advertisement: SwitchBotAdvertisement) -> No
362368 """Update device data from advertisement."""
363369 # Only accept advertisements if the data is not missing
364370 # 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
368371 if self ._device and ble_device_has_changed (self ._device , advertisement .device ):
369372 self ._cached_services = None
370373 self ._device = advertisement .device
@@ -432,9 +435,51 @@ def _check_command_result(
432435 )
433436 return result [index ] in values
434437
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+
435476
436477class 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+ """
438483
439484 def update_from_advertisement (self , advertisement : SwitchBotAdvertisement ) -> None :
440485 """Update device data from advertisement."""
0 commit comments