Add geniushub sensor and binary_sensor (#23811)
* Initial commit * add lastComms and de-lint * dummy commit * dummy commit 2 * refactor to temp in favour of battery * back to battery, and no temp * use snake_case * Bump client * only v3 API exposes device attributes * delint * delint2 * Change GeniusSwitch to GensiusBinarySensor
This commit is contained in:
parent
94a2fd542e
commit
18149dcb8c
5 changed files with 163 additions and 2 deletions
|
@ -60,6 +60,11 @@ async def async_setup(hass, hass_config):
|
|||
hass.async_create_task(async_load_platform(
|
||||
hass, platform, DOMAIN, {}, hass_config))
|
||||
|
||||
if not data._client._api_v1: # pylint: disable=protected-access
|
||||
for platform in ['sensor', 'binary_sensor']:
|
||||
hass.async_create_task(async_load_platform(
|
||||
hass, platform, DOMAIN, {}, hass_config))
|
||||
|
||||
return True
|
||||
|
||||
|
||||
|
|
74
homeassistant/components/geniushub/binary_sensor.py
Normal file
74
homeassistant/components/geniushub/binary_sensor.py
Normal file
|
@ -0,0 +1,74 @@
|
|||
"""Support for Genius Hub binary_sensor devices."""
|
||||
from datetime import datetime
|
||||
import logging
|
||||
|
||||
from homeassistant.components.binary_sensor import BinarySensorDevice
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
|
||||
from . import DOMAIN
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
GH_IS_SWITCH = ['Dual Channel Receiver', 'Electric Switch', 'Smart Plug']
|
||||
|
||||
|
||||
async def async_setup_platform(hass, config, async_add_entities,
|
||||
discovery_info=None):
|
||||
"""Set up the Genius Hub sensor entities."""
|
||||
client = hass.data[DOMAIN]['client']
|
||||
|
||||
switches = [GeniusBinarySensor(client, d)
|
||||
for d in client.hub.device_objs if d.type[:21] in GH_IS_SWITCH]
|
||||
|
||||
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
|
||||
|
||||
if device.type[:21] == 'Dual Channel Receiver':
|
||||
self._name = 'Dual Channel Receiver {}'.format(device.id)
|
||||
else:
|
||||
self._name = '{} {}'.format(device.type, device.id)
|
||||
|
||||
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."""
|
||||
return self._device.state['outputOnOff']
|
||||
|
||||
@property
|
||||
def device_state_attributes(self):
|
||||
"""Return the device state attributes."""
|
||||
attrs = {}
|
||||
attrs['assigned_zone'] = self._device.assignedZones[0]['name']
|
||||
|
||||
last_comms = self._device._info_raw['childValues']['lastComms']['val'] # noqa; pylint: disable=protected-access
|
||||
if last_comms != 0:
|
||||
attrs['last_comms'] = datetime.utcfromtimestamp(
|
||||
last_comms).isoformat()
|
||||
|
||||
return {**attrs}
|
|
@ -3,7 +3,7 @@
|
|||
"name": "Genius Hub",
|
||||
"documentation": "https://www.home-assistant.io/components/geniushub",
|
||||
"requirements": [
|
||||
"geniushub-client==0.4.6"
|
||||
"geniushub-client==0.4.7"
|
||||
],
|
||||
"dependencies": [],
|
||||
"codeowners": ["@zxdavb"]
|
||||
|
|
82
homeassistant/components/geniushub/sensor.py
Normal file
82
homeassistant/components/geniushub/sensor.py
Normal file
|
@ -0,0 +1,82 @@
|
|||
"""Support for Genius Hub sensor devices."""
|
||||
from datetime import datetime
|
||||
import logging
|
||||
|
||||
from homeassistant.const import DEVICE_CLASS_BATTERY
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
from homeassistant.helpers.entity import Entity
|
||||
|
||||
from . import DOMAIN
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
GH_HAS_BATTERY = [
|
||||
'Room Thermostat', 'Genius Valve', 'Room Sensor', 'Radiator Valve']
|
||||
|
||||
|
||||
async def async_setup_platform(hass, config, async_add_entities,
|
||||
discovery_info=None):
|
||||
"""Set up the Genius Hub sensor entities."""
|
||||
client = hass.data[DOMAIN]['client']
|
||||
|
||||
sensors = [GeniusDevice(client, d)
|
||||
for d in client.hub.device_objs if d.type in GH_HAS_BATTERY]
|
||||
|
||||
async_add_entities(sensors)
|
||||
|
||||
|
||||
class GeniusDevice(Entity):
|
||||
"""Representation of a Genius Hub sensor."""
|
||||
|
||||
def __init__(self, client, device):
|
||||
"""Initialize the sensor."""
|
||||
self._client = client
|
||||
self._device = device
|
||||
|
||||
self._name = '{} {}'.format(device.type, device.id)
|
||||
|
||||
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 device_class(self):
|
||||
"""Return the device class of the sensor."""
|
||||
return DEVICE_CLASS_BATTERY
|
||||
|
||||
@property
|
||||
def unit_of_measurement(self):
|
||||
"""Return the unit of measurement of the sensor."""
|
||||
return '%'
|
||||
|
||||
@property
|
||||
def should_poll(self) -> bool:
|
||||
"""Return False as the geniushub devices should not be polled."""
|
||||
return False
|
||||
|
||||
@property
|
||||
def state(self):
|
||||
"""Return the state of the sensor."""
|
||||
return self._device.state['batteryLevel']
|
||||
|
||||
@property
|
||||
def device_state_attributes(self):
|
||||
"""Return the device state attributes."""
|
||||
attrs = {}
|
||||
attrs['assigned_zone'] = self._device.assignedZones[0]['name']
|
||||
|
||||
last_comms = self._device._info_raw['childValues']['lastComms']['val'] # noqa; pylint: disable=protected-access
|
||||
attrs['last_comms'] = datetime.utcfromtimestamp(
|
||||
last_comms).isoformat()
|
||||
|
||||
return {**attrs}
|
|
@ -477,7 +477,7 @@ gearbest_parser==1.0.7
|
|||
geizhals==0.0.9
|
||||
|
||||
# homeassistant.components.geniushub
|
||||
geniushub-client==0.4.6
|
||||
geniushub-client==0.4.7
|
||||
|
||||
# homeassistant.components.geo_json_events
|
||||
# homeassistant.components.nsw_rural_fire_service_feed
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue