Add binary sensor platform to Peblar Rocksolid EV Chargers integration (#133755)

This commit is contained in:
Franck Nijhof 2024-12-21 22:29:11 +01:00 committed by GitHub
parent 85519a312c
commit 5e4e1ce5a7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 236 additions and 0 deletions

View File

@ -29,6 +29,7 @@ from .coordinator import (
)
PLATFORMS = [
Platform.BINARY_SENSOR,
Platform.NUMBER,
Platform.SELECT,
Platform.SENSOR,

View File

@ -0,0 +1,89 @@
"""Support for Peblar binary sensors."""
from __future__ import annotations
from collections.abc import Callable
from dataclasses import dataclass
from homeassistant.components.binary_sensor import (
BinarySensorDeviceClass,
BinarySensorEntity,
BinarySensorEntityDescription,
)
from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from .const import DOMAIN
from .coordinator import PeblarConfigEntry, PeblarData, PeblarDataUpdateCoordinator
@dataclass(frozen=True, kw_only=True)
class PeblarBinarySensorEntityDescription(BinarySensorEntityDescription):
"""Class describing Peblar binary sensor entities."""
is_on_fn: Callable[[PeblarData], bool]
DESCRIPTIONS = [
PeblarBinarySensorEntityDescription(
key="active_error_codes",
translation_key="active_error_codes",
device_class=BinarySensorDeviceClass.PROBLEM,
entity_category=EntityCategory.DIAGNOSTIC,
entity_registry_enabled_default=False,
is_on_fn=lambda x: bool(x.system.active_error_codes),
),
PeblarBinarySensorEntityDescription(
key="active_warning_codes",
translation_key="active_warning_codes",
device_class=BinarySensorDeviceClass.PROBLEM,
entity_category=EntityCategory.DIAGNOSTIC,
entity_registry_enabled_default=False,
is_on_fn=lambda x: bool(x.system.active_warning_codes),
),
]
async def async_setup_entry(
hass: HomeAssistant,
entry: PeblarConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up Peblar binary sensor based on a config entry."""
async_add_entities(
PeblarBinarySensorEntity(entry=entry, description=description)
for description in DESCRIPTIONS
)
class PeblarBinarySensorEntity(
CoordinatorEntity[PeblarDataUpdateCoordinator], BinarySensorEntity
):
"""Defines a Peblar binary sensor entity."""
entity_description: PeblarBinarySensorEntityDescription
_attr_has_entity_name = True
def __init__(
self,
entry: PeblarConfigEntry,
description: PeblarBinarySensorEntityDescription,
) -> None:
"""Initialize the binary sensor entity."""
super().__init__(entry.runtime_data.data_coordinator)
self.entity_description = description
self._attr_unique_id = f"{entry.unique_id}-{description.key}"
self._attr_device_info = DeviceInfo(
identifiers={
(DOMAIN, entry.runtime_data.system_information.product_serial_number)
},
)
@property
def is_on(self) -> bool:
"""Return state of the binary sensor."""
return self.entity_description.is_on_fn(self.coordinator.data)

View File

@ -1,5 +1,13 @@
{
"entity": {
"binary_sensor": {
"active_error_codes": {
"default": "mdi:alert"
},
"active_warning_codes": {
"default": "mdi:alert"
}
},
"number": {
"charge_current_limit": {
"default": "mdi:speedometer"

View File

@ -33,6 +33,14 @@
}
},
"entity": {
"binary_sensor": {
"active_error_codes": {
"name": "Active errors"
},
"active_warning_codes": {
"name": "Active warnings"
}
},
"number": {
"charge_current_limit": {
"name": "Charge limit"

View File

@ -0,0 +1,95 @@
# serializer version: 1
# name: test_entities[binary_sensor][binary_sensor.peblar_ev_charger_active_errors-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': <EntityCategory.DIAGNOSTIC: 'diagnostic'>,
'entity_id': 'binary_sensor.peblar_ev_charger_active_errors',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': <BinarySensorDeviceClass.PROBLEM: 'problem'>,
'original_icon': None,
'original_name': 'Active errors',
'platform': 'peblar',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': 'active_error_codes',
'unique_id': '23-45-A4O-MOF-active_error_codes',
'unit_of_measurement': None,
})
# ---
# name: test_entities[binary_sensor][binary_sensor.peblar_ev_charger_active_errors-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'problem',
'friendly_name': 'Peblar EV Charger Active errors',
}),
'context': <ANY>,
'entity_id': 'binary_sensor.peblar_ev_charger_active_errors',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'off',
})
# ---
# name: test_entities[binary_sensor][binary_sensor.peblar_ev_charger_active_warnings-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': <EntityCategory.DIAGNOSTIC: 'diagnostic'>,
'entity_id': 'binary_sensor.peblar_ev_charger_active_warnings',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': <BinarySensorDeviceClass.PROBLEM: 'problem'>,
'original_icon': None,
'original_name': 'Active warnings',
'platform': 'peblar',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': 'active_warning_codes',
'unique_id': '23-45-A4O-MOF-active_warning_codes',
'unit_of_measurement': None,
})
# ---
# name: test_entities[binary_sensor][binary_sensor.peblar_ev_charger_active_warnings-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'problem',
'friendly_name': 'Peblar EV Charger Active warnings',
}),
'context': <ANY>,
'entity_id': 'binary_sensor.peblar_ev_charger_active_warnings',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'off',
})
# ---

View File

@ -0,0 +1,35 @@
"""Tests for the Peblar binary sensor platform."""
import pytest
from syrupy.assertion import SnapshotAssertion
from homeassistant.components.peblar.const import DOMAIN
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr, entity_registry as er
from tests.common import MockConfigEntry, snapshot_platform
@pytest.mark.parametrize("init_integration", [Platform.BINARY_SENSOR], indirect=True)
@pytest.mark.usefixtures("entity_registry_enabled_by_default", "init_integration")
async def test_entities(
hass: HomeAssistant,
snapshot: SnapshotAssertion,
entity_registry: er.EntityRegistry,
device_registry: dr.DeviceRegistry,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test the binary sensors entities."""
await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id)
# Ensure all entities are correctly assigned to the Peblar device
device_entry = device_registry.async_get_device(
identifiers={(DOMAIN, "23-45-A4O-MOF")}
)
assert device_entry
entity_entries = er.async_entries_for_config_entry(
entity_registry, mock_config_entry.entry_id
)
for entity_entry in entity_entries:
assert entity_entry.device_id == device_entry.id