Skip to content

Commit

Permalink
occupancy sensor
Browse files Browse the repository at this point in the history
  • Loading branch information
dmulcahey committed Dec 8, 2018
1 parent 3ea5e71 commit e3b99d9
Showing 1 changed file with 68 additions and 2 deletions.
70 changes: 68 additions & 2 deletions homeassistant/components/binary_sensor/zha.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
0x002b: 'gas',
0x002d: 'vibration',
}
DEVICE_CLASS_OCCUPANCY = 'occupancy'


async def async_setup_platform(hass, config, async_add_entities,
Expand Down Expand Up @@ -57,9 +58,15 @@ async def _async_setup_entities(hass, config_entry, async_add_entities,
entities = []
for discovery_info in discovery_infos:
from zigpy.zcl.clusters.general import OnOff
from zigpy.zcl.clusters.measurement import OccupancySensing
from zigpy.zcl.clusters.security import IasZone
if IasZone.cluster_id in discovery_info['in_clusters']:
entities.append(await _async_setup_iaszone(discovery_info))
elif OccupancySensing.cluster_id in discovery_info['in_clusters']:
entities.append(await _async_setup_occupancy(
DEVICE_CLASS_OCCUPANCY,
discovery_info
))
elif OnOff.cluster_id in discovery_info['out_clusters']:
entities.append(await _async_setup_remote(discovery_info))

Expand All @@ -82,7 +89,7 @@ async def _async_setup_iaszone(discovery_info):
# If we fail to read from the device, use a non-specific class
pass

return BinarySensor(device_class, **discovery_info)
return IasZoneSensor(device_class, **discovery_info)


async def _async_setup_remote(discovery_info):
Expand All @@ -107,7 +114,29 @@ async def _async_setup_remote(discovery_info):
return remote


class BinarySensor(ZhaEntity, BinarySensorDevice):
async def _async_setup_occupancy(device_class, discovery_info):

sensor = BinarySensor(device_class, **discovery_info)

if discovery_info['new_join']:
from zigpy.exceptions import ZigbeeException
from zigpy.zcl.clusters.measurement import OccupancySensing
endpoint = discovery_info['endpoint']
in_clusters = discovery_info['in_clusters']
cluster = in_clusters[OccupancySensing.cluster_id]
await helpers.configure_reporting(
sensor.entity_id,
cluster,
0,
min_report=0,
max_report=900,
reportable_change=1
)

return sensor


class IasZoneSensor(ZhaEntity, BinarySensorDevice):
"""The ZHA Binary Sensor."""

_domain = DOMAIN
Expand Down Expand Up @@ -288,3 +317,40 @@ async def async_update(self):
only_cache=(not self._initialized)
)
self._state = result.get('on_off', self._state)


class BinarySensor(ZhaEntity, BinarySensorDevice):
"""ZHA switch."""

_domain = DOMAIN
_device_class = None
value_attribute = 0

def __init__(self, device_class, **kwargs):
"""Initialize the ZHA binary sensor."""
super().__init__(**kwargs)
self._device_class = device_class

def attribute_updated(self, attribute, value):
"""Handle attribute update from device."""
_LOGGER.debug("Attribute updated: %s %s %s", self, attribute, value)
if attribute == self.value_attribute:
self._state = bool(value)
self.async_schedule_update_ha_state()

@property
def should_poll(self) -> bool:
"""Let zha handle polling."""
return False

@property
def is_on(self) -> bool:
"""Return if the switch is on based on the statemachine."""
if self._state is None:
return False
return self._state

@property
def device_class(self) -> str:
"""Return device class from component DEVICE_CLASSES."""
return self._device_class

0 comments on commit e3b99d9

Please sign in to comment.