From 003ae881bf2cac3a145ae04296a8bde3db8a719a Mon Sep 17 00:00:00 2001 From: Josef Zweck <24647999+zweckj@users.noreply.github.com> Date: Mon, 18 Nov 2024 15:26:53 +0100 Subject: [PATCH] Add binary_sensor platform to acaia (#130676) Co-authored-by: Joost Lekkerkerker --- homeassistant/components/acaia/__init__.py | 1 + .../components/acaia/binary_sensor.py | 58 +++++++++++++++++++ homeassistant/components/acaia/icons.json | 9 +++ homeassistant/components/acaia/strings.json | 5 ++ .../acaia/snapshots/test_binary_sensor.ambr | 48 +++++++++++++++ tests/components/acaia/test_binary_sensor.py | 28 +++++++++ 6 files changed, 149 insertions(+) create mode 100644 homeassistant/components/acaia/binary_sensor.py create mode 100644 tests/components/acaia/snapshots/test_binary_sensor.ambr create mode 100644 tests/components/acaia/test_binary_sensor.py diff --git a/homeassistant/components/acaia/__init__.py b/homeassistant/components/acaia/__init__.py index f6eccc63b08..44f21533e98 100644 --- a/homeassistant/components/acaia/__init__.py +++ b/homeassistant/components/acaia/__init__.py @@ -6,6 +6,7 @@ from homeassistant.core import HomeAssistant from .coordinator import AcaiaConfigEntry, AcaiaCoordinator PLATFORMS = [ + Platform.BINARY_SENSOR, Platform.BUTTON, Platform.SENSOR, ] diff --git a/homeassistant/components/acaia/binary_sensor.py b/homeassistant/components/acaia/binary_sensor.py new file mode 100644 index 00000000000..9aa4b92e932 --- /dev/null +++ b/homeassistant/components/acaia/binary_sensor.py @@ -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) diff --git a/homeassistant/components/acaia/icons.json b/homeassistant/components/acaia/icons.json index aeab07ee912..59b316a36ce 100644 --- a/homeassistant/components/acaia/icons.json +++ b/homeassistant/components/acaia/icons.json @@ -1,5 +1,14 @@ { "entity": { + "binary_sensor": { + "timer_running": { + "default": "mdi:timer", + "state": { + "on": "mdi:timer-play", + "off": "mdi:timer-off" + } + } + }, "button": { "tare": { "default": "mdi:scale-balance" diff --git a/homeassistant/components/acaia/strings.json b/homeassistant/components/acaia/strings.json index f6a1aeb66fd..0e52e2c0b2f 100644 --- a/homeassistant/components/acaia/strings.json +++ b/homeassistant/components/acaia/strings.json @@ -23,6 +23,11 @@ } }, "entity": { + "binary_sensor": { + "timer_running": { + "name": "Timer running" + } + }, "button": { "tare": { "name": "Tare" diff --git a/tests/components/acaia/snapshots/test_binary_sensor.ambr b/tests/components/acaia/snapshots/test_binary_sensor.ambr new file mode 100644 index 00000000000..113b5f1501e --- /dev/null +++ b/tests/components/acaia/snapshots/test_binary_sensor.ambr @@ -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': , + 'device_class': None, + 'device_id': , + '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': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + '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': , + 'entity_id': 'binary_sensor.lunar_ddeeff_timer_running', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'on', + }) +# --- diff --git a/tests/components/acaia/test_binary_sensor.py b/tests/components/acaia/test_binary_sensor.py new file mode 100644 index 00000000000..a7aa7034d8d --- /dev/null +++ b/tests/components/acaia/test_binary_sensor.py @@ -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)