Skip to content

Commit a3ea10c

Browse files
committed
Adding Hilo notification sensor
1 parent 7dfacf9 commit a3ea10c

File tree

1 file changed

+77
-3
lines changed

1 file changed

+77
-3
lines changed

custom_components/hilo/sensor.py

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import homeassistant.util.dt as dt_util
4040
from pyhilo.device import HiloDevice
4141
from pyhilo.event import Event
42+
from pyhilo.util import from_utc_timestamp
4243

4344
from . import Hilo, HiloEntity
4445
from .const import (
@@ -97,6 +98,9 @@ def generate_entities_from_device(device, hilo, scan_interval):
9798
entities.append(
9899
HiloRewardSensor(hilo, device, scan_interval),
99100
)
101+
entities.append(
102+
HiloNotificationSensor(hilo, device, scan_interval),
103+
)
100104
if device.has_attribute("current_temperature"):
101105
entities.append(TemperatureSensor(hilo, device))
102106
if device.has_attribute("co2"):
@@ -397,6 +401,76 @@ def extra_state_attributes(self):
397401
return {"wifi_signal": self._device.get_value("wifi_status", 0)}
398402

399403

404+
class HiloNotificationSensor(HiloEntity, RestoreEntity, SensorEntity):
405+
"""Hilo Notification sensor.
406+
Its state will be the number of notification waiting in the Hilo app.
407+
"""
408+
409+
_attr_device_class = None
410+
_attr_native_unit_of_measurement = None
411+
_attr_state_class = None
412+
413+
def __init__(self, hilo, device, scan_interval):
414+
self._attr_name = "Notifications Hilo"
415+
super().__init__(hilo, name=self._attr_name, device=device)
416+
self._attr_unique_id = slugify(self._attr_name)
417+
LOG.debug(f"Setting up NotificationSensor entity: {self._attr_name}")
418+
self.scan_interval = timedelta(seconds=scan_interval)
419+
self._state = 0
420+
self._notifications = []
421+
self.async_update = Throttle(self.scan_interval)(self._async_update)
422+
423+
@property
424+
def state(self):
425+
try:
426+
return int(self._state)
427+
except ValueError:
428+
return 0
429+
430+
@property
431+
def icon(self):
432+
if not self._device.available:
433+
return "mdi:lan-disconnect"
434+
if self.state > 0:
435+
return "mdi:bell-alert"
436+
return "mdi:bell-outline"
437+
438+
@property
439+
def should_poll(self):
440+
return True
441+
442+
@property
443+
def extra_state_attributes(self):
444+
return {"notifications": self._notifications}
445+
446+
async def async_added_to_hass(self):
447+
"""Handle entity about to be added to hass event."""
448+
await super().async_added_to_hass()
449+
last_state = await self.async_get_last_state()
450+
if last_state:
451+
self._last_update = dt_util.utcnow()
452+
self._state = last_state.state
453+
454+
async def _async_update(self):
455+
self._notifications = []
456+
for notification in await self._hilo._api.get_event_notifications(
457+
self._hilo.devices.location_id
458+
):
459+
if not notification.get("viewed"):
460+
continue
461+
self._notifications.append(
462+
{
463+
"type_id": notification.get("eventTypeId"),
464+
"event_id": notification.get("eventId"),
465+
"device_id": notification.get("deviceId"),
466+
"date": from_utc_timestamp(notification.get("notificationDateUTC")),
467+
"title": notification.get("notificationTitle"),
468+
"body": notification.get("notificationBody"),
469+
}
470+
)
471+
self._state = len(self._notifications)
472+
473+
400474
class HiloRewardSensor(HiloEntity, RestoreEntity, SensorEntity):
401475
"""Hilo Reward sensor.
402476
Its state will be either the total amount rewarded this season.
@@ -450,7 +524,7 @@ async def _async_update(self):
450524
self._state = season.get("totalReward", 0)
451525
events = []
452526
for raw_event in season.get("events", []):
453-
details = await self._hilo._api.get_events(
527+
details = await self._hilo._api.get_gd_events(
454528
self._hilo.devices.location_id, event_id=raw_event["id"]
455529
)
456530
events.append(Event(**details).as_dict())
@@ -515,9 +589,9 @@ def extra_state_attributes(self):
515589

516590
async def _async_update(self):
517591
self._next_events = []
518-
events = await self._hilo._api.get_events(self._hilo.devices.location_id)
592+
events = await self._hilo._api.get_gd_events(self._hilo.devices.location_id)
519593
for raw_event in events:
520-
details = await self._hilo._api.get_events(
594+
details = await self._hilo._api.get_gd_events(
521595
self._hilo.devices.location_id, event_id=raw_event["id"]
522596
)
523597
event = Event(**details)

0 commit comments

Comments
 (0)