Add button platform to Peblar Rocksolid EV Chargers integration (#133780)

This commit is contained in:
Franck Nijhof 2024-12-22 11:51:01 +01:00 committed by GitHub
parent 84d359c0d9
commit 31c6443a9b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 224 additions and 0 deletions

View File

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

View File

@ -0,0 +1,92 @@
"""Support for Peblar button."""
from __future__ import annotations
from collections.abc import Awaitable, Callable
from dataclasses import dataclass
from typing import Any
from peblar import Peblar
from homeassistant.components.button import (
ButtonDeviceClass,
ButtonEntity,
ButtonEntityDescription,
)
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, PeblarUserConfigurationDataUpdateCoordinator
@dataclass(frozen=True, kw_only=True)
class PeblarButtonEntityDescription(ButtonEntityDescription):
"""Describe a Peblar button."""
press_fn: Callable[[Peblar], Awaitable[Any]]
DESCRIPTIONS = [
PeblarButtonEntityDescription(
key="identify",
device_class=ButtonDeviceClass.IDENTIFY,
entity_category=EntityCategory.CONFIG,
entity_registry_enabled_default=False,
press_fn=lambda x: x.identify(),
),
PeblarButtonEntityDescription(
key="reboot",
device_class=ButtonDeviceClass.RESTART,
entity_category=EntityCategory.CONFIG,
entity_registry_enabled_default=False,
press_fn=lambda x: x.reboot(),
),
]
async def async_setup_entry(
hass: HomeAssistant,
entry: PeblarConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up Peblar buttons based on a config entry."""
async_add_entities(
PeblarButtonEntity(
entry=entry,
description=description,
)
for description in DESCRIPTIONS
)
class PeblarButtonEntity(
CoordinatorEntity[PeblarUserConfigurationDataUpdateCoordinator], ButtonEntity
):
"""Defines an Peblar button."""
entity_description: PeblarButtonEntityDescription
_attr_has_entity_name = True
def __init__(
self,
entry: PeblarConfigEntry,
description: PeblarButtonEntityDescription,
) -> None:
"""Initialize the button entity."""
super().__init__(coordinator=entry.runtime_data.user_configuraton_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)
},
)
async def async_press(self) -> None:
"""Trigger button press on the Peblar device."""
await self.entity_description.press_fn(self.coordinator.peblar)

View File

@ -0,0 +1,95 @@
# serializer version: 1
# name: test_entities[button][button.peblar_ev_charger_identify-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'button',
'entity_category': <EntityCategory.CONFIG: 'config'>,
'entity_id': 'button.peblar_ev_charger_identify',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': <ButtonDeviceClass.IDENTIFY: 'identify'>,
'original_icon': None,
'original_name': 'Identify',
'platform': 'peblar',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': None,
'unique_id': '23-45-A4O-MOF_identify',
'unit_of_measurement': None,
})
# ---
# name: test_entities[button][button.peblar_ev_charger_identify-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'identify',
'friendly_name': 'Peblar EV Charger Identify',
}),
'context': <ANY>,
'entity_id': 'button.peblar_ev_charger_identify',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'unknown',
})
# ---
# name: test_entities[button][button.peblar_ev_charger_restart-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'button',
'entity_category': <EntityCategory.CONFIG: 'config'>,
'entity_id': 'button.peblar_ev_charger_restart',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': <ButtonDeviceClass.RESTART: 'restart'>,
'original_icon': None,
'original_name': 'Restart',
'platform': 'peblar',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': None,
'unique_id': '23-45-A4O-MOF_reboot',
'unit_of_measurement': None,
})
# ---
# name: test_entities[button][button.peblar_ev_charger_restart-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'restart',
'friendly_name': 'Peblar EV Charger Restart',
}),
'context': <ANY>,
'entity_id': 'button.peblar_ev_charger_restart',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'unknown',
})
# ---

View File

@ -0,0 +1,36 @@
"""Tests for the Peblar button 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.freeze_time("2024-12-21 21:45:00")
@pytest.mark.parametrize("init_integration", [Platform.BUTTON], 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 button 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