mirror of
https://github.com/home-assistant/core.git
synced 2025-04-25 09:47:52 +00:00
Add binary sensor for valve connectivity for gardena bluetooth (#96810)
* Add binary_sensor to gardena * Add tests for binary_sensor
This commit is contained in:
parent
2c949d56dc
commit
878429fdec
@ -407,6 +407,7 @@ omit =
|
||||
homeassistant/components/garages_amsterdam/binary_sensor.py
|
||||
homeassistant/components/garages_amsterdam/sensor.py
|
||||
homeassistant/components/gardena_bluetooth/__init__.py
|
||||
homeassistant/components/gardena_bluetooth/binary_sensor.py
|
||||
homeassistant/components/gardena_bluetooth/const.py
|
||||
homeassistant/components/gardena_bluetooth/coordinator.py
|
||||
homeassistant/components/gardena_bluetooth/number.py
|
||||
|
@ -20,7 +20,12 @@ import homeassistant.util.dt as dt_util
|
||||
from .const import DOMAIN
|
||||
from .coordinator import Coordinator, DeviceUnavailable
|
||||
|
||||
PLATFORMS: list[Platform] = [Platform.NUMBER, Platform.SENSOR, Platform.SWITCH]
|
||||
PLATFORMS: list[Platform] = [
|
||||
Platform.BINARY_SENSOR,
|
||||
Platform.NUMBER,
|
||||
Platform.SENSOR,
|
||||
Platform.SWITCH,
|
||||
]
|
||||
LOGGER = logging.getLogger(__name__)
|
||||
TIMEOUT = 20.0
|
||||
DISCONNECT_DELAY = 5
|
||||
|
64
homeassistant/components/gardena_bluetooth/binary_sensor.py
Normal file
64
homeassistant/components/gardena_bluetooth/binary_sensor.py
Normal file
@ -0,0 +1,64 @@
|
||||
"""Support for binary_sensor entities."""
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass, field
|
||||
|
||||
from gardena_bluetooth.const import Valve
|
||||
from gardena_bluetooth.parse import CharacteristicBool
|
||||
|
||||
from homeassistant.components.binary_sensor import (
|
||||
BinarySensorDeviceClass,
|
||||
BinarySensorEntity,
|
||||
BinarySensorEntityDescription,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import EntityCategory
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from .const import DOMAIN
|
||||
from .coordinator import Coordinator, GardenaBluetoothDescriptorEntity
|
||||
|
||||
|
||||
@dataclass
|
||||
class GardenaBluetoothBinarySensorEntityDescription(BinarySensorEntityDescription):
|
||||
"""Description of entity."""
|
||||
|
||||
char: CharacteristicBool = field(default_factory=lambda: CharacteristicBool(""))
|
||||
|
||||
|
||||
DESCRIPTIONS = (
|
||||
GardenaBluetoothBinarySensorEntityDescription(
|
||||
key=Valve.connected_state.uuid,
|
||||
translation_key="valve_connected_state",
|
||||
device_class=BinarySensorDeviceClass.CONNECTIVITY,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
char=Valve.connected_state,
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
||||
) -> None:
|
||||
"""Set up binary sensor based on a config entry."""
|
||||
coordinator: Coordinator = hass.data[DOMAIN][entry.entry_id]
|
||||
entities = [
|
||||
GardenaBluetoothBinarySensor(coordinator, description)
|
||||
for description in DESCRIPTIONS
|
||||
if description.key in coordinator.characteristics
|
||||
]
|
||||
async_add_entities(entities)
|
||||
|
||||
|
||||
class GardenaBluetoothBinarySensor(
|
||||
GardenaBluetoothDescriptorEntity, BinarySensorEntity
|
||||
):
|
||||
"""Representation of a binary sensor."""
|
||||
|
||||
entity_description: GardenaBluetoothBinarySensorEntityDescription
|
||||
|
||||
def _handle_coordinator_update(self) -> None:
|
||||
char = self.entity_description.char
|
||||
self._attr_is_on = self.coordinator.get_cached(char)
|
||||
super()._handle_coordinator_update()
|
@ -19,6 +19,11 @@
|
||||
}
|
||||
},
|
||||
"entity": {
|
||||
"binary_sensor": {
|
||||
"valve_connected_state": {
|
||||
"name": "Valve connection"
|
||||
}
|
||||
},
|
||||
"number": {
|
||||
"remaining_open_time": {
|
||||
"name": "Remaining open time"
|
||||
|
@ -0,0 +1,27 @@
|
||||
# serializer version: 1
|
||||
# name: test_setup[98bd0f12-0b0e-421a-84e5-ddbf75dc6de4-raw0-binary_sensor.mock_title_valve_connection]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'device_class': 'connectivity',
|
||||
'friendly_name': 'Mock Title Valve connection',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'binary_sensor.mock_title_valve_connection',
|
||||
'last_changed': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': 'on',
|
||||
})
|
||||
# ---
|
||||
# name: test_setup[98bd0f12-0b0e-421a-84e5-ddbf75dc6de4-raw0-binary_sensor.mock_title_valve_connection].1
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'device_class': 'connectivity',
|
||||
'friendly_name': 'Mock Title Valve connection',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'binary_sensor.mock_title_valve_connection',
|
||||
'last_changed': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': 'off',
|
||||
})
|
||||
# ---
|
44
tests/components/gardena_bluetooth/test_binary_sensor.py
Normal file
44
tests/components/gardena_bluetooth/test_binary_sensor.py
Normal file
@ -0,0 +1,44 @@
|
||||
"""Test Gardena Bluetooth binary sensor."""
|
||||
|
||||
|
||||
from gardena_bluetooth.const import Valve
|
||||
import pytest
|
||||
from syrupy.assertion import SnapshotAssertion
|
||||
|
||||
from homeassistant.const import Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from . import setup_entry
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("uuid", "raw", "entity_id"),
|
||||
[
|
||||
(
|
||||
Valve.connected_state.uuid,
|
||||
[b"\x01", b"\x00"],
|
||||
"binary_sensor.mock_title_valve_connection",
|
||||
),
|
||||
],
|
||||
)
|
||||
async def test_setup(
|
||||
hass: HomeAssistant,
|
||||
snapshot: SnapshotAssertion,
|
||||
mock_entry: MockConfigEntry,
|
||||
mock_read_char_raw: dict[str, bytes],
|
||||
uuid: str,
|
||||
raw: list[bytes],
|
||||
entity_id: str,
|
||||
) -> None:
|
||||
"""Test setup creates expected entities."""
|
||||
|
||||
mock_read_char_raw[uuid] = raw[0]
|
||||
coordinator = await setup_entry(hass, mock_entry, [Platform.BINARY_SENSOR])
|
||||
assert hass.states.get(entity_id) == snapshot
|
||||
|
||||
for char_raw in raw[1:]:
|
||||
mock_read_char_raw[uuid] = char_raw
|
||||
await coordinator.async_refresh()
|
||||
assert hass.states.get(entity_id) == snapshot
|
Loading…
x
Reference in New Issue
Block a user