Register to Z-Wave sensor updates

pull/44/head
Paulus Schoutsen 2015-02-23 00:01:04 -08:00
parent a013ccf806
commit e9218e2eb2
4 changed files with 76 additions and 55 deletions

View File

@ -39,8 +39,6 @@ def setup(hass, config):
def update_sensor_states(now):
""" Update states of all sensors. """
if sensors:
logger.info("Updating sensor states")
for sensor in sensors.values():
sensor.update_ha_state(hass, True)

View File

@ -4,46 +4,11 @@ from homeassistant.const import (
ATTR_FRIENDLY_NAME, ATTR_BATTERY_LEVEL, ATTR_UNIT_OF_MEASUREMENT,
TEMP_CELCIUS, TEMP_FAHRENHEIT, LIGHT_LUX, ATTR_LOCATION)
def devices_discovered(hass, config, info):
""" """
from louie import connect
from openzwave.network import ZWaveNetwork
VALUE_CLASS_MAP = {
zwave.VALUE_TEMPERATURE: ZWaveTemperatureSensor,
zwave.VALUE_LUMINANCE: ZWaveLuminanceSensor,
zwave.VALUE_RELATIVE_HUMIDITY: ZWaveRelativeHumiditySensor,
}
sensors = []
for node in zwave.NETWORK.nodes.values():
for value, klass in VALUE_CLASS_MAP.items():
if value in node.values:
sensors.append(klass(node))
if sensors[-1] is None:
print("")
print("WTF BBQ")
print(node, klass)
print("")
continue
def value_changed(network, node, value):
""" """
print("")
print("")
print("")
print("ValueChanged in sensor !!", node, value)
print("")
print("")
print("")
# triggered when sensors have updated
connect(value_changed, ZWaveNetwork.SIGNAL_VALUE_CHANGED, weak=False)
return sensors
VALUE_REPORT = 72057594081707603
REPORT_BATTERY = 1
REPORT_TEMPERATURE = 1 << 5
REPORT_HUMIDITY = 1 << 6
REPORT_LUMINOSITY = 1 << 7
class ZWaveSensor(Device):
@ -115,7 +80,7 @@ class ZWaveTemperatureSensor(ZWaveSensor):
@property
def state(self):
""" Returns the state of the sensor. """
return round(self._value.data/1000, 1)
return round(self._value.data, 1)
@property
def unit(self):
@ -152,3 +117,33 @@ class ZWaveLuminanceSensor(ZWaveSensor):
def unit(self):
""" Unit of this sensor. """
return LIGHT_LUX
VALUE_CLASS_MAP = [
(zwave.VALUE_TEMPERATURE, ZWaveTemperatureSensor, REPORT_TEMPERATURE),
(zwave.VALUE_LUMINANCE, ZWaveLuminanceSensor, REPORT_LUMINOSITY),
(zwave.VALUE_RELATIVE_HUMIDITY, ZWaveRelativeHumiditySensor,
REPORT_HUMIDITY),
]
def devices_discovered(hass, config, info):
""" """
# from louie import connect
# from openzwave.network import ZWaveNetwork
sensors = []
for node in zwave.NETWORK.nodes.values():
report_mask = REPORT_BATTERY
for value, klass, sensor_report_mask in VALUE_CLASS_MAP:
if value in node.get_sensors():
sensors.append(klass(node))
report_mask |= sensor_report_mask
if report_mask != REPORT_BATTERY and VALUE_REPORT in node.values:
node.values[VALUE_REPORT].data = report_mask
return sensors

View File

@ -27,6 +27,32 @@ def get_node_value(node, key):
return node.values[key].data if key in node.values else None
def nice_print_node(node):
""" Prints a nice formatted node to the output """
from pprint import pprint
print("")
print("")
print("")
print("FOUND NODE", node.product_name)
pprint({key: getattr(node, key) for key
in dir(node)
if key != 'values' and
not hasattr(getattr(node, key), '__call__')})
print("")
print("")
print("VALUES")
pprint({
value_id: {key: getattr(value, key) for key
in dir(value)
if key[0] != '_' and
not hasattr(getattr(value, key), '__call__')}
for value_id, value in node.values.items()})
print("")
print("")
def setup(hass, config):
"""
Setup Z-wave.
@ -38,27 +64,26 @@ def setup(hass, config):
from openzwave.option import ZWaveOption
from openzwave.network import ZWaveNetwork
use_debug = config[DOMAIN].get(CONF_DEBUG) == '1'
# Setup options
options = ZWaveOption(
config[DOMAIN].get(CONF_USB_STICK_PATH, DEFAULT_CONF_USB_STICK_PATH),
user_path=hass.config_dir)
if config[DOMAIN].get(CONF_DEBUG) == '1':
options.set_console_output(True)
options.set_associate(True)
options.set_console_output(use_debug)
options.lock()
NETWORK = ZWaveNetwork(options, autostart=False)
def log_all(signal):
print("")
print("LOG ALL")
print(signal)
print("")
print("")
print("")
if use_debug:
def log_all(signal):
print("")
print("LOUIE SIGNAL *****", signal)
print("")
connect(log_all, weak=False)
connect(log_all, weak=False)
def zwave_init_done(network):
""" Called when Z-Wave has initialized. """
@ -66,6 +91,9 @@ def setup(hass, config):
# This should be rewritten more efficient when supporting more types
for node in network.nodes.values():
if use_debug:
nice_print_node(node)
if get_node_value(node, VALUE_SENSOR) and not init_sensor:
init_sensor = True

View File

@ -76,7 +76,7 @@ ATTR_LOCATION = "location"
ATTR_BATTERY_LEVEL = "battery_level"
LIGHT_LUX = "LUX"
LIGHT_LUX = "lux"
# #### SERVICES ####
SERVICE_HOMEASSISTANT_STOP = "stop"