2019-02-14 04:35:12 +00:00
|
|
|
"""Support for MySensors switches."""
|
2016-06-12 21:04:45 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2016-04-30 13:27:59 +00:00
|
|
|
from homeassistant.components import mysensors
|
2020-04-26 16:50:37 +00:00
|
|
|
from homeassistant.components.switch import DOMAIN, SwitchEntity
|
2016-06-12 21:04:45 +00:00
|
|
|
from homeassistant.const import ATTR_ENTITY_ID, STATE_OFF, STATE_ON
|
2019-12-05 18:54:43 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2015-11-04 03:53:59 +00:00
|
|
|
|
2019-11-27 12:31:40 +00:00
|
|
|
from .const import DOMAIN as MYSENSORS_DOMAIN, SERVICE_SEND_IR_CODE
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_IR_CODE = "V_IR_SEND"
|
2016-06-12 21:04:45 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
SEND_IR_CODE_SERVICE_SCHEMA = vol.Schema(
|
|
|
|
{vol.Optional(ATTR_ENTITY_ID): cv.entity_ids, vol.Required(ATTR_IR_CODE): cv.string}
|
|
|
|
)
|
2016-06-12 21:04:45 +00:00
|
|
|
|
2015-12-24 01:14:58 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Set up the mysensors platform for switches."""
|
2017-08-25 15:58:05 +00:00
|
|
|
device_class_map = {
|
2019-07-31 19:25:30 +00:00
|
|
|
"S_DOOR": MySensorsSwitch,
|
|
|
|
"S_MOTION": MySensorsSwitch,
|
|
|
|
"S_SMOKE": MySensorsSwitch,
|
|
|
|
"S_LIGHT": MySensorsSwitch,
|
|
|
|
"S_LOCK": MySensorsSwitch,
|
|
|
|
"S_IR": MySensorsIRSwitch,
|
|
|
|
"S_BINARY": MySensorsSwitch,
|
|
|
|
"S_SPRINKLER": MySensorsSwitch,
|
|
|
|
"S_WATER_LEAK": MySensorsSwitch,
|
|
|
|
"S_SOUND": MySensorsSwitch,
|
|
|
|
"S_VIBRATION": MySensorsSwitch,
|
|
|
|
"S_MOISTURE": MySensorsSwitch,
|
|
|
|
"S_WATER_QUALITY": MySensorsSwitch,
|
2017-08-25 15:58:05 +00:00
|
|
|
}
|
|
|
|
mysensors.setup_mysensors_platform(
|
2019-07-31 19:25:30 +00:00
|
|
|
hass,
|
|
|
|
DOMAIN,
|
|
|
|
discovery_info,
|
|
|
|
device_class_map,
|
|
|
|
async_add_entities=async_add_entities,
|
|
|
|
)
|
2016-06-12 21:04:45 +00:00
|
|
|
|
2018-05-11 07:39:18 +00:00
|
|
|
async def async_send_ir_code_service(service):
|
2016-06-12 21:04:45 +00:00
|
|
|
"""Set IR code as device state attribute."""
|
|
|
|
entity_ids = service.data.get(ATTR_ENTITY_ID)
|
|
|
|
ir_code = service.data.get(ATTR_IR_CODE)
|
2017-08-25 15:58:05 +00:00
|
|
|
devices = mysensors.get_mysensors_devices(hass, DOMAIN)
|
2016-06-12 21:04:45 +00:00
|
|
|
|
|
|
|
if entity_ids:
|
2019-07-31 19:25:30 +00:00
|
|
|
_devices = [
|
|
|
|
device
|
|
|
|
for device in devices.values()
|
|
|
|
if isinstance(device, MySensorsIRSwitch)
|
|
|
|
and device.entity_id in entity_ids
|
|
|
|
]
|
2016-06-12 21:04:45 +00:00
|
|
|
else:
|
2019-07-31 19:25:30 +00:00
|
|
|
_devices = [
|
|
|
|
device
|
|
|
|
for device in devices.values()
|
|
|
|
if isinstance(device, MySensorsIRSwitch)
|
|
|
|
]
|
2016-06-12 21:04:45 +00:00
|
|
|
|
|
|
|
kwargs = {ATTR_IR_CODE: ir_code}
|
|
|
|
for device in _devices:
|
2018-05-11 07:39:18 +00:00
|
|
|
await device.async_turn_on(**kwargs)
|
2016-06-12 21:04:45 +00:00
|
|
|
|
2018-04-01 15:36:26 +00:00
|
|
|
hass.services.async_register(
|
2019-11-27 12:31:40 +00:00
|
|
|
MYSENSORS_DOMAIN,
|
2019-07-31 19:25:30 +00:00
|
|
|
SERVICE_SEND_IR_CODE,
|
|
|
|
async_send_ir_code_service,
|
|
|
|
schema=SEND_IR_CODE_SERVICE_SCHEMA,
|
|
|
|
)
|
2015-11-04 03:53:59 +00:00
|
|
|
|
|
|
|
|
2020-04-26 16:50:37 +00:00
|
|
|
class MySensorsSwitch(mysensors.device.MySensorsEntity, SwitchEntity):
|
2016-04-30 13:27:59 +00:00
|
|
|
"""Representation of the value of a MySensors Switch child node."""
|
2015-12-05 23:29:03 +00:00
|
|
|
|
2016-06-12 21:04:45 +00:00
|
|
|
@property
|
|
|
|
def assumed_state(self):
|
|
|
|
"""Return True if unable to access real state of entity."""
|
|
|
|
return self.gateway.optimistic
|
|
|
|
|
2018-03-21 22:55:49 +00:00
|
|
|
@property
|
|
|
|
def current_power_w(self):
|
|
|
|
"""Return the current power usage in W."""
|
|
|
|
set_req = self.gateway.const.SetReq
|
|
|
|
return self._values.get(set_req.V_WATT)
|
|
|
|
|
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."""
|
2017-08-25 15:58:05 +00:00
|
|
|
return self._values.get(self.value_type) == STATE_ON
|
2015-11-04 03:53:59 +00:00
|
|
|
|
2018-05-11 07:39:18 +00:00
|
|
|
async def async_turn_on(self, **kwargs):
|
2015-12-08 00:03:07 +00:00
|
|
|
"""Turn the switch on."""
|
2019-09-25 21:20:02 +00:00
|
|
|
self.gateway.set_child_value(
|
|
|
|
self.node_id, self.child_id, self.value_type, 1, ack=1
|
|
|
|
)
|
2016-01-26 23:15:19 +00:00
|
|
|
if self.gateway.optimistic:
|
2019-02-14 04:35:12 +00:00
|
|
|
# Optimistically assume that switch has changed state
|
2016-01-26 23:15:19 +00:00
|
|
|
self._values[self.value_type] = STATE_ON
|
2020-04-01 21:19:51 +00:00
|
|
|
self.async_write_ha_state()
|
2015-11-04 03:53:59 +00:00
|
|
|
|
2018-05-11 07:39:18 +00:00
|
|
|
async def async_turn_off(self, **kwargs):
|
2015-12-08 01:47:15 +00:00
|
|
|
"""Turn the switch off."""
|
2019-09-25 21:20:02 +00:00
|
|
|
self.gateway.set_child_value(
|
|
|
|
self.node_id, self.child_id, self.value_type, 0, ack=1
|
|
|
|
)
|
2016-01-26 23:15:19 +00:00
|
|
|
if self.gateway.optimistic:
|
2019-02-14 04:35:12 +00:00
|
|
|
# Optimistically assume that switch has changed state
|
2016-01-26 23:15:19 +00:00
|
|
|
self._values[self.value_type] = STATE_OFF
|
2020-04-01 21:19:51 +00:00
|
|
|
self.async_write_ha_state()
|
2015-11-04 03:53:59 +00:00
|
|
|
|
2016-06-12 21:04:45 +00:00
|
|
|
|
|
|
|
class MySensorsIRSwitch(MySensorsSwitch):
|
|
|
|
"""IR switch child class to MySensorsSwitch."""
|
|
|
|
|
|
|
|
def __init__(self, *args):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Set up instance attributes."""
|
2017-08-25 15:58:05 +00:00
|
|
|
super().__init__(*args)
|
2016-06-12 21:04:45 +00:00
|
|
|
self._ir_code = None
|
|
|
|
|
2016-02-28 13:56:07 +00:00
|
|
|
@property
|
2016-06-12 21:04:45 +00:00
|
|
|
def is_on(self):
|
|
|
|
"""Return True if switch is on."""
|
|
|
|
set_req = self.gateway.const.SetReq
|
2017-08-25 15:58:05 +00:00
|
|
|
return self._values.get(set_req.V_LIGHT) == STATE_ON
|
2016-06-12 21:04:45 +00:00
|
|
|
|
2018-05-11 07:39:18 +00:00
|
|
|
async def async_turn_on(self, **kwargs):
|
2016-06-12 21:04:45 +00:00
|
|
|
"""Turn the IR switch on."""
|
|
|
|
set_req = self.gateway.const.SetReq
|
|
|
|
if ATTR_IR_CODE in kwargs:
|
|
|
|
self._ir_code = kwargs[ATTR_IR_CODE]
|
|
|
|
self.gateway.set_child_value(
|
2019-07-31 19:25:30 +00:00
|
|
|
self.node_id, self.child_id, self.value_type, self._ir_code
|
|
|
|
)
|
2019-09-25 21:20:02 +00:00
|
|
|
self.gateway.set_child_value(
|
|
|
|
self.node_id, self.child_id, set_req.V_LIGHT, 1, ack=1
|
|
|
|
)
|
2016-06-12 21:04:45 +00:00
|
|
|
if self.gateway.optimistic:
|
2019-02-14 04:35:12 +00:00
|
|
|
# Optimistically assume that switch has changed state
|
2016-06-12 21:04:45 +00:00
|
|
|
self._values[self.value_type] = self._ir_code
|
|
|
|
self._values[set_req.V_LIGHT] = STATE_ON
|
2020-04-01 21:19:51 +00:00
|
|
|
self.async_write_ha_state()
|
2019-02-14 04:35:12 +00:00
|
|
|
# Turn off switch after switch was turned on
|
2018-05-11 07:39:18 +00:00
|
|
|
await self.async_turn_off()
|
2016-06-12 21:04:45 +00:00
|
|
|
|
2018-05-11 07:39:18 +00:00
|
|
|
async def async_turn_off(self, **kwargs):
|
2016-06-12 21:04:45 +00:00
|
|
|
"""Turn the IR switch off."""
|
|
|
|
set_req = self.gateway.const.SetReq
|
2019-09-25 21:20:02 +00:00
|
|
|
self.gateway.set_child_value(
|
|
|
|
self.node_id, self.child_id, set_req.V_LIGHT, 0, ack=1
|
|
|
|
)
|
2016-06-12 21:04:45 +00:00
|
|
|
if self.gateway.optimistic:
|
2019-02-14 04:35:12 +00:00
|
|
|
# Optimistically assume that switch has changed state
|
2016-06-12 21:04:45 +00:00
|
|
|
self._values[set_req.V_LIGHT] = STATE_OFF
|
2020-04-01 21:19:51 +00:00
|
|
|
self.async_write_ha_state()
|
2016-06-12 21:04:45 +00:00
|
|
|
|
2018-04-01 15:36:26 +00:00
|
|
|
async def async_update(self):
|
2016-06-12 21:04:45 +00:00
|
|
|
"""Update the controller with the latest value from a sensor."""
|
2018-04-01 15:36:26 +00:00
|
|
|
await super().async_update()
|
2017-08-25 15:58:05 +00:00
|
|
|
self._ir_code = self._values.get(self.value_type)
|