-
-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into process-outgoing-telegrams
- Loading branch information
Showing
34 changed files
with
449 additions
and
422 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 16 additions & 52 deletions
68
home-assistant-plugin/custom_components/xknx/binary_sensor.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,43 @@ | ||
"""Support for KNX/IP binary sensors.""" | ||
import asyncio | ||
from typing import Any, Dict, Optional | ||
|
||
from xknx.devices import BinarySensor as XknxBinarySensor | ||
|
||
from homeassistant.components.binary_sensor import BinarySensorEntity | ||
from homeassistant.const import STATE_OFF, STATE_ON | ||
from homeassistant.core import callback | ||
from homeassistant.components.binary_sensor import DEVICE_CLASSES, BinarySensorEntity | ||
|
||
from .const import DATA_XKNX, DOMAIN | ||
from .const import ATTR_COUNTER, DOMAIN | ||
from .knx_entity import KnxEntity | ||
|
||
|
||
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None): | ||
"""Set up binary sensor(s) for KNX platform.""" | ||
entities = [] | ||
for device in hass.data[DATA_XKNX].xknx.devices: | ||
for device in hass.data[DOMAIN].xknx.devices: | ||
if isinstance(device, XknxBinarySensor): | ||
entities.append(KNXBinarySensor(device)) | ||
async_add_entities(entities) | ||
|
||
|
||
class KNXBinarySensor(BinarySensorEntity): | ||
class KNXBinarySensor(KnxEntity, BinarySensorEntity): | ||
"""Representation of a KNX binary sensor.""" | ||
|
||
def __init__(self, device: XknxBinarySensor): | ||
"""Initialize of KNX binary sensor.""" | ||
self.device = device | ||
|
||
@callback | ||
def async_register_callbacks(self): | ||
"""Register callbacks to update hass after device was changed.""" | ||
|
||
async def after_update_callback(device: XknxBinarySensor): | ||
"""Call after device was updated.""" | ||
self.async_write_ha_state() | ||
|
||
self.hass.bus.fire( | ||
f"{DOMAIN}_{self.entity_id}", | ||
{ | ||
# how often has the input sensor been pressed | ||
"counter": device.counter, | ||
"state": STATE_ON if device.state else STATE_OFF, | ||
}, | ||
) | ||
|
||
self.device.register_device_updated_cb(after_update_callback) | ||
|
||
async def async_added_to_hass(self): | ||
"""Store register state change callback.""" | ||
self.async_register_callbacks() | ||
|
||
async def async_update(self): | ||
"""Request a state update from KNX bus.""" | ||
await self.device.sync() | ||
|
||
@property | ||
def name(self): | ||
"""Return the name of the KNX device.""" | ||
return self.device.name | ||
|
||
@property | ||
def available(self): | ||
"""Return True if entity is available.""" | ||
return self.hass.data[DATA_XKNX].connected | ||
|
||
@property | ||
def should_poll(self): | ||
"""No polling needed within KNX.""" | ||
return False | ||
super().__init__(device) | ||
|
||
@property | ||
def device_class(self): | ||
"""Return the class of this sensor.""" | ||
return self.device.device_class | ||
if self._device.device_class in DEVICE_CLASSES: | ||
return self._device.device_class | ||
return None | ||
|
||
@property | ||
def is_on(self): | ||
"""Return true if the binary sensor is on.""" | ||
return self.device.is_on() | ||
return self._device.is_on() | ||
|
||
@property | ||
def device_state_attributes(self) -> Optional[Dict[str, Any]]: | ||
"""Return device specific state attributes.""" | ||
return {ATTR_COUNTER: self._device.counter} |
Oops, something went wrong.