mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +00:00
Added a service to write to KNX group addressed including documentation (#8491)
* Added a service to write to KNX group addressed including documentation * Define parameters as required * Reformating * Moved service documentation to service.yaml * Moved service documentation to services.yaml * Update knx.py
This commit is contained in:
parent
84ebcd8a59
commit
54755df9ea
@ -5,6 +5,7 @@ For more details about this component, please refer to the documentation at
|
|||||||
https://home-assistant.io/components/knx/
|
https://home-assistant.io/components/knx/
|
||||||
"""
|
"""
|
||||||
import logging
|
import logging
|
||||||
|
import os
|
||||||
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
@ -12,6 +13,7 @@ import homeassistant.helpers.config_validation as cv
|
|||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
EVENT_HOMEASSISTANT_STOP, CONF_HOST, CONF_PORT)
|
EVENT_HOMEASSISTANT_STOP, CONF_HOST, CONF_PORT)
|
||||||
from homeassistant.helpers.entity import Entity
|
from homeassistant.helpers.entity import Entity
|
||||||
|
from homeassistant.config import load_yaml_config_file
|
||||||
|
|
||||||
REQUIREMENTS = ['knxip==0.5']
|
REQUIREMENTS = ['knxip==0.5']
|
||||||
|
|
||||||
@ -25,6 +27,9 @@ EVENT_KNX_FRAME_RECEIVED = 'knx_frame_received'
|
|||||||
EVENT_KNX_FRAME_SEND = 'knx_frame_send'
|
EVENT_KNX_FRAME_SEND = 'knx_frame_send'
|
||||||
|
|
||||||
KNXTUNNEL = None
|
KNXTUNNEL = None
|
||||||
|
KNX_ADDRESS = "address"
|
||||||
|
KNX_DATA = "data"
|
||||||
|
KNX_GROUP_WRITE = "group_write"
|
||||||
CONF_LISTEN = "listen"
|
CONF_LISTEN = "listen"
|
||||||
|
|
||||||
CONFIG_SCHEMA = vol.Schema({
|
CONFIG_SCHEMA = vol.Schema({
|
||||||
@ -36,6 +41,11 @@ CONFIG_SCHEMA = vol.Schema({
|
|||||||
}),
|
}),
|
||||||
}, extra=vol.ALLOW_EXTRA)
|
}, extra=vol.ALLOW_EXTRA)
|
||||||
|
|
||||||
|
KNX_WRITE_SCHEMA = vol.Schema({
|
||||||
|
vol.Required(KNX_ADDRESS): vol.All(cv.ensure_list, [cv.string]),
|
||||||
|
vol.Required(KNX_DATA): vol.All(cv.ensure_list, [cv.byte])
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
def setup(hass, config):
|
def setup(hass, config):
|
||||||
"""Set up the connection to the KNX IP interface."""
|
"""Set up the connection to the KNX IP interface."""
|
||||||
@ -65,6 +75,9 @@ def setup(hass, config):
|
|||||||
|
|
||||||
_LOGGER.info("KNX IP tunnel to %s:%i established", host, port)
|
_LOGGER.info("KNX IP tunnel to %s:%i established", host, port)
|
||||||
|
|
||||||
|
descriptions = load_yaml_config_file(
|
||||||
|
os.path.join(os.path.dirname(__file__), 'services.yaml'))
|
||||||
|
|
||||||
def received_knx_event(address, data):
|
def received_knx_event(address, data):
|
||||||
"""Process received KNX message."""
|
"""Process received KNX message."""
|
||||||
if len(data) == 1:
|
if len(data) == 1:
|
||||||
@ -86,47 +99,37 @@ def setup(hass, config):
|
|||||||
hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, close_tunnel)
|
hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, close_tunnel)
|
||||||
|
|
||||||
# Listen to KNX events and send them to the bus
|
# Listen to KNX events and send them to the bus
|
||||||
def handle_knx_send(event):
|
def handle_group_write(call):
|
||||||
"""Bridge knx_frame_send events to the KNX bus."""
|
"""Bridge knx_frame_send events to the KNX bus."""
|
||||||
try:
|
# parameters are pre-validated using KNX_WRITE_SCHEMA
|
||||||
addr = event.data["address"]
|
addrlist = call.data.get("address")
|
||||||
except KeyError:
|
knxdata = call.data.get("data")
|
||||||
_LOGGER.error("KNX group address is missing")
|
|
||||||
return
|
|
||||||
|
|
||||||
try:
|
knxaddrlist = []
|
||||||
data = event.data["data"]
|
for addr in addrlist:
|
||||||
except KeyError:
|
|
||||||
_LOGGER.error("KNX data block missing")
|
|
||||||
return
|
|
||||||
|
|
||||||
knxaddr = None
|
|
||||||
try:
|
|
||||||
addr = int(addr)
|
|
||||||
except ValueError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
if knxaddr is None:
|
|
||||||
try:
|
try:
|
||||||
knxaddr = parse_group_address(addr)
|
_LOGGER.debug("Found %s", addr)
|
||||||
except KNXException:
|
knxaddr = int(addr)
|
||||||
_LOGGER.error("KNX address format incorrect")
|
|
||||||
return
|
|
||||||
|
|
||||||
knxdata = None
|
|
||||||
if isinstance(data, list):
|
|
||||||
knxdata = data
|
|
||||||
else:
|
|
||||||
try:
|
|
||||||
knxdata = [int(data) & 0xff]
|
|
||||||
except ValueError:
|
except ValueError:
|
||||||
_LOGGER.error("KNX data format incorrect")
|
knxaddr = None
|
||||||
return
|
|
||||||
|
|
||||||
KNXTUNNEL.group_write(knxaddr, knxdata)
|
if knxaddr is None:
|
||||||
|
try:
|
||||||
|
knxaddr = parse_group_address(addr)
|
||||||
|
except KNXException:
|
||||||
|
_LOGGER.error("KNX address format incorrect: %s", addr)
|
||||||
|
|
||||||
|
knxaddrlist.append(knxaddr)
|
||||||
|
|
||||||
|
for addr in knxaddrlist:
|
||||||
|
KNXTUNNEL.group_write(addr, knxdata)
|
||||||
|
|
||||||
# Listen for when knx_frame_send event is fired
|
# Listen for when knx_frame_send event is fired
|
||||||
hass.bus.listen(EVENT_KNX_FRAME_SEND, handle_knx_send)
|
hass.services.register(DOMAIN,
|
||||||
|
KNX_GROUP_WRITE,
|
||||||
|
handle_group_write,
|
||||||
|
descriptions[DOMAIN][KNX_GROUP_WRITE],
|
||||||
|
schema=KNX_WRITE_SCHEMA)
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
@ -520,3 +520,16 @@ wake_on_lan:
|
|||||||
broadcast_address:
|
broadcast_address:
|
||||||
description: Optional broadcast IP where to send the magic packet.
|
description: Optional broadcast IP where to send the magic packet.
|
||||||
example: '192.168.255.255'
|
example: '192.168.255.255'
|
||||||
|
|
||||||
|
knx:
|
||||||
|
group_write:
|
||||||
|
description: Turn a light on
|
||||||
|
|
||||||
|
fields:
|
||||||
|
address:
|
||||||
|
description: Group address(es) to write to
|
||||||
|
example: '1/1/0'
|
||||||
|
|
||||||
|
data:
|
||||||
|
description: KNX data to send
|
||||||
|
example: 1
|
||||||
|
Loading…
x
Reference in New Issue
Block a user