Add binary sensors to flexit_bacnet integration (#108571)
* Adds binary sensors to flexit_bacnet integration * Review comments * Removes binary sensor for electric heater Will add switch or service later
This commit is contained in:
parent
7c86ea7e16
commit
ed270f558a
5 changed files with 158 additions and 1 deletions
|
@ -8,7 +8,7 @@ from homeassistant.core import HomeAssistant
|
||||||
from .const import DOMAIN
|
from .const import DOMAIN
|
||||||
from .coordinator import FlexitCoordinator
|
from .coordinator import FlexitCoordinator
|
||||||
|
|
||||||
PLATFORMS: list[Platform] = [Platform.CLIMATE, Platform.SENSOR]
|
PLATFORMS: list[Platform] = [Platform.BINARY_SENSOR, Platform.CLIMATE, Platform.SENSOR]
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
|
|
72
homeassistant/components/flexit_bacnet/binary_sensor.py
Normal file
72
homeassistant/components/flexit_bacnet/binary_sensor.py
Normal file
|
@ -0,0 +1,72 @@
|
||||||
|
"""The Flexit Nordic (BACnet) integration."""
|
||||||
|
from collections.abc import Callable
|
||||||
|
from dataclasses import dataclass
|
||||||
|
|
||||||
|
from flexit_bacnet import FlexitBACnet
|
||||||
|
|
||||||
|
from homeassistant.components.binary_sensor import (
|
||||||
|
BinarySensorDeviceClass,
|
||||||
|
BinarySensorEntity,
|
||||||
|
BinarySensorEntityDescription,
|
||||||
|
)
|
||||||
|
from homeassistant.config_entries import ConfigEntry
|
||||||
|
from homeassistant.core import HomeAssistant
|
||||||
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
|
from . import FlexitCoordinator
|
||||||
|
from .const import DOMAIN
|
||||||
|
from .entity import FlexitEntity
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(kw_only=True, frozen=True)
|
||||||
|
class FlexitBinarySensorEntityDescription(BinarySensorEntityDescription):
|
||||||
|
"""Describes a Flexit binary sensor entity."""
|
||||||
|
|
||||||
|
value_fn: Callable[[FlexitBACnet], bool]
|
||||||
|
|
||||||
|
|
||||||
|
SENSOR_TYPES: tuple[FlexitBinarySensorEntityDescription, ...] = (
|
||||||
|
FlexitBinarySensorEntityDescription(
|
||||||
|
key="air_filter_polluted",
|
||||||
|
device_class=BinarySensorDeviceClass.PROBLEM,
|
||||||
|
translation_key="air_filter_polluted",
|
||||||
|
value_fn=lambda data: data.air_filter_polluted,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
async def async_setup_entry(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
config_entry: ConfigEntry,
|
||||||
|
async_add_entities: AddEntitiesCallback,
|
||||||
|
) -> None:
|
||||||
|
"""Set up Flexit (bacnet) binary sensor from a config entry."""
|
||||||
|
coordinator: FlexitCoordinator = hass.data[DOMAIN][config_entry.entry_id]
|
||||||
|
|
||||||
|
async_add_entities(
|
||||||
|
FlexitBinarySensor(coordinator, description) for description in SENSOR_TYPES
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class FlexitBinarySensor(FlexitEntity, BinarySensorEntity):
|
||||||
|
"""Representation of a Flexit binary Sensor."""
|
||||||
|
|
||||||
|
entity_description: FlexitBinarySensorEntityDescription
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
coordinator: FlexitCoordinator,
|
||||||
|
entity_description: FlexitBinarySensorEntityDescription,
|
||||||
|
) -> None:
|
||||||
|
"""Initialize Flexit (bacnet) sensor."""
|
||||||
|
super().__init__(coordinator)
|
||||||
|
|
||||||
|
self.entity_description = entity_description
|
||||||
|
self._attr_unique_id = (
|
||||||
|
f"{coordinator.device.serial_number}-{entity_description.key}"
|
||||||
|
)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def is_on(self) -> bool:
|
||||||
|
"""Return value of binary sensor."""
|
||||||
|
return self.entity_description.value_fn(self.coordinator.data)
|
|
@ -17,6 +17,11 @@
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"entity": {
|
"entity": {
|
||||||
|
"binary_sensor": {
|
||||||
|
"air_filter_polluted": {
|
||||||
|
"name": "Air filter polluted"
|
||||||
|
}
|
||||||
|
},
|
||||||
"sensor": {
|
"sensor": {
|
||||||
"outside_air_temperature": {
|
"outside_air_temperature": {
|
||||||
"name": "Outside air temperature"
|
"name": "Outside air temperature"
|
||||||
|
|
|
@ -0,0 +1,45 @@
|
||||||
|
# serializer version: 1
|
||||||
|
# name: test_binary_sensors[binary_sensor.device_name_air_filter_polluted-entry]
|
||||||
|
EntityRegistryEntrySnapshot({
|
||||||
|
'aliases': set({
|
||||||
|
}),
|
||||||
|
'area_id': None,
|
||||||
|
'capabilities': None,
|
||||||
|
'config_entry_id': <ANY>,
|
||||||
|
'device_class': None,
|
||||||
|
'device_id': <ANY>,
|
||||||
|
'disabled_by': None,
|
||||||
|
'domain': 'binary_sensor',
|
||||||
|
'entity_category': None,
|
||||||
|
'entity_id': 'binary_sensor.device_name_air_filter_polluted',
|
||||||
|
'has_entity_name': True,
|
||||||
|
'hidden_by': None,
|
||||||
|
'icon': None,
|
||||||
|
'id': <ANY>,
|
||||||
|
'name': None,
|
||||||
|
'options': dict({
|
||||||
|
}),
|
||||||
|
'original_device_class': <BinarySensorDeviceClass.PROBLEM: 'problem'>,
|
||||||
|
'original_icon': None,
|
||||||
|
'original_name': 'Air filter polluted',
|
||||||
|
'platform': 'flexit_bacnet',
|
||||||
|
'previous_unique_id': None,
|
||||||
|
'supported_features': 0,
|
||||||
|
'translation_key': 'air_filter_polluted',
|
||||||
|
'unique_id': '0000-0001-air_filter_polluted',
|
||||||
|
'unit_of_measurement': None,
|
||||||
|
})
|
||||||
|
# ---
|
||||||
|
# name: test_binary_sensors[binary_sensor.device_name_air_filter_polluted-state]
|
||||||
|
StateSnapshot({
|
||||||
|
'attributes': ReadOnlyDict({
|
||||||
|
'device_class': 'problem',
|
||||||
|
'friendly_name': 'Device Name Air filter polluted',
|
||||||
|
}),
|
||||||
|
'context': <ANY>,
|
||||||
|
'entity_id': 'binary_sensor.device_name_air_filter_polluted',
|
||||||
|
'last_changed': <ANY>,
|
||||||
|
'last_updated': <ANY>,
|
||||||
|
'state': 'on',
|
||||||
|
})
|
||||||
|
# ---
|
35
tests/components/flexit_bacnet/test_binary_sensor.py
Normal file
35
tests/components/flexit_bacnet/test_binary_sensor.py
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
"""Tests for the Flexit Nordic (BACnet) binary sensor entities."""
|
||||||
|
from unittest.mock import AsyncMock
|
||||||
|
|
||||||
|
from syrupy.assertion import SnapshotAssertion
|
||||||
|
|
||||||
|
from homeassistant.const import Platform
|
||||||
|
from homeassistant.core import HomeAssistant
|
||||||
|
from homeassistant.helpers import entity_registry as er
|
||||||
|
|
||||||
|
from tests.common import MockConfigEntry
|
||||||
|
from tests.components.flexit_bacnet import setup_with_selected_platforms
|
||||||
|
|
||||||
|
|
||||||
|
async def test_binary_sensors(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
snapshot: SnapshotAssertion,
|
||||||
|
entity_registry: er.EntityRegistry,
|
||||||
|
mock_flexit_bacnet: AsyncMock,
|
||||||
|
mock_config_entry: MockConfigEntry,
|
||||||
|
) -> None:
|
||||||
|
"""Test binary sensor states are correctly collected from library."""
|
||||||
|
|
||||||
|
await setup_with_selected_platforms(
|
||||||
|
hass, mock_config_entry, [Platform.BINARY_SENSOR]
|
||||||
|
)
|
||||||
|
entity_entries = er.async_entries_for_config_entry(
|
||||||
|
entity_registry, mock_config_entry.entry_id
|
||||||
|
)
|
||||||
|
|
||||||
|
assert entity_entries
|
||||||
|
for entity_entry in entity_entries:
|
||||||
|
assert entity_entry == snapshot(name=f"{entity_entry.entity_id}-entry")
|
||||||
|
assert hass.states.get(entity_entry.entity_id) == snapshot(
|
||||||
|
name=f"{entity_entry.entity_id}-state"
|
||||||
|
)
|
Loading…
Add table
Reference in a new issue