Skip to content

Commit

Permalink
feat: add button for opening the daily pot
Browse files Browse the repository at this point in the history
  • Loading branch information
SplinterHead committed Dec 19, 2023
1 parent 09d098f commit a4e93be
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
10 changes: 9 additions & 1 deletion custom_components/honeygain/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from .config_flow import CannotConnect, InvalidAuth
from .const import DOMAIN, LOGGER, UPDATE_INTERVAL_MINS

PLATFORMS: list[Platform] = [Platform.SENSOR]
PLATFORMS: list[Platform] = [Platform.SENSOR, Platform.BUTTON]

UPDATE_INTERVAL = timedelta(minutes=UPDATE_INTERVAL_MINS)

Expand Down Expand Up @@ -71,3 +71,11 @@ def update(self) -> None:
LOGGER.warning("Failed to connect to Honeygain for update")
except InvalidAuth:
LOGGER.warning("Failed to authenticate with Honeygain for update")

def open_daily_pot(self) -> None:
"""Open the daily pot if it's available."""
try:
self.honeygain.open_honeypot()
except Exception as exc:
LOGGER.error("Failed to open daily pot: %s", exc)
raise Exception from exc
46 changes: 46 additions & 0 deletions custom_components/honeygain/button.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"""Initialise HomeAssistant buttons for Honeygain."""
from homeassistant.components.button import ButtonEntity, ButtonEntityDescription
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from . import HoneygainData
from .const import DOMAIN


async def async_setup_entry(
hass: HomeAssistant,
entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Button set up for HoneyGain."""
honeygain_data: HoneygainData = hass.data[DOMAIN][entry.entry_id]
buttons: list[ButtonEntity] = [HoneygainPotButton(hass, honeygain_data)]
async_add_entities(buttons)


class HoneygainPotButton(ButtonEntity):
"""Generate buttons for Honeygain actions."""

hass: HomeAssistant
button_description: ButtonEntityDescription
_honeygain_data: HoneygainData

def __init__(self, hass: HomeAssistant, honeygain_data: HoneygainData) -> None:
"""Initialise a button."""
self.hass = hass
self._honeygain_data = honeygain_data
self.button_description = ButtonEntityDescription(
key="open_lucky_pot",
name="Open lucky pot",
icon="mdi:gift-open",
)
self.entity_id = f"button.{self.button_description.key}"
self._attr_name = self.button_description.name
self._attr_icon = self.button_description.icon
self._attr_unique_id = f"honeygain-{self._honeygain_data.user['referral_code']}-{self.button_description.key}"

def press(self) -> None:
"""Handle the button press."""
self._honeygain_data.open_daily_pot()
self._honeygain_data.update()

0 comments on commit a4e93be

Please sign in to comment.