2015-11-04 03:53:59 +00:00
|
|
|
"""
|
2015-12-05 23:29:03 +00:00
|
|
|
homeassistant.components.switch.mysensors
|
2015-11-04 03:53:59 +00:00
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
Support for MySensors switches.
|
|
|
|
|
|
|
|
For more details about this platform, please refer to the documentation at
|
|
|
|
https://home-assistant.io/components/sensor.mysensors.html
|
|
|
|
"""
|
|
|
|
import logging
|
2015-12-31 04:48:23 +00:00
|
|
|
from collections import defaultdict
|
2015-11-04 03:53:59 +00:00
|
|
|
|
|
|
|
from homeassistant.components.switch import SwitchDevice
|
|
|
|
|
|
|
|
from homeassistant.const import (
|
|
|
|
ATTR_BATTERY_LEVEL,
|
|
|
|
STATE_ON, STATE_OFF)
|
|
|
|
|
|
|
|
import homeassistant.components.mysensors as mysensors
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2015-12-23 22:20:39 +00:00
|
|
|
DEPENDENCIES = []
|
2015-11-04 03:53:59 +00:00
|
|
|
|
2015-12-24 01:14:58 +00:00
|
|
|
|
2015-11-04 03:53:59 +00:00
|
|
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
2015-12-08 00:03:07 +00:00
|
|
|
"""Setup the mysensors platform for switches."""
|
2015-12-31 04:48:23 +00:00
|
|
|
# Only act if loaded via mysensors by discovery event.
|
|
|
|
# Otherwise gateway is not setup.
|
|
|
|
if discovery_info is None:
|
|
|
|
return
|
|
|
|
|
|
|
|
for gateway in mysensors.GATEWAYS.values():
|
|
|
|
# Define the S_TYPES and V_TYPES that the platform should handle as
|
|
|
|
# states.
|
|
|
|
s_types = [
|
|
|
|
gateway.const.Presentation.S_DOOR,
|
|
|
|
gateway.const.Presentation.S_MOTION,
|
|
|
|
gateway.const.Presentation.S_SMOKE,
|
|
|
|
gateway.const.Presentation.S_LIGHT,
|
|
|
|
gateway.const.Presentation.S_LOCK,
|
|
|
|
]
|
|
|
|
v_types = [
|
|
|
|
gateway.const.SetReq.V_ARMED,
|
|
|
|
gateway.const.SetReq.V_LIGHT,
|
|
|
|
gateway.const.SetReq.V_LOCK_STATUS,
|
|
|
|
]
|
|
|
|
if float(gateway.version) >= 1.5:
|
|
|
|
s_types.extend([
|
2016-01-10 03:10:38 +00:00
|
|
|
gateway.const.Presentation.S_BINARY,
|
2015-12-31 04:48:23 +00:00
|
|
|
gateway.const.Presentation.S_SPRINKLER,
|
|
|
|
gateway.const.Presentation.S_WATER_LEAK,
|
|
|
|
gateway.const.Presentation.S_SOUND,
|
|
|
|
gateway.const.Presentation.S_VIBRATION,
|
|
|
|
gateway.const.Presentation.S_MOISTURE,
|
|
|
|
])
|
|
|
|
v_types.extend([gateway.const.SetReq.V_STATUS, ])
|
|
|
|
|
|
|
|
devices = defaultdict(list)
|
|
|
|
gateway.platform_callbacks.append(mysensors.pf_callback_factory(
|
|
|
|
s_types, v_types, devices, add_devices, MySensorsSwitch))
|
2015-11-04 03:53:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
class MySensorsSwitch(SwitchDevice):
|
2015-12-08 00:03:07 +00:00
|
|
|
"""Represent the value of a MySensors child node."""
|
2015-11-04 03:53:59 +00:00
|
|
|
|
2015-12-31 04:48:23 +00:00
|
|
|
# pylint: disable=too-many-arguments
|
2015-11-04 03:53:59 +00:00
|
|
|
|
2015-12-31 04:48:23 +00:00
|
|
|
def __init__(self, gateway, node_id, child_id, name, value_type):
|
2015-12-08 00:03:07 +00:00
|
|
|
"""Setup class attributes on instantiation.
|
|
|
|
|
|
|
|
Args:
|
2016-01-10 03:10:38 +00:00
|
|
|
gateway (GatewayWrapper): Gateway object.
|
2015-12-08 00:03:07 +00:00
|
|
|
node_id (str): Id of node.
|
|
|
|
child_id (str): Id of child.
|
2015-12-08 01:47:15 +00:00
|
|
|
name (str): Entity name.
|
2015-12-08 00:03:07 +00:00
|
|
|
value_type (str): Value type of child. Value is entity state.
|
|
|
|
|
|
|
|
Attributes:
|
2016-01-10 03:10:38 +00:00
|
|
|
gateway (GatewayWrapper): Gateway object
|
2015-12-08 00:03:07 +00:00
|
|
|
node_id (str): Id of node.
|
|
|
|
child_id (str): Id of child.
|
2015-12-08 01:47:15 +00:00
|
|
|
_name (str): Entity name.
|
2015-12-08 00:03:07 +00:00
|
|
|
value_type (str): Value type of child. Value is entity state.
|
|
|
|
battery_level (int): Node battery level.
|
|
|
|
_values (dict): Child values. Non state values set as state attributes.
|
|
|
|
"""
|
2015-12-31 04:48:23 +00:00
|
|
|
self.gateway = gateway
|
2015-11-04 03:53:59 +00:00
|
|
|
self.node_id = node_id
|
|
|
|
self.child_id = child_id
|
2015-12-08 00:03:07 +00:00
|
|
|
self._name = name
|
2015-11-04 03:53:59 +00:00
|
|
|
self.value_type = value_type
|
2015-12-08 00:03:07 +00:00
|
|
|
self.battery_level = 0
|
2015-12-05 23:29:03 +00:00
|
|
|
self._values = {}
|
|
|
|
|
2015-11-04 03:53:59 +00:00
|
|
|
@property
|
|
|
|
def should_poll(self):
|
2015-12-08 00:03:07 +00:00
|
|
|
"""MySensor gateway pushes its state to HA."""
|
2015-11-04 03:53:59 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
2015-12-08 01:47:15 +00:00
|
|
|
"""The name of this entity."""
|
2015-11-04 03:53:59 +00:00
|
|
|
return self._name
|
|
|
|
|
2015-12-05 23:29:03 +00:00
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
2015-12-08 00:03:07 +00:00
|
|
|
"""Return device specific state attributes."""
|
2015-12-05 23:29:03 +00:00
|
|
|
device_attr = dict(self._values)
|
|
|
|
device_attr.pop(self.value_type, None)
|
|
|
|
return device_attr
|
|
|
|
|
2015-11-04 03:53:59 +00:00
|
|
|
@property
|
|
|
|
def state_attributes(self):
|
2015-12-08 00:03:07 +00:00
|
|
|
"""Return the state attributes."""
|
2015-12-05 23:29:03 +00:00
|
|
|
data = {
|
2016-01-10 03:10:38 +00:00
|
|
|
mysensors.ATTR_PORT: self.gateway.port,
|
2015-12-05 23:29:03 +00:00
|
|
|
mysensors.ATTR_NODE_ID: self.node_id,
|
|
|
|
mysensors.ATTR_CHILD_ID: self.child_id,
|
2015-11-04 03:53:59 +00:00
|
|
|
ATTR_BATTERY_LEVEL: self.battery_level,
|
|
|
|
}
|
|
|
|
|
2015-12-05 23:29:03 +00:00
|
|
|
device_attr = self.device_state_attributes
|
|
|
|
|
|
|
|
if device_attr is not None:
|
|
|
|
data.update(device_attr)
|
|
|
|
|
|
|
|
return data
|
|
|
|
|
2015-11-04 03:53:59 +00:00
|
|
|
@property
|
|
|
|
def is_on(self):
|
2015-12-08 00:03:07 +00:00
|
|
|
"""Return True if switch is on."""
|
|
|
|
if self.value_type in self._values:
|
|
|
|
return self._values[self.value_type] == STATE_ON
|
|
|
|
return False
|
2015-11-04 03:53:59 +00:00
|
|
|
|
|
|
|
def turn_on(self):
|
2015-12-08 00:03:07 +00:00
|
|
|
"""Turn the switch on."""
|
2015-12-31 04:48:23 +00:00
|
|
|
self.gateway.set_child_value(
|
2015-11-04 03:53:59 +00:00
|
|
|
self.node_id, self.child_id, self.value_type, 1)
|
2015-12-05 23:29:03 +00:00
|
|
|
self._values[self.value_type] = STATE_ON
|
2015-11-04 03:53:59 +00:00
|
|
|
self.update_ha_state()
|
|
|
|
|
|
|
|
def turn_off(self):
|
2015-12-08 01:47:15 +00:00
|
|
|
"""Turn the switch off."""
|
2015-12-31 04:48:23 +00:00
|
|
|
self.gateway.set_child_value(
|
2015-11-04 03:53:59 +00:00
|
|
|
self.node_id, self.child_id, self.value_type, 0)
|
2015-12-05 23:29:03 +00:00
|
|
|
self._values[self.value_type] = STATE_OFF
|
2015-11-04 03:53:59 +00:00
|
|
|
self.update_ha_state()
|
|
|
|
|
2015-12-31 04:48:23 +00:00
|
|
|
def update(self):
|
2015-12-08 00:03:07 +00:00
|
|
|
"""Update the controller with the latest value from a sensor."""
|
2015-12-31 04:48:23 +00:00
|
|
|
node = self.gateway.sensors[self.node_id]
|
|
|
|
child = node.children[self.child_id]
|
|
|
|
for value_type, value in child.values.items():
|
2015-12-05 23:29:03 +00:00
|
|
|
_LOGGER.info(
|
|
|
|
"%s: value_type %s, value = %s", self._name, value_type, value)
|
2015-12-31 04:48:23 +00:00
|
|
|
if value_type == self.gateway.const.SetReq.V_ARMED or \
|
|
|
|
value_type == self.gateway.const.SetReq.V_STATUS or \
|
|
|
|
value_type == self.gateway.const.SetReq.V_LIGHT or \
|
|
|
|
value_type == self.gateway.const.SetReq.V_LOCK_STATUS:
|
2015-12-05 23:29:03 +00:00
|
|
|
self._values[value_type] = (
|
|
|
|
STATE_ON if int(value) == 1 else STATE_OFF)
|
|
|
|
else:
|
|
|
|
self._values[value_type] = value
|
2015-12-31 04:48:23 +00:00
|
|
|
self.battery_level = node.battery_level
|