mirror of
https://github.com/home-assistant/core.git
synced 2025-07-17 02:07:09 +00:00
Add binary sensor to Snoo (#140729)
* Add binary sensor * Update homeassistant/components/snoo/binary_sensor.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> --------- Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
This commit is contained in:
parent
056616f9c5
commit
214d14b06b
@ -17,7 +17,7 @@ from .coordinator import SnooConfigEntry, SnooCoordinator
|
|||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
PLATFORMS: list[Platform] = [Platform.SELECT, Platform.SENSOR]
|
PLATFORMS: list[Platform] = [Platform.BINARY_SENSOR, Platform.SELECT, Platform.SENSOR]
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(hass: HomeAssistant, entry: SnooConfigEntry) -> bool:
|
async def async_setup_entry(hass: HomeAssistant, entry: SnooConfigEntry) -> bool:
|
||||||
|
70
homeassistant/components/snoo/binary_sensor.py
Normal file
70
homeassistant/components/snoo/binary_sensor.py
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
"""Support for Snoo Binary Sensors."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from collections.abc import Callable
|
||||||
|
from dataclasses import dataclass
|
||||||
|
|
||||||
|
from python_snoo.containers import SnooData
|
||||||
|
|
||||||
|
from homeassistant.components.binary_sensor import (
|
||||||
|
BinarySensorDeviceClass,
|
||||||
|
BinarySensorEntity,
|
||||||
|
BinarySensorEntityDescription,
|
||||||
|
EntityCategory,
|
||||||
|
)
|
||||||
|
from homeassistant.core import HomeAssistant
|
||||||
|
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||||
|
|
||||||
|
from .coordinator import SnooConfigEntry
|
||||||
|
from .entity import SnooDescriptionEntity
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True, kw_only=True)
|
||||||
|
class SnooBinarySensorEntityDescription(BinarySensorEntityDescription):
|
||||||
|
"""Describes a Snoo Binary Sensor."""
|
||||||
|
|
||||||
|
value_fn: Callable[[SnooData], bool]
|
||||||
|
|
||||||
|
|
||||||
|
BINARY_SENSOR_DESCRIPTIONS: list[SnooBinarySensorEntityDescription] = [
|
||||||
|
SnooBinarySensorEntityDescription(
|
||||||
|
key="left_clip",
|
||||||
|
translation_key="left_clip",
|
||||||
|
value_fn=lambda data: data.left_safety_clip,
|
||||||
|
device_class=BinarySensorDeviceClass.CONNECTIVITY,
|
||||||
|
entity_category=EntityCategory.DIAGNOSTIC,
|
||||||
|
),
|
||||||
|
SnooBinarySensorEntityDescription(
|
||||||
|
key="right_clip",
|
||||||
|
translation_key="right_clip",
|
||||||
|
value_fn=lambda data: data.left_safety_clip,
|
||||||
|
device_class=BinarySensorDeviceClass.CONNECTIVITY,
|
||||||
|
entity_category=EntityCategory.DIAGNOSTIC,
|
||||||
|
),
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
async def async_setup_entry(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
entry: SnooConfigEntry,
|
||||||
|
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||||
|
) -> None:
|
||||||
|
"""Set up Snoo device."""
|
||||||
|
coordinators = entry.runtime_data
|
||||||
|
async_add_entities(
|
||||||
|
SnooBinarySensor(coordinator, description)
|
||||||
|
for coordinator in coordinators.values()
|
||||||
|
for description in BINARY_SENSOR_DESCRIPTIONS
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class SnooBinarySensor(SnooDescriptionEntity, BinarySensorEntity):
|
||||||
|
"""A Binary sensor using Snoo coordinator."""
|
||||||
|
|
||||||
|
entity_description: SnooBinarySensorEntityDescription
|
||||||
|
|
||||||
|
@property
|
||||||
|
def is_on(self) -> bool:
|
||||||
|
"""Return true if the binary sensor is on."""
|
||||||
|
return self.entity_description.value_fn(self.coordinator.data)
|
@ -27,6 +27,15 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"entity": {
|
"entity": {
|
||||||
|
"binary_sensor": {
|
||||||
|
"left_clip": {
|
||||||
|
"name": "Left safety clip"
|
||||||
|
},
|
||||||
|
"right_clip": {
|
||||||
|
"name": "Right safety clip"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
"sensor": {
|
"sensor": {
|
||||||
"state": {
|
"state": {
|
||||||
"name": "State",
|
"name": "State",
|
||||||
|
30
tests/components/snoo/test_binary_sensor.py
Normal file
30
tests/components/snoo/test_binary_sensor.py
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
"""Test Snoo Binary Sensors."""
|
||||||
|
|
||||||
|
from unittest.mock import AsyncMock
|
||||||
|
|
||||||
|
from homeassistant.const import STATE_ON, STATE_UNAVAILABLE
|
||||||
|
from homeassistant.core import HomeAssistant
|
||||||
|
|
||||||
|
from . import async_init_integration, find_update_callback
|
||||||
|
from .const import MOCK_SNOO_DATA
|
||||||
|
|
||||||
|
|
||||||
|
async def test_binary_sensors(hass: HomeAssistant, bypass_api: AsyncMock) -> None:
|
||||||
|
"""Test binary sensors and check test values are correctly set."""
|
||||||
|
await async_init_integration(hass)
|
||||||
|
assert len(hass.states.async_all("binary_sensor")) == 2
|
||||||
|
assert (
|
||||||
|
hass.states.get("binary_sensor.test_snoo_left_safety_clip").state
|
||||||
|
== STATE_UNAVAILABLE
|
||||||
|
)
|
||||||
|
assert (
|
||||||
|
hass.states.get("binary_sensor.test_snoo_right_safety_clip").state
|
||||||
|
== STATE_UNAVAILABLE
|
||||||
|
)
|
||||||
|
find_update_callback(bypass_api, "random_num")(MOCK_SNOO_DATA)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
assert len(hass.states.async_all("binary_sensor")) == 2
|
||||||
|
assert hass.states.get("binary_sensor.test_snoo_left_safety_clip").state == STATE_ON
|
||||||
|
assert (
|
||||||
|
hass.states.get("binary_sensor.test_snoo_right_safety_clip").state == STATE_ON
|
||||||
|
)
|
Loading…
x
Reference in New Issue
Block a user