mirror of
https://github.com/home-assistant/core.git
synced 2025-07-28 15:47:12 +00:00
Add binary_sensor platform to acaia (#130676)
Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
This commit is contained in:
parent
eb06dc214f
commit
003ae881bf
@ -6,6 +6,7 @@ from homeassistant.core import HomeAssistant
|
|||||||
from .coordinator import AcaiaConfigEntry, AcaiaCoordinator
|
from .coordinator import AcaiaConfigEntry, AcaiaCoordinator
|
||||||
|
|
||||||
PLATFORMS = [
|
PLATFORMS = [
|
||||||
|
Platform.BINARY_SENSOR,
|
||||||
Platform.BUTTON,
|
Platform.BUTTON,
|
||||||
Platform.SENSOR,
|
Platform.SENSOR,
|
||||||
]
|
]
|
||||||
|
58
homeassistant/components/acaia/binary_sensor.py
Normal file
58
homeassistant/components/acaia/binary_sensor.py
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
"""Binary sensor platform for Acaia scales."""
|
||||||
|
|
||||||
|
from collections.abc import Callable
|
||||||
|
from dataclasses import dataclass
|
||||||
|
|
||||||
|
from aioacaia.acaiascale import AcaiaScale
|
||||||
|
|
||||||
|
from homeassistant.components.binary_sensor import (
|
||||||
|
BinarySensorDeviceClass,
|
||||||
|
BinarySensorEntity,
|
||||||
|
BinarySensorEntityDescription,
|
||||||
|
)
|
||||||
|
from homeassistant.core import HomeAssistant
|
||||||
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
|
from .coordinator import AcaiaConfigEntry
|
||||||
|
from .entity import AcaiaEntity
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(kw_only=True, frozen=True)
|
||||||
|
class AcaiaBinarySensorEntityDescription(BinarySensorEntityDescription):
|
||||||
|
"""Description for Acaia binary sensor entities."""
|
||||||
|
|
||||||
|
is_on_fn: Callable[[AcaiaScale], bool]
|
||||||
|
|
||||||
|
|
||||||
|
BINARY_SENSORS: tuple[AcaiaBinarySensorEntityDescription, ...] = (
|
||||||
|
AcaiaBinarySensorEntityDescription(
|
||||||
|
key="timer_running",
|
||||||
|
translation_key="timer_running",
|
||||||
|
device_class=BinarySensorDeviceClass.RUNNING,
|
||||||
|
is_on_fn=lambda scale: scale.timer_running,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
async def async_setup_entry(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
entry: AcaiaConfigEntry,
|
||||||
|
async_add_entities: AddEntitiesCallback,
|
||||||
|
) -> None:
|
||||||
|
"""Set up binary sensors."""
|
||||||
|
|
||||||
|
coordinator = entry.runtime_data
|
||||||
|
async_add_entities(
|
||||||
|
AcaiaBinarySensor(coordinator, description) for description in BINARY_SENSORS
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class AcaiaBinarySensor(AcaiaEntity, BinarySensorEntity):
|
||||||
|
"""Representation of an Acaia binary sensor."""
|
||||||
|
|
||||||
|
entity_description: AcaiaBinarySensorEntityDescription
|
||||||
|
|
||||||
|
@property
|
||||||
|
def is_on(self) -> bool:
|
||||||
|
"""Return true if the binary sensor is on."""
|
||||||
|
return self.entity_description.is_on_fn(self._scale)
|
@ -1,5 +1,14 @@
|
|||||||
{
|
{
|
||||||
"entity": {
|
"entity": {
|
||||||
|
"binary_sensor": {
|
||||||
|
"timer_running": {
|
||||||
|
"default": "mdi:timer",
|
||||||
|
"state": {
|
||||||
|
"on": "mdi:timer-play",
|
||||||
|
"off": "mdi:timer-off"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
"button": {
|
"button": {
|
||||||
"tare": {
|
"tare": {
|
||||||
"default": "mdi:scale-balance"
|
"default": "mdi:scale-balance"
|
||||||
|
@ -23,6 +23,11 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"entity": {
|
"entity": {
|
||||||
|
"binary_sensor": {
|
||||||
|
"timer_running": {
|
||||||
|
"name": "Timer running"
|
||||||
|
}
|
||||||
|
},
|
||||||
"button": {
|
"button": {
|
||||||
"tare": {
|
"tare": {
|
||||||
"name": "Tare"
|
"name": "Tare"
|
||||||
|
48
tests/components/acaia/snapshots/test_binary_sensor.ambr
Normal file
48
tests/components/acaia/snapshots/test_binary_sensor.ambr
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
# serializer version: 1
|
||||||
|
# name: test_binary_sensors[binary_sensor.lunar_ddeeff_timer_running-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.lunar_ddeeff_timer_running',
|
||||||
|
'has_entity_name': True,
|
||||||
|
'hidden_by': None,
|
||||||
|
'icon': None,
|
||||||
|
'id': <ANY>,
|
||||||
|
'labels': set({
|
||||||
|
}),
|
||||||
|
'name': None,
|
||||||
|
'options': dict({
|
||||||
|
}),
|
||||||
|
'original_device_class': <BinarySensorDeviceClass.RUNNING: 'running'>,
|
||||||
|
'original_icon': None,
|
||||||
|
'original_name': 'Timer running',
|
||||||
|
'platform': 'acaia',
|
||||||
|
'previous_unique_id': None,
|
||||||
|
'supported_features': 0,
|
||||||
|
'translation_key': 'timer_running',
|
||||||
|
'unique_id': 'aa:bb:cc:dd:ee:ff_timer_running',
|
||||||
|
'unit_of_measurement': None,
|
||||||
|
})
|
||||||
|
# ---
|
||||||
|
# name: test_binary_sensors[binary_sensor.lunar_ddeeff_timer_running-state]
|
||||||
|
StateSnapshot({
|
||||||
|
'attributes': ReadOnlyDict({
|
||||||
|
'device_class': 'running',
|
||||||
|
'friendly_name': 'LUNAR-DDEEFF Timer running',
|
||||||
|
}),
|
||||||
|
'context': <ANY>,
|
||||||
|
'entity_id': 'binary_sensor.lunar_ddeeff_timer_running',
|
||||||
|
'last_changed': <ANY>,
|
||||||
|
'last_reported': <ANY>,
|
||||||
|
'last_updated': <ANY>,
|
||||||
|
'state': 'on',
|
||||||
|
})
|
||||||
|
# ---
|
28
tests/components/acaia/test_binary_sensor.py
Normal file
28
tests/components/acaia/test_binary_sensor.py
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
"""Test binary sensors for acaia integration."""
|
||||||
|
|
||||||
|
from unittest.mock import MagicMock, patch
|
||||||
|
|
||||||
|
from syrupy import SnapshotAssertion
|
||||||
|
|
||||||
|
from homeassistant.const import Platform
|
||||||
|
from homeassistant.core import HomeAssistant
|
||||||
|
from homeassistant.helpers import entity_registry as er
|
||||||
|
|
||||||
|
from . import setup_integration
|
||||||
|
|
||||||
|
from tests.common import MockConfigEntry, snapshot_platform
|
||||||
|
|
||||||
|
|
||||||
|
async def test_binary_sensors(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
entity_registry: er.EntityRegistry,
|
||||||
|
snapshot: SnapshotAssertion,
|
||||||
|
mock_scale: MagicMock,
|
||||||
|
mock_config_entry: MockConfigEntry,
|
||||||
|
) -> None:
|
||||||
|
"""Test the acaia binary sensors."""
|
||||||
|
|
||||||
|
with patch("homeassistant.components.acaia.PLATFORMS", [Platform.BINARY_SENSOR]):
|
||||||
|
await setup_integration(hass, mock_config_entry)
|
||||||
|
|
||||||
|
await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id)
|
Loading…
x
Reference in New Issue
Block a user