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

Use voluptuous for KNX #3345

Merged
merged 2 commits into from
Sep 14, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 4 additions & 7 deletions homeassistant/components/binary_sensor/knx.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,14 @@
https://home-assistant.io/components/binary_sensor.knx/
"""
from homeassistant.components.binary_sensor import BinarySensorDevice
from homeassistant.components.knx import (
KNXConfig, KNXGroupAddress)
from homeassistant.components.knx import (KNXConfig, KNXGroupAddress)

DEPENDENCIES = ["knx"]
DEPENDENCIES = ['knx']


def setup_platform(hass, config, add_entities, discovery_info=None):
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Setup the KNX binary sensor platform."""
add_entities([
KNXSwitch(hass, KNXConfig(config))
])
add_devices([KNXSwitch(hass, KNXConfig(config))])


class KNXSwitch(KNXGroupAddress, BinarySensorDevice):
Expand Down
44 changes: 27 additions & 17 deletions homeassistant/components/climate/knx.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,37 @@
Support for KNX thermostats.

For more details about this platform, please refer to the documentation
https://home-assistant.io/components/knx/
https://home-assistant.io/components/climate.knx/
"""
import logging

from homeassistant.components.climate import ClimateDevice
from homeassistant.const import TEMP_CELSIUS, ATTR_TEMPERATURE
import voluptuous as vol

from homeassistant.components.knx import (
KNXConfig, KNXMultiAddressDevice)

DEPENDENCIES = ["knx"]
from homeassistant.components.climate import (ClimateDevice, PLATFORM_SCHEMA)
from homeassistant.components.knx import (KNXConfig, KNXMultiAddressDevice)
from homeassistant.const import (CONF_NAME, TEMP_CELSIUS, ATTR_TEMPERATURE)
import homeassistant.helpers.config_validation as cv

_LOGGER = logging.getLogger(__name__)

CONF_ADDRESS = 'address'
CONF_SETPOINT_ADDRESS = 'setpoint_address'
CONF_TEMPERATURE_ADDRESS = 'temperature_address'

DEFAULT_NAME = 'KNX Thermostat'
DEPENDENCIES = ['knx']

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_ADDRESS): cv.string,
vol.Required(CONF_SETPOINT_ADDRESS): cv.string,
vol.Required(CONF_TEMPERATURE_ADDRESS): cv.string,
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
})


def setup_platform(hass, config, add_entities, discovery_info=None):
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Create and add an entity based on the configuration."""
add_entities([
KNXThermostat(hass, KNXConfig(config))
])
add_devices([KNXThermostat(hass, KNXConfig(config))])


class KNXThermostat(KNXMultiAddressDevice, ClimateDevice):
Expand All @@ -39,9 +50,8 @@ class KNXThermostat(KNXMultiAddressDevice, ClimateDevice):

def __init__(self, hass, config):
"""Initialize the thermostat based on the given configuration."""
KNXMultiAddressDevice.__init__(self, hass, config,
["temperature", "setpoint"],
["mode"])
KNXMultiAddressDevice.__init__(
self, hass, config, ['temperature', 'setpoint'], ['mode'])

self._unit_of_measurement = TEMP_CELSIUS # KNX always used celsius
self._away = False # not yet supported
Expand All @@ -62,14 +72,14 @@ def current_temperature(self):
"""Return the current temperature."""
from knxip.conversion import knx2_to_float

return knx2_to_float(self.value("temperature"))
return knx2_to_float(self.value('temperature'))

@property
def target_temperature(self):
"""Return the temperature we try to reach."""
from knxip.conversion import knx2_to_float

return knx2_to_float(self.value("setpoint"))
return knx2_to_float(self.value('setpoint'))

def set_temperature(self, **kwargs):
"""Set new target temperature."""
Expand All @@ -78,7 +88,7 @@ def set_temperature(self, **kwargs):
return
from knxip.conversion import float_to_knx2

self.set_value("setpoint", float_to_knx2(temperature))
self.set_value('setpoint', float_to_knx2(temperature))
_LOGGER.debug("Set target temperature to %s", temperature)

def set_operation_mode(self, operation_mode):
Expand Down
34 changes: 18 additions & 16 deletions homeassistant/components/knx.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,30 @@
"""
import logging

from homeassistant.const import EVENT_HOMEASSISTANT_STOP
import voluptuous as vol

from homeassistant.const import (
EVENT_HOMEASSISTANT_STOP, CONF_HOST, CONF_PORT)
from homeassistant.helpers.entity import Entity
import homeassistant.helpers.config_validation as cv

DOMAIN = "knx"
REQUIREMENTS = ['knxip==0.3.3']

EVENT_KNX_FRAME_RECEIVED = "knx_frame_received"
_LOGGER = logging.getLogger(__name__)

CONF_HOST = "host"
CONF_PORT = "port"
DEFAULT_PORT = '3671'
DOMAIN = 'knx'

DEFAULT_PORT = "3671"
EVENT_KNX_FRAME_RECEIVED = 'knx_frame_received'

KNXTUNNEL = None

_LOGGER = logging.getLogger(__name__)
CONFIG_SCHEMA = vol.Schema({
DOMAIN: vol.Schema({
vol.Required(CONF_HOST): cv.string,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be optional with a default value of 0.0.0.0

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your feedback. I will change it.

vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
}),
}, extra=vol.ALLOW_EXTRA)


def setup(hass, config):
Expand All @@ -31,17 +39,11 @@ def setup(hass, config):
from knxip.ip import KNXIPTunnel
from knxip.core import KNXException

host = config[DOMAIN].get(CONF_HOST, None)
host = config[DOMAIN].get(CONF_HOST)
port = config[DOMAIN].get(CONF_PORT)

if host is None:
if host is "0.0.0.0":
_LOGGER.debug("Will try to auto-detect KNX/IP gateway")
host = "0.0.0.0"

try:
port = int(config[DOMAIN].get(CONF_PORT, DEFAULT_PORT))
except ValueError:
_LOGGER.exception("Can't parse KNX IP interface port")
return False

KNXTUNNEL = KNXIPTunnel(host, port)
try:
Expand Down
27 changes: 19 additions & 8 deletions homeassistant/components/switch/knx.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,29 @@
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/switch.knx/
"""
from homeassistant.components.switch import SwitchDevice
from homeassistant.components.knx import (
KNXConfig, KNXGroupAddress)
import voluptuous as vol

DEPENDENCIES = ["knx"]
from homeassistant.components.knx import (KNXConfig, KNXGroupAddress)
from homeassistant.components.switch import (SwitchDevice, PLATFORM_SCHEMA)
from homeassistant.const import CONF_NAME
import homeassistant.helpers.config_validation as cv

CONF_ADDRESS = 'address'
CONF_STATE_ADDRESS = 'state_address'

def setup_platform(hass, config, add_entities, discovery_info=None):
DEFAULT_NAME = 'KNX Switch'
DEPENDENCIES = ['knx']

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_ADDRESS): cv.string,
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
vol.Optional(CONF_STATE_ADDRESS): cv.string,
})


def setup_platform(hass, config, add_devices, discovery_info=None):
"""Setup the KNX switch platform."""
add_entities([
KNXSwitch(hass, KNXConfig(config))
])
add_devices([KNXSwitch(hass, KNXConfig(config))])


class KNXSwitch(KNXGroupAddress, SwitchDevice):
Expand Down