Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KNX: new config option to broadcast outside temperature to KNX bus. #10708

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions homeassistant/components/knx.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import voluptuous as vol

from homeassistant.helpers import discovery
from homeassistant.helpers.event import async_track_state_change
import homeassistant.helpers.config_validation as cv
from homeassistant.const import EVENT_HOMEASSISTANT_STOP, \
CONF_HOST, CONF_PORT
Expand All @@ -27,6 +28,9 @@
CONF_KNX_FIRE_EVENT = "fire_event"
CONF_KNX_FIRE_EVENT_FILTER = "fire_event_filter"
CONF_KNX_STATE_UPDATER = "state_updater"
CONF_KNX_TIME_ADDRESS = "time_address"
CONF_KNX_OUTSIDE_TEMPERATURE_SENSOR = "outside_temperature_sensor"
CONF_KNX_OUTSIDE_TEMPERATURE_ADDRESS = "outside_temperature_address"

SERVICE_KNX_SEND = "send"
SERVICE_KNX_ATTR_ADDRESS = "address"
Expand Down Expand Up @@ -60,7 +64,12 @@
vol.All(
cv.ensure_list,
[cv.string]),
vol.Optional(CONF_KNX_TIME_ADDRESS): cv.string,
vol.Optional(CONF_KNX_STATE_UPDATER, default=True): cv.boolean,
vol.Inclusive(CONF_KNX_OUTSIDE_TEMPERATURE_SENSOR, 'outside_temp'):
cv.string,
vol.Inclusive(CONF_KNX_OUTSIDE_TEMPERATURE_ADDRESS, 'outside_temp'):
cv.string,
})
}, extra=vol.ALLOW_EXTRA)

Expand Down Expand Up @@ -97,6 +106,9 @@ def async_setup(hass, config):
ATTR_DISCOVER_DEVICES: found_devices
}, config))

if CONF_KNX_TIME_ADDRESS in config[DOMAIN]:
_add_time_device(hass, config)

hass.services.async_register(
DOMAIN, SERVICE_KNX_SEND,
hass.data[DATA_KNX].service_send_to_knx_bus,
Expand All @@ -105,6 +117,17 @@ def async_setup(hass, config):
return True


def _add_time_device(hass, config):
"""Create time broadcasting device and add it to xknx device queue."""
import xknx
group_address_time = config[DOMAIN][CONF_KNX_TIME_ADDRESS]
time = xknx.devices.Time(
hass.data[DATA_KNX].xknx,
'Time',
group_address=group_address_time)
hass.data[DATA_KNX].xknx.devices.add(time)


def _get_devices(hass, discovery_type):
return list(
map(lambda device: device.name,
Expand Down Expand Up @@ -207,6 +230,12 @@ def register_callbacks(self):
self.xknx.telegram_queue.register_telegram_received_cb(
self.telegram_received_cb, address_filters)

if CONF_KNX_OUTSIDE_TEMPERATURE_ADDRESS in self.config[DOMAIN]:
sensor_entity_id = \
self.config[DOMAIN][CONF_KNX_OUTSIDE_TEMPERATURE_SENSOR]
async_track_state_change(self.hass, sensor_entity_id,
self.async_broadcast_temperature)

@asyncio.coroutine
def telegram_received_cb(self, telegram):
"""Callback invoked after a KNX telegram was received."""
Expand Down Expand Up @@ -237,6 +266,20 @@ def calculate_payload(attr_payload):
telegram.group_address = address
yield from self.xknx.telegrams.put(telegram)

@asyncio.coroutine
def async_broadcast_temperature(self, entity_id, old_state, new_state):
"""Broadcast new temperature of sensor to KNX bus."""
from xknx.knx import Telegram, Address, DPTArray, DPTFloat
if new_state is None:
return
address = Address(
self.config[DOMAIN][CONF_KNX_OUTSIDE_TEMPERATURE_ADDRESS])
payload = DPTArray(DPTFloat().to_knx(float(new_state.state)))
telegram = Telegram()
telegram.group_address = address
telegram.payload = payload
yield from self.xknx.telegrams.put(telegram)


class KNXAutomation():
"""Wrapper around xknx.devices.ActionCallback object.."""
Expand Down