Skip to content

Commit a4e93be

Browse files
committed
feat: add button for opening the daily pot
1 parent 09d098f commit a4e93be

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

custom_components/honeygain/__init__.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from .config_flow import CannotConnect, InvalidAuth
1313
from .const import DOMAIN, LOGGER, UPDATE_INTERVAL_MINS
1414

15-
PLATFORMS: list[Platform] = [Platform.SENSOR]
15+
PLATFORMS: list[Platform] = [Platform.SENSOR, Platform.BUTTON]
1616

1717
UPDATE_INTERVAL = timedelta(minutes=UPDATE_INTERVAL_MINS)
1818

@@ -71,3 +71,11 @@ def update(self) -> None:
7171
LOGGER.warning("Failed to connect to Honeygain for update")
7272
except InvalidAuth:
7373
LOGGER.warning("Failed to authenticate with Honeygain for update")
74+
75+
def open_daily_pot(self) -> None:
76+
"""Open the daily pot if it's available."""
77+
try:
78+
self.honeygain.open_honeypot()
79+
except Exception as exc:
80+
LOGGER.error("Failed to open daily pot: %s", exc)
81+
raise Exception from exc
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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

Comments
 (0)