Refactor mysensors callback and add validation (#9069)
* Refactor mysensors callback and add validation
* Add mysensors entity class. The mysensors entity class inherits from
a more general mysensors device class.
* Extract mysensors name function.
* Add setup_mysensors_platform for mysensors platforms.
* Add mysensors const schemas.
* Update mysensors callback and add child validation.
* Remove gateway wrapper class.
* Add better logging for mysensors callback.
* Add discover_persistent_devices function.
* Remove discovery in mysensors component setup.
* Clean up gateway storage in hass.data.
* Update all mysensors platforms.
* Add repr for MySensorsNotificationDevice.
* Fix bug in mysensors climate target temperatures.
* Clean up platforms. Child validation simplifies assumptions in
platforms.
* Remove not needed try except statements. All messages are validated
already in pymysensors.
* Clean up logging.
* Add timer debug logging if callback is slow.
* Upgrade pymysensors to 0.11.0.
* Make dispatch callback async
* Pass tuple device_args and optional add_devices
* Also return new_devices as list instead of dictionary.
This commit is contained in:
1 parent
044b96e3cd
commit
8775c54d29
10 files changed
+492
-669
No files matched your search
@@ -4,7 +4,6 @@ Support for MySensors switches.
|
||||
For more details about this platform, please refer to the documentation at
|
||||
https://home-assistant.io/components/switch.mysensors/
|
||||
"""
|
||||
import logging
|
||||
import os
|
||||
|
||||
import voluptuous as vol
|
||||
@@ -15,9 +14,6 @@ from homeassistant.components.switch import DOMAIN, SwitchDevice
|
||||
from homeassistant.config import load_yaml_config_file
|
||||
from homeassistant.const import ATTR_ENTITY_ID, STATE_OFF, STATE_ON
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
DEPENDENCIES = []
|
||||
|
||||
ATTR_IR_CODE = 'V_IR_SEND'
|
||||
SERVICE_SEND_IR_CODE = 'mysensors_send_ir_code'
|
||||
|
||||
@@ -29,82 +25,37 @@ SEND_IR_CODE_SERVICE_SCHEMA = vol.Schema({
|
||||
|
||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||
"""Set up the mysensors platform for switches."""
|
||||
# Only act if loaded via mysensors by discovery event.
|
||||
# Otherwise gateway is not setup.
|
||||
if discovery_info is None:
|
||||
return
|
||||
|
||||
gateways = hass.data.get(mysensors.MYSENSORS_GATEWAYS)
|
||||
if not gateways:
|
||||
return
|
||||
|
||||
platform_devices = []
|
||||
|
||||
for gateway in gateways:
|
||||
# Define the S_TYPES and V_TYPES that the platform should handle as
|
||||
# states. Map them in a dict of lists.
|
||||
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],
|
||||
pres.S_LIGHT: [set_req.V_LIGHT],
|
||||
pres.S_LOCK: [set_req.V_LOCK_STATUS],
|
||||
pres.S_IR: [set_req.V_IR_SEND],
|
||||
}
|
||||
device_class_map = {
|
||||
pres.S_DOOR: MySensorsSwitch,
|
||||
pres.S_MOTION: MySensorsSwitch,
|
||||
pres.S_SMOKE: MySensorsSwitch,
|
||||
pres.S_LIGHT: MySensorsSwitch,
|
||||
pres.S_LOCK: MySensorsSwitch,
|
||||
pres.S_IR: MySensorsIRSwitch,
|
||||
}
|
||||
if float(gateway.protocol_version) >= 1.5:
|
||||
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],
|
||||
})
|
||||
map_sv_types[pres.S_LIGHT].append(set_req.V_STATUS)
|
||||
device_class_map.update({
|
||||
pres.S_BINARY: MySensorsSwitch,
|
||||
pres.S_SPRINKLER: MySensorsSwitch,
|
||||
pres.S_WATER_LEAK: MySensorsSwitch,
|
||||
pres.S_SOUND: MySensorsSwitch,
|
||||
pres.S_VIBRATION: MySensorsSwitch,
|
||||
pres.S_MOISTURE: MySensorsSwitch,
|
||||
})
|
||||
if float(gateway.protocol_version) >= 2.0:
|
||||
map_sv_types.update({
|
||||
pres.S_WATER_QUALITY: [set_req.V_STATUS],
|
||||
})
|
||||
device_class_map.update({
|
||||
pres.S_WATER_QUALITY: MySensorsSwitch,
|
||||
})
|
||||
|
||||
devices = {}
|
||||
gateway.platform_callbacks.append(mysensors.pf_callback_factory(
|
||||
map_sv_types, devices, device_class_map, add_devices))
|
||||
platform_devices.append(devices)
|
||||
device_class_map = {
|
||||
'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,
|
||||
}
|
||||
mysensors.setup_mysensors_platform(
|
||||
hass, DOMAIN, discovery_info, device_class_map,
|
||||
add_devices=add_devices)
|
||||
|
||||
def send_ir_code_service(service):
|
||||
"""Set IR code as device state attribute."""
|
||||
entity_ids = service.data.get(ATTR_ENTITY_ID)
|
||||
ir_code = service.data.get(ATTR_IR_CODE)
|
||||
devices = mysensors.get_mysensors_devices(hass, DOMAIN)
|
||||
|
||||
if entity_ids:
|
||||
_devices = [device for gw_devs in platform_devices
|
||||
for device in gw_devs.values()
|
||||
_devices = [device for device in devices.values()
|
||||
if isinstance(device, MySensorsIRSwitch) and
|
||||
device.entity_id in entity_ids]
|
||||
else:
|
||||
_devices = [device for gw_devs in platform_devices
|
||||
for device in gw_devs.values()
|
||||
_devices = [device for device in devices.values()
|
||||
if isinstance(device, MySensorsIRSwitch)]
|
||||
|
||||
kwargs = {ATTR_IR_CODE: ir_code}
|
||||
@@ -120,7 +71,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||
schema=SEND_IR_CODE_SERVICE_SCHEMA)
|
||||
|
||||
|
||||
class MySensorsSwitch(mysensors.MySensorsDeviceEntity, SwitchDevice):
|
||||
class MySensorsSwitch(mysensors.MySensorsEntity, SwitchDevice):
|
||||
"""Representation of the value of a MySensors Switch child node."""
|
||||
|
||||
@property
|
||||
@@ -131,9 +82,7 @@ class MySensorsSwitch(mysensors.MySensorsDeviceEntity, SwitchDevice):
|
||||
@property
|
||||
def is_on(self):
|
||||
"""Return True if switch is on."""
|
||||
if self.value_type in self._values:
|
||||
return self._values[self.value_type] == STATE_ON
|
||||
return False
|
||||
return self._values.get(self.value_type) == STATE_ON
|
||||
|
||||
def turn_on(self, **kwargs):
|
||||
"""Turn the switch on."""
|
||||
@@ -159,24 +108,18 @@ class MySensorsIRSwitch(MySensorsSwitch):
|
||||
|
||||
def __init__(self, *args):
|
||||
"""Set up instance attributes."""
|
||||
MySensorsSwitch.__init__(self, *args)
|
||||
super().__init__(*args)
|
||||
self._ir_code = None
|
||||
|
||||
@property
|
||||
def is_on(self):
|
||||
"""Return True if switch is on."""
|
||||
set_req = self.gateway.const.SetReq
|
||||
if set_req.V_LIGHT in self._values:
|
||||
return self._values[set_req.V_LIGHT] == STATE_ON
|
||||
return False
|
||||
return self._values.get(set_req.V_LIGHT) == STATE_ON
|
||||
|
||||
def turn_on(self, **kwargs):
|
||||
"""Turn the IR switch on."""
|
||||
set_req = self.gateway.const.SetReq
|
||||
if set_req.V_LIGHT not in self._values:
|
||||
_LOGGER.error('missing value_type: %s at node: %s, child: %s',
|
||||
set_req.V_LIGHT.name, self.node_id, self.child_id)
|
||||
return
|
||||
if ATTR_IR_CODE in kwargs:
|
||||
self._ir_code = kwargs[ATTR_IR_CODE]
|
||||
self.gateway.set_child_value(
|
||||
@@ -194,10 +137,6 @@ class MySensorsIRSwitch(MySensorsSwitch):
|
||||
def turn_off(self, **kwargs):
|
||||
"""Turn the IR switch off."""
|
||||
set_req = self.gateway.const.SetReq
|
||||
if set_req.V_LIGHT not in self._values:
|
||||
_LOGGER.error('missing value_type: %s at node: %s, child: %s',
|
||||
set_req.V_LIGHT.name, self.node_id, self.child_id)
|
||||
return
|
||||
self.gateway.set_child_value(
|
||||
self.node_id, self.child_id, set_req.V_LIGHT, 0)
|
||||
if self.gateway.optimistic:
|
||||
@@ -207,6 +146,5 @@ class MySensorsIRSwitch(MySensorsSwitch):
|
||||
|
||||
def update(self):
|
||||
"""Update the controller with the latest value from a sensor."""
|
||||
MySensorsSwitch.update(self)
|
||||
if self.value_type in self._values:
|
||||
self._ir_code = self._values[self.value_type]
|
||||
super().update()
|
||||
self._ir_code = self._values.get(self.value_type)
|
||||
Reference in new issue
Block a user