Skip to content

Commit

Permalink
Add online binary sensor entity for supported devices
Browse files Browse the repository at this point in the history
  • Loading branch information
natekspencer committed Dec 9, 2022
1 parent 7fd8b98 commit ef7311c
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions custom_components/vivint/binary_sensor.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""Support for Vivint binary sensors."""
from __future__ import annotations

from datetime import datetime, timedelta

from vivintpy.devices import VivintDevice
Expand All @@ -15,6 +17,7 @@
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import CALLBACK_TYPE, HomeAssistant, callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity import EntityCategory
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.event import async_call_later
from homeassistant.util.dt import utcnow
Expand Down Expand Up @@ -51,6 +54,18 @@ async def async_setup_entry(
entity_description=ENTITY_DESCRIPTION_MOTION,
)
)
elif hasattr(device, "node_online"):
entities.append(
VivintOnlineBinarySensorEntity(
device=device, hub=hub, key="node_online"
)
)
elif hasattr(device, "is_online"):
entities.append(
VivintOnlineBinarySensorEntity(
device=device, hub=hub, key="is_online"
)
)

if not entities:
return
Expand Down Expand Up @@ -199,3 +214,27 @@ def async_cancel_motion_stopped_callback(self) -> None:
if self._motion_stopped_callback is not None:
self._motion_stopped_callback()
self._motion_stopped_callback = None


class VivintOnlineBinarySensorEntity(VivintEntity, BinarySensorEntity):
"""Vivint online binary sensor entity."""

_attr_device_class = BinarySensorDeviceClass.CONNECTIVITY
_attr_entity_category = EntityCategory.DIAGNOSTIC
_attr_has_entity_name = True
_attr_name = "Online"

def __init__(self, device: VivintDevice, hub: VivintHub, key: str) -> None:
"""Initialize a Vivint online binary sensor entity."""
super().__init__(device=device, hub=hub)
self._key = key

@property
def unique_id(self) -> str:
"""Return a unique ID."""
return f"{self.device.alarm_panel.id}-{self.device.id}-online"

@property
def is_on(self) -> bool:
"""Return true if the binary sensor is on."""
return getattr(self.device, self._key)

0 comments on commit ef7311c

Please sign in to comment.