|
| 1 | +"""Initialise HomeAssistant buttons for Honeygain.""" |
| 2 | +from homeassistant.components.button import ButtonEntity, ButtonEntityDescription |
| 3 | +from homeassistant.config_entries import ConfigEntry |
| 4 | +from homeassistant.core import HomeAssistant |
| 5 | +from homeassistant.helpers.entity_platform import AddEntitiesCallback |
| 6 | + |
| 7 | +from . import HoneygainData |
| 8 | +from .const import DOMAIN |
| 9 | + |
| 10 | + |
| 11 | +async def async_setup_entry( |
| 12 | + hass: HomeAssistant, |
| 13 | + entry: ConfigEntry, |
| 14 | + async_add_entities: AddEntitiesCallback, |
| 15 | +) -> None: |
| 16 | + """Button set up for HoneyGain.""" |
| 17 | + honeygain_data: HoneygainData = hass.data[DOMAIN][entry.entry_id] |
| 18 | + buttons: list[ButtonEntity] = [HoneygainPotButton(hass, honeygain_data)] |
| 19 | + async_add_entities(buttons) |
| 20 | + |
| 21 | + |
| 22 | +class HoneygainPotButton(ButtonEntity): |
| 23 | + """Generate buttons for Honeygain actions.""" |
| 24 | + |
| 25 | + hass: HomeAssistant |
| 26 | + button_description: ButtonEntityDescription |
| 27 | + _honeygain_data: HoneygainData |
| 28 | + |
| 29 | + def __init__(self, hass: HomeAssistant, honeygain_data: HoneygainData) -> None: |
| 30 | + """Initialise a button.""" |
| 31 | + self.hass = hass |
| 32 | + self._honeygain_data = honeygain_data |
| 33 | + self.button_description = ButtonEntityDescription( |
| 34 | + key="open_lucky_pot", |
| 35 | + name="Open lucky pot", |
| 36 | + icon="mdi:gift-open", |
| 37 | + ) |
| 38 | + self.entity_id = f"button.{self.button_description.key}" |
| 39 | + self._attr_name = self.button_description.name |
| 40 | + self._attr_icon = self.button_description.icon |
| 41 | + self._attr_unique_id = f"honeygain-{self._honeygain_data.user['referral_code']}-{self.button_description.key}" |
| 42 | + |
| 43 | + def press(self) -> None: |
| 44 | + """Handle the button press.""" |
| 45 | + self._honeygain_data.open_daily_pot() |
| 46 | + self._honeygain_data.update() |
0 commit comments