2020-09-21 16:08:35 +00:00
|
|
|
"""Base class for KNX devices."""
|
2021-03-31 15:04:22 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-03-26 15:10:55 +00:00
|
|
|
from typing import cast
|
|
|
|
|
2021-06-19 03:22:27 +00:00
|
|
|
from xknx.devices import Device as XknxDevice
|
2020-09-21 16:08:35 +00:00
|
|
|
|
|
|
|
from homeassistant.helpers.entity import Entity
|
|
|
|
|
2021-03-26 15:10:55 +00:00
|
|
|
from . import KNXModule
|
2020-09-21 16:08:35 +00:00
|
|
|
from .const import DOMAIN
|
|
|
|
|
|
|
|
|
|
|
|
class KnxEntity(Entity):
|
|
|
|
"""Representation of a KNX entity."""
|
|
|
|
|
2021-06-26 12:30:36 +00:00
|
|
|
_attr_should_poll = False
|
|
|
|
|
2021-03-26 15:10:55 +00:00
|
|
|
def __init__(self, device: XknxDevice) -> None:
|
2020-09-21 16:08:35 +00:00
|
|
|
"""Set up device."""
|
|
|
|
self._device = device
|
|
|
|
|
|
|
|
@property
|
2021-03-26 15:10:55 +00:00
|
|
|
def name(self) -> str:
|
2020-09-21 16:08:35 +00:00
|
|
|
"""Return the name of the KNX device."""
|
|
|
|
return self._device.name
|
|
|
|
|
|
|
|
@property
|
2021-03-26 15:10:55 +00:00
|
|
|
def available(self) -> bool:
|
2020-09-21 16:08:35 +00:00
|
|
|
"""Return True if entity is available."""
|
2021-03-26 15:10:55 +00:00
|
|
|
knx_module = cast(KNXModule, self.hass.data[DOMAIN])
|
|
|
|
return knx_module.connected
|
2020-09-21 16:08:35 +00:00
|
|
|
|
2021-03-26 15:10:55 +00:00
|
|
|
async def async_update(self) -> None:
|
2020-09-21 16:08:35 +00:00
|
|
|
"""Request a state update from KNX bus."""
|
|
|
|
await self._device.sync()
|
|
|
|
|
2021-03-26 15:10:55 +00:00
|
|
|
async def after_update_callback(self, device: XknxDevice) -> None:
|
2020-09-21 16:08:35 +00:00
|
|
|
"""Call after device was updated."""
|
|
|
|
self.async_write_ha_state()
|
|
|
|
|
|
|
|
async def async_added_to_hass(self) -> None:
|
|
|
|
"""Store register state change callback."""
|
|
|
|
self._device.register_device_updated_cb(self.after_update_callback)
|
|
|
|
|
|
|
|
async def async_will_remove_from_hass(self) -> None:
|
|
|
|
"""Disconnect device object when removed."""
|
|
|
|
self._device.unregister_device_updated_cb(self.after_update_callback)
|