Add binary sensor platform to the Lektrico integration (#129872)

pull/130150/head
Lektri.co 2024-11-08 16:54:46 +02:00 committed by GitHub
parent 51e691f832
commit 6b90d8ff1a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 686 additions and 1 deletions

View File

@ -12,6 +12,7 @@ from .coordinator import LektricoDeviceDataUpdateCoordinator
# List the platforms that charger supports.
CHARGERS_PLATFORMS: list[Platform] = [
Platform.BINARY_SENSOR,
Platform.BUTTON,
Platform.NUMBER,
Platform.SENSOR,

View File

@ -0,0 +1,139 @@
"""Support for Lektrico binary sensors entities."""
from collections.abc import Callable
from dataclasses import dataclass
from typing import Any
from homeassistant.components.binary_sensor import (
BinarySensorDeviceClass,
BinarySensorEntity,
BinarySensorEntityDescription,
)
from homeassistant.const import ATTR_SERIAL_NUMBER, CONF_TYPE, EntityCategory
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import LektricoConfigEntry, LektricoDeviceDataUpdateCoordinator
from .entity import LektricoEntity
@dataclass(frozen=True, kw_only=True)
class LektricoBinarySensorEntityDescription(BinarySensorEntityDescription):
"""Describes Lektrico binary sensor entity."""
value_fn: Callable[[dict[str, Any]], bool]
BINARY_SENSORS: tuple[LektricoBinarySensorEntityDescription, ...] = (
LektricoBinarySensorEntityDescription(
key="state_e_activated",
translation_key="state_e_activated",
entity_category=EntityCategory.DIAGNOSTIC,
device_class=BinarySensorDeviceClass.PROBLEM,
value_fn=lambda data: bool(data["state_e_activated"]),
),
LektricoBinarySensorEntityDescription(
key="overtemp",
translation_key="overtemp",
entity_category=EntityCategory.DIAGNOSTIC,
device_class=BinarySensorDeviceClass.PROBLEM,
value_fn=lambda data: bool(data["overtemp"]),
),
LektricoBinarySensorEntityDescription(
key="critical_temp",
translation_key="critical_temp",
entity_category=EntityCategory.DIAGNOSTIC,
device_class=BinarySensorDeviceClass.PROBLEM,
value_fn=lambda data: bool(data["critical_temp"]),
),
LektricoBinarySensorEntityDescription(
key="overcurrent",
translation_key="overcurrent",
entity_category=EntityCategory.DIAGNOSTIC,
device_class=BinarySensorDeviceClass.PROBLEM,
value_fn=lambda data: bool(data["overcurrent"]),
),
LektricoBinarySensorEntityDescription(
key="meter_fault",
translation_key="meter_fault",
entity_category=EntityCategory.DIAGNOSTIC,
device_class=BinarySensorDeviceClass.PROBLEM,
value_fn=lambda data: bool(data["meter_fault"]),
),
LektricoBinarySensorEntityDescription(
key="undervoltage",
translation_key="undervoltage",
entity_category=EntityCategory.DIAGNOSTIC,
device_class=BinarySensorDeviceClass.PROBLEM,
value_fn=lambda data: bool(data["undervoltage_error"]),
),
LektricoBinarySensorEntityDescription(
key="overvoltage",
translation_key="overvoltage",
entity_category=EntityCategory.DIAGNOSTIC,
device_class=BinarySensorDeviceClass.PROBLEM,
value_fn=lambda data: bool(data["overvoltage_error"]),
),
LektricoBinarySensorEntityDescription(
key="rcd_error",
translation_key="rcd_error",
entity_category=EntityCategory.DIAGNOSTIC,
device_class=BinarySensorDeviceClass.PROBLEM,
value_fn=lambda data: bool(data["rcd_error"]),
),
LektricoBinarySensorEntityDescription(
key="cp_diode_failure",
translation_key="cp_diode_failure",
entity_category=EntityCategory.DIAGNOSTIC,
device_class=BinarySensorDeviceClass.PROBLEM,
value_fn=lambda data: bool(data["cp_diode_failure"]),
),
LektricoBinarySensorEntityDescription(
key="contactor_failure",
translation_key="contactor_failure",
entity_category=EntityCategory.DIAGNOSTIC,
device_class=BinarySensorDeviceClass.PROBLEM,
value_fn=lambda data: bool(data["contactor_failure"]),
),
)
async def async_setup_entry(
hass: HomeAssistant,
entry: LektricoConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up Lektrico binary sensor entities based on a config entry."""
coordinator = entry.runtime_data
async_add_entities(
LektricoBinarySensor(
description,
coordinator,
f"{entry.data[CONF_TYPE]}_{entry.data[ATTR_SERIAL_NUMBER]}",
)
for description in BINARY_SENSORS
)
class LektricoBinarySensor(LektricoEntity, BinarySensorEntity):
"""Defines a Lektrico binary sensor entity."""
entity_description: LektricoBinarySensorEntityDescription
def __init__(
self,
description: LektricoBinarySensorEntityDescription,
coordinator: LektricoDeviceDataUpdateCoordinator,
device_name: str,
) -> None:
"""Initialize Lektrico binary sensor."""
super().__init__(coordinator, device_name)
self.entity_description = description
self._coordinator = coordinator
self._attr_unique_id = f"{coordinator.serial_number}_{description.key}"
@property
def is_on(self) -> bool:
"""Return the state of the binary sensor."""
return self.entity_description.value_fn(self.coordinator.data)

View File

@ -22,6 +22,38 @@
}
},
"entity": {
"binary_sensor": {
"state_e_activated": {
"name": "Ev error"
},
"overtemp": {
"name": "Thermal throttling"
},
"critical_temp": {
"name": "Overheating"
},
"overcurrent": {
"name": "Overcurrent"
},
"meter_fault": {
"name": "Metering error"
},
"undervoltage": {
"name": "Undervoltage"
},
"overvoltage": {
"name": "Overvoltage"
},
"rcd_error": {
"name": "Rcd error"
},
"cp_diode_failure": {
"name": "Ev diode short"
},
"contactor_failure": {
"name": "Relay contacts welded"
}
},
"button": {
"charge_start": {
"name": "Charge start"

View File

@ -14,5 +14,15 @@
"dynamic_current": 32,
"user_current": 32,
"lb_mode": 0,
"require_auth": true
"require_auth": true,
"state_e_activated": false,
"undervoltage_error": true,
"rcd_error": false,
"meter_fault": false,
"overcurrent": false,
"overtemp": false,
"overvoltage_error": false,
"contactor_failure": false,
"cp_diode_failure": false,
"critical_temp": false
}

View File

@ -0,0 +1,471 @@
# serializer version: 1
# name: test_all_entities[binary_sensor.1p7k_500006_ev_diode_short-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.1p7k_500006_ev_diode_short',
'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': 'Ev diode short',
'platform': 'lektrico',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': 'cp_diode_failure',
'unique_id': '500006_cp_diode_failure',
'unit_of_measurement': None,
})
# ---
# name: test_all_entities[binary_sensor.1p7k_500006_ev_diode_short-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'problem',
'friendly_name': '1p7k_500006 Ev diode short',
}),
'context': <ANY>,
'entity_id': 'binary_sensor.1p7k_500006_ev_diode_short',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'off',
})
# ---
# name: test_all_entities[binary_sensor.1p7k_500006_ev_error-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.1p7k_500006_ev_error',
'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': 'Ev error',
'platform': 'lektrico',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': 'state_e_activated',
'unique_id': '500006_state_e_activated',
'unit_of_measurement': None,
})
# ---
# name: test_all_entities[binary_sensor.1p7k_500006_ev_error-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'problem',
'friendly_name': '1p7k_500006 Ev error',
}),
'context': <ANY>,
'entity_id': 'binary_sensor.1p7k_500006_ev_error',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'off',
})
# ---
# name: test_all_entities[binary_sensor.1p7k_500006_metering_error-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.1p7k_500006_metering_error',
'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': 'Metering error',
'platform': 'lektrico',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': 'meter_fault',
'unique_id': '500006_meter_fault',
'unit_of_measurement': None,
})
# ---
# name: test_all_entities[binary_sensor.1p7k_500006_metering_error-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'problem',
'friendly_name': '1p7k_500006 Metering error',
}),
'context': <ANY>,
'entity_id': 'binary_sensor.1p7k_500006_metering_error',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'off',
})
# ---
# name: test_all_entities[binary_sensor.1p7k_500006_overcurrent-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.1p7k_500006_overcurrent',
'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': 'Overcurrent',
'platform': 'lektrico',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': 'overcurrent',
'unique_id': '500006_overcurrent',
'unit_of_measurement': None,
})
# ---
# name: test_all_entities[binary_sensor.1p7k_500006_overcurrent-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'problem',
'friendly_name': '1p7k_500006 Overcurrent',
}),
'context': <ANY>,
'entity_id': 'binary_sensor.1p7k_500006_overcurrent',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'off',
})
# ---
# name: test_all_entities[binary_sensor.1p7k_500006_overheating-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.1p7k_500006_overheating',
'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': 'Overheating',
'platform': 'lektrico',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': 'critical_temp',
'unique_id': '500006_critical_temp',
'unit_of_measurement': None,
})
# ---
# name: test_all_entities[binary_sensor.1p7k_500006_overheating-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'problem',
'friendly_name': '1p7k_500006 Overheating',
}),
'context': <ANY>,
'entity_id': 'binary_sensor.1p7k_500006_overheating',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'off',
})
# ---
# name: test_all_entities[binary_sensor.1p7k_500006_overvoltage-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.1p7k_500006_overvoltage',
'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': 'Overvoltage',
'platform': 'lektrico',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': 'overvoltage',
'unique_id': '500006_overvoltage',
'unit_of_measurement': None,
})
# ---
# name: test_all_entities[binary_sensor.1p7k_500006_overvoltage-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'problem',
'friendly_name': '1p7k_500006 Overvoltage',
}),
'context': <ANY>,
'entity_id': 'binary_sensor.1p7k_500006_overvoltage',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'off',
})
# ---
# name: test_all_entities[binary_sensor.1p7k_500006_rcd_error-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.1p7k_500006_rcd_error',
'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': 'Rcd error',
'platform': 'lektrico',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': 'rcd_error',
'unique_id': '500006_rcd_error',
'unit_of_measurement': None,
})
# ---
# name: test_all_entities[binary_sensor.1p7k_500006_rcd_error-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'problem',
'friendly_name': '1p7k_500006 Rcd error',
}),
'context': <ANY>,
'entity_id': 'binary_sensor.1p7k_500006_rcd_error',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'off',
})
# ---
# name: test_all_entities[binary_sensor.1p7k_500006_relay_contacts_welded-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.1p7k_500006_relay_contacts_welded',
'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': 'Relay contacts welded',
'platform': 'lektrico',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': 'contactor_failure',
'unique_id': '500006_contactor_failure',
'unit_of_measurement': None,
})
# ---
# name: test_all_entities[binary_sensor.1p7k_500006_relay_contacts_welded-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'problem',
'friendly_name': '1p7k_500006 Relay contacts welded',
}),
'context': <ANY>,
'entity_id': 'binary_sensor.1p7k_500006_relay_contacts_welded',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'off',
})
# ---
# name: test_all_entities[binary_sensor.1p7k_500006_thermal_throttling-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.1p7k_500006_thermal_throttling',
'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': 'Thermal throttling',
'platform': 'lektrico',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': 'overtemp',
'unique_id': '500006_overtemp',
'unit_of_measurement': None,
})
# ---
# name: test_all_entities[binary_sensor.1p7k_500006_thermal_throttling-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'problem',
'friendly_name': '1p7k_500006 Thermal throttling',
}),
'context': <ANY>,
'entity_id': 'binary_sensor.1p7k_500006_thermal_throttling',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'off',
})
# ---
# name: test_all_entities[binary_sensor.1p7k_500006_undervoltage-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.1p7k_500006_undervoltage',
'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': 'Undervoltage',
'platform': 'lektrico',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': 'undervoltage',
'unique_id': '500006_undervoltage',
'unit_of_measurement': None,
})
# ---
# name: test_all_entities[binary_sensor.1p7k_500006_undervoltage-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'problem',
'friendly_name': '1p7k_500006 Undervoltage',
}),
'context': <ANY>,
'entity_id': 'binary_sensor.1p7k_500006_undervoltage',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'on',
})
# ---

View File

@ -0,0 +1,32 @@
"""Tests for the Lektrico binary sensor platform."""
from unittest.mock import AsyncMock, 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_all_entities(
hass: HomeAssistant,
snapshot: SnapshotAssertion,
mock_device: AsyncMock,
mock_config_entry: MockConfigEntry,
entity_registry: er.EntityRegistry,
) -> None:
"""Test all entities."""
with patch.multiple(
"homeassistant.components.lektrico",
CHARGERS_PLATFORMS=[Platform.BINARY_SENSOR],
LB_DEVICES_PLATFORMS=[Platform.BINARY_SENSOR],
):
await setup_integration(hass, mock_config_entry)
await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id)