diff --git a/homeassistant/components/zinvolt/__init__.py b/homeassistant/components/zinvolt/__init__.py index e112dfc01e2..adee797b00a 100644 --- a/homeassistant/components/zinvolt/__init__.py +++ b/homeassistant/components/zinvolt/__init__.py @@ -15,6 +15,7 @@ from homeassistant.helpers.aiohttp_client import async_get_clientsession from .coordinator import ZinvoltConfigEntry, ZinvoltDeviceCoordinator _PLATFORMS: list[Platform] = [ + Platform.BINARY_SENSOR, Platform.SENSOR, Platform.NUMBER, ] diff --git a/homeassistant/components/zinvolt/binary_sensor.py b/homeassistant/components/zinvolt/binary_sensor.py new file mode 100644 index 00000000000..2ba73f5ea6b --- /dev/null +++ b/homeassistant/components/zinvolt/binary_sensor.py @@ -0,0 +1,71 @@ +"""Binary sensor platform for Zinvolt integration.""" + +from collections.abc import Callable +from dataclasses import dataclass + +from zinvolt.models import BatteryState + +from homeassistant.components.binary_sensor import ( + BinarySensorDeviceClass, + BinarySensorEntity, + BinarySensorEntityDescription, +) +from homeassistant.const import EntityCategory +from homeassistant.core import HomeAssistant +from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback + +from .coordinator import ZinvoltConfigEntry, ZinvoltDeviceCoordinator +from .entity import ZinvoltEntity + + +@dataclass(kw_only=True, frozen=True) +class ZinvoltBatteryStateDescription(BinarySensorEntityDescription): + """Binary sensor description for Zinvolt battery state.""" + + is_on_fn: Callable[[BatteryState], bool] + + +SENSORS: tuple[ZinvoltBatteryStateDescription, ...] = ( + ZinvoltBatteryStateDescription( + key="on_grid", + translation_key="on_grid", + entity_category=EntityCategory.DIAGNOSTIC, + device_class=BinarySensorDeviceClass.CONNECTIVITY, + is_on_fn=lambda state: state.current_power.on_grid, + ), +) + + +async def async_setup_entry( + hass: HomeAssistant, + entry: ZinvoltConfigEntry, + async_add_entities: AddConfigEntryEntitiesCallback, +) -> None: + """Initialize the entries.""" + + async_add_entities( + ZinvoltBatteryStateBinarySensor(coordinator, description) + for description in SENSORS + for coordinator in entry.runtime_data.values() + ) + + +class ZinvoltBatteryStateBinarySensor(ZinvoltEntity, BinarySensorEntity): + """Zinvolt battery state binary sensor.""" + + entity_description: ZinvoltBatteryStateDescription + + def __init__( + self, + coordinator: ZinvoltDeviceCoordinator, + description: ZinvoltBatteryStateDescription, + ) -> None: + """Initialize the binary sensor.""" + super().__init__(coordinator) + self.entity_description = description + self._attr_unique_id = f"{coordinator.data.serial_number}.{description.key}" + + @property + def is_on(self) -> bool: + """Return the state of the binary sensor.""" + return self.entity_description.is_on_fn(self.coordinator.data) diff --git a/homeassistant/components/zinvolt/strings.json b/homeassistant/components/zinvolt/strings.json index 9b613651c50..fe06fac602d 100644 --- a/homeassistant/components/zinvolt/strings.json +++ b/homeassistant/components/zinvolt/strings.json @@ -22,6 +22,11 @@ } }, "entity": { + "binary_sensor": { + "on_grid": { + "name": "Grid connection" + } + }, "number": { "lower_threshold": { "name": "Minimum charge level" diff --git a/tests/components/zinvolt/snapshots/test_binary_sensor.ambr b/tests/components/zinvolt/snapshots/test_binary_sensor.ambr new file mode 100644 index 00000000000..6894b6c8c20 --- /dev/null +++ b/tests/components/zinvolt/snapshots/test_binary_sensor.ambr @@ -0,0 +1,51 @@ +# serializer version: 1 +# name: test_all_entities[binary_sensor.zinvolt_batterij_grid_connection-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'binary_sensor', + 'entity_category': , + 'entity_id': 'binary_sensor.zinvolt_batterij_grid_connection', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'Grid connection', + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Grid connection', + 'platform': 'zinvolt', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': 'on_grid', + 'unique_id': 'ZVG011025120088.on_grid', + 'unit_of_measurement': None, + }) +# --- +# name: test_all_entities[binary_sensor.zinvolt_batterij_grid_connection-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'connectivity', + 'friendly_name': 'Zinvolt Batterij Grid connection', + }), + 'context': , + 'entity_id': 'binary_sensor.zinvolt_batterij_grid_connection', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'on', + }) +# --- diff --git a/tests/components/zinvolt/test_binary_sensor.py b/tests/components/zinvolt/test_binary_sensor.py new file mode 100644 index 00000000000..72e14bcd466 --- /dev/null +++ b/tests/components/zinvolt/test_binary_sensor.py @@ -0,0 +1,27 @@ +"""Tests for the Zinvolt binary sensor.""" + +from unittest.mock import AsyncMock, patch + +from syrupy.assertion 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_all_entities( + hass: HomeAssistant, + snapshot: SnapshotAssertion, + mock_zinvolt_client: AsyncMock, + mock_config_entry: MockConfigEntry, + entity_registry: er.EntityRegistry, +) -> None: + """Test all entities.""" + with patch("homeassistant.components.zinvolt._PLATFORMS", [Platform.BINARY_SENSOR]): + await setup_integration(hass, mock_config_entry) + + await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id)