|
39 | 39 | import homeassistant.util.dt as dt_util
|
40 | 40 | from pyhilo.device import HiloDevice
|
41 | 41 | from pyhilo.event import Event
|
| 42 | +from pyhilo.util import from_utc_timestamp |
42 | 43 |
|
43 | 44 | from . import Hilo, HiloEntity
|
44 | 45 | from .const import (
|
@@ -97,6 +98,9 @@ def generate_entities_from_device(device, hilo, scan_interval):
|
97 | 98 | entities.append(
|
98 | 99 | HiloRewardSensor(hilo, device, scan_interval),
|
99 | 100 | )
|
| 101 | + entities.append( |
| 102 | + HiloNotificationSensor(hilo, device, scan_interval), |
| 103 | + ) |
100 | 104 | if device.has_attribute("current_temperature"):
|
101 | 105 | entities.append(TemperatureSensor(hilo, device))
|
102 | 106 | if device.has_attribute("co2"):
|
@@ -397,6 +401,76 @@ def extra_state_attributes(self):
|
397 | 401 | return {"wifi_signal": self._device.get_value("wifi_status", 0)}
|
398 | 402 |
|
399 | 403 |
|
| 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 | + |
400 | 474 | class HiloRewardSensor(HiloEntity, RestoreEntity, SensorEntity):
|
401 | 475 | """Hilo Reward sensor.
|
402 | 476 | Its state will be either the total amount rewarded this season.
|
@@ -450,7 +524,7 @@ async def _async_update(self):
|
450 | 524 | self._state = season.get("totalReward", 0)
|
451 | 525 | events = []
|
452 | 526 | for raw_event in season.get("events", []):
|
453 |
| - details = await self._hilo._api.get_events( |
| 527 | + details = await self._hilo._api.get_gd_events( |
454 | 528 | self._hilo.devices.location_id, event_id=raw_event["id"]
|
455 | 529 | )
|
456 | 530 | events.append(Event(**details).as_dict())
|
@@ -515,9 +589,9 @@ def extra_state_attributes(self):
|
515 | 589 |
|
516 | 590 | async def _async_update(self):
|
517 | 591 | 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) |
519 | 593 | for raw_event in events:
|
520 |
| - details = await self._hilo._api.get_events( |
| 594 | + details = await self._hilo._api.get_gd_events( |
521 | 595 | self._hilo.devices.location_id, event_id=raw_event["id"]
|
522 | 596 | )
|
523 | 597 | event = Event(**details)
|
|
0 commit comments