From 4b7650f2d237a91f20bc365f2e1c25c67e1aa3a7 Mon Sep 17 00:00:00 2001 From: Nick Kuiper <65495045+NickKoepr@users.noreply.github.com> Date: Wed, 14 May 2025 19:37:16 +0200 Subject: [PATCH] Add buttons to Blue current integration (#143964) * Add buttons to Blue current integration * Apply feedback * Changed configEntry to use the BlueCurrentConfigEntry. The connector is now accessed via the entry instead of hass.data. * Changed test_buttons_created test to use the snapshot_platform function. Also removed the entry.unique_id check in the test_charge_point_buttons function because this is not needed anymore, according to https://github.com/home-assistant/core/pull/114000#discussion_r1627201872 * Applied requested changes. Changes requested by joostlek. * Moved has_value from BlueCurrentEntity to class level. This value was still inside the __init__ function, so the value was not overwritten by the ChargePointButton. --------- Co-authored-by: Floris272 --- .../components/blue_current/__init__.py | 2 +- .../components/blue_current/button.py | 89 +++++++++++ .../components/blue_current/entity.py | 5 +- .../components/blue_current/icons.json | 11 ++ .../components/blue_current/strings.json | 11 ++ .../blue_current/snapshots/test_button.ambr | 144 ++++++++++++++++++ tests/components/blue_current/test_button.py | 51 +++++++ 7 files changed, 308 insertions(+), 5 deletions(-) create mode 100644 homeassistant/components/blue_current/button.py create mode 100644 tests/components/blue_current/snapshots/test_button.ambr create mode 100644 tests/components/blue_current/test_button.py diff --git a/homeassistant/components/blue_current/__init__.py b/homeassistant/components/blue_current/__init__.py index 6d0ccd7b6db..775ca16a12a 100644 --- a/homeassistant/components/blue_current/__init__.py +++ b/homeassistant/components/blue_current/__init__.py @@ -24,7 +24,7 @@ from .const import DOMAIN, EVSE_ID, LOGGER, MODEL_TYPE type BlueCurrentConfigEntry = ConfigEntry[Connector] -PLATFORMS = [Platform.SENSOR] +PLATFORMS = [Platform.BUTTON, Platform.SENSOR] CHARGE_POINTS = "CHARGE_POINTS" DATA = "data" DELAY = 5 diff --git a/homeassistant/components/blue_current/button.py b/homeassistant/components/blue_current/button.py new file mode 100644 index 00000000000..9d2cde547ca --- /dev/null +++ b/homeassistant/components/blue_current/button.py @@ -0,0 +1,89 @@ +"""Support for Blue Current buttons.""" + +from __future__ import annotations + +from collections.abc import Callable, Coroutine +from dataclasses import dataclass +from typing import Any + +from bluecurrent_api.client import Client + +from homeassistant.components.button import ( + ButtonDeviceClass, + ButtonEntity, + ButtonEntityDescription, +) +from homeassistant.core import HomeAssistant +from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback + +from . import BlueCurrentConfigEntry, Connector +from .entity import ChargepointEntity + + +@dataclass(kw_only=True, frozen=True) +class ChargePointButtonEntityDescription(ButtonEntityDescription): + """Describes a Blue Current button entity.""" + + function: Callable[[Client, str], Coroutine[Any, Any, None]] + + +CHARGE_POINT_BUTTONS = ( + ChargePointButtonEntityDescription( + key="reset", + translation_key="reset", + function=lambda client, evse_id: client.reset(evse_id), + device_class=ButtonDeviceClass.RESTART, + ), + ChargePointButtonEntityDescription( + key="reboot", + translation_key="reboot", + function=lambda client, evse_id: client.reboot(evse_id), + device_class=ButtonDeviceClass.RESTART, + ), + ChargePointButtonEntityDescription( + key="stop_charge_session", + translation_key="stop_charge_session", + function=lambda client, evse_id: client.stop_session(evse_id), + ), +) + + +async def async_setup_entry( + hass: HomeAssistant, + entry: BlueCurrentConfigEntry, + async_add_entities: AddConfigEntryEntitiesCallback, +) -> None: + """Set up Blue Current buttons.""" + connector: Connector = entry.runtime_data + async_add_entities( + ChargePointButton( + connector, + button, + evse_id, + ) + for evse_id in connector.charge_points + for button in CHARGE_POINT_BUTTONS + ) + + +class ChargePointButton(ChargepointEntity, ButtonEntity): + """Define a charge point button.""" + + has_value = True + entity_description: ChargePointButtonEntityDescription + + def __init__( + self, + connector: Connector, + description: ChargePointButtonEntityDescription, + evse_id: str, + ) -> None: + """Initialize the button.""" + super().__init__(connector, evse_id) + + self.entity_description = description + self._attr_unique_id = f"{description.key}_{evse_id}" + + async def async_press(self) -> None: + """Handle the button press.""" + await self.entity_description.function(self.connector.client, self.evse_id) diff --git a/homeassistant/components/blue_current/entity.py b/homeassistant/components/blue_current/entity.py index cae7d420c99..426b7c06845 100644 --- a/homeassistant/components/blue_current/entity.py +++ b/homeassistant/components/blue_current/entity.py @@ -1,7 +1,5 @@ """Entity representing a Blue Current charge point.""" -from abc import abstractmethod - from homeassistant.const import ATTR_NAME from homeassistant.core import callback from homeassistant.helpers.device_registry import DeviceInfo @@ -17,12 +15,12 @@ class BlueCurrentEntity(Entity): _attr_has_entity_name = True _attr_should_poll = False + has_value = False def __init__(self, connector: Connector, signal: str) -> None: """Initialize the entity.""" self.connector = connector self.signal = signal - self.has_value = False async def async_added_to_hass(self) -> None: """Register callbacks.""" @@ -43,7 +41,6 @@ class BlueCurrentEntity(Entity): return self.connector.connected and self.has_value @callback - @abstractmethod def update_from_latest_data(self) -> None: """Update the entity from the latest data.""" diff --git a/homeassistant/components/blue_current/icons.json b/homeassistant/components/blue_current/icons.json index b5a5f2be81e..ce936902e91 100644 --- a/homeassistant/components/blue_current/icons.json +++ b/homeassistant/components/blue_current/icons.json @@ -19,6 +19,17 @@ "current_left": { "default": "mdi:gauge" } + }, + "button": { + "reset": { + "default": "mdi:restart" + }, + "reboot": { + "default": "mdi:restart-alert" + }, + "stop_charge_session": { + "default": "mdi:stop" + } } } } diff --git a/homeassistant/components/blue_current/strings.json b/homeassistant/components/blue_current/strings.json index a8a9aff7f08..28eb20fa912 100644 --- a/homeassistant/components/blue_current/strings.json +++ b/homeassistant/components/blue_current/strings.json @@ -113,6 +113,17 @@ "grid_max_current": { "name": "Max grid current" } + }, + "button": { + "stop_charge_session": { + "name": "Stop charge session" + }, + "reboot": { + "name": "Reboot" + }, + "reset": { + "name": "Reset" + } } } } diff --git a/tests/components/blue_current/snapshots/test_button.ambr b/tests/components/blue_current/snapshots/test_button.ambr new file mode 100644 index 00000000000..0dc27892ceb --- /dev/null +++ b/tests/components/blue_current/snapshots/test_button.ambr @@ -0,0 +1,144 @@ +# serializer version: 1 +# name: test_buttons_created[button.101_reboot-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'button', + 'entity_category': None, + 'entity_id': 'button.101_reboot', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Reboot', + 'platform': 'blue_current', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'reboot', + 'unique_id': 'reboot_101', + 'unit_of_measurement': None, + }) +# --- +# name: test_buttons_created[button.101_reboot-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'restart', + 'friendly_name': '101 Reboot', + }), + 'context': , + 'entity_id': 'button.101_reboot', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'unknown', + }) +# --- +# name: test_buttons_created[button.101_reset-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'button', + 'entity_category': None, + 'entity_id': 'button.101_reset', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Reset', + 'platform': 'blue_current', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'reset', + 'unique_id': 'reset_101', + 'unit_of_measurement': None, + }) +# --- +# name: test_buttons_created[button.101_reset-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'restart', + 'friendly_name': '101 Reset', + }), + 'context': , + 'entity_id': 'button.101_reset', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'unknown', + }) +# --- +# name: test_buttons_created[button.101_stop_charge_session-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'button', + 'entity_category': None, + 'entity_id': 'button.101_stop_charge_session', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Stop charge session', + 'platform': 'blue_current', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'stop_charge_session', + 'unique_id': 'stop_charge_session_101', + 'unit_of_measurement': None, + }) +# --- +# name: test_buttons_created[button.101_stop_charge_session-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': '101 Stop charge session', + }), + 'context': , + 'entity_id': 'button.101_stop_charge_session', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'unknown', + }) +# --- diff --git a/tests/components/blue_current/test_button.py b/tests/components/blue_current/test_button.py new file mode 100644 index 00000000000..7b9e7a7e7ce --- /dev/null +++ b/tests/components/blue_current/test_button.py @@ -0,0 +1,51 @@ +"""The tests for Blue Current buttons.""" + +import pytest +from syrupy.assertion import SnapshotAssertion + +from homeassistant.components.button import DOMAIN as BUTTON_DOMAIN, SERVICE_PRESS +from homeassistant.const import ATTR_ENTITY_ID, STATE_UNKNOWN, Platform +from homeassistant.core import HomeAssistant +from homeassistant.helpers import entity_registry as er + +from . import init_integration + +from tests.common import MockConfigEntry, snapshot_platform + +charge_point_buttons = ["stop_charge_session", "reset", "reboot"] + + +async def test_buttons_created( + hass: HomeAssistant, + snapshot: SnapshotAssertion, + config_entry: MockConfigEntry, + entity_registry: er.EntityRegistry, +) -> None: + """Test if all buttons are created.""" + await init_integration(hass, config_entry, Platform.BUTTON) + + await snapshot_platform(hass, entity_registry, snapshot, config_entry.entry_id) + + +@pytest.mark.freeze_time("2023-01-13 12:00:00+00:00") +async def test_charge_point_buttons( + hass: HomeAssistant, config_entry: MockConfigEntry +) -> None: + """Test the underlying charge point buttons.""" + await init_integration(hass, config_entry, Platform.BUTTON) + + for button in charge_point_buttons: + state = hass.states.get(f"button.101_{button}") + assert state is not None + assert state.state == STATE_UNKNOWN + + await hass.services.async_call( + BUTTON_DOMAIN, + SERVICE_PRESS, + {ATTR_ENTITY_ID: f"button.101_{button}"}, + blocking=True, + ) + + state = hass.states.get(f"button.101_{button}") + assert state + assert state.state == "2023-01-13T12:00:00+00:00"