Fix switchbot writing state too frequently (#78094)

pull/78168/head
J. Nick Koston 2022-09-09 18:13:27 -05:00 committed by GitHub
parent fcb6888f87
commit d98ed03845
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 21 additions and 7 deletions

View File

@ -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)

View File

@ -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": [

View File

@ -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

View File

@ -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

View File

@ -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