2016-06-24 08:06:58 +00:00
|
|
|
"""
|
2016-06-30 08:33:34 +00:00
|
|
|
Support for Homematic devices.
|
2016-06-24 08:06:58 +00:00
|
|
|
|
|
|
|
For more details about this component, please refer to the documentation at
|
|
|
|
https://home-assistant.io/components/homematic/
|
|
|
|
"""
|
2016-07-13 21:15:21 +00:00
|
|
|
import os
|
2016-06-24 08:06:58 +00:00
|
|
|
import time
|
|
|
|
import logging
|
2016-06-25 16:34:35 +00:00
|
|
|
from functools import partial
|
2016-07-13 21:15:21 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant.const import (EVENT_HOMEASSISTANT_STOP, STATE_UNKNOWN,
|
|
|
|
CONF_USERNAME, CONF_PASSWORD)
|
2016-06-24 08:06:58 +00:00
|
|
|
from homeassistant.helpers.entity import Entity
|
2016-07-13 21:15:21 +00:00
|
|
|
from homeassistant.helpers import discovery
|
|
|
|
from homeassistant.config import load_yaml_config_file
|
2016-06-24 08:06:58 +00:00
|
|
|
|
|
|
|
DOMAIN = 'homematic'
|
2016-08-09 20:55:25 +00:00
|
|
|
REQUIREMENTS = ["pyhomematic==0.1.11"]
|
2016-06-24 08:06:58 +00:00
|
|
|
|
|
|
|
HOMEMATIC = None
|
2016-06-26 21:18:18 +00:00
|
|
|
HOMEMATIC_LINK_DELAY = 0.5
|
2016-06-24 08:06:58 +00:00
|
|
|
|
2016-07-13 21:15:21 +00:00
|
|
|
DISCOVER_SWITCHES = 'homematic.switch'
|
|
|
|
DISCOVER_LIGHTS = 'homematic.light'
|
|
|
|
DISCOVER_SENSORS = 'homematic.sensor'
|
|
|
|
DISCOVER_BINARY_SENSORS = 'homematic.binary_sensor'
|
|
|
|
DISCOVER_ROLLERSHUTTER = 'homematic.rollershutter'
|
|
|
|
DISCOVER_THERMOSTATS = 'homematic.thermostat'
|
|
|
|
|
|
|
|
ATTR_DISCOVER_DEVICES = 'devices'
|
|
|
|
ATTR_PARAM = 'param'
|
|
|
|
ATTR_CHANNEL = 'channel'
|
|
|
|
ATTR_NAME = 'name'
|
|
|
|
ATTR_ADDRESS = 'address'
|
2016-06-24 08:06:58 +00:00
|
|
|
|
2016-07-13 21:15:21 +00:00
|
|
|
EVENT_KEYPRESS = 'homematic.keypress'
|
|
|
|
EVENT_IMPULSE = 'homematic.impulse'
|
2016-06-29 20:42:35 +00:00
|
|
|
|
2016-07-13 21:15:21 +00:00
|
|
|
SERVICE_VIRTUALKEY = 'virtualkey'
|
2016-06-24 08:06:58 +00:00
|
|
|
|
|
|
|
HM_DEVICE_TYPES = {
|
2016-07-13 21:15:21 +00:00
|
|
|
DISCOVER_SWITCHES: ['Switch', 'SwitchPowermeter'],
|
|
|
|
DISCOVER_LIGHTS: ['Dimmer'],
|
|
|
|
DISCOVER_SENSORS: ['SwitchPowermeter', 'Motion', 'MotionV2',
|
|
|
|
'RemoteMotion', 'ThermostatWall', 'AreaThermostat',
|
|
|
|
'RotaryHandleSensor', 'WaterSensor', 'PowermeterGas',
|
|
|
|
'LuxSensor'],
|
|
|
|
DISCOVER_THERMOSTATS: ['Thermostat', 'ThermostatWall', 'MAXThermostat'],
|
|
|
|
DISCOVER_BINARY_SENSORS: ['ShutterContact', 'Smoke', 'SmokeV2', 'Motion',
|
|
|
|
'MotionV2', 'RemoteMotion'],
|
|
|
|
DISCOVER_ROLLERSHUTTER: ['Blind']
|
2016-06-24 08:06:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
HM_IGNORE_DISCOVERY_NODE = [
|
2016-07-13 21:15:21 +00:00
|
|
|
'ACTUAL_TEMPERATURE'
|
2016-06-24 08:06:58 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
HM_ATTRIBUTE_SUPPORT = {
|
2016-07-13 21:15:21 +00:00
|
|
|
'LOWBAT': ['Battery', {0: 'High', 1: 'Low'}],
|
|
|
|
'ERROR': ['Sabotage', {0: 'No', 1: 'Yes'}],
|
|
|
|
'RSSI_DEVICE': ['RSSI', {}],
|
|
|
|
'VALVE_STATE': ['Valve', {}],
|
|
|
|
'BATTERY_STATE': ['Battery', {}],
|
|
|
|
'CONTROL_MODE': ['Mode', {0: 'Auto', 1: 'Manual', 2: 'Away', 3: 'Boost'}],
|
|
|
|
'POWER': ['Power', {}],
|
|
|
|
'CURRENT': ['Current', {}],
|
|
|
|
'VOLTAGE': ['Voltage', {}]
|
2016-06-24 08:06:58 +00:00
|
|
|
}
|
|
|
|
|
2016-06-29 20:42:35 +00:00
|
|
|
HM_PRESS_EVENTS = [
|
2016-07-13 21:15:21 +00:00
|
|
|
'PRESS_SHORT',
|
|
|
|
'PRESS_LONG',
|
|
|
|
'PRESS_CONT',
|
|
|
|
'PRESS_LONG_RELEASE'
|
|
|
|
]
|
|
|
|
|
|
|
|
HM_IMPULSE_EVENTS = [
|
|
|
|
'SEQUENCE_OK'
|
2016-06-29 20:42:35 +00:00
|
|
|
]
|
|
|
|
|
2016-06-24 08:06:58 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2016-07-13 21:15:21 +00:00
|
|
|
CONF_RESOLVENAMES_OPTIONS = [
|
|
|
|
'metadata',
|
|
|
|
'json',
|
|
|
|
'xml',
|
|
|
|
False
|
|
|
|
]
|
|
|
|
|
|
|
|
CONF_LOCAL_IP = 'local_ip'
|
|
|
|
CONF_LOCAL_PORT = 'local_port'
|
|
|
|
CONF_REMOTE_IP = 'remote_ip'
|
|
|
|
CONF_REMOTE_PORT = 'remote_port'
|
|
|
|
CONF_RESOLVENAMES = 'resolvenames'
|
|
|
|
CONF_DELAY = 'delay'
|
|
|
|
|
|
|
|
PLATFORM_SCHEMA = vol.Schema({
|
|
|
|
vol.Required(CONF_LOCAL_IP): vol.Coerce(str),
|
|
|
|
vol.Optional(CONF_LOCAL_PORT, default=8943):
|
|
|
|
vol.All(vol.Coerce(int),
|
|
|
|
vol.Range(min=1, max=65535)),
|
|
|
|
vol.Required(CONF_REMOTE_IP): vol.Coerce(str),
|
|
|
|
vol.Optional(CONF_REMOTE_PORT, default=2001):
|
|
|
|
vol.All(vol.Coerce(int),
|
|
|
|
vol.Range(min=1, max=65535)),
|
|
|
|
vol.Optional(CONF_RESOLVENAMES, default=False):
|
|
|
|
vol.In(CONF_RESOLVENAMES_OPTIONS),
|
|
|
|
vol.Optional(CONF_USERNAME, default="Admin"): vol.Coerce(str),
|
|
|
|
vol.Optional(CONF_PASSWORD, default=""): vol.Coerce(str),
|
|
|
|
vol.Optional(CONF_DELAY, default=0.5): vol.Coerce(float)
|
|
|
|
})
|
|
|
|
|
|
|
|
SCHEMA_SERVICE_VIRTUALKEY = vol.Schema({
|
|
|
|
vol.Required(ATTR_ADDRESS): vol.Coerce(str),
|
|
|
|
vol.Required(ATTR_CHANNEL): vol.Coerce(int),
|
|
|
|
vol.Required(ATTR_PARAM): vol.Coerce(str)
|
|
|
|
})
|
|
|
|
|
2016-06-24 08:06:58 +00:00
|
|
|
|
|
|
|
# pylint: disable=unused-argument
|
|
|
|
def setup(hass, config):
|
|
|
|
"""Setup the Homematic component."""
|
2016-06-26 21:18:18 +00:00
|
|
|
global HOMEMATIC, HOMEMATIC_LINK_DELAY
|
2016-06-24 08:06:58 +00:00
|
|
|
|
|
|
|
from pyhomematic import HMConnection
|
|
|
|
|
2016-07-13 21:15:21 +00:00
|
|
|
local_ip = config[DOMAIN][0].get(CONF_LOCAL_IP)
|
|
|
|
local_port = config[DOMAIN][0].get(CONF_LOCAL_PORT)
|
|
|
|
remote_ip = config[DOMAIN][0].get(CONF_REMOTE_IP)
|
|
|
|
remote_port = config[DOMAIN][0].get(CONF_REMOTE_PORT)
|
|
|
|
resolvenames = config[DOMAIN][0].get(CONF_RESOLVENAMES)
|
|
|
|
username = config[DOMAIN][0].get(CONF_USERNAME)
|
|
|
|
password = config[DOMAIN][0].get(CONF_PASSWORD)
|
|
|
|
HOMEMATIC_LINK_DELAY = config[DOMAIN][0].get(CONF_DELAY)
|
2016-06-24 08:06:58 +00:00
|
|
|
|
|
|
|
if remote_ip is None or local_ip is None:
|
|
|
|
_LOGGER.error("Missing remote CCU/Homegear or local address")
|
|
|
|
return False
|
|
|
|
|
|
|
|
# Create server thread
|
2016-06-25 16:34:35 +00:00
|
|
|
bound_system_callback = partial(system_callback_handler, hass, config)
|
2016-06-24 08:06:58 +00:00
|
|
|
HOMEMATIC = HMConnection(local=local_ip,
|
|
|
|
localport=local_port,
|
|
|
|
remote=remote_ip,
|
|
|
|
remoteport=remote_port,
|
2016-06-25 16:34:35 +00:00
|
|
|
systemcallback=bound_system_callback,
|
2016-06-25 16:54:14 +00:00
|
|
|
resolvenames=resolvenames,
|
2016-06-29 20:42:35 +00:00
|
|
|
rpcusername=username,
|
|
|
|
rpcpassword=password,
|
2016-06-24 08:06:58 +00:00
|
|
|
interface_id="homeassistant")
|
|
|
|
|
|
|
|
# Start server thread, connect to peer, initialize to receive events
|
|
|
|
HOMEMATIC.start()
|
|
|
|
|
|
|
|
# Stops server when Homeassistant is shutting down
|
|
|
|
hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, HOMEMATIC.stop)
|
|
|
|
hass.config.components.append(DOMAIN)
|
|
|
|
|
2016-07-13 21:15:21 +00:00
|
|
|
# regeister homematic services
|
|
|
|
descriptions = load_yaml_config_file(
|
|
|
|
os.path.join(os.path.dirname(__file__), 'services.yaml'))
|
|
|
|
|
|
|
|
hass.services.register(DOMAIN, SERVICE_VIRTUALKEY,
|
|
|
|
_hm_service_virtualkey,
|
|
|
|
descriptions[DOMAIN][SERVICE_VIRTUALKEY],
|
|
|
|
SCHEMA_SERVICE_VIRTUALKEY)
|
|
|
|
|
2016-06-24 08:06:58 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
# pylint: disable=too-many-branches
|
2016-06-25 16:34:35 +00:00
|
|
|
def system_callback_handler(hass, config, src, *args):
|
2016-06-24 08:06:58 +00:00
|
|
|
"""Callback handler."""
|
|
|
|
if src == 'newDevices':
|
2016-06-26 21:18:18 +00:00
|
|
|
_LOGGER.debug("newDevices with: %s", str(args))
|
2016-06-24 08:06:58 +00:00
|
|
|
# pylint: disable=unused-variable
|
|
|
|
(interface_id, dev_descriptions) = args
|
|
|
|
key_dict = {}
|
|
|
|
# Get list of all keys of the devices (ignoring channels)
|
|
|
|
for dev in dev_descriptions:
|
|
|
|
key_dict[dev['ADDRESS'].split(':')[0]] = True
|
2016-06-26 21:18:18 +00:00
|
|
|
|
2016-06-29 20:42:35 +00:00
|
|
|
# Register EVENTS
|
|
|
|
# Search all device with a EVENTNODE that include data
|
|
|
|
bound_event_callback = partial(_hm_event_handler, hass)
|
|
|
|
for dev in key_dict:
|
|
|
|
if dev not in HOMEMATIC.devices:
|
|
|
|
continue
|
|
|
|
|
|
|
|
hmdevice = HOMEMATIC.devices.get(dev)
|
|
|
|
# have events?
|
|
|
|
if len(hmdevice.EVENTNODE) > 0:
|
|
|
|
_LOGGER.debug("Register Events from %s", dev)
|
|
|
|
hmdevice.setEventCallback(callback=bound_event_callback,
|
|
|
|
bequeath=True)
|
|
|
|
|
2016-06-24 08:06:58 +00:00
|
|
|
# If configuration allows autodetection of devices,
|
|
|
|
# all devices not configured are added.
|
2016-06-28 20:53:53 +00:00
|
|
|
if key_dict:
|
2016-06-24 08:06:58 +00:00
|
|
|
for component_name, discovery_type in (
|
|
|
|
('switch', DISCOVER_SWITCHES),
|
|
|
|
('light', DISCOVER_LIGHTS),
|
|
|
|
('rollershutter', DISCOVER_ROLLERSHUTTER),
|
|
|
|
('binary_sensor', DISCOVER_BINARY_SENSORS),
|
|
|
|
('sensor', DISCOVER_SENSORS),
|
|
|
|
('thermostat', DISCOVER_THERMOSTATS)):
|
|
|
|
# Get all devices of a specific type
|
2016-06-28 20:53:53 +00:00
|
|
|
found_devices = _get_devices(discovery_type, key_dict)
|
2016-06-24 08:06:58 +00:00
|
|
|
|
|
|
|
# When devices of this type are found
|
|
|
|
# they are setup in HA and an event is fired
|
|
|
|
if found_devices:
|
2016-06-25 19:03:41 +00:00
|
|
|
# Fire discovery event
|
2016-06-25 19:37:51 +00:00
|
|
|
discovery.load_platform(hass, component_name, DOMAIN, {
|
|
|
|
ATTR_DISCOVER_DEVICES: found_devices
|
|
|
|
}, config)
|
2016-06-25 13:10:19 +00:00
|
|
|
|
2016-06-24 08:06:58 +00:00
|
|
|
|
|
|
|
def _get_devices(device_type, keys):
|
2016-06-30 08:33:34 +00:00
|
|
|
"""Get the Homematic devices."""
|
2016-06-24 08:06:58 +00:00
|
|
|
# run
|
|
|
|
device_arr = []
|
|
|
|
for key in keys:
|
|
|
|
device = HOMEMATIC.devices[key]
|
2016-06-29 20:42:35 +00:00
|
|
|
class_name = device.__class__.__name__
|
2016-06-25 13:10:19 +00:00
|
|
|
metadata = {}
|
|
|
|
|
2016-06-29 20:42:35 +00:00
|
|
|
# is class supported by discovery type
|
|
|
|
if class_name not in HM_DEVICE_TYPES[device_type]:
|
|
|
|
continue
|
|
|
|
|
2016-06-25 13:10:19 +00:00
|
|
|
# Load metadata if needed to generate a param list
|
2016-06-25 16:19:05 +00:00
|
|
|
if device_type == DISCOVER_SENSORS:
|
2016-06-25 13:10:19 +00:00
|
|
|
metadata.update(device.SENSORNODE)
|
2016-06-25 16:19:05 +00:00
|
|
|
elif device_type == DISCOVER_BINARY_SENSORS:
|
2016-06-25 13:10:19 +00:00
|
|
|
metadata.update(device.BINARYNODE)
|
|
|
|
|
2016-06-29 20:42:35 +00:00
|
|
|
params = _create_params_list(device, metadata, device_type)
|
2016-06-25 16:19:05 +00:00
|
|
|
if params:
|
|
|
|
# Generate options for 1...n elements with 1...n params
|
|
|
|
for channel in range(1, device.ELEMENT + 1):
|
|
|
|
_LOGGER.debug("Handling %s:%i", key, channel)
|
|
|
|
if channel in params:
|
|
|
|
for param in params[channel]:
|
|
|
|
name = _create_ha_name(name=device.NAME,
|
|
|
|
channel=channel,
|
|
|
|
param=param)
|
|
|
|
device_dict = dict(platform="homematic",
|
|
|
|
address=key,
|
|
|
|
name=name,
|
2016-06-29 20:42:35 +00:00
|
|
|
channel=channel)
|
2016-06-25 16:19:05 +00:00
|
|
|
if param is not None:
|
2016-06-29 20:42:35 +00:00
|
|
|
device_dict[ATTR_PARAM] = param
|
2016-06-25 16:19:05 +00:00
|
|
|
|
|
|
|
# Add new device
|
|
|
|
device_arr.append(device_dict)
|
|
|
|
else:
|
|
|
|
_LOGGER.debug("Channel %i not in params", channel)
|
|
|
|
else:
|
|
|
|
_LOGGER.debug("Got no params for %s", key)
|
|
|
|
_LOGGER.debug("%s autodiscovery: %s",
|
|
|
|
device_type, str(device_arr))
|
2016-06-24 08:06:58 +00:00
|
|
|
return device_arr
|
|
|
|
|
|
|
|
|
2016-06-29 20:42:35 +00:00
|
|
|
def _create_params_list(hmdevice, metadata, device_type):
|
2016-06-24 08:06:58 +00:00
|
|
|
"""Create a list from HMDevice with all possible parameters in config."""
|
|
|
|
params = {}
|
2016-06-29 20:42:35 +00:00
|
|
|
merge = False
|
|
|
|
|
|
|
|
# use merge?
|
|
|
|
if device_type == DISCOVER_SENSORS:
|
|
|
|
merge = True
|
|
|
|
elif device_type == DISCOVER_BINARY_SENSORS:
|
|
|
|
merge = True
|
2016-06-24 08:06:58 +00:00
|
|
|
|
|
|
|
# Search in sensor and binary metadata per elements
|
2016-06-25 16:19:05 +00:00
|
|
|
for channel in range(1, hmdevice.ELEMENT + 1):
|
2016-06-24 08:06:58 +00:00
|
|
|
param_chan = []
|
2016-06-29 20:42:35 +00:00
|
|
|
for node, meta_chan in metadata.items():
|
|
|
|
try:
|
2016-06-25 16:19:05 +00:00
|
|
|
# Is this attribute ignored?
|
|
|
|
if node in HM_IGNORE_DISCOVERY_NODE:
|
|
|
|
continue
|
|
|
|
if meta_chan == 'c' or meta_chan is None:
|
|
|
|
# Only channel linked data
|
|
|
|
param_chan.append(node)
|
|
|
|
elif channel == 1:
|
|
|
|
# First channel can have other data channel
|
|
|
|
param_chan.append(node)
|
2016-06-29 20:42:35 +00:00
|
|
|
except (TypeError, ValueError):
|
|
|
|
_LOGGER.error("Exception generating %s (%s)",
|
|
|
|
hmdevice.ADDRESS, str(metadata))
|
|
|
|
|
|
|
|
# default parameter is merge is off
|
|
|
|
if len(param_chan) == 0 and not merge:
|
2016-06-24 08:06:58 +00:00
|
|
|
param_chan.append(None)
|
2016-06-29 20:42:35 +00:00
|
|
|
|
2016-06-24 08:06:58 +00:00
|
|
|
# Add to channel
|
2016-06-29 20:42:35 +00:00
|
|
|
if len(param_chan) > 0:
|
|
|
|
params.update({channel: param_chan})
|
2016-06-24 08:06:58 +00:00
|
|
|
|
|
|
|
_LOGGER.debug("Create param list for %s with: %s", hmdevice.ADDRESS,
|
|
|
|
str(params))
|
|
|
|
return params
|
|
|
|
|
|
|
|
|
|
|
|
def _create_ha_name(name, channel, param):
|
|
|
|
"""Generate a unique object name."""
|
|
|
|
# HMDevice is a simple device
|
|
|
|
if channel == 1 and param is None:
|
|
|
|
return name
|
|
|
|
|
|
|
|
# Has multiple elements/channels
|
|
|
|
if channel > 1 and param is None:
|
2016-06-25 13:10:19 +00:00
|
|
|
return "{} {}".format(name, channel)
|
2016-06-24 08:06:58 +00:00
|
|
|
|
|
|
|
# With multiple param first elements
|
|
|
|
if channel == 1 and param is not None:
|
2016-06-25 13:10:19 +00:00
|
|
|
return "{} {}".format(name, param)
|
2016-06-24 08:06:58 +00:00
|
|
|
|
|
|
|
# Multiple param on object with multiple elements
|
|
|
|
if channel > 1 and param is not None:
|
2016-06-25 13:10:19 +00:00
|
|
|
return "{} {} {}".format(name, channel, param)
|
2016-06-24 08:06:58 +00:00
|
|
|
|
|
|
|
|
2016-06-25 19:37:51 +00:00
|
|
|
def setup_hmdevice_discovery_helper(hmdevicetype, discovery_info,
|
|
|
|
add_callback_devices):
|
|
|
|
"""Helper to setup Homematic devices with discovery info."""
|
2016-06-29 20:42:35 +00:00
|
|
|
for config in discovery_info[ATTR_DISCOVER_DEVICES]:
|
2016-06-28 20:53:53 +00:00
|
|
|
_LOGGER.debug("Add device %s from config: %s",
|
|
|
|
str(hmdevicetype), str(config))
|
2016-06-24 08:06:58 +00:00
|
|
|
|
2016-06-28 20:53:53 +00:00
|
|
|
# create object and add to HA
|
|
|
|
new_device = hmdevicetype(config)
|
|
|
|
add_callback_devices([new_device])
|
2016-06-24 08:06:58 +00:00
|
|
|
|
2016-06-28 20:53:53 +00:00
|
|
|
# link to HM
|
|
|
|
new_device.link_homematic()
|
2016-06-26 21:18:18 +00:00
|
|
|
|
2016-06-24 08:06:58 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
2016-06-29 20:42:35 +00:00
|
|
|
def _hm_event_handler(hass, device, caller, attribute, value):
|
|
|
|
"""Handle all pyhomematic device events."""
|
2016-06-30 21:54:04 +00:00
|
|
|
try:
|
|
|
|
channel = int(device.split(":")[1])
|
|
|
|
address = device.split(":")[0]
|
|
|
|
hmdevice = HOMEMATIC.devices.get(address)
|
|
|
|
except (TypeError, ValueError):
|
|
|
|
_LOGGER.error("Event handling channel convert error!")
|
|
|
|
return
|
2016-06-29 20:42:35 +00:00
|
|
|
|
|
|
|
# is not a event?
|
|
|
|
if attribute not in hmdevice.EVENTNODE:
|
|
|
|
return
|
|
|
|
|
2016-06-30 21:54:04 +00:00
|
|
|
_LOGGER.debug("Event %s for %s channel %i", attribute,
|
2016-06-29 20:42:35 +00:00
|
|
|
hmdevice.NAME, channel)
|
|
|
|
|
2016-07-13 21:15:21 +00:00
|
|
|
# keypress event
|
2016-06-29 20:42:35 +00:00
|
|
|
if attribute in HM_PRESS_EVENTS:
|
|
|
|
hass.bus.fire(EVENT_KEYPRESS, {
|
|
|
|
ATTR_NAME: hmdevice.NAME,
|
|
|
|
ATTR_PARAM: attribute,
|
|
|
|
ATTR_CHANNEL: channel
|
|
|
|
})
|
|
|
|
return
|
|
|
|
|
2016-07-13 21:15:21 +00:00
|
|
|
# impulse event
|
|
|
|
if attribute in HM_IMPULSE_EVENTS:
|
|
|
|
hass.bus.fire(EVENT_KEYPRESS, {
|
|
|
|
ATTR_NAME: hmdevice.NAME,
|
|
|
|
ATTR_CHANNEL: channel
|
|
|
|
})
|
|
|
|
return
|
|
|
|
|
2016-06-29 20:42:35 +00:00
|
|
|
_LOGGER.warning("Event is unknown and not forwarded to HA")
|
|
|
|
|
|
|
|
|
2016-07-13 21:15:21 +00:00
|
|
|
def _hm_service_virtualkey(call):
|
|
|
|
"""Callback for handle virtualkey services."""
|
|
|
|
address = call.data.get(ATTR_ADDRESS)
|
|
|
|
channel = call.data.get(ATTR_CHANNEL)
|
|
|
|
param = call.data.get(ATTR_PARAM)
|
|
|
|
|
|
|
|
if address not in HOMEMATIC.devices:
|
|
|
|
_LOGGER.error("%s not found for service virtualkey!", address)
|
|
|
|
return
|
|
|
|
hmdevice = HOMEMATIC.devices.get(address)
|
|
|
|
|
|
|
|
# if param exists for this device
|
|
|
|
if param not in hmdevice.ACTIONNODE:
|
|
|
|
_LOGGER.error("%s not datapoint in hm device %s", param, address)
|
|
|
|
return
|
|
|
|
|
|
|
|
# channel exists?
|
|
|
|
if channel > hmdevice.ELEMENT:
|
|
|
|
_LOGGER.error("%i is not a channel in hm device %s", channel, address)
|
|
|
|
return
|
|
|
|
|
|
|
|
# call key
|
|
|
|
hmdevice.actionNodeData(param, 1, channel)
|
|
|
|
|
|
|
|
|
2016-06-24 08:06:58 +00:00
|
|
|
class HMDevice(Entity):
|
2016-06-30 08:33:34 +00:00
|
|
|
"""The Homematic device base object."""
|
2016-06-24 08:06:58 +00:00
|
|
|
|
|
|
|
# pylint: disable=too-many-instance-attributes
|
|
|
|
def __init__(self, config):
|
2016-06-30 08:33:34 +00:00
|
|
|
"""Initialize a generic Homematic device."""
|
2016-06-29 20:42:35 +00:00
|
|
|
self._name = config.get(ATTR_NAME, None)
|
|
|
|
self._address = config.get(ATTR_ADDRESS, None)
|
|
|
|
self._channel = config.get(ATTR_CHANNEL, 1)
|
|
|
|
self._state = config.get(ATTR_PARAM, None)
|
2016-06-24 08:06:58 +00:00
|
|
|
self._data = {}
|
|
|
|
self._hmdevice = None
|
|
|
|
self._connected = False
|
|
|
|
self._available = False
|
|
|
|
|
|
|
|
# Set param to uppercase
|
|
|
|
if self._state:
|
|
|
|
self._state = self._state.upper()
|
|
|
|
|
|
|
|
# Generate name
|
|
|
|
if not self._name:
|
|
|
|
self._name = _create_ha_name(name=self._address,
|
|
|
|
channel=self._channel,
|
|
|
|
param=self._state)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def should_poll(self):
|
2016-06-30 08:33:34 +00:00
|
|
|
"""Return false. Homematic states are pushed by the XML RPC Server."""
|
2016-06-24 08:06:58 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the device."""
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def assumed_state(self):
|
2016-06-30 08:33:34 +00:00
|
|
|
"""Return true if unable to access real state of the device."""
|
2016-06-24 08:06:58 +00:00
|
|
|
return not self._available
|
|
|
|
|
|
|
|
@property
|
|
|
|
def available(self):
|
2016-06-30 08:33:34 +00:00
|
|
|
"""Return true if device is available."""
|
2016-06-24 08:06:58 +00:00
|
|
|
return self._available
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Return device specific state attributes."""
|
|
|
|
attr = {}
|
|
|
|
|
2016-06-29 20:42:35 +00:00
|
|
|
# no data available to create
|
|
|
|
if not self.available:
|
|
|
|
return attr
|
|
|
|
|
2016-06-24 08:06:58 +00:00
|
|
|
# Generate an attributes list
|
|
|
|
for node, data in HM_ATTRIBUTE_SUPPORT.items():
|
|
|
|
# Is an attributes and exists for this object
|
|
|
|
if node in self._data:
|
|
|
|
value = data[1].get(self._data[node], self._data[node])
|
|
|
|
attr[data[0]] = value
|
|
|
|
|
2016-06-29 20:42:35 +00:00
|
|
|
# static attributes
|
|
|
|
attr["ID"] = self._hmdevice.ADDRESS
|
|
|
|
|
2016-06-24 08:06:58 +00:00
|
|
|
return attr
|
|
|
|
|
2016-06-26 21:18:18 +00:00
|
|
|
def link_homematic(self):
|
2016-06-30 08:33:34 +00:00
|
|
|
"""Connect to Homematic."""
|
2016-06-26 21:18:18 +00:00
|
|
|
# device is already linked
|
|
|
|
if self._connected:
|
|
|
|
return True
|
|
|
|
|
2016-06-24 08:06:58 +00:00
|
|
|
# Does a HMDevice from pyhomematic exist?
|
|
|
|
if self._address in HOMEMATIC.devices:
|
|
|
|
# Init
|
|
|
|
self._hmdevice = HOMEMATIC.devices[self._address]
|
|
|
|
self._connected = True
|
|
|
|
|
2016-06-30 08:33:34 +00:00
|
|
|
# Check if Homematic class is okay for HA class
|
2016-06-24 08:06:58 +00:00
|
|
|
_LOGGER.info("Start linking %s to %s", self._address, self._name)
|
|
|
|
if self._check_hm_to_ha_object():
|
2016-06-25 14:25:33 +00:00
|
|
|
try:
|
|
|
|
# Init datapoints of this object
|
|
|
|
self._init_data_struct()
|
2016-06-26 21:18:18 +00:00
|
|
|
if HOMEMATIC_LINK_DELAY:
|
2016-06-25 14:25:33 +00:00
|
|
|
# We delay / pause loading of data to avoid overloading
|
|
|
|
# of CCU / Homegear when doing auto detection
|
2016-06-26 21:18:18 +00:00
|
|
|
time.sleep(HOMEMATIC_LINK_DELAY)
|
2016-06-25 14:25:33 +00:00
|
|
|
self._load_init_data_from_hm()
|
|
|
|
_LOGGER.debug("%s datastruct: %s",
|
|
|
|
self._name, str(self._data))
|
|
|
|
|
|
|
|
# Link events from pyhomatic
|
|
|
|
self._subscribe_homematic_events()
|
|
|
|
self._available = not self._hmdevice.UNREACH
|
|
|
|
# pylint: disable=broad-except
|
|
|
|
except Exception as err:
|
|
|
|
self._connected = False
|
2016-06-25 20:20:09 +00:00
|
|
|
_LOGGER.error("Exception while linking %s: %s",
|
|
|
|
self._address, str(err))
|
2016-06-24 08:06:58 +00:00
|
|
|
else:
|
2016-06-30 08:33:34 +00:00
|
|
|
_LOGGER.critical("Delink %s object from HM", self._name)
|
2016-06-24 08:06:58 +00:00
|
|
|
self._connected = False
|
|
|
|
|
|
|
|
# Update HA
|
2016-06-26 21:18:18 +00:00
|
|
|
_LOGGER.debug("%s linking done, send update_ha_state", self._name)
|
2016-06-24 08:06:58 +00:00
|
|
|
self.update_ha_state()
|
2016-06-25 14:25:33 +00:00
|
|
|
else:
|
|
|
|
_LOGGER.debug("%s not found in HOMEMATIC.devices", self._address)
|
2016-06-24 08:06:58 +00:00
|
|
|
|
|
|
|
def _hm_event_callback(self, device, caller, attribute, value):
|
|
|
|
"""Handle all pyhomematic device events."""
|
2016-06-26 21:18:18 +00:00
|
|
|
_LOGGER.debug("%s received event '%s' value: %s", self._name,
|
2016-06-24 08:06:58 +00:00
|
|
|
attribute, value)
|
|
|
|
have_change = False
|
|
|
|
|
|
|
|
# Is data needed for this instance?
|
|
|
|
if attribute in self._data:
|
|
|
|
# Did data change?
|
|
|
|
if self._data[attribute] != value:
|
|
|
|
self._data[attribute] = value
|
|
|
|
have_change = True
|
|
|
|
|
|
|
|
# If available it has changed
|
|
|
|
if attribute is "UNREACH":
|
|
|
|
self._available = bool(value)
|
|
|
|
have_change = True
|
|
|
|
|
2016-06-29 20:42:35 +00:00
|
|
|
# If it has changed data point, update HA
|
2016-06-24 08:06:58 +00:00
|
|
|
if have_change:
|
|
|
|
_LOGGER.debug("%s update_ha_state after '%s'", self._name,
|
|
|
|
attribute)
|
|
|
|
self.update_ha_state()
|
|
|
|
|
|
|
|
def _subscribe_homematic_events(self):
|
|
|
|
"""Subscribe all required events to handle job."""
|
|
|
|
channels_to_sub = {}
|
|
|
|
|
|
|
|
# Push data to channels_to_sub from hmdevice metadata
|
|
|
|
for metadata in (self._hmdevice.SENSORNODE, self._hmdevice.BINARYNODE,
|
|
|
|
self._hmdevice.ATTRIBUTENODE,
|
|
|
|
self._hmdevice.WRITENODE, self._hmdevice.EVENTNODE,
|
|
|
|
self._hmdevice.ACTIONNODE):
|
|
|
|
for node, channel in metadata.items():
|
|
|
|
# Data is needed for this instance
|
|
|
|
if node in self._data:
|
|
|
|
# chan is current channel
|
|
|
|
if channel == 'c' or channel is None:
|
|
|
|
channel = self._channel
|
|
|
|
# Prepare for subscription
|
|
|
|
try:
|
2016-07-13 21:15:21 +00:00
|
|
|
if int(channel) >= 0:
|
2016-06-24 08:06:58 +00:00
|
|
|
channels_to_sub.update({int(channel): True})
|
|
|
|
except (ValueError, TypeError):
|
|
|
|
_LOGGER("Invalid channel in metadata from %s",
|
|
|
|
self._name)
|
|
|
|
|
|
|
|
# Set callbacks
|
|
|
|
for channel in channels_to_sub:
|
|
|
|
_LOGGER.debug("Subscribe channel %s from %s",
|
|
|
|
str(channel), self._name)
|
|
|
|
self._hmdevice.setEventCallback(callback=self._hm_event_callback,
|
|
|
|
bequeath=False,
|
|
|
|
channel=channel)
|
|
|
|
|
|
|
|
def _load_init_data_from_hm(self):
|
|
|
|
"""Load first value from pyhomematic."""
|
|
|
|
if not self._connected:
|
|
|
|
return False
|
|
|
|
|
|
|
|
# Read data from pyhomematic
|
|
|
|
for metadata, funct in (
|
|
|
|
(self._hmdevice.ATTRIBUTENODE,
|
|
|
|
self._hmdevice.getAttributeData),
|
|
|
|
(self._hmdevice.WRITENODE, self._hmdevice.getWriteData),
|
|
|
|
(self._hmdevice.SENSORNODE, self._hmdevice.getSensorData),
|
|
|
|
(self._hmdevice.BINARYNODE, self._hmdevice.getBinaryData)):
|
|
|
|
for node in metadata:
|
|
|
|
if node in self._data:
|
|
|
|
self._data[node] = funct(name=node, channel=self._channel)
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
def _hm_set_state(self, value):
|
2016-06-29 20:42:35 +00:00
|
|
|
"""Set data to main datapoint."""
|
2016-06-24 08:06:58 +00:00
|
|
|
if self._state in self._data:
|
|
|
|
self._data[self._state] = value
|
|
|
|
|
|
|
|
def _hm_get_state(self):
|
2016-06-29 20:42:35 +00:00
|
|
|
"""Get data from main datapoint."""
|
2016-06-24 08:06:58 +00:00
|
|
|
if self._state in self._data:
|
|
|
|
return self._data[self._state]
|
|
|
|
return None
|
|
|
|
|
|
|
|
def _check_hm_to_ha_object(self):
|
2016-06-30 08:33:34 +00:00
|
|
|
"""Check if it is possible to use the Homematic object as this HA type.
|
2016-06-24 08:06:58 +00:00
|
|
|
|
|
|
|
NEEDS overwrite by inherit!
|
|
|
|
"""
|
|
|
|
if not self._connected or self._hmdevice is None:
|
|
|
|
_LOGGER.error("HA object is not linked to homematic.")
|
|
|
|
return False
|
|
|
|
|
|
|
|
# Check if button option is correctly set for this object
|
|
|
|
if self._channel > self._hmdevice.ELEMENT:
|
|
|
|
_LOGGER.critical("Button option is not correct for this object!")
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
def _init_data_struct(self):
|
2016-06-30 08:33:34 +00:00
|
|
|
"""Generate a data dict (self._data) from the Homematic metadata.
|
2016-06-24 08:06:58 +00:00
|
|
|
|
|
|
|
NEEDS overwrite by inherit!
|
|
|
|
"""
|
|
|
|
# Add all attributes to data dict
|
|
|
|
for data_note in self._hmdevice.ATTRIBUTENODE:
|
|
|
|
self._data.update({data_note: STATE_UNKNOWN})
|