Register to Z-Wave sensor updates

This commit is contained in:
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): def update_sensor_states(now):
""" Update states of all sensors. """ """ Update states of all sensors. """
if sensors: if sensors:
logger.info("Updating sensor states")
for sensor in sensors.values(): for sensor in sensors.values():
sensor.update_ha_state(hass, True) 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, ATTR_FRIENDLY_NAME, ATTR_BATTERY_LEVEL, ATTR_UNIT_OF_MEASUREMENT,
TEMP_CELCIUS, TEMP_FAHRENHEIT, LIGHT_LUX, ATTR_LOCATION) TEMP_CELCIUS, TEMP_FAHRENHEIT, LIGHT_LUX, ATTR_LOCATION)
VALUE_REPORT = 72057594081707603
def devices_discovered(hass, config, info): REPORT_BATTERY = 1
""" """ REPORT_TEMPERATURE = 1 << 5
from louie import connect REPORT_HUMIDITY = 1 << 6
from openzwave.network import ZWaveNetwork REPORT_LUMINOSITY = 1 << 7
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
class ZWaveSensor(Device): class ZWaveSensor(Device):
@ -115,7 +80,7 @@ class ZWaveTemperatureSensor(ZWaveSensor):
@property @property
def state(self): def state(self):
""" Returns the state of the sensor. """ """ Returns the state of the sensor. """
return round(self._value.data/1000, 1) return round(self._value.data, 1)
@property @property
def unit(self): def unit(self):
@ -152,3 +117,33 @@ class ZWaveLuminanceSensor(ZWaveSensor):
def unit(self): def unit(self):
""" Unit of this sensor. """ """ Unit of this sensor. """
return LIGHT_LUX 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 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): def setup(hass, config):
""" """
Setup Z-wave. Setup Z-wave.
@ -38,24 +64,23 @@ def setup(hass, config):
from openzwave.option import ZWaveOption from openzwave.option import ZWaveOption
from openzwave.network import ZWaveNetwork from openzwave.network import ZWaveNetwork
use_debug = config[DOMAIN].get(CONF_DEBUG) == '1'
# Setup options # Setup options
options = ZWaveOption( options = ZWaveOption(
config[DOMAIN].get(CONF_USB_STICK_PATH, DEFAULT_CONF_USB_STICK_PATH), config[DOMAIN].get(CONF_USB_STICK_PATH, DEFAULT_CONF_USB_STICK_PATH),
user_path=hass.config_dir) user_path=hass.config_dir)
if config[DOMAIN].get(CONF_DEBUG) == '1': options.set_associate(True)
options.set_console_output(True) options.set_console_output(use_debug)
options.lock() options.lock()
NETWORK = ZWaveNetwork(options, autostart=False) NETWORK = ZWaveNetwork(options, autostart=False)
if use_debug:
def log_all(signal): def log_all(signal):
print("") print("")
print("LOG ALL") print("LOUIE SIGNAL *****", signal)
print(signal)
print("")
print("")
print("") print("")
connect(log_all, weak=False) connect(log_all, weak=False)
@ -66,6 +91,9 @@ def setup(hass, config):
# This should be rewritten more efficient when supporting more types # This should be rewritten more efficient when supporting more types
for node in network.nodes.values(): for node in network.nodes.values():
if use_debug:
nice_print_node(node)
if get_node_value(node, VALUE_SENSOR) and not init_sensor: if get_node_value(node, VALUE_SENSOR) and not init_sensor:
init_sensor = True init_sensor = True

View File

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