Add blebox binary_sensor platform (#79535)

* Add binary_sensor platform, with test.

* Applied suggestions by @epenet

* refactor: as @epenet suggested, passing entity_description to init

* Update homeassistant/components/blebox/binary_sensor.py

@epenet suggestion, moved refactored logic of create_blebox_entities into BleBoxBinarySensorEntity init

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>

* refactor: as @epenet class selector and entity creation moved to binary_sensor

* refactor: list comprehension in entity list setup in binary sensor

* Update homeassistant/components/blebox/binary_sensor.py

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>
This commit is contained in:
Michał Huryn 2022-10-20 14:35:28 +02:00 committed by GitHub
parent aea7a9af18
commit 4e4682d2e0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 101 additions and 0 deletions

View file

@ -19,6 +19,7 @@ from .const import DEFAULT_SETUP_TIMEOUT, DOMAIN, PRODUCT
_LOGGER = logging.getLogger(__name__)
PLATFORMS = [
Platform.BINARY_SENSOR,
Platform.BUTTON,
Platform.CLIMATE,
Platform.COVER,

View file

@ -0,0 +1,55 @@
"""BleBox binary sensor entities."""
from blebox_uniapi.binary_sensor import BinarySensor as BinarySensorFeature
from blebox_uniapi.box import Box
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 DOMAIN, PRODUCT, BleBoxEntity
BINARY_SENSOR_TYPES = (
BinarySensorEntityDescription(
key="moisture",
device_class=BinarySensorDeviceClass.MOISTURE,
),
)
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up a BleBox entry."""
product: Box = hass.data[DOMAIN][config_entry.entry_id][PRODUCT]
entities = [
BleBoxBinarySensorEntity(feature, description)
for feature in product.features.get("binary_sensors", [])
for description in BINARY_SENSOR_TYPES
if description.key == feature.device_class
]
async_add_entities(entities, True)
class BleBoxBinarySensorEntity(BleBoxEntity[BinarySensorFeature], BinarySensorEntity):
"""Representation of a BleBox binary sensor feature."""
def __init__(
self, feature: BinarySensorFeature, description: BinarySensorEntityDescription
) -> None:
"""Initialize a BleBox binary sensor feature."""
super().__init__(feature)
self.entity_description = description
@property
def is_on(self) -> bool:
"""Return the state."""
return self._feature.state

View file

@ -0,0 +1,45 @@
"""Blebox binary_sensor entities test."""
from unittest.mock import PropertyMock
import blebox_uniapi
import pytest
from homeassistant.components.binary_sensor import BinarySensorDeviceClass
from homeassistant.const import ATTR_DEVICE_CLASS, STATE_ON
from homeassistant.helpers import device_registry as dr
from .conftest import async_setup_entity, mock_feature
@pytest.fixture(name="rainsensor")
def airsensor_fixture():
"""Return a default air quality fixture."""
feature = mock_feature(
"binary_sensors",
blebox_uniapi.binary_sensor.Rain,
unique_id="BleBox-windRainSensor-ea68e74f4f49-0.rain",
full_name="windRainSensor-0.rain",
device_class="moisture",
)
product = feature.product
type(product).name = PropertyMock(return_value="My rain sensor")
type(product).model = PropertyMock(return_value="rainSensor")
return (feature, "binary_sensor.windrainsensor_0_rain")
async def test_init(rainsensor, hass, config):
"""Test binary_sensor initialisation."""
_, entity_id = rainsensor
entry = await async_setup_entity(hass, config, entity_id)
assert entry.unique_id == "BleBox-windRainSensor-ea68e74f4f49-0.rain"
state = hass.states.get(entity_id)
assert state.name == "windRainSensor-0.rain"
assert state.attributes[ATTR_DEVICE_CLASS] == BinarySensorDeviceClass.MOISTURE
assert state.state == STATE_ON
device_registry = dr.async_get(hass)
device = device_registry.async_get(entry.device_id)
assert device.name == "My rain sensor"