2019-02-14 04:35:12 +00:00
|
|
|
"""Support KNX devices."""
|
2024-03-08 14:01:29 +00:00
|
|
|
|
2021-03-18 12:07:04 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2020-11-12 13:34:08 +00:00
|
|
|
import asyncio
|
2023-02-20 13:48:56 +00:00
|
|
|
import contextlib
|
2018-01-21 06:35:38 +00:00
|
|
|
import logging
|
2023-02-20 13:48:56 +00:00
|
|
|
from pathlib import Path
|
2016-07-10 17:36:54 +00:00
|
|
|
|
2016-09-14 06:03:30 +00:00
|
|
|
import voluptuous as vol
|
2019-10-22 05:38:21 +00:00
|
|
|
from xknx import XKNX
|
2021-10-14 17:54:48 +00:00
|
|
|
from xknx.core import XknxConnectionState
|
2021-01-22 14:27:51 +00:00
|
|
|
from xknx.core.telegram_queue import TelegramQueue
|
2024-01-07 22:26:46 +00:00
|
|
|
from xknx.dpt import DPTBase
|
2023-04-22 16:25:14 +00:00
|
|
|
from xknx.exceptions import ConversionError, CouldNotParseTelegram, XKNXException
|
2022-03-30 19:10:47 +00:00
|
|
|
from xknx.io import ConnectionConfig, ConnectionType, SecureConfig
|
2021-04-24 12:18:14 +00:00
|
|
|
from xknx.telegram import AddressFilter, Telegram
|
2024-01-07 22:26:46 +00:00
|
|
|
from xknx.telegram.address import DeviceGroupAddress, GroupAddress, InternalGroupAddress
|
|
|
|
from xknx.telegram.apci import GroupValueResponse, GroupValueWrite
|
2016-09-14 06:03:30 +00:00
|
|
|
|
2021-11-20 10:30:41 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2018-02-26 07:44:09 +00:00
|
|
|
from homeassistant.const import (
|
2021-11-06 18:31:25 +00:00
|
|
|
CONF_EVENT,
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_HOST,
|
|
|
|
CONF_PORT,
|
2021-11-06 18:31:25 +00:00
|
|
|
CONF_TYPE,
|
2019-07-31 19:25:30 +00:00
|
|
|
EVENT_HOMEASSISTANT_STOP,
|
2021-12-15 18:30:38 +00:00
|
|
|
Platform,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2024-01-07 22:26:46 +00:00
|
|
|
from homeassistant.core import Event, HomeAssistant
|
|
|
|
from homeassistant.exceptions import ConfigEntryNotReady
|
2017-09-07 07:11:55 +00:00
|
|
|
from homeassistant.helpers import discovery
|
2017-04-30 05:04:49 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2020-11-12 13:34:08 +00:00
|
|
|
from homeassistant.helpers.reload import async_integration_yaml_config
|
2022-03-30 19:10:47 +00:00
|
|
|
from homeassistant.helpers.storage import STORAGE_DIR
|
2021-03-27 21:20:11 +00:00
|
|
|
from homeassistant.helpers.typing import ConfigType
|
2020-08-26 16:03:03 +00:00
|
|
|
|
2021-06-01 06:59:23 +00:00
|
|
|
from .const import (
|
2021-11-20 10:30:41 +00:00
|
|
|
CONF_KNX_CONNECTION_TYPE,
|
2021-06-01 06:59:23 +00:00
|
|
|
CONF_KNX_EXPOSE,
|
|
|
|
CONF_KNX_INDIVIDUAL_ADDRESS,
|
2022-03-30 19:10:47 +00:00
|
|
|
CONF_KNX_KNXKEY_FILENAME,
|
|
|
|
CONF_KNX_KNXKEY_PASSWORD,
|
2022-03-29 14:46:02 +00:00
|
|
|
CONF_KNX_LOCAL_IP,
|
|
|
|
CONF_KNX_MCAST_GRP,
|
|
|
|
CONF_KNX_MCAST_PORT,
|
|
|
|
CONF_KNX_RATE_LIMIT,
|
|
|
|
CONF_KNX_ROUTE_BACK,
|
2021-06-01 06:59:23 +00:00
|
|
|
CONF_KNX_ROUTING,
|
2022-11-27 22:33:12 +00:00
|
|
|
CONF_KNX_ROUTING_BACKBONE_KEY,
|
|
|
|
CONF_KNX_ROUTING_SECURE,
|
|
|
|
CONF_KNX_ROUTING_SYNC_LATENCY_TOLERANCE,
|
2022-03-30 19:10:47 +00:00
|
|
|
CONF_KNX_SECURE_DEVICE_AUTHENTICATION,
|
|
|
|
CONF_KNX_SECURE_USER_ID,
|
|
|
|
CONF_KNX_SECURE_USER_PASSWORD,
|
2022-03-29 14:46:02 +00:00
|
|
|
CONF_KNX_STATE_UPDATER,
|
2023-05-22 16:09:59 +00:00
|
|
|
CONF_KNX_TELEGRAM_LOG_SIZE,
|
2021-06-01 06:59:23 +00:00
|
|
|
CONF_KNX_TUNNELING,
|
2022-01-26 05:36:07 +00:00
|
|
|
CONF_KNX_TUNNELING_TCP,
|
2022-03-30 19:10:47 +00:00
|
|
|
CONF_KNX_TUNNELING_TCP_SECURE,
|
2021-12-15 18:30:38 +00:00
|
|
|
DATA_HASS_CONFIG,
|
2021-11-20 10:30:41 +00:00
|
|
|
DATA_KNX_CONFIG,
|
2021-06-01 06:59:23 +00:00
|
|
|
DOMAIN,
|
|
|
|
KNX_ADDRESS,
|
2021-12-03 17:29:38 +00:00
|
|
|
SUPPORTED_PLATFORMS,
|
2023-05-22 16:09:59 +00:00
|
|
|
TELEGRAM_LOG_DEFAULT,
|
2021-06-01 06:59:23 +00:00
|
|
|
)
|
2023-03-19 13:13:52 +00:00
|
|
|
from .device import KNXInterfaceDevice
|
2021-03-26 15:10:55 +00:00
|
|
|
from .expose import KNXExposeSensor, KNXExposeTime, create_knx_exposure
|
2023-07-09 19:15:55 +00:00
|
|
|
from .project import STORAGE_KEY as PROJECT_STORAGE_KEY, KNXProject
|
2020-08-26 16:03:03 +00:00
|
|
|
from .schema import (
|
|
|
|
BinarySensorSchema,
|
2021-11-10 19:34:35 +00:00
|
|
|
ButtonSchema,
|
2020-08-26 16:03:03 +00:00
|
|
|
ClimateSchema,
|
|
|
|
CoverSchema,
|
2023-07-24 19:12:37 +00:00
|
|
|
DateSchema,
|
2023-07-25 09:04:05 +00:00
|
|
|
DateTimeSchema,
|
2021-11-06 18:31:25 +00:00
|
|
|
EventSchema,
|
2020-08-26 16:03:03 +00:00
|
|
|
ExposeSchema,
|
2021-02-10 08:09:34 +00:00
|
|
|
FanSchema,
|
2020-08-26 16:03:03 +00:00
|
|
|
LightSchema,
|
|
|
|
NotifySchema,
|
2021-06-23 15:20:49 +00:00
|
|
|
NumberSchema,
|
2020-08-26 16:03:03 +00:00
|
|
|
SceneSchema,
|
2021-06-24 08:54:04 +00:00
|
|
|
SelectSchema,
|
2020-08-26 16:03:03 +00:00
|
|
|
SensorSchema,
|
|
|
|
SwitchSchema,
|
2022-11-29 17:05:51 +00:00
|
|
|
TextSchema,
|
2023-06-28 13:19:32 +00:00
|
|
|
TimeSchema,
|
2020-08-31 09:38:52 +00:00
|
|
|
WeatherSchema,
|
2020-08-26 16:03:03 +00:00
|
|
|
)
|
2024-01-07 22:26:46 +00:00
|
|
|
from .services import register_knx_services
|
2023-07-09 19:15:55 +00:00
|
|
|
from .telegrams import STORAGE_KEY as TELEGRAMS_STORAGE_KEY, Telegrams
|
2023-05-10 22:13:22 +00:00
|
|
|
from .websocket import register_panel
|
2017-09-07 07:11:55 +00:00
|
|
|
|
2019-02-14 04:35:12 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2021-06-01 06:59:23 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONFIG_SCHEMA = vol.Schema(
|
|
|
|
{
|
2021-01-22 14:27:51 +00:00
|
|
|
DOMAIN: vol.All(
|
2021-11-06 18:31:25 +00:00
|
|
|
# deprecated since 2021.12
|
2022-03-29 14:46:02 +00:00
|
|
|
cv.deprecated(CONF_KNX_STATE_UPDATER),
|
|
|
|
cv.deprecated(CONF_KNX_RATE_LIMIT),
|
2021-11-20 10:30:41 +00:00
|
|
|
cv.deprecated(CONF_KNX_ROUTING),
|
|
|
|
cv.deprecated(CONF_KNX_TUNNELING),
|
|
|
|
cv.deprecated(CONF_KNX_INDIVIDUAL_ADDRESS),
|
2022-03-29 14:46:02 +00:00
|
|
|
cv.deprecated(CONF_KNX_MCAST_GRP),
|
|
|
|
cv.deprecated(CONF_KNX_MCAST_PORT),
|
|
|
|
cv.deprecated("event_filter"),
|
2021-03-10 20:32:22 +00:00
|
|
|
# deprecated since 2021.4
|
2021-04-08 12:54:43 +00:00
|
|
|
cv.deprecated("config_file"),
|
2021-03-01 10:55:55 +00:00
|
|
|
# deprecated since 2021.2
|
2022-03-29 14:46:02 +00:00
|
|
|
cv.deprecated("fire_event"),
|
|
|
|
cv.deprecated("fire_event_filter"),
|
2021-01-22 14:27:51 +00:00
|
|
|
vol.Schema(
|
|
|
|
{
|
2021-11-06 18:31:25 +00:00
|
|
|
**EventSchema.SCHEMA,
|
2021-06-01 06:59:23 +00:00
|
|
|
**ExposeSchema.platform_node(),
|
|
|
|
**BinarySensorSchema.platform_node(),
|
2021-11-10 19:34:35 +00:00
|
|
|
**ButtonSchema.platform_node(),
|
2021-06-01 06:59:23 +00:00
|
|
|
**ClimateSchema.platform_node(),
|
|
|
|
**CoverSchema.platform_node(),
|
2023-07-24 19:12:37 +00:00
|
|
|
**DateSchema.platform_node(),
|
2023-07-25 09:04:05 +00:00
|
|
|
**DateTimeSchema.platform_node(),
|
2021-06-01 06:59:23 +00:00
|
|
|
**FanSchema.platform_node(),
|
|
|
|
**LightSchema.platform_node(),
|
|
|
|
**NotifySchema.platform_node(),
|
2021-06-23 15:20:49 +00:00
|
|
|
**NumberSchema.platform_node(),
|
2021-06-01 06:59:23 +00:00
|
|
|
**SceneSchema.platform_node(),
|
2021-06-24 08:54:04 +00:00
|
|
|
**SelectSchema.platform_node(),
|
2021-06-01 06:59:23 +00:00
|
|
|
**SensorSchema.platform_node(),
|
|
|
|
**SwitchSchema.platform_node(),
|
2022-11-29 17:05:51 +00:00
|
|
|
**TextSchema.platform_node(),
|
2023-06-28 13:19:32 +00:00
|
|
|
**TimeSchema.platform_node(),
|
2021-06-01 06:59:23 +00:00
|
|
|
**WeatherSchema.platform_node(),
|
2021-01-22 14:27:51 +00:00
|
|
|
}
|
|
|
|
),
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
|
|
|
},
|
|
|
|
extra=vol.ALLOW_EXTRA,
|
|
|
|
)
|
|
|
|
|
2016-07-10 17:36:54 +00:00
|
|
|
|
2021-03-27 21:20:11 +00:00
|
|
|
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
2021-11-20 10:30:41 +00:00
|
|
|
"""Start the KNX integration."""
|
2021-12-15 18:30:38 +00:00
|
|
|
hass.data[DATA_HASS_CONFIG] = config
|
2021-11-20 10:30:41 +00:00
|
|
|
conf: ConfigType | None = config.get(DOMAIN)
|
|
|
|
|
|
|
|
if conf is None:
|
|
|
|
# If we have a config entry, setup is done by that config entry.
|
|
|
|
# If there is no config entry, this should fail.
|
|
|
|
return bool(hass.config_entries.async_entries(DOMAIN))
|
|
|
|
|
|
|
|
conf = dict(conf)
|
|
|
|
hass.data[DATA_KNX_CONFIG] = conf
|
2023-05-10 22:13:22 +00:00
|
|
|
|
2024-01-07 22:26:46 +00:00
|
|
|
register_knx_services(hass)
|
|
|
|
|
2021-11-20 10:30:41 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|
|
|
"""Load a config entry."""
|
2023-02-04 17:52:26 +00:00
|
|
|
# `config` is None when reloading the integration
|
|
|
|
# or no `knx` key in configuration.yaml
|
2022-03-29 14:46:02 +00:00
|
|
|
if (config := hass.data.get(DATA_KNX_CONFIG)) is None:
|
2022-01-17 04:44:21 +00:00
|
|
|
_conf = await async_integration_yaml_config(hass, DOMAIN)
|
|
|
|
if not _conf or DOMAIN not in _conf:
|
|
|
|
_LOGGER.warning(
|
|
|
|
"No `knx:` key found in configuration.yaml. See "
|
|
|
|
"https://www.home-assistant.io/integrations/knx/ "
|
|
|
|
"for KNX entity configuration documentation"
|
|
|
|
)
|
|
|
|
# generate defaults
|
2022-03-29 14:46:02 +00:00
|
|
|
config = CONFIG_SCHEMA({DOMAIN: {}})[DOMAIN]
|
2022-01-17 04:44:21 +00:00
|
|
|
else:
|
2022-03-29 14:46:02 +00:00
|
|
|
config = _conf[DOMAIN]
|
2016-07-10 17:36:54 +00:00
|
|
|
try:
|
2021-11-20 10:30:41 +00:00
|
|
|
knx_module = KNXModule(hass, config, entry)
|
2021-02-23 21:59:16 +00:00
|
|
|
await knx_module.start()
|
2017-09-07 07:11:55 +00:00
|
|
|
except XKNXException as ex:
|
2021-11-20 10:30:41 +00:00
|
|
|
raise ConfigEntryNotReady from ex
|
2017-07-07 05:21:40 +00:00
|
|
|
|
2022-03-29 14:46:02 +00:00
|
|
|
hass.data[DATA_KNX_CONFIG] = config
|
2021-11-20 10:30:41 +00:00
|
|
|
hass.data[DOMAIN] = knx_module
|
|
|
|
|
|
|
|
if CONF_KNX_EXPOSE in config:
|
|
|
|
for expose_config in config[CONF_KNX_EXPOSE]:
|
2021-02-23 21:59:16 +00:00
|
|
|
knx_module.exposures.append(
|
|
|
|
create_knx_exposure(hass, knx_module.xknx, expose_config)
|
2021-02-22 05:38:17 +00:00
|
|
|
)
|
2023-03-19 13:13:52 +00:00
|
|
|
# always forward sensor for system entities (telegram counter, etc.)
|
Ensure config entries are not unloaded while their platforms are setting up (#118767)
* Report non-awaited/non-locked config entry platform forwards
Its currently possible for config entries to be reloaded while their platforms
are being forwarded if platform forwards are not awaited or done after the
config entry is setup since the lock will not be held in this case.
In https://developers.home-assistant.io/blog/2022/07/08/config_entry_forwards
we advised to await platform forwards to ensure this does not happen, however
for sleeping devices and late discovered devices, platform forwards may happen
later.
If config platform forwards are happening during setup, they should be awaited
If config entry platform forwards are not happening during setup, instead
async_late_forward_entry_setups should be used which will hold the lock to
prevent the config entry from being unloaded while its platforms are being
setup
* Report non-awaited/non-locked config entry platform forwards
Its currently possible for config entries to be reloaded while their platforms
are being forwarded if platform forwards are not awaited or done after the
config entry is setup since the lock will not be held in this case.
In https://developers.home-assistant.io/blog/2022/07/08/config_entry_forwards
we advised to await platform forwards to ensure this does not happen, however
for sleeping devices and late discovered devices, platform forwards may happen
later.
If config platform forwards are happening during setup, they should be awaited
If config entry platform forwards are not happening during setup, instead
async_late_forward_entry_setups should be used which will hold the lock to
prevent the config entry from being unloaded while its platforms are being
setup
* run with error on to find them
* cert_exp, hold lock
* cert_exp, hold lock
* shelly async_late_forward_entry_setups
* compact
* compact
* found another
* patch up mobileapp
* patch up hue tests
* patch up smartthings
* fix mqtt
* fix esphome
* zwave_js
* mqtt
* rework
* fixes
* fix mocking
* fix mocking
* do not call async_forward_entry_setup directly
* docstrings
* docstrings
* docstrings
* add comments
* doc strings
* fixed all in core, turn off strict
* coverage
* coverage
* missing
* coverage
2024-06-05 01:34:39 +00:00
|
|
|
platforms = {platform for platform in SUPPORTED_PLATFORMS if platform in config}
|
|
|
|
platforms.add(Platform.SENSOR)
|
|
|
|
await hass.config_entries.async_forward_entry_setups(entry, platforms)
|
2021-11-20 10:30:41 +00:00
|
|
|
|
2024-04-24 05:51:02 +00:00
|
|
|
# set up notify service for backwards compatibility - remove 2024.11
|
2021-12-15 18:30:38 +00:00
|
|
|
if NotifySchema.PLATFORM in config:
|
2018-07-23 12:05:38 +00:00
|
|
|
hass.async_create_task(
|
2021-04-15 07:47:43 +00:00
|
|
|
discovery.async_load_platform(
|
2022-01-10 09:30:47 +00:00
|
|
|
hass, Platform.NOTIFY, DOMAIN, {}, hass.data[DATA_HASS_CONFIG]
|
2021-04-15 07:47:43 +00:00
|
|
|
)
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2017-09-07 07:11:55 +00:00
|
|
|
|
2023-05-10 22:13:22 +00:00
|
|
|
await register_panel(hass)
|
|
|
|
|
2021-11-20 10:30:41 +00:00
|
|
|
return True
|
2020-11-12 13:34:08 +00:00
|
|
|
|
|
|
|
|
2021-11-20 10:30:41 +00:00
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|
|
|
"""Unloading the KNX platforms."""
|
|
|
|
# if not loaded directly return
|
|
|
|
if not hass.data.get(DOMAIN):
|
|
|
|
return True
|
2020-11-12 13:34:08 +00:00
|
|
|
|
2021-11-20 10:30:41 +00:00
|
|
|
knx_module: KNXModule = hass.data[DOMAIN]
|
|
|
|
for exposure in knx_module.exposures:
|
|
|
|
exposure.shutdown()
|
2020-11-12 13:34:08 +00:00
|
|
|
|
2021-11-20 10:30:41 +00:00
|
|
|
unload_ok = await hass.config_entries.async_unload_platforms(
|
|
|
|
entry,
|
|
|
|
[
|
2023-08-03 21:23:12 +00:00
|
|
|
Platform.SENSOR, # always unload system entities (telegram counter, etc.)
|
|
|
|
*[
|
|
|
|
platform
|
|
|
|
for platform in SUPPORTED_PLATFORMS
|
|
|
|
if platform in hass.data[DATA_KNX_CONFIG]
|
2024-04-24 05:51:02 +00:00
|
|
|
and platform is not Platform.SENSOR
|
2023-08-03 21:23:12 +00:00
|
|
|
],
|
2021-11-20 10:30:41 +00:00
|
|
|
],
|
2020-11-12 13:34:08 +00:00
|
|
|
)
|
2021-11-20 10:30:41 +00:00
|
|
|
if unload_ok:
|
|
|
|
await knx_module.stop()
|
|
|
|
hass.data.pop(DOMAIN)
|
|
|
|
hass.data.pop(DATA_KNX_CONFIG)
|
2020-11-12 13:34:08 +00:00
|
|
|
|
2021-11-20 10:30:41 +00:00
|
|
|
return unload_ok
|
|
|
|
|
|
|
|
|
2021-11-20 15:15:33 +00:00
|
|
|
async def async_update_entry(hass: HomeAssistant, entry: ConfigEntry) -> None:
|
2021-11-20 10:30:41 +00:00
|
|
|
"""Update a given config entry."""
|
2021-11-20 15:15:33 +00:00
|
|
|
await hass.config_entries.async_reload(entry.entry_id)
|
2016-07-10 17:36:54 +00:00
|
|
|
|
|
|
|
|
2023-02-20 13:48:56 +00:00
|
|
|
async def async_remove_entry(hass: HomeAssistant, entry: ConfigEntry) -> None:
|
|
|
|
"""Remove a config entry."""
|
|
|
|
|
2023-07-09 19:15:55 +00:00
|
|
|
def remove_files(storage_dir: Path, knxkeys_filename: str | None) -> None:
|
|
|
|
"""Remove KNX files."""
|
|
|
|
if knxkeys_filename is not None:
|
|
|
|
with contextlib.suppress(FileNotFoundError):
|
|
|
|
(storage_dir / knxkeys_filename).unlink()
|
2023-02-20 13:48:56 +00:00
|
|
|
with contextlib.suppress(FileNotFoundError):
|
2023-07-09 19:15:55 +00:00
|
|
|
(storage_dir / PROJECT_STORAGE_KEY).unlink()
|
|
|
|
with contextlib.suppress(FileNotFoundError):
|
|
|
|
(storage_dir / TELEGRAMS_STORAGE_KEY).unlink()
|
2023-02-20 13:48:56 +00:00
|
|
|
with contextlib.suppress(FileNotFoundError, OSError):
|
2023-07-09 19:15:55 +00:00
|
|
|
(storage_dir / DOMAIN).rmdir()
|
2023-02-20 13:48:56 +00:00
|
|
|
|
2023-07-09 19:15:55 +00:00
|
|
|
storage_dir = Path(hass.config.path(STORAGE_DIR))
|
|
|
|
knxkeys_filename = entry.data.get(CONF_KNX_KNXKEY_FILENAME)
|
|
|
|
await hass.async_add_executor_job(remove_files, storage_dir, knxkeys_filename)
|
2023-02-20 13:48:56 +00:00
|
|
|
|
|
|
|
|
2018-07-20 08:45:20 +00:00
|
|
|
class KNXModule:
|
2017-09-07 07:11:55 +00:00
|
|
|
"""Representation of KNX Object."""
|
2016-07-10 17:36:54 +00:00
|
|
|
|
2021-11-20 10:30:41 +00:00
|
|
|
def __init__(
|
|
|
|
self, hass: HomeAssistant, config: ConfigType, entry: ConfigEntry
|
|
|
|
) -> None:
|
2021-03-26 15:10:55 +00:00
|
|
|
"""Initialize KNX module."""
|
2017-09-07 07:11:55 +00:00
|
|
|
self.hass = hass
|
|
|
|
self.config = config
|
2018-01-07 21:39:14 +00:00
|
|
|
self.connected = False
|
2021-03-26 15:10:55 +00:00
|
|
|
self.exposures: list[KNXExposeSensor | KNXExposeTime] = []
|
|
|
|
self.service_exposures: dict[str, KNXExposeSensor | KNXExposeTime] = {}
|
2021-11-20 10:30:41 +00:00
|
|
|
self.entry = entry
|
2017-09-07 07:11:55 +00:00
|
|
|
|
2023-05-10 22:13:22 +00:00
|
|
|
self.project = KNXProject(hass=hass, entry=entry)
|
|
|
|
|
2023-03-19 13:13:52 +00:00
|
|
|
self.xknx = XKNX(
|
|
|
|
connection_config=self.connection_config(),
|
|
|
|
rate_limit=self.entry.data[CONF_KNX_RATE_LIMIT],
|
|
|
|
state_updater=self.entry.data[CONF_KNX_STATE_UPDATER],
|
|
|
|
)
|
2021-10-14 17:54:48 +00:00
|
|
|
self.xknx.connection_manager.register_connection_state_changed_cb(
|
|
|
|
self.connection_state_changed_cb
|
|
|
|
)
|
2023-05-22 16:09:59 +00:00
|
|
|
self.telegrams = Telegrams(
|
|
|
|
hass=hass,
|
|
|
|
xknx=self.xknx,
|
|
|
|
project=self.project,
|
|
|
|
log_size=entry.data.get(CONF_KNX_TELEGRAM_LOG_SIZE, TELEGRAM_LOG_DEFAULT),
|
|
|
|
)
|
2023-03-19 13:13:52 +00:00
|
|
|
self.interface_device = KNXInterfaceDevice(
|
|
|
|
hass=hass, entry=entry, xknx=self.xknx
|
|
|
|
)
|
2021-01-22 14:27:51 +00:00
|
|
|
|
2021-11-06 18:31:25 +00:00
|
|
|
self._address_filter_transcoder: dict[AddressFilter, type[DPTBase]] = {}
|
2024-01-07 22:26:46 +00:00
|
|
|
self.group_address_transcoder: dict[DeviceGroupAddress, type[DPTBase]] = {}
|
|
|
|
self.knx_event_callback: TelegramQueue.Callback = self.register_event_callback()
|
2021-11-06 18:31:25 +00:00
|
|
|
|
2021-11-20 10:30:41 +00:00
|
|
|
self.entry.async_on_unload(
|
|
|
|
self.hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, self.stop)
|
|
|
|
)
|
|
|
|
self.entry.async_on_unload(self.entry.add_update_listener(async_update_entry))
|
|
|
|
|
2021-03-26 15:10:55 +00:00
|
|
|
async def start(self) -> None:
|
|
|
|
"""Start XKNX object. Connect to tunneling or Routing device."""
|
2023-05-10 22:13:22 +00:00
|
|
|
await self.project.load_project()
|
2023-07-09 19:15:55 +00:00
|
|
|
await self.telegrams.load_history()
|
2020-09-30 08:04:56 +00:00
|
|
|
await self.xknx.start()
|
2017-09-07 07:11:55 +00:00
|
|
|
|
2021-11-20 10:30:41 +00:00
|
|
|
async def stop(self, event: Event | None = None) -> None:
|
2021-03-26 15:10:55 +00:00
|
|
|
"""Stop XKNX object. Disconnect from tunneling or Routing device."""
|
2018-02-24 18:24:33 +00:00
|
|
|
await self.xknx.stop()
|
2023-07-09 19:15:55 +00:00
|
|
|
await self.telegrams.save_history()
|
2017-09-07 07:11:55 +00:00
|
|
|
|
2021-03-26 15:10:55 +00:00
|
|
|
def connection_config(self) -> ConnectionConfig:
|
2017-09-07 07:11:55 +00:00
|
|
|
"""Return the connection_config."""
|
2022-03-29 14:46:02 +00:00
|
|
|
_conn_type: str = self.entry.data[CONF_KNX_CONNECTION_TYPE]
|
2023-02-20 13:48:56 +00:00
|
|
|
_knxkeys_file: str | None = (
|
|
|
|
self.hass.config.path(
|
|
|
|
STORAGE_DIR,
|
|
|
|
self.entry.data[CONF_KNX_KNXKEY_FILENAME],
|
|
|
|
)
|
|
|
|
if self.entry.data.get(CONF_KNX_KNXKEY_FILENAME) is not None
|
|
|
|
else None
|
|
|
|
)
|
2021-11-20 10:30:41 +00:00
|
|
|
if _conn_type == CONF_KNX_ROUTING:
|
|
|
|
return ConnectionConfig(
|
|
|
|
connection_type=ConnectionType.ROUTING,
|
2022-11-26 05:49:11 +00:00
|
|
|
individual_address=self.entry.data[CONF_KNX_INDIVIDUAL_ADDRESS],
|
|
|
|
multicast_group=self.entry.data[CONF_KNX_MCAST_GRP],
|
|
|
|
multicast_port=self.entry.data[CONF_KNX_MCAST_PORT],
|
2022-03-29 14:46:02 +00:00
|
|
|
local_ip=self.entry.data.get(CONF_KNX_LOCAL_IP),
|
2021-11-20 10:30:41 +00:00
|
|
|
auto_reconnect=True,
|
2023-02-20 13:48:56 +00:00
|
|
|
secure_config=SecureConfig(
|
|
|
|
knxkeys_password=self.entry.data.get(CONF_KNX_KNXKEY_PASSWORD),
|
|
|
|
knxkeys_file_path=_knxkeys_file,
|
|
|
|
),
|
2022-03-28 21:56:29 +00:00
|
|
|
threaded=True,
|
2021-11-20 10:30:41 +00:00
|
|
|
)
|
|
|
|
if _conn_type == CONF_KNX_TUNNELING:
|
|
|
|
return ConnectionConfig(
|
|
|
|
connection_type=ConnectionType.TUNNELING,
|
2022-03-29 14:46:02 +00:00
|
|
|
gateway_ip=self.entry.data[CONF_HOST],
|
|
|
|
gateway_port=self.entry.data[CONF_PORT],
|
|
|
|
local_ip=self.entry.data.get(CONF_KNX_LOCAL_IP),
|
|
|
|
route_back=self.entry.data.get(CONF_KNX_ROUTE_BACK, False),
|
2021-11-20 10:30:41 +00:00
|
|
|
auto_reconnect=True,
|
2023-02-20 13:48:56 +00:00
|
|
|
secure_config=SecureConfig(
|
|
|
|
knxkeys_password=self.entry.data.get(CONF_KNX_KNXKEY_PASSWORD),
|
|
|
|
knxkeys_file_path=_knxkeys_file,
|
|
|
|
),
|
2022-03-28 21:56:29 +00:00
|
|
|
threaded=True,
|
2021-02-20 19:08:59 +00:00
|
|
|
)
|
2022-01-26 05:36:07 +00:00
|
|
|
if _conn_type == CONF_KNX_TUNNELING_TCP:
|
|
|
|
return ConnectionConfig(
|
|
|
|
connection_type=ConnectionType.TUNNELING_TCP,
|
2022-03-29 14:46:02 +00:00
|
|
|
gateway_ip=self.entry.data[CONF_HOST],
|
|
|
|
gateway_port=self.entry.data[CONF_PORT],
|
2022-01-26 05:36:07 +00:00
|
|
|
auto_reconnect=True,
|
2023-02-20 13:48:56 +00:00
|
|
|
secure_config=SecureConfig(
|
|
|
|
knxkeys_password=self.entry.data.get(CONF_KNX_KNXKEY_PASSWORD),
|
|
|
|
knxkeys_file_path=_knxkeys_file,
|
|
|
|
),
|
2022-03-28 21:56:29 +00:00
|
|
|
threaded=True,
|
2022-01-26 05:36:07 +00:00
|
|
|
)
|
2022-11-27 22:33:12 +00:00
|
|
|
if _conn_type == CONF_KNX_TUNNELING_TCP_SECURE:
|
2022-03-30 19:10:47 +00:00
|
|
|
return ConnectionConfig(
|
|
|
|
connection_type=ConnectionType.TUNNELING_TCP_SECURE,
|
|
|
|
gateway_ip=self.entry.data[CONF_HOST],
|
|
|
|
gateway_port=self.entry.data[CONF_PORT],
|
|
|
|
secure_config=SecureConfig(
|
|
|
|
user_id=self.entry.data.get(CONF_KNX_SECURE_USER_ID),
|
|
|
|
user_password=self.entry.data.get(CONF_KNX_SECURE_USER_PASSWORD),
|
|
|
|
device_authentication_password=self.entry.data.get(
|
|
|
|
CONF_KNX_SECURE_DEVICE_AUTHENTICATION
|
|
|
|
),
|
|
|
|
knxkeys_password=self.entry.data.get(CONF_KNX_KNXKEY_PASSWORD),
|
2023-02-20 13:48:56 +00:00
|
|
|
knxkeys_file_path=_knxkeys_file,
|
2022-03-30 19:10:47 +00:00
|
|
|
),
|
|
|
|
auto_reconnect=True,
|
|
|
|
threaded=True,
|
|
|
|
)
|
2022-11-27 22:33:12 +00:00
|
|
|
if _conn_type == CONF_KNX_ROUTING_SECURE:
|
|
|
|
return ConnectionConfig(
|
|
|
|
connection_type=ConnectionType.ROUTING_SECURE,
|
|
|
|
individual_address=self.entry.data[CONF_KNX_INDIVIDUAL_ADDRESS],
|
|
|
|
multicast_group=self.entry.data[CONF_KNX_MCAST_GRP],
|
|
|
|
multicast_port=self.entry.data[CONF_KNX_MCAST_PORT],
|
|
|
|
local_ip=self.entry.data.get(CONF_KNX_LOCAL_IP),
|
|
|
|
secure_config=SecureConfig(
|
|
|
|
backbone_key=self.entry.data.get(CONF_KNX_ROUTING_BACKBONE_KEY),
|
|
|
|
latency_ms=self.entry.data.get(
|
|
|
|
CONF_KNX_ROUTING_SYNC_LATENCY_TOLERANCE
|
|
|
|
),
|
|
|
|
knxkeys_password=self.entry.data.get(CONF_KNX_KNXKEY_PASSWORD),
|
2023-02-20 13:48:56 +00:00
|
|
|
knxkeys_file_path=_knxkeys_file,
|
2022-11-27 22:33:12 +00:00
|
|
|
),
|
|
|
|
auto_reconnect=True,
|
|
|
|
threaded=True,
|
|
|
|
)
|
2022-03-28 21:56:29 +00:00
|
|
|
return ConnectionConfig(
|
|
|
|
auto_reconnect=True,
|
2023-02-20 13:48:56 +00:00
|
|
|
secure_config=SecureConfig(
|
|
|
|
knxkeys_password=self.entry.data.get(CONF_KNX_KNXKEY_PASSWORD),
|
|
|
|
knxkeys_file_path=_knxkeys_file,
|
|
|
|
),
|
2022-03-28 21:56:29 +00:00
|
|
|
threaded=True,
|
|
|
|
)
|
2017-09-07 07:11:55 +00:00
|
|
|
|
2021-11-06 18:31:25 +00:00
|
|
|
async def connection_state_changed_cb(self, state: XknxConnectionState) -> None:
|
|
|
|
"""Call invoked after a KNX connection state change was received."""
|
|
|
|
self.connected = state == XknxConnectionState.CONNECTED
|
|
|
|
if tasks := [device.after_update() for device in self.xknx.devices]:
|
|
|
|
await asyncio.gather(*tasks)
|
|
|
|
|
2021-03-26 15:10:55 +00:00
|
|
|
async def telegram_received_cb(self, telegram: Telegram) -> None:
|
2018-01-21 06:35:38 +00:00
|
|
|
"""Call invoked after a KNX telegram was received."""
|
2021-01-04 02:07:12 +00:00
|
|
|
# Not all telegrams have serializable data.
|
2021-11-06 18:31:25 +00:00
|
|
|
data: int | tuple[int, ...] | None = None
|
|
|
|
value = None
|
2021-03-26 15:10:55 +00:00
|
|
|
if (
|
|
|
|
isinstance(telegram.payload, (GroupValueWrite, GroupValueResponse))
|
|
|
|
and telegram.payload.value is not None
|
2021-11-06 18:31:25 +00:00
|
|
|
and isinstance(
|
|
|
|
telegram.destination_address, (GroupAddress, InternalGroupAddress)
|
|
|
|
)
|
2021-03-26 15:10:55 +00:00
|
|
|
):
|
2021-01-04 02:07:12 +00:00
|
|
|
data = telegram.payload.value.value
|
2023-04-22 16:25:14 +00:00
|
|
|
if transcoder := (
|
2024-01-07 22:26:46 +00:00
|
|
|
self.group_address_transcoder.get(telegram.destination_address)
|
2023-04-22 16:25:14 +00:00
|
|
|
or next(
|
|
|
|
(
|
|
|
|
_transcoder
|
|
|
|
for _filter, _transcoder in self._address_filter_transcoder.items()
|
|
|
|
if _filter.match(telegram.destination_address)
|
|
|
|
),
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
):
|
|
|
|
try:
|
|
|
|
value = transcoder.from_knx(telegram.payload.value)
|
|
|
|
except (ConversionError, CouldNotParseTelegram) as err:
|
|
|
|
_LOGGER.warning(
|
2021-11-06 18:31:25 +00:00
|
|
|
(
|
2023-04-22 16:25:14 +00:00
|
|
|
"Error in `knx_event` at decoding type '%s' from"
|
|
|
|
" telegram %s\n%s"
|
2021-11-06 18:31:25 +00:00
|
|
|
),
|
2023-04-22 16:25:14 +00:00
|
|
|
transcoder.__name__,
|
|
|
|
telegram,
|
|
|
|
err,
|
2021-11-06 18:31:25 +00:00
|
|
|
)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
self.hass.bus.async_fire(
|
|
|
|
"knx_event",
|
2021-01-04 02:07:12 +00:00
|
|
|
{
|
|
|
|
"data": data,
|
|
|
|
"destination": str(telegram.destination_address),
|
|
|
|
"direction": telegram.direction.value,
|
2021-11-06 18:31:25 +00:00
|
|
|
"value": value,
|
2021-01-04 02:07:12 +00:00
|
|
|
"source": str(telegram.source_address),
|
|
|
|
"telegramtype": telegram.payload.__class__.__name__,
|
|
|
|
},
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2016-07-10 17:36:54 +00:00
|
|
|
|
2021-11-06 18:31:25 +00:00
|
|
|
def register_event_callback(self) -> TelegramQueue.Callback:
|
|
|
|
"""Register callback for knx_event within XKNX TelegramQueue."""
|
2022-03-29 14:46:02 +00:00
|
|
|
address_filters = []
|
2021-11-20 10:30:41 +00:00
|
|
|
for filter_set in self.config[CONF_EVENT]:
|
2021-11-06 18:31:25 +00:00
|
|
|
_filters = list(map(AddressFilter, filter_set[KNX_ADDRESS]))
|
|
|
|
address_filters.extend(_filters)
|
|
|
|
if (dpt := filter_set.get(CONF_TYPE)) and (
|
|
|
|
transcoder := DPTBase.parse_transcoder(dpt)
|
|
|
|
):
|
|
|
|
self._address_filter_transcoder.update(
|
2023-02-04 17:52:26 +00:00
|
|
|
{
|
|
|
|
_filter: transcoder # type: ignore[type-abstract]
|
|
|
|
for _filter in _filters
|
|
|
|
}
|
2021-11-06 18:31:25 +00:00
|
|
|
)
|
|
|
|
|
2021-04-26 12:23:21 +00:00
|
|
|
return self.xknx.telegram_queue.register_telegram_received_cb(
|
2021-01-22 14:27:51 +00:00
|
|
|
self.telegram_received_cb,
|
|
|
|
address_filters=address_filters,
|
|
|
|
group_addresses=[],
|
2021-02-20 18:45:04 +00:00
|
|
|
match_for_outgoing=True,
|
2021-01-22 14:27:51 +00:00
|
|
|
)
|