Add BLE sensor to Aladdin_connect (#76221)

* Add BLE sensor
Default Enable BLE & Battery for Model 02

* recommended changes

* Recommended changes

Model 02 -> 01 (oops)
2x async_block_till_done() not needed.
This commit is contained in:
mkmer 2022-08-17 03:09:19 -04:00 committed by GitHub
parent 7e366a78e6
commit 5ef6b5a300
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 98 additions and 2 deletions

View File

@ -12,3 +12,4 @@ class DoorDevice(TypedDict):
name: str
status: str
serial: str
model: str

View File

@ -56,6 +56,15 @@ SENSORS: tuple[AccSensorEntityDescription, ...] = (
state_class=SensorStateClass.MEASUREMENT,
value_fn=AladdinConnectClient.get_rssi_status,
),
AccSensorEntityDescription(
key="ble_strength",
name="BLE Strength",
device_class=SensorDeviceClass.SIGNAL_STRENGTH,
entity_registry_enabled_default=False,
native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS,
state_class=SensorStateClass.MEASUREMENT,
value_fn=AladdinConnectClient.get_ble_strength,
),
)
@ -89,14 +98,17 @@ class AladdinConnectSensor(SensorEntity):
device: DoorDevice,
description: AccSensorEntityDescription,
) -> None:
"""Initialize a sensor for an Abode device."""
"""Initialize a sensor for an Aladdin Connect device."""
self._device_id = device["device_id"]
self._number = device["door_number"]
self._name = device["name"]
self._model = device["model"]
self._acc = acc
self.entity_description = description
self._attr_unique_id = f"{self._device_id}-{self._number}-{description.key}"
self._attr_has_entity_name = True
if self._model == "01" and description.key in ("battery_level", "ble_strength"):
self._attr_entity_registry_enabled_default = True
@property
def device_info(self) -> DeviceInfo | None:
@ -105,6 +117,7 @@ class AladdinConnectSensor(SensorEntity):
identifiers={(DOMAIN, self._device_id)},
name=self._name,
manufacturer="Overhead Door",
model=self._model,
)
@property

View File

@ -11,6 +11,7 @@ DEVICE_CONFIG_OPEN = {
"status": "open",
"link_status": "Connected",
"serial": "12345",
"model": "02",
}
@ -31,6 +32,8 @@ def fixture_mock_aladdinconnect_api():
mock_opener.get_battery_status.return_value = "99"
mock_opener.async_get_rssi_status = AsyncMock(return_value="-55")
mock_opener.get_rssi_status.return_value = "-55"
mock_opener.async_get_ble_strength = AsyncMock(return_value="-45")
mock_opener.get_ble_strength.return_value = "-45"
mock_opener.get_doors = AsyncMock(return_value=[DEVICE_CONFIG_OPEN])
mock_opener.register_callback = mock.Mock(return_value=True)

View File

@ -1,6 +1,6 @@
"""Test the Aladdin Connect Sensors."""
from datetime import timedelta
from unittest.mock import MagicMock, patch
from unittest.mock import AsyncMock, MagicMock, patch
from homeassistant.components.aladdin_connect.const import DOMAIN
from homeassistant.components.aladdin_connect.cover import SCAN_INTERVAL
@ -10,6 +10,17 @@ from homeassistant.util.dt import utcnow
from tests.common import MockConfigEntry, async_fire_time_changed
DEVICE_CONFIG_MODEL_01 = {
"device_id": 533255,
"door_number": 1,
"name": "home",
"status": "closed",
"link_status": "Connected",
"serial": "12345",
"model": "01",
}
CONFIG = {"username": "test-user", "password": "test-password"}
RELOAD_AFTER_UPDATE_DELAY = timedelta(seconds=31)
@ -83,3 +94,71 @@ async def test_sensors(
state = hass.states.get("sensor.home_wi_fi_rssi")
assert state
async def test_sensors_model_01(
hass: HomeAssistant,
mock_aladdinconnect_api: MagicMock,
) -> None:
"""Test Sensors for AladdinConnect."""
config_entry = MockConfigEntry(
domain=DOMAIN,
data=CONFIG,
unique_id="test-id",
)
config_entry.add_to_hass(hass)
await hass.async_block_till_done()
with patch(
"homeassistant.components.aladdin_connect.AladdinConnectClient",
return_value=mock_aladdinconnect_api,
):
mock_aladdinconnect_api.get_doors = AsyncMock(
return_value=[DEVICE_CONFIG_MODEL_01]
)
await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
registry = entity_registry.async_get(hass)
entry = registry.async_get("sensor.home_battery_level")
assert entry
assert entry.disabled is False
assert entry.disabled_by is None
state = hass.states.get("sensor.home_battery_level")
assert state
entry = registry.async_get("sensor.home_wi_fi_rssi")
await hass.async_block_till_done()
assert entry
assert entry.disabled
assert entry.disabled_by is entity_registry.RegistryEntryDisabler.INTEGRATION
update_entry = registry.async_update_entity(
entry.entity_id, **{"disabled_by": None}
)
await hass.async_block_till_done()
assert update_entry != entry
assert update_entry.disabled is False
state = hass.states.get("sensor.home_wi_fi_rssi")
assert state is None
update_entry = registry.async_update_entity(
entry.entity_id, **{"disabled_by": None}
)
await hass.async_block_till_done()
async_fire_time_changed(
hass,
utcnow() + SCAN_INTERVAL,
)
await hass.async_block_till_done()
state = hass.states.get("sensor.home_wi_fi_rssi")
assert state
entry = registry.async_get("sensor.home_ble_strength")
await hass.async_block_till_done()
assert entry
assert entry.disabled is False
assert entry.disabled_by is None
state = hass.states.get("sensor.home_ble_strength")
assert state