From bbe63bca4733b31b6f6cf29270cdd62765309fdf Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 16 Oct 2022 02:16:02 -1000 Subject: [PATCH] Bump pySwitchbot to 0.20.0 for bleak 0.19 changes (#80389) --- .../components/switchbot/coordinator.py | 2 -- homeassistant/components/switchbot/manifest.json | 2 +- homeassistant/components/switchbot/sensor.py | 16 ++++++++++++---- requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- 5 files changed, 15 insertions(+), 9 deletions(-) diff --git a/homeassistant/components/switchbot/coordinator.py b/homeassistant/components/switchbot/coordinator.py index 7f4f3c77b99..ee93c74af37 100644 --- a/homeassistant/components/switchbot/coordinator.py +++ b/homeassistant/components/switchbot/coordinator.py @@ -60,7 +60,6 @@ class SwitchbotDataUpdateCoordinator(PassiveBluetoothDataUpdateCoordinator): self.device_name = device_name self.base_unique_id = base_unique_id self.model = model - self.service_info: bluetooth.BluetoothServiceInfoBleak | None = None self._ready_event = asyncio.Event() @callback @@ -71,7 +70,6 @@ class SwitchbotDataUpdateCoordinator(PassiveBluetoothDataUpdateCoordinator): ) -> None: """Handle a Bluetooth event.""" self.ble_device = service_info.device - self.service_info = service_info if adv := switchbot.parse_advertisement_data( service_info.device, service_info.advertisement ): diff --git a/homeassistant/components/switchbot/manifest.json b/homeassistant/components/switchbot/manifest.json index 282bf6aa447..316adc6cd7a 100644 --- a/homeassistant/components/switchbot/manifest.json +++ b/homeassistant/components/switchbot/manifest.json @@ -2,7 +2,7 @@ "domain": "switchbot", "name": "SwitchBot", "documentation": "https://www.home-assistant.io/integrations/switchbot", - "requirements": ["PySwitchbot==0.19.15"], + "requirements": ["PySwitchbot==0.20.0"], "config_flow": true, "dependencies": ["bluetooth"], "codeowners": [ diff --git a/homeassistant/components/switchbot/sensor.py b/homeassistant/components/switchbot/sensor.py index 9dd0ef3900b..9b1baf805bb 100644 --- a/homeassistant/components/switchbot/sensor.py +++ b/homeassistant/components/switchbot/sensor.py @@ -1,6 +1,7 @@ """Support for SwitchBot sensors.""" from __future__ import annotations +from homeassistant.components.bluetooth import async_last_service_info from homeassistant.components.sensor import ( SensorDeviceClass, SensorEntity, @@ -106,7 +107,7 @@ class SwitchBotSensor(SwitchbotEntity, SensorEntity): self.entity_description = SENSOR_TYPES[sensor] @property - def native_value(self) -> str | int: + def native_value(self) -> str | int | None: """Return the state of the sensor.""" return self.data["data"][self._sensor] @@ -115,7 +116,14 @@ class SwitchbotRSSISensor(SwitchBotSensor): """Representation of a Switchbot RSSI sensor.""" @property - def native_value(self) -> str | int: + def native_value(self) -> str | int | None: """Return the state of the sensor.""" - assert self.coordinator.service_info is not None - return self.coordinator.service_info.rssi + # Switchbot supports both connectable and non-connectable devices + # so we need to request the rssi value based on the connectable instead + # of the nearest scanner since that is the RSSI that matters for controlling + # the device. + if service_info := async_last_service_info( + self.hass, self._address, self.coordinator.connectable + ): + return service_info.rssi + return None diff --git a/requirements_all.txt b/requirements_all.txt index 924b821c45c..2490a8f1aac 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -37,7 +37,7 @@ PyRMVtransport==0.3.3 PySocks==1.7.1 # homeassistant.components.switchbot -PySwitchbot==0.19.15 +PySwitchbot==0.20.0 # homeassistant.components.transport_nsw PyTransportNSW==0.1.1 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index e3f504c15c0..44cd73094e9 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -33,7 +33,7 @@ PyRMVtransport==0.3.3 PySocks==1.7.1 # homeassistant.components.switchbot -PySwitchbot==0.19.15 +PySwitchbot==0.20.0 # homeassistant.components.transport_nsw PyTransportNSW==0.1.1