"""Button entity that triggers a Main Gate Controller cycle.""" from __future__ import annotations from homeassistant.components.button import ButtonEntity from homeassistant.core import HomeAssistant from homeassistant.helpers.device_registry import DeviceInfo from homeassistant.helpers.entity_platform import AddEntitiesCallback from . import MainGateConfigEntry, build_device_info from .coordinator import MainGateController async def async_setup_entry( hass: HomeAssistant, entry: MainGateConfigEntry, async_add_entities: AddEntitiesCallback, ) -> None: """Set up the Open button for a gate.""" controller: MainGateController = entry.runtime_data async_add_entities([MainGateOpenButton(controller, entry)]) class MainGateOpenButton(ButtonEntity): """Button that runs the full open/countdown/close cycle.""" _attr_should_poll = False _attr_has_entity_name = True _attr_translation_key = "open" _attr_icon = "mdi:gate-open" def __init__(self, controller: MainGateController, entry: MainGateConfigEntry) -> None: self._controller = controller self._entry = entry self._attr_unique_id = f"{entry.entry_id}_open" @property def device_info(self) -> DeviceInfo: return build_device_info(self._entry) async def async_press(self) -> None: """Handle the button press: start the cycle if idle. Behaviour is ``mode: single`` — a second press while a cycle is in progress is silently ignored and an info-level log entry is written. """ self._controller.async_trigger()