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