2015-11-04 03:53:59 +00:00
|
|
|
"""
|
|
|
|
Support for MySensors switches.
|
|
|
|
|
|
|
|
For more details about this platform, please refer to the documentation at
|
2016-02-15 02:47:51 +00:00
|
|
|
https://home-assistant.io/components/switch.mysensors/
|
2015-11-04 03:53:59 +00:00
|
|
|
"""
|
|
|
|
import logging
|
|
|
|
|
2016-04-30 13:27:59 +00:00
|
|
|
from homeassistant.components import mysensors
|
2016-02-19 05:27:50 +00:00
|
|
|
from homeassistant.components.switch import SwitchDevice
|
2016-04-30 13:27:59 +00:00
|
|
|
from homeassistant.const import STATE_OFF, STATE_ON
|
2015-11-04 03:53:59 +00:00
|
|
|
|
|
|
|
_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
|
2016-01-26 23:15:19 +00:00
|
|
|
# states. Map them in a dict of lists.
|
2016-01-27 18:47:48 +00:00
|
|
|
pres = gateway.const.Presentation
|
|
|
|
set_req = gateway.const.SetReq
|
|
|
|
map_sv_types = {
|
|
|
|
pres.S_DOOR: [set_req.V_ARMED],
|
|
|
|
pres.S_MOTION: [set_req.V_ARMED],
|
|
|
|
pres.S_SMOKE: [set_req.V_ARMED],
|
2016-02-24 23:48:29 +00:00
|
|
|
pres.S_LIGHT: [set_req.V_LIGHT],
|
2016-01-27 18:47:48 +00:00
|
|
|
pres.S_LOCK: [set_req.V_LOCK_STATUS],
|
|
|
|
}
|
2015-12-31 04:48:23 +00:00
|
|
|
if float(gateway.version) >= 1.5:
|
2016-01-27 18:47:48 +00:00
|
|
|
map_sv_types.update({
|
|
|
|
pres.S_BINARY: [set_req.V_STATUS, set_req.V_LIGHT],
|
|
|
|
pres.S_SPRINKLER: [set_req.V_STATUS],
|
|
|
|
pres.S_WATER_LEAK: [set_req.V_ARMED],
|
|
|
|
pres.S_SOUND: [set_req.V_ARMED],
|
|
|
|
pres.S_VIBRATION: [set_req.V_ARMED],
|
|
|
|
pres.S_MOISTURE: [set_req.V_ARMED],
|
|
|
|
})
|
2016-02-24 23:48:29 +00:00
|
|
|
map_sv_types[pres.S_LIGHT].append(set_req.V_STATUS)
|
2015-12-31 04:48:23 +00:00
|
|
|
|
2016-01-26 02:35:34 +00:00
|
|
|
devices = {}
|
2015-12-31 04:48:23 +00:00
|
|
|
gateway.platform_callbacks.append(mysensors.pf_callback_factory(
|
2016-01-27 18:47:48 +00:00
|
|
|
map_sv_types, devices, add_devices, MySensorsSwitch))
|
2015-11-04 03:53:59 +00:00
|
|
|
|
|
|
|
|
2016-04-30 13:27:59 +00:00
|
|
|
class MySensorsSwitch(mysensors.MySensorsDeviceEntity, SwitchDevice):
|
|
|
|
"""Representation of the value of a MySensors Switch child node."""
|
2015-12-05 23:29:03 +00:00
|
|
|
|
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)
|
2016-01-26 23:15:19 +00:00
|
|
|
if self.gateway.optimistic:
|
|
|
|
# optimistically assume that switch has changed state
|
|
|
|
self._values[self.value_type] = STATE_ON
|
|
|
|
self.update_ha_state()
|
2015-11-04 03:53:59 +00:00
|
|
|
|
|
|
|
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)
|
2016-01-26 23:15:19 +00:00
|
|
|
if self.gateway.optimistic:
|
|
|
|
# optimistically assume that switch has changed state
|
|
|
|
self._values[self.value_type] = STATE_OFF
|
|
|
|
self.update_ha_state()
|
2015-11-04 03:53:59 +00:00
|
|
|
|
2016-02-28 13:56:07 +00:00
|
|
|
@property
|
|
|
|
def assumed_state(self):
|
|
|
|
"""Return True if unable to access real state of entity."""
|
|
|
|
return self.gateway.optimistic
|