* Add ZHA core typing helper. * Add aux_channels to ZHA rule matching. * Add match rule claim_channels() method. * Expose underlying zigpy device. * Not sure we need this one. * Move "base" channels. * Framework for channel discovery. * Make DEVICE_CLASS and REMOTE_DEVICE_TYPE default dicts. * Remove attribute reporting configuration registry. * Refactor channels. - Refactor zha events - Use compound IDs and unique_ids - Refactor signal dispatching on attribute updates * Use unique id compatible with entities unique ids. * Refactor ZHA Entity registry. Let match rule to check for the match. * Refactor discovery to use new channels. * Cleanup ZDO channel. Remove unused zha store call. * Handle channel configuration and initialization. * Refactor ZHA Device to use new channels. * Refactor ZHA Gateway to use new discovery framework. Use hass.data for entity info intermediate store. * Don't keep entities in hass.data. * ZHA gateway new discovery framework. * Refactor ZHA platform loading. * Don't update ZHA entities, when restoring from zigpy. * ZHA entity discover tests. * Add AnalogInput sensor. * Remove 0xFC02 based entity from Keen smart vents. * Clean up IAS channels. * Refactor entity restoration. * Fix lumi.router entities name. * Rename EndpointsChannel to ChannelPool. * Make Channels.pools a list. * Fix cover test. * Fix FakeDevice class. * Fix device actions. * Fix channels typing. * Revert update_before_add=False * Refactor channel class matching. * Use a helper function for adding entities. * Make Pylint happy. * Rebase cleanup. * Update coverage for ZHA device type overrides. * Use cluster_id for single output cluster registry. * Remove ZHA typing from coverage. * Fix tests. * Address comments. * Address comments.
100 lines
3.2 KiB
Python
100 lines
3.2 KiB
Python
"""Support for the ZHA platform."""
|
|
import functools
|
|
import logging
|
|
import time
|
|
|
|
from homeassistant.components.device_tracker import DOMAIN, SOURCE_TYPE_ROUTER
|
|
from homeassistant.components.device_tracker.config_entry import ScannerEntity
|
|
from homeassistant.core import callback
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
|
|
|
from .core import discovery
|
|
from .core.const import (
|
|
CHANNEL_POWER_CONFIGURATION,
|
|
DATA_ZHA,
|
|
DATA_ZHA_DISPATCHERS,
|
|
SIGNAL_ADD_ENTITIES,
|
|
SIGNAL_ATTR_UPDATED,
|
|
)
|
|
from .core.registries import ZHA_ENTITIES
|
|
from .entity import ZhaEntity
|
|
from .sensor import Battery
|
|
|
|
STRICT_MATCH = functools.partial(ZHA_ENTITIES.strict_match, DOMAIN)
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
|
"""Set up the Zigbee Home Automation device tracker from config entry."""
|
|
entities_to_create = hass.data[DATA_ZHA][DOMAIN] = []
|
|
|
|
unsub = async_dispatcher_connect(
|
|
hass,
|
|
SIGNAL_ADD_ENTITIES,
|
|
functools.partial(
|
|
discovery.async_add_entities, async_add_entities, entities_to_create
|
|
),
|
|
)
|
|
hass.data[DATA_ZHA][DATA_ZHA_DISPATCHERS].append(unsub)
|
|
|
|
|
|
@STRICT_MATCH(channel_names=CHANNEL_POWER_CONFIGURATION)
|
|
class ZHADeviceScannerEntity(ScannerEntity, ZhaEntity):
|
|
"""Represent a tracked device."""
|
|
|
|
def __init__(self, unique_id, zha_device, channels, **kwargs):
|
|
"""Initialize the ZHA device tracker."""
|
|
super().__init__(unique_id, zha_device, channels, **kwargs)
|
|
self._battery_channel = self.cluster_channels.get(CHANNEL_POWER_CONFIGURATION)
|
|
self._connected = False
|
|
self._keepalive_interval = 60
|
|
self._should_poll = True
|
|
self._battery_level = None
|
|
|
|
async def async_added_to_hass(self):
|
|
"""Run when about to be added to hass."""
|
|
await super().async_added_to_hass()
|
|
if self._battery_channel:
|
|
await self.async_accept_signal(
|
|
self._battery_channel,
|
|
SIGNAL_ATTR_UPDATED,
|
|
self.async_battery_percentage_remaining_updated,
|
|
)
|
|
|
|
async def async_update(self):
|
|
"""Handle polling."""
|
|
if self.zha_device.last_seen is None:
|
|
self._connected = False
|
|
else:
|
|
difference = time.time() - self.zha_device.last_seen
|
|
if difference > self._keepalive_interval:
|
|
self._connected = False
|
|
else:
|
|
self._connected = True
|
|
|
|
@property
|
|
def is_connected(self):
|
|
"""Return true if the device is connected to the network."""
|
|
return self._connected
|
|
|
|
@property
|
|
def source_type(self):
|
|
"""Return the source type, eg gps or router, of the device."""
|
|
return SOURCE_TYPE_ROUTER
|
|
|
|
@callback
|
|
def async_battery_percentage_remaining_updated(self, value):
|
|
"""Handle tracking."""
|
|
self.debug("battery_percentage_remaining updated: %s", value)
|
|
self._connected = True
|
|
self._battery_level = Battery.formatter(value)
|
|
self.async_schedule_update_ha_state()
|
|
|
|
@property
|
|
def battery_level(self):
|
|
"""Return the battery level of the device.
|
|
|
|
Percentage from 0-100.
|
|
"""
|
|
return self._battery_level
|