Skip to content

Commit

Permalink
Merge pull request #10 from SplinterHead/feat/today_stats
Browse files Browse the repository at this point in the history
feat: add sensors for current day stats
  • Loading branch information
SplinterHead authored Sep 27, 2024
2 parents ddf1e72 + 8c82d9c commit b7c1cec
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@ This integration uses a config flow to collect credentials
The integration will create sensor entities for metrics that relate to your account:

- Account balance
- Today's current earnings
- Number of active devices
- Today's earnings
- Today's credits
- Today's shared bandwidth
- Today's referral earnings
- Today's Lucky Pot earnings

### Button
The integration exposes a button for automating tasks:
Expand Down
2 changes: 2 additions & 0 deletions custom_components/honeygain/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ def __init__(self, honeygain: HoneyGain) -> None:
self.user: dict = {}
self.balances: dict = {}
self.stats: dict = {}
self.today_stats: dict = {}

@Throttle(UPDATE_INTERVAL)
def update(self) -> None:
Expand All @@ -67,6 +68,7 @@ def update(self) -> None:
self.user = self.honeygain.me()
self.balances = self.honeygain.balances()
self.stats = self.honeygain.stats()
self.today_stats = self.honeygain.stats_today()
except CannotConnect:
LOGGER.warning("Failed to connect to Honeygain for update")
except InvalidAuth:
Expand Down
42 changes: 37 additions & 5 deletions custom_components/honeygain/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
SensorStateClass,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CURRENCY_DOLLAR
from homeassistant.const import CURRENCY_DOLLAR, DATA_MEGABYTES
from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceEntryType
from homeassistant.helpers.entity import DeviceInfo
Expand All @@ -26,6 +26,7 @@ class SensorValueEntityDescription(SensorEntityDescription):


HONEYGAIN_SENSORS: list[SensorValueEntityDescription] = [
# Account Balance
SensorValueEntityDescription(
key="account_balance",
name="Account balance",
Expand All @@ -34,6 +35,14 @@ class SensorValueEntityDescription(SensorEntityDescription):
native_unit_of_measurement=CURRENCY_DOLLAR,
value=lambda x: f'{x.balances.get("payout").get("usd_cents") / 100:.2f}',
),
# Active Devices
SensorValueEntityDescription(
key="active_devices",
name="Active device count",
icon="mdi:server-network",
value=lambda x: x.user.get("active_devices_count"),
),
# Daily Stats
SensorValueEntityDescription(
key="today_earnings",
name="Today's earnings",
Expand All @@ -43,10 +52,33 @@ class SensorValueEntityDescription(SensorEntityDescription):
value=lambda x: f'{x.balances.get("realtime").get("usd_cents") / 100:.2f}',
),
SensorValueEntityDescription(
key="active_devices",
name="Active device count",
icon="mdi:server-network",
value=lambda x: x.user.get("active_devices_count"),
key="today_credits",
name="Today's credits",
icon="mdi:calendar-today",
state_class=SensorStateClass.TOTAL,
value=lambda x: x.today_stats.get("gathering").get("credits"),
),
SensorValueEntityDescription(
key="today_bandwidth",
name="Today's shared bandwidth",
icon="mdi:upload",
state_class=SensorStateClass.TOTAL,
native_unit_of_measurement=DATA_MEGABYTES,
value=lambda x: f'{round(x.today_stats.get("gathering").get("bytes"), -4) / 1000000}',
),
SensorValueEntityDescription(
key="today_referral_credits",
name="Today's referral credits",
icon="mdi:account-multiple",
state_class=SensorStateClass.TOTAL,
value=lambda x: x.today_stats.get("referral").get("credits"),
),
SensorValueEntityDescription(
key="today_lucky_pot_credits",
name="Today's Lucky Pot credits",
icon="mdi:gift-open",
state_class=SensorStateClass.TOTAL,
value=lambda x: x.today_stats.get("winning").get("credits"),
),
]

Expand Down

0 comments on commit b7c1cec

Please sign in to comment.