Fix switchbot writing state too frequently (#78094)
parent
fcb6888f87
commit
d98ed03845
|
@ -69,13 +69,16 @@ class SwitchbotDataUpdateCoordinator(PassiveBluetoothDataUpdateCoordinator):
|
|||
change: bluetooth.BluetoothChange,
|
||||
) -> None:
|
||||
"""Handle a Bluetooth event."""
|
||||
self.ble_device = service_info.device
|
||||
if adv := switchbot.parse_advertisement_data(
|
||||
service_info.device, service_info.advertisement
|
||||
):
|
||||
self.data = flatten_sensors_data(adv.data)
|
||||
if "modelName" in self.data:
|
||||
self._ready_event.set()
|
||||
_LOGGER.debug("%s: Switchbot data: %s", self.ble_device.address, self.data)
|
||||
if not self.device.advertisement_changed(adv):
|
||||
return
|
||||
self.data = flatten_sensors_data(adv.data)
|
||||
self.device.update_from_advertisement(adv)
|
||||
super()._async_handle_bluetooth_event(service_info, change)
|
||||
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
"domain": "switchbot",
|
||||
"name": "SwitchBot",
|
||||
"documentation": "https://www.home-assistant.io/integrations/switchbot",
|
||||
"requirements": ["PySwitchbot==0.18.27"],
|
||||
"requirements": ["PySwitchbot==0.19.0"],
|
||||
"config_flow": true,
|
||||
"dependencies": ["bluetooth"],
|
||||
"codeowners": [
|
||||
|
|
|
@ -71,14 +71,16 @@ async def async_setup_entry(
|
|||
) -> None:
|
||||
"""Set up Switchbot sensor based on a config entry."""
|
||||
coordinator: SwitchbotDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
||||
async_add_entities(
|
||||
entities = [
|
||||
SwitchBotSensor(
|
||||
coordinator,
|
||||
sensor,
|
||||
)
|
||||
for sensor in coordinator.data["data"]
|
||||
if sensor in SENSOR_TYPES
|
||||
)
|
||||
]
|
||||
entities.append(SwitchbotRSSISensor(coordinator, "rssi"))
|
||||
async_add_entities(entities)
|
||||
|
||||
|
||||
class SwitchBotSensor(SwitchbotEntity, SensorEntity):
|
||||
|
@ -98,6 +100,15 @@ class SwitchBotSensor(SwitchbotEntity, SensorEntity):
|
|||
self.entity_description = SENSOR_TYPES[sensor]
|
||||
|
||||
@property
|
||||
def native_value(self) -> str:
|
||||
def native_value(self) -> str | int:
|
||||
"""Return the state of the sensor."""
|
||||
return self.data["data"][self._sensor]
|
||||
|
||||
|
||||
class SwitchbotRSSISensor(SwitchBotSensor):
|
||||
"""Representation of a Switchbot RSSI sensor."""
|
||||
|
||||
@property
|
||||
def native_value(self) -> str | int:
|
||||
"""Return the state of the sensor."""
|
||||
return self.coordinator.ble_device.rssi
|
||||
|
|
|
@ -37,7 +37,7 @@ PyRMVtransport==0.3.3
|
|||
PySocks==1.7.1
|
||||
|
||||
# homeassistant.components.switchbot
|
||||
PySwitchbot==0.18.27
|
||||
PySwitchbot==0.19.0
|
||||
|
||||
# homeassistant.components.transport_nsw
|
||||
PyTransportNSW==0.1.1
|
||||
|
|
|
@ -33,7 +33,7 @@ PyRMVtransport==0.3.3
|
|||
PySocks==1.7.1
|
||||
|
||||
# homeassistant.components.switchbot
|
||||
PySwitchbot==0.18.27
|
||||
PySwitchbot==0.19.0
|
||||
|
||||
# homeassistant.components.transport_nsw
|
||||
PyTransportNSW==0.1.1
|
||||
|
|
Loading…
Reference in New Issue