Skip to content

Commit

Permalink
Add sleepy devices support
Browse files Browse the repository at this point in the history
  • Loading branch information
Ernst79 committed May 18, 2023
1 parent 3cdcfb1 commit d602e6e
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 7 deletions.
13 changes: 13 additions & 0 deletions homeassistant/components/bthome/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
PassiveBluetoothProcessorCoordinator,
PassiveBluetoothProcessorEntity,
)
from homeassistant.const import ATTR_MODEL
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.sensor import sensor_device_info_to_hass_device_info
Expand Down Expand Up @@ -195,3 +196,15 @@ class BTHomeBluetoothBinarySensorEntity(
def is_on(self) -> bool | None:
"""Return the native value."""
return self.processor.entity_data.get(self.entity_key)

@property
def available(self) -> bool:
"""Return True if entity is available."""
if self.device_info:
if self.device_info[ATTR_MODEL] and self.device_info[ATTR_MODEL].endswith(
"(trigger based device)"
):
# Trigger based devices sleep for an indeterminate amount of time
# so there is no way to track their availability.
return True
return super().available
2 changes: 1 addition & 1 deletion homeassistant/components/bthome/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@
"dependencies": ["bluetooth_adapters"],
"documentation": "https://www.home-assistant.io/integrations/bthome",
"iot_class": "local_push",
"requirements": ["bthome-ble==2.9.0"]
"requirements": ["bthome-ble==2.11.2"]
}
13 changes: 13 additions & 0 deletions homeassistant/components/bthome/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
SensorStateClass,
)
from homeassistant.const import (
ATTR_MODEL,
CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
CONCENTRATION_PARTS_PER_MILLION,
DEGREE,
Expand Down Expand Up @@ -365,3 +366,15 @@ class BTHomeBluetoothSensorEntity(
def native_value(self) -> int | float | None:
"""Return the native value."""
return self.processor.entity_data.get(self.entity_key)

@property
def available(self) -> bool:
"""Return True if entity is available."""
if self.device_info:
if self.device_info[ATTR_MODEL] and self.device_info[ATTR_MODEL].endswith(
"(trigger based device)"
):
# Trigger based devices sleep for an indeterminate amount of time
# so there is no way to track their availability.
return True
return super().available
2 changes: 1 addition & 1 deletion requirements_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ brunt==1.2.0
bt_proximity==0.2.1

# homeassistant.components.bthome
bthome-ble==2.9.0
bthome-ble==2.11.2

# homeassistant.components.bt_home_hub_5
bthomehub5-devicelist==0.1.1
Expand Down
2 changes: 1 addition & 1 deletion requirements_test_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ brottsplatskartan==0.0.1
brunt==1.2.0

# homeassistant.components.bthome
bthome-ble==2.9.0
bthome-ble==2.11.2

# homeassistant.components.buienradar
buienradar==1.0.5
Expand Down
2 changes: 1 addition & 1 deletion tests/components/bthome/test_binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ async def test_v1_binary_sensors(
"A4:C1:38:8D:18:B2",
make_bthome_v2_adv(
"A4:C1:38:8D:18:B2",
b"\x40\x11\x00",
b"\x44\x11\x00",
),
None,
[
Expand Down
4 changes: 1 addition & 3 deletions tests/components/bthome/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ async def test_v1_sensors(
"A4:C1:38:8D:18:B2",
make_bthome_v2_adv(
"A4:C1:38:8D:18:B2",
b"\x40\x09\x60",
b"\x44\x09\x60",
),
None,
[
Expand Down Expand Up @@ -1008,9 +1008,7 @@ async def test_v2_sensors(
assert len(hass.states.async_all()) == len(result)

for meas in result:
_LOGGER.error(meas)
sensor = hass.states.get(meas["sensor_entity"])
_LOGGER.error(hass.states)
sensor_attr = sensor.attributes
assert sensor.state == meas["expected_state"]
assert sensor_attr[ATTR_FRIENDLY_NAME] == meas["friendly_name"]
Expand Down

0 comments on commit d602e6e

Please sign in to comment.