core/homeassistant/components/zwave.py

126 lines
3.6 KiB
Python
Raw Normal View History

2015-02-26 07:27:17 +00:00
"""
homeassistant.components.zwave
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Connects Home Assistant to a Z-Wave network.
"""
from pprint import pprint
from homeassistant import bootstrap
from homeassistant.const import (
EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP,
EVENT_PLATFORM_DISCOVERED, ATTR_SERVICE, ATTR_DISCOVERED)
DOMAIN = "zwave"
DEPENDENCIES = []
REQUIREMENTS = ['pydispatcher==2.0.5']
CONF_USB_STICK_PATH = "usb_path"
DEFAULT_CONF_USB_STICK_PATH = "/zwaveusbstick"
CONF_DEBUG = "debug"
DISCOVER_SENSORS = "zwave.sensors"
2015-02-26 07:27:17 +00:00
COMMAND_CLASS_SENSOR_BINARY = 48
COMMAND_CLASS_SENSOR_MULTILEVEL = 49
COMMAND_CLASS_BATTERY = 128
# list of tuple (DOMAIN, discovered service, supported command classes)
DISCOVERY_COMPONENTS = [
('sensor', DISCOVER_SENSORS,
[COMMAND_CLASS_SENSOR_BINARY, COMMAND_CLASS_SENSOR_MULTILEVEL]),
]
ATTR_NODE_ID = "node_id"
ATTR_VALUE_ID = "value_id"
NETWORK = None
2015-02-26 07:27:17 +00:00
def _obj_to_dict(obj):
""" Converts an obj into a hash for debug. """
return {key: getattr(obj, key) for key
in dir(obj)
if key[0] != '_' and not hasattr(getattr(obj, key), '__call__')}
2015-02-23 08:01:04 +00:00
def nice_print_node(node):
2015-02-26 07:27:17 +00:00
""" Prints a nice formatted node to the output (debug method) """
node_dict = _obj_to_dict(node)
node_dict['values'] = {value_id: _obj_to_dict(value)
for value_id, value in node.values.items()}
2015-02-23 08:01:04 +00:00
2015-02-26 07:27:17 +00:00
print("\n\n\n")
2015-02-23 08:01:04 +00:00
print("FOUND NODE", node.product_name)
2015-02-26 07:27:17 +00:00
pprint(node_dict)
print("\n\n\n")
2015-02-23 08:01:04 +00:00
def setup(hass, config):
"""
Setup Z-wave.
Will automatically load components to support devices found on the network.
"""
2015-02-26 07:27:17 +00:00
# pylint: disable=global-statement, import-error
global NETWORK
2015-03-01 06:49:27 +00:00
from pydispatch import dispatcher
from openzwave.option import ZWaveOption
from openzwave.network import ZWaveNetwork
use_debug = str(config[DOMAIN].get(CONF_DEBUG)) == '1'
2015-02-23 08:01:04 +00:00
# Setup options
options = ZWaveOption(
config[DOMAIN].get(CONF_USB_STICK_PATH, DEFAULT_CONF_USB_STICK_PATH),
user_path=hass.config.config_dir)
2015-02-23 08:01:04 +00:00
options.set_console_output(use_debug)
options.lock()
NETWORK = ZWaveNetwork(options, autostart=False)
2015-02-23 08:01:04 +00:00
if use_debug:
2015-02-26 07:27:17 +00:00
def log_all(signal, value=None):
2015-03-19 02:15:48 +00:00
""" Log all the signals. """
2015-02-23 08:01:04 +00:00
print("")
2015-03-19 02:15:48 +00:00
print("SIGNAL *****", signal)
2015-02-26 07:27:17 +00:00
if value and signal in (ZWaveNetwork.SIGNAL_VALUE_CHANGED,
ZWaveNetwork.SIGNAL_VALUE_ADDED):
pprint(_obj_to_dict(value))
2015-02-23 08:01:04 +00:00
print("")
2015-03-01 06:49:27 +00:00
dispatcher.connect(log_all, weak=False)
2015-02-26 07:27:17 +00:00
def value_added(node, value):
""" Called when a value is added to a node on the network. """
for component, discovery_service, command_ids in DISCOVERY_COMPONENTS:
if value.command_class in command_ids:
# Ensure component is loaded
bootstrap.setup_component(hass, component, config)
# Fire discovery event
hass.bus.fire(EVENT_PLATFORM_DISCOVERED, {
2015-02-26 07:27:17 +00:00
ATTR_SERVICE: discovery_service,
ATTR_DISCOVERED: {
ATTR_NODE_ID: node.node_id,
ATTR_VALUE_ID: value.value_id,
}
})
2015-03-01 06:49:27 +00:00
dispatcher.connect(
2015-02-26 07:27:17 +00:00
value_added, ZWaveNetwork.SIGNAL_VALUE_ADDED, weak=False)
def stop_zwave(event):
""" Stop Z-wave. """
NETWORK.stop()
def start_zwave(event):
""" Called when Home Assistant starts up. """
NETWORK.start()
hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, stop_zwave)
hass.bus.listen_once(EVENT_HOMEASSISTANT_START, start_zwave)
return True