2019-02-14 04:35:12 +00:00
|
|
|
"""Support KNX devices."""
|
2021-03-18 12:07:04 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2020-11-12 13:34:08 +00:00
|
|
|
import asyncio
|
2018-01-21 06:35:38 +00:00
|
|
|
import logging
|
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-01-22 14:27:51 +00:00
|
|
|
from xknx.core.telegram_queue import TelegramQueue
|
2020-08-26 16:03:03 +00:00
|
|
|
from xknx.dpt import DPTArray, DPTBase, DPTBinary
|
2019-10-22 05:38:21 +00:00
|
|
|
from xknx.exceptions import XKNXException
|
2020-09-20 21:40:36 +00:00
|
|
|
from xknx.io import (
|
|
|
|
DEFAULT_MCAST_GRP,
|
|
|
|
DEFAULT_MCAST_PORT,
|
|
|
|
ConnectionConfig,
|
|
|
|
ConnectionType,
|
|
|
|
)
|
2020-05-13 13:19:00 +00:00
|
|
|
from xknx.telegram import AddressFilter, GroupAddress, Telegram
|
2021-02-20 19:09:23 +00:00
|
|
|
from xknx.telegram.apci import GroupValueRead, GroupValueResponse, GroupValueWrite
|
2016-09-14 06:03:30 +00:00
|
|
|
|
2018-02-26 07:44:09 +00:00
|
|
|
from homeassistant.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_HOST,
|
|
|
|
CONF_PORT,
|
|
|
|
EVENT_HOMEASSISTANT_STOP,
|
2020-11-12 13:34:08 +00:00
|
|
|
SERVICE_RELOAD,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2021-02-22 05:38:17 +00:00
|
|
|
from homeassistant.exceptions import HomeAssistantError
|
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.entity_platform import async_get_platforms
|
|
|
|
from homeassistant.helpers.reload import async_integration_yaml_config
|
|
|
|
from homeassistant.helpers.service import async_register_admin_service
|
|
|
|
from homeassistant.helpers.typing import ServiceCallType
|
2020-08-26 16:03:03 +00:00
|
|
|
|
2021-03-01 20:59:36 +00:00
|
|
|
from .const import DOMAIN, KNX_ADDRESS, SupportedPlatforms
|
2021-02-22 05:38:17 +00:00
|
|
|
from .expose import create_knx_exposure
|
2020-08-26 16:03:03 +00:00
|
|
|
from .factory import create_knx_device
|
|
|
|
from .schema import (
|
|
|
|
BinarySensorSchema,
|
|
|
|
ClimateSchema,
|
|
|
|
ConnectionSchema,
|
|
|
|
CoverSchema,
|
|
|
|
ExposeSchema,
|
2021-02-10 08:09:34 +00:00
|
|
|
FanSchema,
|
2020-08-26 16:03:03 +00:00
|
|
|
LightSchema,
|
|
|
|
NotifySchema,
|
|
|
|
SceneSchema,
|
|
|
|
SensorSchema,
|
|
|
|
SwitchSchema,
|
2020-08-31 09:38:52 +00:00
|
|
|
WeatherSchema,
|
2021-02-24 07:37:41 +00:00
|
|
|
ga_validator,
|
|
|
|
ia_validator,
|
2021-03-16 20:33:56 +00:00
|
|
|
sensor_type_validator,
|
2020-08-26 16:03:03 +00:00
|
|
|
)
|
2017-09-07 07:11:55 +00:00
|
|
|
|
2019-02-14 04:35:12 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2017-09-07 07:11:55 +00:00
|
|
|
CONF_KNX_CONFIG = "config_file"
|
2016-07-10 17:36:54 +00:00
|
|
|
|
2017-09-07 07:11:55 +00:00
|
|
|
CONF_KNX_ROUTING = "routing"
|
|
|
|
CONF_KNX_TUNNELING = "tunneling"
|
|
|
|
CONF_KNX_FIRE_EVENT = "fire_event"
|
2021-01-22 14:27:51 +00:00
|
|
|
CONF_KNX_EVENT_FILTER = "event_filter"
|
2020-09-20 21:40:36 +00:00
|
|
|
CONF_KNX_INDIVIDUAL_ADDRESS = "individual_address"
|
|
|
|
CONF_KNX_MCAST_GRP = "multicast_group"
|
|
|
|
CONF_KNX_MCAST_PORT = "multicast_port"
|
2017-10-15 21:46:55 +00:00
|
|
|
CONF_KNX_STATE_UPDATER = "state_updater"
|
2019-03-07 21:53:42 +00:00
|
|
|
CONF_KNX_RATE_LIMIT = "rate_limit"
|
2018-02-26 07:44:09 +00:00
|
|
|
CONF_KNX_EXPOSE = "expose"
|
2020-08-26 16:03:03 +00:00
|
|
|
|
2017-09-07 07:11:55 +00:00
|
|
|
SERVICE_KNX_SEND = "send"
|
|
|
|
SERVICE_KNX_ATTR_PAYLOAD = "payload"
|
2020-08-26 16:03:03 +00:00
|
|
|
SERVICE_KNX_ATTR_TYPE = "type"
|
2021-01-22 14:27:51 +00:00
|
|
|
SERVICE_KNX_ATTR_REMOVE = "remove"
|
|
|
|
SERVICE_KNX_EVENT_REGISTER = "event_register"
|
2021-02-22 05:38:17 +00:00
|
|
|
SERVICE_KNX_EXPOSURE_REGISTER = "exposure_register"
|
2021-02-20 19:09:23 +00:00
|
|
|
SERVICE_KNX_READ = "read"
|
2017-09-07 07:11:55 +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-03-10 20:32:22 +00:00
|
|
|
# deprecated since 2021.4
|
2021-03-02 23:50:40 +00:00
|
|
|
cv.deprecated(CONF_KNX_CONFIG),
|
2021-03-01 10:55:55 +00:00
|
|
|
# deprecated since 2021.2
|
2021-01-22 14:27:51 +00:00
|
|
|
cv.deprecated(CONF_KNX_FIRE_EVENT),
|
|
|
|
cv.deprecated("fire_event_filter", replacement_key=CONF_KNX_EVENT_FILTER),
|
|
|
|
vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Optional(CONF_KNX_CONFIG): cv.string,
|
|
|
|
vol.Exclusive(
|
|
|
|
CONF_KNX_ROUTING, "connection_type"
|
|
|
|
): ConnectionSchema.ROUTING_SCHEMA,
|
|
|
|
vol.Exclusive(
|
|
|
|
CONF_KNX_TUNNELING, "connection_type"
|
|
|
|
): ConnectionSchema.TUNNELING_SCHEMA,
|
|
|
|
vol.Optional(CONF_KNX_FIRE_EVENT): cv.boolean,
|
|
|
|
vol.Optional(CONF_KNX_EVENT_FILTER, default=[]): vol.All(
|
|
|
|
cv.ensure_list, [cv.string]
|
|
|
|
),
|
|
|
|
vol.Optional(
|
|
|
|
CONF_KNX_INDIVIDUAL_ADDRESS, default=XKNX.DEFAULT_ADDRESS
|
2021-02-24 07:37:41 +00:00
|
|
|
): ia_validator,
|
2021-01-22 14:27:51 +00:00
|
|
|
vol.Optional(
|
|
|
|
CONF_KNX_MCAST_GRP, default=DEFAULT_MCAST_GRP
|
|
|
|
): cv.string,
|
|
|
|
vol.Optional(
|
|
|
|
CONF_KNX_MCAST_PORT, default=DEFAULT_MCAST_PORT
|
|
|
|
): cv.port,
|
|
|
|
vol.Optional(CONF_KNX_STATE_UPDATER, default=True): cv.boolean,
|
|
|
|
vol.Optional(CONF_KNX_RATE_LIMIT, default=20): vol.All(
|
|
|
|
vol.Coerce(int), vol.Range(min=1, max=100)
|
|
|
|
),
|
|
|
|
vol.Optional(CONF_KNX_EXPOSE): vol.All(
|
|
|
|
cv.ensure_list, [ExposeSchema.SCHEMA]
|
|
|
|
),
|
2021-02-24 00:26:17 +00:00
|
|
|
vol.Optional(SupportedPlatforms.COVER.value): vol.All(
|
2021-01-22 14:27:51 +00:00
|
|
|
cv.ensure_list, [CoverSchema.SCHEMA]
|
|
|
|
),
|
2021-02-24 00:26:17 +00:00
|
|
|
vol.Optional(SupportedPlatforms.BINARY_SENSOR.value): vol.All(
|
2021-01-22 14:27:51 +00:00
|
|
|
cv.ensure_list, [BinarySensorSchema.SCHEMA]
|
|
|
|
),
|
2021-02-24 00:26:17 +00:00
|
|
|
vol.Optional(SupportedPlatforms.LIGHT.value): vol.All(
|
2021-01-22 14:27:51 +00:00
|
|
|
cv.ensure_list, [LightSchema.SCHEMA]
|
|
|
|
),
|
2021-02-24 00:26:17 +00:00
|
|
|
vol.Optional(SupportedPlatforms.CLIMATE.value): vol.All(
|
2021-01-22 14:27:51 +00:00
|
|
|
cv.ensure_list, [ClimateSchema.SCHEMA]
|
|
|
|
),
|
2021-02-24 00:26:17 +00:00
|
|
|
vol.Optional(SupportedPlatforms.NOTIFY.value): vol.All(
|
2021-01-22 14:27:51 +00:00
|
|
|
cv.ensure_list, [NotifySchema.SCHEMA]
|
|
|
|
),
|
2021-02-24 00:26:17 +00:00
|
|
|
vol.Optional(SupportedPlatforms.SWITCH.value): vol.All(
|
2021-01-22 14:27:51 +00:00
|
|
|
cv.ensure_list, [SwitchSchema.SCHEMA]
|
|
|
|
),
|
2021-02-24 00:26:17 +00:00
|
|
|
vol.Optional(SupportedPlatforms.SENSOR.value): vol.All(
|
2021-01-22 14:27:51 +00:00
|
|
|
cv.ensure_list, [SensorSchema.SCHEMA]
|
|
|
|
),
|
2021-02-24 00:26:17 +00:00
|
|
|
vol.Optional(SupportedPlatforms.SCENE.value): vol.All(
|
2021-01-22 14:27:51 +00:00
|
|
|
cv.ensure_list, [SceneSchema.SCHEMA]
|
|
|
|
),
|
2021-02-24 00:26:17 +00:00
|
|
|
vol.Optional(SupportedPlatforms.WEATHER.value): vol.All(
|
2021-01-22 14:27:51 +00:00
|
|
|
cv.ensure_list, [WeatherSchema.SCHEMA]
|
|
|
|
),
|
2021-02-24 00:26:17 +00:00
|
|
|
vol.Optional(SupportedPlatforms.FAN.value): vol.All(
|
2021-02-10 08:09:34 +00:00
|
|
|
cv.ensure_list, [FanSchema.SCHEMA]
|
|
|
|
),
|
2021-01-22 14:27:51 +00:00
|
|
|
}
|
|
|
|
),
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
|
|
|
},
|
|
|
|
extra=vol.ALLOW_EXTRA,
|
|
|
|
)
|
|
|
|
|
2021-01-04 03:49:29 +00:00
|
|
|
SERVICE_KNX_SEND_SCHEMA = vol.Any(
|
|
|
|
vol.Schema(
|
|
|
|
{
|
2021-03-01 20:59:36 +00:00
|
|
|
vol.Required(KNX_ADDRESS): vol.All(
|
2021-03-01 10:51:59 +00:00
|
|
|
cv.ensure_list,
|
|
|
|
[ga_validator],
|
|
|
|
),
|
2021-01-04 03:49:29 +00:00
|
|
|
vol.Required(SERVICE_KNX_ATTR_PAYLOAD): cv.match_all,
|
2021-03-16 20:33:56 +00:00
|
|
|
vol.Required(SERVICE_KNX_ATTR_TYPE): sensor_type_validator,
|
2021-01-04 03:49:29 +00:00
|
|
|
}
|
|
|
|
),
|
|
|
|
vol.Schema(
|
|
|
|
# without type given payload is treated as raw bytes
|
|
|
|
{
|
2021-03-01 20:59:36 +00:00
|
|
|
vol.Required(KNX_ADDRESS): vol.All(
|
2021-03-01 10:51:59 +00:00
|
|
|
cv.ensure_list,
|
|
|
|
[ga_validator],
|
|
|
|
),
|
2021-01-04 03:49:29 +00:00
|
|
|
vol.Required(SERVICE_KNX_ATTR_PAYLOAD): vol.Any(
|
|
|
|
cv.positive_int, [cv.positive_int]
|
|
|
|
),
|
|
|
|
}
|
|
|
|
),
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2017-07-20 05:01:05 +00:00
|
|
|
|
2021-02-20 19:09:23 +00:00
|
|
|
SERVICE_KNX_READ_SCHEMA = vol.Schema(
|
|
|
|
{
|
2021-03-01 20:59:36 +00:00
|
|
|
vol.Required(KNX_ADDRESS): vol.All(
|
2021-02-20 19:09:23 +00:00
|
|
|
cv.ensure_list,
|
2021-02-24 07:37:41 +00:00
|
|
|
[ga_validator],
|
2021-02-20 19:09:23 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2021-01-22 14:27:51 +00:00
|
|
|
SERVICE_KNX_EVENT_REGISTER_SCHEMA = vol.Schema(
|
|
|
|
{
|
2021-03-01 20:59:36 +00:00
|
|
|
vol.Required(KNX_ADDRESS): vol.All(
|
2021-03-01 10:51:59 +00:00
|
|
|
cv.ensure_list,
|
|
|
|
[ga_validator],
|
|
|
|
),
|
2021-01-22 14:27:51 +00:00
|
|
|
vol.Optional(SERVICE_KNX_ATTR_REMOVE, default=False): cv.boolean,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2021-02-22 05:38:17 +00:00
|
|
|
SERVICE_KNX_EXPOSURE_REGISTER_SCHEMA = vol.Any(
|
|
|
|
ExposeSchema.SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Optional(SERVICE_KNX_ATTR_REMOVE, default=False): cv.boolean,
|
|
|
|
}
|
|
|
|
),
|
|
|
|
vol.Schema(
|
|
|
|
# for removing only `address` is required
|
|
|
|
{
|
2021-03-01 20:59:36 +00:00
|
|
|
vol.Required(KNX_ADDRESS): ga_validator,
|
2021-02-22 05:38:17 +00:00
|
|
|
vol.Required(SERVICE_KNX_ATTR_REMOVE): vol.All(cv.boolean, True),
|
|
|
|
},
|
|
|
|
extra=vol.ALLOW_EXTRA,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
2016-07-10 17:36:54 +00:00
|
|
|
|
2018-02-24 18:24:33 +00:00
|
|
|
async def async_setup(hass, config):
|
2018-01-21 06:35:38 +00:00
|
|
|
"""Set up the KNX component."""
|
2016-07-10 17:36:54 +00:00
|
|
|
try:
|
2021-02-23 21:59:16 +00:00
|
|
|
knx_module = KNXModule(hass, config)
|
|
|
|
hass.data[DOMAIN] = knx_module
|
|
|
|
await knx_module.start()
|
2017-09-07 07:11:55 +00:00
|
|
|
except XKNXException as ex:
|
2020-09-20 21:40:36 +00:00
|
|
|
_LOGGER.warning("Could not connect to KNX interface: %s", ex)
|
2018-01-07 21:39:14 +00:00
|
|
|
hass.components.persistent_notification.async_create(
|
2020-09-20 21:40:36 +00:00
|
|
|
f"Could not connect to KNX interface: <br><b>{ex}</b>", title="KNX"
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2017-07-07 05:21:40 +00:00
|
|
|
|
2021-02-22 05:38:17 +00:00
|
|
|
if CONF_KNX_EXPOSE in config[DOMAIN]:
|
|
|
|
for expose_config in config[DOMAIN][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
|
|
|
)
|
|
|
|
|
2020-08-30 18:13:47 +00:00
|
|
|
for platform in SupportedPlatforms:
|
|
|
|
if platform.value in config[DOMAIN]:
|
|
|
|
for device_config in config[DOMAIN][platform.value]:
|
2021-02-23 21:59:16 +00:00
|
|
|
create_knx_device(platform, knx_module.xknx, device_config)
|
2020-08-26 16:03:03 +00:00
|
|
|
|
2020-08-30 18:13:47 +00:00
|
|
|
# We need to wait until all entities are loaded into the device list since they could also be created from other platforms
|
|
|
|
for platform in SupportedPlatforms:
|
2018-07-23 12:05:38 +00:00
|
|
|
hass.async_create_task(
|
2020-08-30 18:13:47 +00:00
|
|
|
discovery.async_load_platform(hass, platform.value, DOMAIN, {}, config)
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2017-09-07 07:11:55 +00:00
|
|
|
|
|
|
|
hass.services.async_register(
|
2019-07-31 19:25:30 +00:00
|
|
|
DOMAIN,
|
|
|
|
SERVICE_KNX_SEND,
|
2021-02-23 21:59:16 +00:00
|
|
|
knx_module.service_send_to_knx_bus,
|
2019-07-31 19:25:30 +00:00
|
|
|
schema=SERVICE_KNX_SEND_SCHEMA,
|
|
|
|
)
|
2017-07-12 10:21:15 +00:00
|
|
|
|
2021-02-20 19:09:23 +00:00
|
|
|
hass.services.async_register(
|
|
|
|
DOMAIN,
|
|
|
|
SERVICE_KNX_READ,
|
2021-02-23 21:59:16 +00:00
|
|
|
knx_module.service_read_to_knx_bus,
|
2021-02-20 19:09:23 +00:00
|
|
|
schema=SERVICE_KNX_READ_SCHEMA,
|
|
|
|
)
|
|
|
|
|
2021-01-22 14:27:51 +00:00
|
|
|
async_register_admin_service(
|
|
|
|
hass,
|
|
|
|
DOMAIN,
|
|
|
|
SERVICE_KNX_EVENT_REGISTER,
|
2021-02-23 21:59:16 +00:00
|
|
|
knx_module.service_event_register_modify,
|
2021-01-22 14:27:51 +00:00
|
|
|
schema=SERVICE_KNX_EVENT_REGISTER_SCHEMA,
|
|
|
|
)
|
|
|
|
|
2021-02-22 05:38:17 +00:00
|
|
|
async_register_admin_service(
|
|
|
|
hass,
|
|
|
|
DOMAIN,
|
|
|
|
SERVICE_KNX_EXPOSURE_REGISTER,
|
2021-02-23 21:59:16 +00:00
|
|
|
knx_module.service_exposure_register_modify,
|
2021-02-22 05:38:17 +00:00
|
|
|
schema=SERVICE_KNX_EXPOSURE_REGISTER_SCHEMA,
|
|
|
|
)
|
|
|
|
|
2020-11-12 13:34:08 +00:00
|
|
|
async def reload_service_handler(service_call: ServiceCallType) -> None:
|
|
|
|
"""Remove all KNX components and load new ones from config."""
|
|
|
|
|
|
|
|
# First check for config file. If for some reason it is no longer there
|
|
|
|
# or knx is no longer mentioned, stop the reload.
|
|
|
|
config = await async_integration_yaml_config(hass, DOMAIN)
|
|
|
|
|
|
|
|
if not config or DOMAIN not in config:
|
|
|
|
return
|
|
|
|
|
2021-02-23 21:59:16 +00:00
|
|
|
await knx_module.xknx.stop()
|
2020-11-12 13:34:08 +00:00
|
|
|
|
|
|
|
await asyncio.gather(
|
|
|
|
*[platform.async_reset() for platform in async_get_platforms(hass, DOMAIN)]
|
|
|
|
)
|
|
|
|
|
|
|
|
await async_setup(hass, config)
|
|
|
|
|
|
|
|
async_register_admin_service(
|
|
|
|
hass, DOMAIN, SERVICE_RELOAD, reload_service_handler, schema=vol.Schema({})
|
|
|
|
)
|
|
|
|
|
2016-07-10 17:36:54 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
def __init__(self, hass, config):
|
2018-01-21 06:35:38 +00:00
|
|
|
"""Initialize of 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
|
2018-02-26 07:44:09 +00:00
|
|
|
self.exposures = []
|
2021-02-22 05:38:17 +00:00
|
|
|
self.service_exposures = {}
|
2017-09-07 07:11:55 +00:00
|
|
|
|
2021-01-22 14:27:51 +00:00
|
|
|
self.init_xknx()
|
|
|
|
self._knx_event_callback: TelegramQueue.Callback = self.register_callback()
|
|
|
|
|
2017-09-07 07:11:55 +00:00
|
|
|
def init_xknx(self):
|
2018-01-21 06:35:38 +00:00
|
|
|
"""Initialize of KNX object."""
|
2019-07-31 19:25:30 +00:00
|
|
|
self.xknx = XKNX(
|
|
|
|
config=self.config_file(),
|
2020-09-20 21:40:36 +00:00
|
|
|
own_address=self.config[DOMAIN][CONF_KNX_INDIVIDUAL_ADDRESS],
|
2019-07-31 19:25:30 +00:00
|
|
|
rate_limit=self.config[DOMAIN][CONF_KNX_RATE_LIMIT],
|
2020-09-20 21:40:36 +00:00
|
|
|
multicast_group=self.config[DOMAIN][CONF_KNX_MCAST_GRP],
|
|
|
|
multicast_port=self.config[DOMAIN][CONF_KNX_MCAST_PORT],
|
2020-09-30 08:04:56 +00:00
|
|
|
connection_config=self.connection_config(),
|
|
|
|
state_updater=self.config[DOMAIN][CONF_KNX_STATE_UPDATER],
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2017-09-07 07:11:55 +00:00
|
|
|
|
2018-02-24 18:24:33 +00:00
|
|
|
async def start(self):
|
2017-09-07 07:11:55 +00:00
|
|
|
"""Start KNX object. Connect to tunneling or Routing device."""
|
2020-09-30 08:04:56 +00:00
|
|
|
await self.xknx.start()
|
2017-09-07 07:11:55 +00:00
|
|
|
self.hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, self.stop)
|
2018-01-07 21:39:14 +00:00
|
|
|
self.connected = True
|
2017-09-07 07:11:55 +00:00
|
|
|
|
2018-02-24 18:24:33 +00:00
|
|
|
async def stop(self, event):
|
2017-09-07 07:11:55 +00:00
|
|
|
"""Stop KNX object. Disconnect from tunneling or Routing device."""
|
2018-02-24 18:24:33 +00:00
|
|
|
await self.xknx.stop()
|
2017-09-07 07:11:55 +00:00
|
|
|
|
|
|
|
def config_file(self):
|
|
|
|
"""Resolve and return the full path of xknx.yaml if configured."""
|
|
|
|
config_file = self.config[DOMAIN].get(CONF_KNX_CONFIG)
|
|
|
|
if not config_file:
|
|
|
|
return None
|
|
|
|
if not config_file.startswith("/"):
|
|
|
|
return self.hass.config.path(config_file)
|
|
|
|
return config_file
|
|
|
|
|
|
|
|
def connection_config(self):
|
|
|
|
"""Return the connection_config."""
|
|
|
|
if CONF_KNX_TUNNELING in self.config[DOMAIN]:
|
|
|
|
return self.connection_config_tunneling()
|
2018-07-23 08:16:05 +00:00
|
|
|
if CONF_KNX_ROUTING in self.config[DOMAIN]:
|
2017-09-07 07:11:55 +00:00
|
|
|
return self.connection_config_routing()
|
2020-09-30 08:04:56 +00:00
|
|
|
# config from xknx.yaml always has priority later on
|
2021-02-08 10:23:50 +00:00
|
|
|
return ConnectionConfig(auto_reconnect=True)
|
2017-09-07 07:11:55 +00:00
|
|
|
|
|
|
|
def connection_config_routing(self):
|
|
|
|
"""Return the connection_config if routing is configured."""
|
2021-02-20 19:08:59 +00:00
|
|
|
local_ip = None
|
|
|
|
# all configuration values are optional
|
|
|
|
if self.config[DOMAIN][CONF_KNX_ROUTING] is not None:
|
|
|
|
local_ip = self.config[DOMAIN][CONF_KNX_ROUTING].get(
|
|
|
|
ConnectionSchema.CONF_KNX_LOCAL_IP
|
|
|
|
)
|
2017-09-07 07:11:55 +00:00
|
|
|
return ConnectionConfig(
|
2019-07-31 19:25:30 +00:00
|
|
|
connection_type=ConnectionType.ROUTING, local_ip=local_ip
|
|
|
|
)
|
2017-09-07 07:11:55 +00:00
|
|
|
|
|
|
|
def connection_config_tunneling(self):
|
|
|
|
"""Return the connection_config if tunneling is configured."""
|
2020-03-31 22:22:20 +00:00
|
|
|
gateway_ip = self.config[DOMAIN][CONF_KNX_TUNNELING][CONF_HOST]
|
2020-09-20 21:40:36 +00:00
|
|
|
gateway_port = self.config[DOMAIN][CONF_KNX_TUNNELING][CONF_PORT]
|
2020-08-26 16:03:03 +00:00
|
|
|
local_ip = self.config[DOMAIN][CONF_KNX_TUNNELING].get(
|
|
|
|
ConnectionSchema.CONF_KNX_LOCAL_IP
|
|
|
|
)
|
2017-09-07 07:11:55 +00:00
|
|
|
return ConnectionConfig(
|
2019-07-31 19:25:30 +00:00
|
|
|
connection_type=ConnectionType.TUNNELING,
|
|
|
|
gateway_ip=gateway_ip,
|
|
|
|
gateway_port=gateway_port,
|
|
|
|
local_ip=local_ip,
|
2020-03-31 22:22:20 +00:00
|
|
|
auto_reconnect=True,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2017-09-07 07:11:55 +00:00
|
|
|
|
2018-02-24 18:24:33 +00:00
|
|
|
async def telegram_received_cb(self, telegram):
|
2018-01-21 06:35:38 +00:00
|
|
|
"""Call invoked after a KNX telegram was received."""
|
2021-01-04 02:07:12 +00:00
|
|
|
data = None
|
|
|
|
|
|
|
|
# Not all telegrams have serializable data.
|
|
|
|
if isinstance(telegram.payload, (GroupValueWrite, GroupValueResponse)):
|
|
|
|
data = telegram.payload.value.value
|
|
|
|
|
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,
|
|
|
|
"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-01-22 14:27:51 +00:00
|
|
|
def register_callback(self) -> TelegramQueue.Callback:
|
|
|
|
"""Register callback within XKNX TelegramQueue."""
|
|
|
|
address_filters = list(
|
|
|
|
map(AddressFilter, self.config[DOMAIN][CONF_KNX_EVENT_FILTER])
|
|
|
|
)
|
|
|
|
return self.xknx.telegram_queue.register_telegram_received_cb(
|
|
|
|
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
|
|
|
)
|
|
|
|
|
|
|
|
async def service_event_register_modify(self, call):
|
|
|
|
"""Service for adding or removing a GroupAddress to the knx_event filter."""
|
2021-03-01 20:59:36 +00:00
|
|
|
attr_address = call.data.get(KNX_ADDRESS)
|
2021-03-01 10:51:59 +00:00
|
|
|
group_addresses = map(GroupAddress, attr_address)
|
|
|
|
|
2021-01-22 14:27:51 +00:00
|
|
|
if call.data.get(SERVICE_KNX_ATTR_REMOVE):
|
2021-03-01 10:51:59 +00:00
|
|
|
for group_address in group_addresses:
|
|
|
|
try:
|
|
|
|
self._knx_event_callback.group_addresses.remove(group_address)
|
|
|
|
except ValueError:
|
|
|
|
_LOGGER.warning(
|
|
|
|
"Service event_register could not remove event for '%s'",
|
|
|
|
str(group_address),
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
for group_address in group_addresses:
|
|
|
|
if group_address not in self._knx_event_callback.group_addresses:
|
|
|
|
self._knx_event_callback.group_addresses.append(group_address)
|
|
|
|
_LOGGER.debug(
|
|
|
|
"Service event_register registered event for '%s'",
|
|
|
|
str(group_address),
|
|
|
|
)
|
2021-01-22 14:27:51 +00:00
|
|
|
|
2021-02-22 05:38:17 +00:00
|
|
|
async def service_exposure_register_modify(self, call):
|
|
|
|
"""Service for adding or removing an exposure to KNX bus."""
|
2021-03-01 20:59:36 +00:00
|
|
|
group_address = call.data.get(KNX_ADDRESS)
|
2021-02-22 05:38:17 +00:00
|
|
|
|
|
|
|
if call.data.get(SERVICE_KNX_ATTR_REMOVE):
|
|
|
|
try:
|
|
|
|
removed_exposure = self.service_exposures.pop(group_address)
|
|
|
|
except KeyError as err:
|
|
|
|
raise HomeAssistantError(
|
|
|
|
f"Could not find exposure for '{group_address}' to remove."
|
|
|
|
) from err
|
|
|
|
else:
|
|
|
|
removed_exposure.shutdown()
|
|
|
|
return
|
|
|
|
|
|
|
|
if group_address in self.service_exposures:
|
|
|
|
replaced_exposure = self.service_exposures.pop(group_address)
|
|
|
|
_LOGGER.warning(
|
|
|
|
"Service exposure_register replacing already registered exposure for '%s' - %s",
|
|
|
|
group_address,
|
|
|
|
replaced_exposure.device.name,
|
|
|
|
)
|
|
|
|
replaced_exposure.shutdown()
|
|
|
|
exposure = create_knx_exposure(self.hass, self.xknx, call.data)
|
|
|
|
self.service_exposures[group_address] = exposure
|
|
|
|
_LOGGER.debug(
|
|
|
|
"Service exposure_register registered exposure for '%s' - %s",
|
|
|
|
group_address,
|
|
|
|
exposure.device.name,
|
|
|
|
)
|
|
|
|
|
2018-02-24 18:24:33 +00:00
|
|
|
async def service_send_to_knx_bus(self, call):
|
2017-09-23 15:15:46 +00:00
|
|
|
"""Service for sending an arbitrary KNX message to the KNX bus."""
|
2021-03-01 20:59:36 +00:00
|
|
|
attr_address = call.data.get(KNX_ADDRESS)
|
2021-03-01 10:51:59 +00:00
|
|
|
attr_payload = call.data.get(SERVICE_KNX_ATTR_PAYLOAD)
|
2020-08-26 16:03:03 +00:00
|
|
|
attr_type = call.data.get(SERVICE_KNX_ATTR_TYPE)
|
2017-09-07 07:11:55 +00:00
|
|
|
|
2021-03-18 12:07:04 +00:00
|
|
|
payload: DPTBinary | DPTArray
|
2021-03-01 10:51:59 +00:00
|
|
|
if attr_type is not None:
|
|
|
|
transcoder = DPTBase.parse_transcoder(attr_type)
|
|
|
|
if transcoder is None:
|
|
|
|
raise ValueError(f"Invalid type for knx.send service: {attr_type}")
|
|
|
|
payload = DPTArray(transcoder.to_knx(attr_payload))
|
|
|
|
elif isinstance(attr_payload, int):
|
|
|
|
payload = DPTBinary(attr_payload)
|
|
|
|
else:
|
|
|
|
payload = DPTArray(attr_payload)
|
|
|
|
|
|
|
|
for address in attr_address:
|
|
|
|
telegram = Telegram(
|
|
|
|
destination_address=GroupAddress(address),
|
|
|
|
payload=GroupValueWrite(payload),
|
|
|
|
)
|
|
|
|
await self.xknx.telegrams.put(telegram)
|
2017-09-07 07:11:55 +00:00
|
|
|
|
2021-02-20 19:09:23 +00:00
|
|
|
async def service_read_to_knx_bus(self, call):
|
|
|
|
"""Service for sending a GroupValueRead telegram to the KNX bus."""
|
2021-03-01 20:59:36 +00:00
|
|
|
for address in call.data.get(KNX_ADDRESS):
|
2021-02-20 19:09:23 +00:00
|
|
|
telegram = Telegram(
|
|
|
|
destination_address=GroupAddress(address),
|
|
|
|
payload=GroupValueRead(),
|
|
|
|
)
|
|
|
|
await self.xknx.telegrams.put(telegram)
|