|
| 1 | +"""Button entities for the OpenClaw integration. |
| 2 | +
|
| 3 | +Provides dashboard-friendly buttons for common actions: |
| 4 | +- Clear History — clears in-memory conversation history |
| 5 | +- Sync History — triggers backend history re-sync |
| 6 | +- Run Diagnostics — fires a connectivity check against the gateway |
| 7 | +""" |
| 8 | + |
| 9 | +from __future__ import annotations |
| 10 | + |
| 11 | +import logging |
| 12 | + |
| 13 | +from homeassistant.components.button import ButtonEntity, ButtonEntityDescription |
| 14 | +from homeassistant.config_entries import ConfigEntry |
| 15 | +from homeassistant.core import HomeAssistant |
| 16 | +from homeassistant.helpers.entity_platform import AddEntitiesCallback |
| 17 | +from homeassistant.helpers.update_coordinator import CoordinatorEntity |
| 18 | + |
| 19 | +from .api import OpenClawApiClient |
| 20 | +from .const import DOMAIN |
| 21 | +from .coordinator import OpenClawCoordinator |
| 22 | + |
| 23 | +_LOGGER = logging.getLogger(__name__) |
| 24 | + |
| 25 | +BUTTON_DESCRIPTIONS: tuple[ButtonEntityDescription, ...] = ( |
| 26 | + ButtonEntityDescription( |
| 27 | + key="clear_history", |
| 28 | + translation_key="clear_history", |
| 29 | + name="OpenClaw Clear History", |
| 30 | + icon="mdi:delete-sweep", |
| 31 | + ), |
| 32 | + ButtonEntityDescription( |
| 33 | + key="sync_history", |
| 34 | + translation_key="sync_history", |
| 35 | + name="OpenClaw Sync History", |
| 36 | + icon="mdi:sync", |
| 37 | + ), |
| 38 | + ButtonEntityDescription( |
| 39 | + key="run_diagnostics", |
| 40 | + translation_key="run_diagnostics", |
| 41 | + name="OpenClaw Run Diagnostics", |
| 42 | + icon="mdi:stethoscope", |
| 43 | + ), |
| 44 | +) |
| 45 | + |
| 46 | + |
| 47 | +async def async_setup_entry( |
| 48 | + hass: HomeAssistant, |
| 49 | + entry: ConfigEntry, |
| 50 | + async_add_entities: AddEntitiesCallback, |
| 51 | +) -> None: |
| 52 | + """Set up OpenClaw button entities from a config entry.""" |
| 53 | + entry_data: dict = hass.data[DOMAIN][entry.entry_id] |
| 54 | + coordinator: OpenClawCoordinator = entry_data["coordinator"] |
| 55 | + client: OpenClawApiClient = entry_data["client"] |
| 56 | + |
| 57 | + entities = [ |
| 58 | + OpenClawButton(coordinator, client, description, entry, hass) |
| 59 | + for description in BUTTON_DESCRIPTIONS |
| 60 | + ] |
| 61 | + async_add_entities(entities) |
| 62 | + |
| 63 | + |
| 64 | +class OpenClawButton(CoordinatorEntity[OpenClawCoordinator], ButtonEntity): |
| 65 | + """Button entity for OpenClaw actions.""" |
| 66 | + |
| 67 | + _attr_has_entity_name = True |
| 68 | + |
| 69 | + def __init__( |
| 70 | + self, |
| 71 | + coordinator: OpenClawCoordinator, |
| 72 | + client: OpenClawApiClient, |
| 73 | + description: ButtonEntityDescription, |
| 74 | + entry: ConfigEntry, |
| 75 | + hass: HomeAssistant, |
| 76 | + ) -> None: |
| 77 | + """Initialize the button.""" |
| 78 | + super().__init__(coordinator) |
| 79 | + self.entity_description = description |
| 80 | + self._client = client |
| 81 | + self._entry = entry |
| 82 | + self._hass = hass |
| 83 | + self._attr_unique_id = f"{entry.entry_id}_{description.key}" |
| 84 | + self._attr_device_info = { |
| 85 | + "identifiers": {(DOMAIN, entry.entry_id)}, |
| 86 | + "name": "OpenClaw Assistant", |
| 87 | + "manufacturer": "OpenClaw", |
| 88 | + "model": "OpenClaw Gateway", |
| 89 | + } |
| 90 | + |
| 91 | + async def async_press(self) -> None: |
| 92 | + """Handle button press.""" |
| 93 | + key = self.entity_description.key |
| 94 | + |
| 95 | + if key == "clear_history": |
| 96 | + store_key = f"{DOMAIN}_chat_history" |
| 97 | + store = self._hass.data.get(store_key) |
| 98 | + if isinstance(store, dict): |
| 99 | + store.clear() |
| 100 | + _LOGGER.info("OpenClaw chat history cleared via button") |
| 101 | + |
| 102 | + elif key == "sync_history": |
| 103 | + await self.coordinator.async_request_refresh() |
| 104 | + _LOGGER.info("OpenClaw history sync triggered via button") |
| 105 | + |
| 106 | + elif key == "run_diagnostics": |
| 107 | + try: |
| 108 | + alive = await self._client.async_check_alive() |
| 109 | + if alive: |
| 110 | + _LOGGER.info("OpenClaw diagnostics: gateway is reachable") |
| 111 | + else: |
| 112 | + _LOGGER.warning("OpenClaw diagnostics: gateway did not respond") |
| 113 | + except Exception as err: # noqa: BLE001 |
| 114 | + _LOGGER.error("OpenClaw diagnostics failed: %s", err) |
| 115 | + await self.coordinator.async_request_refresh() |
0 commit comments