2019-05-14 21:30:26 +00:00
|
|
|
"""Support for Genius Hub binary_sensor devices."""
|
|
|
|
import logging
|
|
|
|
|
|
|
|
from homeassistant.components.binary_sensor import BinarySensorDevice
|
|
|
|
from homeassistant.core import callback
|
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
2019-07-31 17:44:09 +00:00
|
|
|
from homeassistant.util.dt import utc_from_timestamp
|
2019-05-14 21:30:26 +00:00
|
|
|
|
|
|
|
from . import DOMAIN
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
GH_IS_SWITCH = ["Dual Channel Receiver", "Electric Switch", "Smart Plug"]
|
2019-05-14 21:30:26 +00:00
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
2019-05-14 21:30:26 +00:00
|
|
|
"""Set up the Genius Hub sensor entities."""
|
2019-07-31 19:25:30 +00:00
|
|
|
client = hass.data[DOMAIN]["client"]
|
2019-05-14 21:30:26 +00:00
|
|
|
|
2019-06-17 16:27:06 +00:00
|
|
|
devices = [d for d in client.hub.device_objs if d.type is not None]
|
2019-07-31 19:25:30 +00:00
|
|
|
switches = [
|
|
|
|
GeniusBinarySensor(client, d) for d in devices if d.type[:21] in GH_IS_SWITCH
|
|
|
|
]
|
2019-05-14 21:30:26 +00:00
|
|
|
|
|
|
|
async_add_entities(switches)
|
|
|
|
|
|
|
|
|
|
|
|
class GeniusBinarySensor(BinarySensorDevice):
|
|
|
|
"""Representation of a Genius Hub binary_sensor."""
|
|
|
|
|
|
|
|
def __init__(self, client, device):
|
|
|
|
"""Initialize the binary sensor."""
|
|
|
|
self._client = client
|
|
|
|
self._device = device
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
if device.type[:21] == "Dual Channel Receiver":
|
|
|
|
self._name = "Dual Channel Receiver {}".format(device.id)
|
2019-05-14 21:30:26 +00:00
|
|
|
else:
|
2019-07-31 19:25:30 +00:00
|
|
|
self._name = "{} {}".format(device.type, device.id)
|
2019-05-14 21:30:26 +00:00
|
|
|
|
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""Set up a listener when this entity is added to HA."""
|
|
|
|
async_dispatcher_connect(self.hass, DOMAIN, self._refresh)
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def _refresh(self):
|
|
|
|
self.async_schedule_update_ha_state(force_refresh=True)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the sensor."""
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def should_poll(self) -> bool:
|
|
|
|
"""Return False as the geniushub devices should not be polled."""
|
|
|
|
return False
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return the status of the sensor."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return self._device.state["outputOnOff"]
|
2019-05-14 21:30:26 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Return the device state attributes."""
|
|
|
|
attrs = {}
|
2019-07-31 19:25:30 +00:00
|
|
|
attrs["assigned_zone"] = self._device.assignedZones[0]["name"]
|
2019-05-14 21:30:26 +00:00
|
|
|
|
2019-07-31 19:46:17 +00:00
|
|
|
# noqa; pylint: disable=protected-access
|
|
|
|
last_comms = self._device._raw_json["childValues"]["lastComms"]["val"]
|
2019-05-14 21:30:26 +00:00
|
|
|
if last_comms != 0:
|
2019-07-31 19:25:30 +00:00
|
|
|
attrs["last_comms"] = utc_from_timestamp(last_comms).isoformat()
|
2019-05-14 21:30:26 +00:00
|
|
|
|
|
|
|
return {**attrs}
|