Get Z-Wave sensors to work with Home Assistant
This commit is contained in:
1 parent
85f7f5589d
commit
e7b9b86c64
8 files changed
+313
-42
No files matched your search
@@ -8,19 +8,13 @@ from datetime import timedelta
|
||||
|
||||
from homeassistant.loader import get_component
|
||||
import homeassistant.util as util
|
||||
from homeassistant.const import (
|
||||
STATE_OPEN)
|
||||
from homeassistant.helpers import (
|
||||
platform_devices_from_config)
|
||||
from homeassistant.components import group, discovery, wink
|
||||
platform_devices_from_config, generate_entity_id)
|
||||
from homeassistant.components import discovery, wink, zwave
|
||||
|
||||
DOMAIN = 'sensor'
|
||||
DEPENDENCIES = []
|
||||
|
||||
GROUP_NAME_ALL_SENSORS = 'all_sensors'
|
||||
ENTITY_ID_ALL_SENSORS = group.ENTITY_ID_FORMAT.format(
|
||||
GROUP_NAME_ALL_SENSORS)
|
||||
|
||||
ENTITY_ID_FORMAT = DOMAIN + '.{}'
|
||||
|
||||
MIN_TIME_BETWEEN_SCANS = timedelta(seconds=1)
|
||||
@@ -28,18 +22,12 @@ MIN_TIME_BETWEEN_SCANS = timedelta(seconds=1)
|
||||
# Maps discovered services to their platforms
|
||||
DISCOVERY_PLATFORMS = {
|
||||
wink.DISCOVER_SENSORS: 'wink',
|
||||
zwave.DISCOVER_SENSORS: 'zwave',
|
||||
}
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def is_on(hass, entity_id=None):
|
||||
""" Returns if the sensor is open based on the statemachine. """
|
||||
entity_id = entity_id or ENTITY_ID_ALL_SENSORS
|
||||
|
||||
return hass.states.is_state(entity_id, STATE_OPEN)
|
||||
|
||||
|
||||
def setup(hass, config):
|
||||
""" Track states and offer events for sensors. """
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -58,10 +46,6 @@ def setup(hass, config):
|
||||
|
||||
update_sensor_states(None)
|
||||
|
||||
# Track all sensors in a group
|
||||
sensor_group = group.Group(
|
||||
hass, GROUP_NAME_ALL_SENSORS, sensors.keys(), False)
|
||||
|
||||
def sensor_discovered(service, info):
|
||||
""" Called when a sensor is discovered. """
|
||||
platform = get_component("{}.{}".format(
|
||||
@@ -71,16 +55,13 @@ def setup(hass, config):
|
||||
|
||||
for sensor in discovered:
|
||||
if sensor is not None and sensor not in sensors.values():
|
||||
sensor.entity_id = util.ensure_unique_string(
|
||||
ENTITY_ID_FORMAT.format(util.slugify(sensor.name)),
|
||||
sensors.keys())
|
||||
sensor.entity_id = generate_entity_id(
|
||||
ENTITY_ID_FORMAT, sensor.name, sensors.keys())
|
||||
|
||||
sensors[sensor.entity_id] = sensor
|
||||
|
||||
sensor.update_ha_state(hass)
|
||||
|
||||
sensor_group.update_tracked_entity_ids(sensors.keys())
|
||||
|
||||
discovery.listen(hass, DISCOVERY_PLATFORMS.keys(), sensor_discovered)
|
||||
|
||||
# Fire every 3 seconds
|
||||
|
||||
@@ -0,0 +1,154 @@
|
||||
import homeassistant.components.zwave as zwave
|
||||
from homeassistant.helpers import Device
|
||||
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
|
||||
|
||||
|
||||
class ZWaveSensor(Device):
|
||||
def __init__(self, node, sensor_value):
|
||||
self._node = node
|
||||
self._value = node.values[sensor_value]
|
||||
|
||||
@property
|
||||
def unique_id(self):
|
||||
""" Returns a unique id. """
|
||||
return "ZWAVE-{}-{}".format(self._node.node_id, self._value)
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
""" Returns the name of the device. """
|
||||
name = self._node.name or "{} {}".format(
|
||||
self._node.manufacturer_name, self._node.product_name)
|
||||
|
||||
return "{} {}".format(name, self._value.label)
|
||||
|
||||
@property
|
||||
def state(self):
|
||||
""" Returns the state of the sensor. """
|
||||
return self._value.data
|
||||
|
||||
@property
|
||||
def state_attributes(self):
|
||||
""" Returns the state attributes. """
|
||||
attrs = {
|
||||
ATTR_FRIENDLY_NAME: self.name
|
||||
}
|
||||
|
||||
battery_level = zwave.get_node_value(
|
||||
self._node, zwave.VALUE_BATTERY_LEVEL)
|
||||
|
||||
if battery_level is not None:
|
||||
attrs[ATTR_BATTERY_LEVEL] = battery_level
|
||||
|
||||
unit = self.unit
|
||||
|
||||
if unit is not None:
|
||||
attrs[ATTR_UNIT_OF_MEASUREMENT] = unit
|
||||
|
||||
location = self._node.location
|
||||
|
||||
if location:
|
||||
attrs[ATTR_LOCATION] = location
|
||||
|
||||
attrs.update(self.get_sensor_attributes())
|
||||
|
||||
return attrs
|
||||
|
||||
@property
|
||||
def unit(self):
|
||||
""" Unit if sensor has one. """
|
||||
return None
|
||||
|
||||
def get_sensor_attributes(self):
|
||||
""" Get sensor attributes. """
|
||||
return {}
|
||||
|
||||
|
||||
class ZWaveTemperatureSensor(ZWaveSensor):
|
||||
""" Represents a ZWave Temperature Sensor. """
|
||||
|
||||
def __init__(self, node):
|
||||
super().__init__(node, zwave.VALUE_TEMPERATURE)
|
||||
|
||||
@property
|
||||
def state(self):
|
||||
""" Returns the state of the sensor. """
|
||||
return round(self._value.data/1000, 1)
|
||||
|
||||
@property
|
||||
def unit(self):
|
||||
""" Unit of this sensor. """
|
||||
unit = self._value.units
|
||||
|
||||
if unit == 'C':
|
||||
return TEMP_CELCIUS
|
||||
elif unit == 'F':
|
||||
return TEMP_FAHRENHEIT
|
||||
else:
|
||||
return None
|
||||
|
||||
|
||||
class ZWaveRelativeHumiditySensor(ZWaveSensor):
|
||||
""" Represents a ZWave Relative Humidity Sensor. """
|
||||
|
||||
def __init__(self, node):
|
||||
super().__init__(node, zwave.VALUE_RELATIVE_HUMIDITY)
|
||||
|
||||
@property
|
||||
def unit(self):
|
||||
""" Unit of this sensor. """
|
||||
return '%'
|
||||
|
||||
|
||||
class ZWaveLuminanceSensor(ZWaveSensor):
|
||||
""" Represents a ZWave luminance Sensor. """
|
||||
|
||||
def __init__(self, node):
|
||||
super().__init__(node, zwave.VALUE_LUMINANCE)
|
||||
|
||||
@property
|
||||
def unit(self):
|
||||
""" Unit of this sensor. """
|
||||
return LIGHT_LUX
|
||||
Reference in new issue
Block a user