Skip to content

Commit

Permalink
Adding Hilo notification sensor
Browse files Browse the repository at this point in the history
  • Loading branch information
valleedelisle committed Jan 16, 2022
1 parent 7dfacf9 commit a3ea10c
Showing 1 changed file with 77 additions and 3 deletions.
80 changes: 77 additions & 3 deletions custom_components/hilo/sensor.py
Expand Up @@ -39,6 +39,7 @@
import homeassistant.util.dt as dt_util
from pyhilo.device import HiloDevice
from pyhilo.event import Event
from pyhilo.util import from_utc_timestamp

from . import Hilo, HiloEntity
from .const import (
Expand Down Expand Up @@ -97,6 +98,9 @@ def generate_entities_from_device(device, hilo, scan_interval):
entities.append(
HiloRewardSensor(hilo, device, scan_interval),
)
entities.append(
HiloNotificationSensor(hilo, device, scan_interval),
)
if device.has_attribute("current_temperature"):
entities.append(TemperatureSensor(hilo, device))
if device.has_attribute("co2"):
Expand Down Expand Up @@ -397,6 +401,76 @@ def extra_state_attributes(self):
return {"wifi_signal": self._device.get_value("wifi_status", 0)}


class HiloNotificationSensor(HiloEntity, RestoreEntity, SensorEntity):
"""Hilo Notification sensor.
Its state will be the number of notification waiting in the Hilo app.
"""

_attr_device_class = None
_attr_native_unit_of_measurement = None
_attr_state_class = None

def __init__(self, hilo, device, scan_interval):
self._attr_name = "Notifications Hilo"
super().__init__(hilo, name=self._attr_name, device=device)
self._attr_unique_id = slugify(self._attr_name)
LOG.debug(f"Setting up NotificationSensor entity: {self._attr_name}")
self.scan_interval = timedelta(seconds=scan_interval)
self._state = 0
self._notifications = []
self.async_update = Throttle(self.scan_interval)(self._async_update)

@property
def state(self):
try:
return int(self._state)
except ValueError:
return 0

@property
def icon(self):
if not self._device.available:
return "mdi:lan-disconnect"
if self.state > 0:
return "mdi:bell-alert"
return "mdi:bell-outline"

@property
def should_poll(self):
return True

@property
def extra_state_attributes(self):
return {"notifications": self._notifications}

async def async_added_to_hass(self):
"""Handle entity about to be added to hass event."""
await super().async_added_to_hass()
last_state = await self.async_get_last_state()
if last_state:
self._last_update = dt_util.utcnow()
self._state = last_state.state

async def _async_update(self):
self._notifications = []
for notification in await self._hilo._api.get_event_notifications(
self._hilo.devices.location_id
):
if not notification.get("viewed"):
continue
self._notifications.append(
{
"type_id": notification.get("eventTypeId"),
"event_id": notification.get("eventId"),
"device_id": notification.get("deviceId"),
"date": from_utc_timestamp(notification.get("notificationDateUTC")),
"title": notification.get("notificationTitle"),
"body": notification.get("notificationBody"),
}
)
self._state = len(self._notifications)


class HiloRewardSensor(HiloEntity, RestoreEntity, SensorEntity):
"""Hilo Reward sensor.
Its state will be either the total amount rewarded this season.
Expand Down Expand Up @@ -450,7 +524,7 @@ async def _async_update(self):
self._state = season.get("totalReward", 0)
events = []
for raw_event in season.get("events", []):
details = await self._hilo._api.get_events(
details = await self._hilo._api.get_gd_events(
self._hilo.devices.location_id, event_id=raw_event["id"]
)
events.append(Event(**details).as_dict())
Expand Down Expand Up @@ -515,9 +589,9 @@ def extra_state_attributes(self):

async def _async_update(self):
self._next_events = []
events = await self._hilo._api.get_events(self._hilo.devices.location_id)
events = await self._hilo._api.get_gd_events(self._hilo.devices.location_id)
for raw_event in events:
details = await self._hilo._api.get_events(
details = await self._hilo._api.get_gd_events(
self._hilo.devices.location_id, event_id=raw_event["id"]
)
event = Event(**details)
Expand Down

0 comments on commit a3ea10c

Please sign in to comment.