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:
parent
044b96e3cd
commit
8775c54d29
10 changed files with 492 additions and 669 deletions
|
@ -4,62 +4,27 @@ Support for MySensors binary sensors.
|
||||||
For more details about this platform, please refer to the documentation at
|
For more details about this platform, please refer to the documentation at
|
||||||
https://home-assistant.io/components/binary_sensor.mysensors/
|
https://home-assistant.io/components/binary_sensor.mysensors/
|
||||||
"""
|
"""
|
||||||
import logging
|
|
||||||
|
|
||||||
from homeassistant.components import mysensors
|
from homeassistant.components import mysensors
|
||||||
from homeassistant.components.binary_sensor import (DEVICE_CLASSES,
|
from homeassistant.components.binary_sensor import (DEVICE_CLASSES, DOMAIN,
|
||||||
BinarySensorDevice)
|
BinarySensorDevice)
|
||||||
from homeassistant.const import STATE_ON
|
from homeassistant.const import STATE_ON
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
|
||||||
DEPENDENCIES = []
|
|
||||||
|
|
||||||
|
|
||||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||||
"""Set up the MySensors platform for sensors."""
|
"""Setup the mysensors platform for binary sensors."""
|
||||||
# Only act if loaded via mysensors by discovery event.
|
mysensors.setup_mysensors_platform(
|
||||||
# Otherwise gateway is not setup.
|
hass, DOMAIN, discovery_info, MySensorsBinarySensor,
|
||||||
if discovery_info is None:
|
add_devices=add_devices)
|
||||||
return
|
|
||||||
|
|
||||||
gateways = hass.data.get(mysensors.MYSENSORS_GATEWAYS)
|
|
||||||
if not gateways:
|
|
||||||
return
|
|
||||||
|
|
||||||
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_TRIPPED],
|
|
||||||
pres.S_MOTION: [set_req.V_TRIPPED],
|
|
||||||
pres.S_SMOKE: [set_req.V_TRIPPED],
|
|
||||||
}
|
|
||||||
if float(gateway.protocol_version) >= 1.5:
|
|
||||||
map_sv_types.update({
|
|
||||||
pres.S_SPRINKLER: [set_req.V_TRIPPED],
|
|
||||||
pres.S_WATER_LEAK: [set_req.V_TRIPPED],
|
|
||||||
pres.S_SOUND: [set_req.V_TRIPPED],
|
|
||||||
pres.S_VIBRATION: [set_req.V_TRIPPED],
|
|
||||||
pres.S_MOISTURE: [set_req.V_TRIPPED],
|
|
||||||
})
|
|
||||||
|
|
||||||
devices = {}
|
|
||||||
gateway.platform_callbacks.append(mysensors.pf_callback_factory(
|
|
||||||
map_sv_types, devices, MySensorsBinarySensor, add_devices))
|
|
||||||
|
|
||||||
|
|
||||||
class MySensorsBinarySensor(
|
class MySensorsBinarySensor(
|
||||||
mysensors.MySensorsDeviceEntity, BinarySensorDevice):
|
mysensors.MySensorsEntity, BinarySensorDevice):
|
||||||
"""Represent the value of a MySensors Binary Sensor child node."""
|
"""Represent the value of a MySensors Binary Sensor child node."""
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_on(self):
|
def is_on(self):
|
||||||
"""Return True if the binary sensor is on."""
|
"""Return True if the binary sensor is on."""
|
||||||
if self.value_type in self._values:
|
return self._values.get(self.value_type) == STATE_ON
|
||||||
return self._values[self.value_type] == STATE_ON
|
|
||||||
return False
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def device_class(self):
|
def device_class(self):
|
||||||
|
|
|
@ -4,15 +4,11 @@ MySensors platform that offers a Climate (MySensors-HVAC) component.
|
||||||
For more details about this platform, please refer to the documentation
|
For more details about this platform, please refer to the documentation
|
||||||
https://home-assistant.io/components/climate.mysensors/
|
https://home-assistant.io/components/climate.mysensors/
|
||||||
"""
|
"""
|
||||||
import logging
|
|
||||||
|
|
||||||
from homeassistant.components import mysensors
|
from homeassistant.components import mysensors
|
||||||
from homeassistant.components.climate import (
|
from homeassistant.components.climate import (
|
||||||
STATE_COOL, STATE_HEAT, STATE_OFF, STATE_AUTO, ClimateDevice,
|
ATTR_TARGET_TEMP_HIGH, ATTR_TARGET_TEMP_LOW, DOMAIN, STATE_AUTO,
|
||||||
ATTR_TARGET_TEMP_HIGH, ATTR_TARGET_TEMP_LOW)
|
STATE_COOL, STATE_HEAT, STATE_OFF, ClimateDevice)
|
||||||
from homeassistant.const import TEMP_CELSIUS, TEMP_FAHRENHEIT, ATTR_TEMPERATURE
|
from homeassistant.const import ATTR_TEMPERATURE, TEMP_CELSIUS, TEMP_FAHRENHEIT
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
DICT_HA_TO_MYS = {
|
DICT_HA_TO_MYS = {
|
||||||
STATE_AUTO: 'AutoChangeOver',
|
STATE_AUTO: 'AutoChangeOver',
|
||||||
|
@ -29,28 +25,12 @@ DICT_MYS_TO_HA = {
|
||||||
|
|
||||||
|
|
||||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||||
"""Set up the mysensors climate."""
|
"""Setup the mysensors climate."""
|
||||||
if discovery_info is None:
|
mysensors.setup_mysensors_platform(
|
||||||
return
|
hass, DOMAIN, discovery_info, MySensorsHVAC, add_devices=add_devices)
|
||||||
|
|
||||||
gateways = hass.data.get(mysensors.MYSENSORS_GATEWAYS)
|
|
||||||
if not gateways:
|
|
||||||
return
|
|
||||||
|
|
||||||
for gateway in gateways:
|
|
||||||
if float(gateway.protocol_version) < 1.5:
|
|
||||||
continue
|
|
||||||
pres = gateway.const.Presentation
|
|
||||||
set_req = gateway.const.SetReq
|
|
||||||
map_sv_types = {
|
|
||||||
pres.S_HVAC: [set_req.V_HVAC_FLOW_STATE],
|
|
||||||
}
|
|
||||||
devices = {}
|
|
||||||
gateway.platform_callbacks.append(mysensors.pf_callback_factory(
|
|
||||||
map_sv_types, devices, MySensorsHVAC, add_devices))
|
|
||||||
|
|
||||||
|
|
||||||
class MySensorsHVAC(mysensors.MySensorsDeviceEntity, ClimateDevice):
|
class MySensorsHVAC(mysensors.MySensorsEntity, ClimateDevice):
|
||||||
"""Representation of a MySensors HVAC."""
|
"""Representation of a MySensors HVAC."""
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
@ -84,26 +64,28 @@ class MySensorsHVAC(mysensors.MySensorsDeviceEntity, ClimateDevice):
|
||||||
temp = self._values.get(set_req.V_HVAC_SETPOINT_COOL)
|
temp = self._values.get(set_req.V_HVAC_SETPOINT_COOL)
|
||||||
if temp is None:
|
if temp is None:
|
||||||
temp = self._values.get(set_req.V_HVAC_SETPOINT_HEAT)
|
temp = self._values.get(set_req.V_HVAC_SETPOINT_HEAT)
|
||||||
return float(temp)
|
return float(temp) if temp is not None else None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def target_temperature_high(self):
|
def target_temperature_high(self):
|
||||||
"""Return the highbound target temperature we try to reach."""
|
"""Return the highbound target temperature we try to reach."""
|
||||||
set_req = self.gateway.const.SetReq
|
set_req = self.gateway.const.SetReq
|
||||||
if set_req.V_HVAC_SETPOINT_HEAT in self._values:
|
if set_req.V_HVAC_SETPOINT_HEAT in self._values:
|
||||||
return float(self._values.get(set_req.V_HVAC_SETPOINT_COOL))
|
temp = self._values.get(set_req.V_HVAC_SETPOINT_COOL)
|
||||||
|
return float(temp) if temp is not None else None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def target_temperature_low(self):
|
def target_temperature_low(self):
|
||||||
"""Return the lowbound target temperature we try to reach."""
|
"""Return the lowbound target temperature we try to reach."""
|
||||||
set_req = self.gateway.const.SetReq
|
set_req = self.gateway.const.SetReq
|
||||||
if set_req.V_HVAC_SETPOINT_COOL in self._values:
|
if set_req.V_HVAC_SETPOINT_COOL in self._values:
|
||||||
return float(self._values.get(set_req.V_HVAC_SETPOINT_HEAT))
|
temp = self._values.get(set_req.V_HVAC_SETPOINT_HEAT)
|
||||||
|
return float(temp) if temp is not None else None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def current_operation(self):
|
def current_operation(self):
|
||||||
"""Return current operation ie. heat, cool, idle."""
|
"""Return current operation ie. heat, cool, idle."""
|
||||||
return self._values.get(self.gateway.const.SetReq.V_HVAC_FLOW_STATE)
|
return self._values.get(self.value_type)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def operation_list(self):
|
def operation_list(self):
|
||||||
|
@ -128,7 +110,7 @@ class MySensorsHVAC(mysensors.MySensorsDeviceEntity, ClimateDevice):
|
||||||
high = kwargs.get(ATTR_TARGET_TEMP_HIGH)
|
high = kwargs.get(ATTR_TARGET_TEMP_HIGH)
|
||||||
heat = self._values.get(set_req.V_HVAC_SETPOINT_HEAT)
|
heat = self._values.get(set_req.V_HVAC_SETPOINT_HEAT)
|
||||||
cool = self._values.get(set_req.V_HVAC_SETPOINT_COOL)
|
cool = self._values.get(set_req.V_HVAC_SETPOINT_COOL)
|
||||||
updates = ()
|
updates = []
|
||||||
if temp is not None:
|
if temp is not None:
|
||||||
if heat is not None:
|
if heat is not None:
|
||||||
# Set HEAT Target temperature
|
# Set HEAT Target temperature
|
||||||
|
@ -146,7 +128,7 @@ class MySensorsHVAC(mysensors.MySensorsDeviceEntity, ClimateDevice):
|
||||||
self.gateway.set_child_value(
|
self.gateway.set_child_value(
|
||||||
self.node_id, self.child_id, value_type, value)
|
self.node_id, self.child_id, value_type, value)
|
||||||
if self.gateway.optimistic:
|
if self.gateway.optimistic:
|
||||||
# optimistically assume that switch has changed state
|
# optimistically assume that device has changed state
|
||||||
self._values[value_type] = value
|
self._values[value_type] = value
|
||||||
self.schedule_update_ha_state()
|
self.schedule_update_ha_state()
|
||||||
|
|
||||||
|
@ -156,54 +138,22 @@ class MySensorsHVAC(mysensors.MySensorsDeviceEntity, ClimateDevice):
|
||||||
self.gateway.set_child_value(
|
self.gateway.set_child_value(
|
||||||
self.node_id, self.child_id, set_req.V_HVAC_SPEED, fan)
|
self.node_id, self.child_id, set_req.V_HVAC_SPEED, fan)
|
||||||
if self.gateway.optimistic:
|
if self.gateway.optimistic:
|
||||||
# optimistically assume that switch has changed state
|
# optimistically assume that device has changed state
|
||||||
self._values[set_req.V_HVAC_SPEED] = fan
|
self._values[set_req.V_HVAC_SPEED] = fan
|
||||||
self.schedule_update_ha_state()
|
self.schedule_update_ha_state()
|
||||||
|
|
||||||
def set_operation_mode(self, operation_mode):
|
def set_operation_mode(self, operation_mode):
|
||||||
"""Set new target temperature."""
|
"""Set new target temperature."""
|
||||||
set_req = self.gateway.const.SetReq
|
|
||||||
self.gateway.set_child_value(
|
self.gateway.set_child_value(
|
||||||
self.node_id, self.child_id, set_req.V_HVAC_FLOW_STATE,
|
self.node_id, self.child_id, self.value_type,
|
||||||
DICT_HA_TO_MYS[operation_mode])
|
DICT_HA_TO_MYS[operation_mode])
|
||||||
if self.gateway.optimistic:
|
if self.gateway.optimistic:
|
||||||
# optimistically assume that switch has changed state
|
# optimistically assume that device has changed state
|
||||||
self._values[set_req.V_HVAC_FLOW_STATE] = operation_mode
|
self._values[self.value_type] = operation_mode
|
||||||
self.schedule_update_ha_state()
|
self.schedule_update_ha_state()
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
"""Update the controller with the latest value from a sensor."""
|
"""Update the controller with the latest value from a sensor."""
|
||||||
set_req = self.gateway.const.SetReq
|
super().update()
|
||||||
node = self.gateway.sensors[self.node_id]
|
self._values[self.value_type] = DICT_MYS_TO_HA[
|
||||||
child = node.children[self.child_id]
|
self._values[self.value_type]]
|
||||||
for value_type, value in child.values.items():
|
|
||||||
_LOGGER.debug(
|
|
||||||
"%s: value_type %s, value = %s", self._name, value_type, value)
|
|
||||||
if value_type == set_req.V_HVAC_FLOW_STATE:
|
|
||||||
self._values[value_type] = DICT_MYS_TO_HA[value]
|
|
||||||
else:
|
|
||||||
self._values[value_type] = value
|
|
||||||
|
|
||||||
def set_humidity(self, humidity):
|
|
||||||
"""Set new target humidity."""
|
|
||||||
_LOGGER.error("Service Not Implemented yet")
|
|
||||||
|
|
||||||
def set_swing_mode(self, swing_mode):
|
|
||||||
"""Set new target swing operation."""
|
|
||||||
_LOGGER.error("Service Not Implemented yet")
|
|
||||||
|
|
||||||
def turn_away_mode_on(self):
|
|
||||||
"""Turn away mode on."""
|
|
||||||
_LOGGER.error("Service Not Implemented yet")
|
|
||||||
|
|
||||||
def turn_away_mode_off(self):
|
|
||||||
"""Turn away mode off."""
|
|
||||||
_LOGGER.error("Service Not Implemented yet")
|
|
||||||
|
|
||||||
def turn_aux_heat_on(self):
|
|
||||||
"""Turn auxillary heater on."""
|
|
||||||
_LOGGER.error("Service Not Implemented yet")
|
|
||||||
|
|
||||||
def turn_aux_heat_off(self):
|
|
||||||
"""Turn auxillary heater off."""
|
|
||||||
_LOGGER.error("Service Not Implemented yet")
|
|
||||||
|
|
|
@ -4,42 +4,18 @@ Support for MySensors covers.
|
||||||
For more details about this platform, please refer to the documentation at
|
For more details about this platform, please refer to the documentation at
|
||||||
https://home-assistant.io/components/cover.mysensors/
|
https://home-assistant.io/components/cover.mysensors/
|
||||||
"""
|
"""
|
||||||
import logging
|
|
||||||
|
|
||||||
from homeassistant.components import mysensors
|
from homeassistant.components import mysensors
|
||||||
from homeassistant.components.cover import CoverDevice, ATTR_POSITION
|
from homeassistant.components.cover import CoverDevice, ATTR_POSITION, DOMAIN
|
||||||
from homeassistant.const import STATE_ON, STATE_OFF
|
from homeassistant.const import STATE_ON, STATE_OFF
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
DEPENDENCIES = []
|
|
||||||
|
|
||||||
|
|
||||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||||
"""Set up the MySensors platform for covers."""
|
"""Setup the mysensors platform for covers."""
|
||||||
if discovery_info is None:
|
mysensors.setup_mysensors_platform(
|
||||||
return
|
hass, DOMAIN, discovery_info, MySensorsCover, add_devices=add_devices)
|
||||||
|
|
||||||
gateways = hass.data.get(mysensors.MYSENSORS_GATEWAYS)
|
|
||||||
if not gateways:
|
|
||||||
return
|
|
||||||
|
|
||||||
for gateway in gateways:
|
|
||||||
pres = gateway.const.Presentation
|
|
||||||
set_req = gateway.const.SetReq
|
|
||||||
map_sv_types = {
|
|
||||||
pres.S_COVER: [set_req.V_DIMMER, set_req.V_LIGHT],
|
|
||||||
}
|
|
||||||
if float(gateway.protocol_version) >= 1.5:
|
|
||||||
map_sv_types.update({
|
|
||||||
pres.S_COVER: [set_req.V_PERCENTAGE, set_req.V_STATUS],
|
|
||||||
})
|
|
||||||
devices = {}
|
|
||||||
gateway.platform_callbacks.append(mysensors.pf_callback_factory(
|
|
||||||
map_sv_types, devices, MySensorsCover, add_devices))
|
|
||||||
|
|
||||||
|
|
||||||
class MySensorsCover(mysensors.MySensorsDeviceEntity, CoverDevice):
|
class MySensorsCover(mysensors.MySensorsEntity, CoverDevice):
|
||||||
"""Representation of the value of a MySensors Cover child node."""
|
"""Representation of the value of a MySensors Cover child node."""
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
|
|
@ -4,61 +4,51 @@ Support for tracking MySensors devices.
|
||||||
For more details about this platform, please refer to the documentation at
|
For more details about this platform, please refer to the documentation at
|
||||||
https://home-assistant.io/components/device_tracker.mysensors/
|
https://home-assistant.io/components/device_tracker.mysensors/
|
||||||
"""
|
"""
|
||||||
import logging
|
|
||||||
|
|
||||||
from homeassistant.components import mysensors
|
from homeassistant.components import mysensors
|
||||||
|
from homeassistant.components.device_tracker import DOMAIN
|
||||||
|
from homeassistant.helpers.dispatcher import dispatcher_connect
|
||||||
from homeassistant.util import slugify
|
from homeassistant.util import slugify
|
||||||
|
|
||||||
DEPENDENCIES = ['mysensors']
|
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
|
|
||||||
def setup_scanner(hass, config, see, discovery_info=None):
|
def setup_scanner(hass, config, see, discovery_info=None):
|
||||||
"""Set up the MySensors tracker."""
|
"""Set up the MySensors device scanner."""
|
||||||
def mysensors_callback(gateway, msg):
|
new_devices = mysensors.setup_mysensors_platform(
|
||||||
"""Set up callback for mysensors platform."""
|
hass, DOMAIN, discovery_info, MySensorsDeviceScanner,
|
||||||
node = gateway.sensors[msg.node_id]
|
device_args=(see, ))
|
||||||
if node.sketch_name is None:
|
if not new_devices:
|
||||||
_LOGGER.debug("No sketch_name: node %s", msg.node_id)
|
return False
|
||||||
return
|
|
||||||
|
|
||||||
pres = gateway.const.Presentation
|
for device in new_devices:
|
||||||
set_req = gateway.const.SetReq
|
dev_id = (
|
||||||
|
id(device.gateway), device.node_id, device.child_id,
|
||||||
child = node.children.get(msg.child_id)
|
device.value_type)
|
||||||
if child is None:
|
dispatcher_connect(
|
||||||
return
|
hass, mysensors.SIGNAL_CALLBACK.format(*dev_id),
|
||||||
position = child.values.get(set_req.V_POSITION)
|
device.update_callback)
|
||||||
if child.type != pres.S_GPS or position is None:
|
|
||||||
return
|
|
||||||
try:
|
|
||||||
latitude, longitude, _ = position.split(',')
|
|
||||||
except ValueError:
|
|
||||||
_LOGGER.error("Payload for V_POSITION %s is not of format "
|
|
||||||
"latitude, longitude, altitude", position)
|
|
||||||
return
|
|
||||||
name = '{} {} {}'.format(
|
|
||||||
node.sketch_name, msg.node_id, child.id)
|
|
||||||
attr = {
|
|
||||||
mysensors.ATTR_CHILD_ID: child.id,
|
|
||||||
mysensors.ATTR_DESCRIPTION: child.description,
|
|
||||||
mysensors.ATTR_DEVICE: gateway.device,
|
|
||||||
mysensors.ATTR_NODE_ID: msg.node_id,
|
|
||||||
}
|
|
||||||
see(
|
|
||||||
dev_id=slugify(name),
|
|
||||||
host_name=name,
|
|
||||||
gps=(latitude, longitude),
|
|
||||||
battery=node.battery_level,
|
|
||||||
attributes=attr
|
|
||||||
)
|
|
||||||
|
|
||||||
gateways = hass.data.get(mysensors.MYSENSORS_GATEWAYS)
|
|
||||||
|
|
||||||
for gateway in gateways:
|
|
||||||
if float(gateway.protocol_version) < 2.0:
|
|
||||||
continue
|
|
||||||
gateway.platform_callbacks.append(mysensors_callback)
|
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
class MySensorsDeviceScanner(mysensors.MySensorsDevice):
|
||||||
|
"""Represent a MySensors scanner."""
|
||||||
|
|
||||||
|
def __init__(self, see, *args):
|
||||||
|
"""Set up instance."""
|
||||||
|
super().__init__(*args)
|
||||||
|
self.see = see
|
||||||
|
|
||||||
|
def update_callback(self):
|
||||||
|
"""Update the device."""
|
||||||
|
self.update()
|
||||||
|
node = self.gateway.sensors[self.node_id]
|
||||||
|
child = node.children[self.child_id]
|
||||||
|
position = child.values[self.value_type]
|
||||||
|
latitude, longitude, _ = position.split(',')
|
||||||
|
|
||||||
|
self.see(
|
||||||
|
dev_id=slugify(self.name),
|
||||||
|
host_name=self.name,
|
||||||
|
gps=(latitude, longitude),
|
||||||
|
battery=node.battery_level,
|
||||||
|
attributes=self.device_state_attributes
|
||||||
|
)
|
||||||
|
|
|
@ -4,64 +4,35 @@ Support for MySensors lights.
|
||||||
For more details about this platform, please refer to the documentation at
|
For more details about this platform, please refer to the documentation at
|
||||||
https://home-assistant.io/components/light.mysensors/
|
https://home-assistant.io/components/light.mysensors/
|
||||||
"""
|
"""
|
||||||
import logging
|
|
||||||
|
|
||||||
from homeassistant.components import mysensors
|
from homeassistant.components import mysensors
|
||||||
from homeassistant.components.light import (
|
from homeassistant.components.light import (
|
||||||
ATTR_BRIGHTNESS, ATTR_RGB_COLOR, ATTR_WHITE_VALUE,
|
ATTR_BRIGHTNESS, ATTR_RGB_COLOR, ATTR_WHITE_VALUE, DOMAIN,
|
||||||
SUPPORT_BRIGHTNESS, SUPPORT_RGB_COLOR, SUPPORT_WHITE_VALUE, Light)
|
SUPPORT_BRIGHTNESS, SUPPORT_RGB_COLOR, SUPPORT_WHITE_VALUE, Light)
|
||||||
from homeassistant.const import STATE_OFF, STATE_ON
|
from homeassistant.const import STATE_OFF, STATE_ON
|
||||||
from homeassistant.util.color import rgb_hex_to_rgb_list
|
from homeassistant.util.color import rgb_hex_to_rgb_list
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
|
||||||
ATTR_VALUE = 'value'
|
|
||||||
ATTR_VALUE_TYPE = 'value_type'
|
|
||||||
|
|
||||||
SUPPORT_MYSENSORS = (SUPPORT_BRIGHTNESS | SUPPORT_RGB_COLOR |
|
SUPPORT_MYSENSORS = (SUPPORT_BRIGHTNESS | SUPPORT_RGB_COLOR |
|
||||||
SUPPORT_WHITE_VALUE)
|
SUPPORT_WHITE_VALUE)
|
||||||
|
|
||||||
|
|
||||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||||
"""Set up the MySensors platform for lights."""
|
"""Setup the mysensors platform for lights."""
|
||||||
if discovery_info is None:
|
device_class_map = {
|
||||||
return
|
'S_DIMMER': MySensorsLightDimmer,
|
||||||
|
'S_RGB_LIGHT': MySensorsLightRGB,
|
||||||
gateways = hass.data.get(mysensors.MYSENSORS_GATEWAYS)
|
'S_RGBW_LIGHT': MySensorsLightRGBW,
|
||||||
if not gateways:
|
}
|
||||||
return
|
mysensors.setup_mysensors_platform(
|
||||||
|
hass, DOMAIN, discovery_info, device_class_map,
|
||||||
for gateway in gateways:
|
add_devices=add_devices)
|
||||||
# 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_DIMMER: [set_req.V_DIMMER],
|
|
||||||
}
|
|
||||||
device_class_map = {
|
|
||||||
pres.S_DIMMER: MySensorsLightDimmer,
|
|
||||||
}
|
|
||||||
if float(gateway.protocol_version) >= 1.5:
|
|
||||||
map_sv_types.update({
|
|
||||||
pres.S_RGB_LIGHT: [set_req.V_RGB],
|
|
||||||
pres.S_RGBW_LIGHT: [set_req.V_RGBW],
|
|
||||||
})
|
|
||||||
map_sv_types[pres.S_DIMMER].append(set_req.V_PERCENTAGE)
|
|
||||||
device_class_map.update({
|
|
||||||
pres.S_RGB_LIGHT: MySensorsLightRGB,
|
|
||||||
pres.S_RGBW_LIGHT: MySensorsLightRGBW,
|
|
||||||
})
|
|
||||||
devices = {}
|
|
||||||
gateway.platform_callbacks.append(mysensors.pf_callback_factory(
|
|
||||||
map_sv_types, devices, device_class_map, add_devices))
|
|
||||||
|
|
||||||
|
|
||||||
class MySensorsLight(mysensors.MySensorsDeviceEntity, Light):
|
class MySensorsLight(mysensors.MySensorsEntity, Light):
|
||||||
"""Representation of a MySensors Light child node."""
|
"""Representation of a MySensors Light child node."""
|
||||||
|
|
||||||
def __init__(self, *args):
|
def __init__(self, *args):
|
||||||
"""Initialize a MySensors Light."""
|
"""Initialize a MySensors Light."""
|
||||||
mysensors.MySensorsDeviceEntity.__init__(self, *args)
|
super().__init__(*args)
|
||||||
self._state = None
|
self._state = None
|
||||||
self._brightness = None
|
self._brightness = None
|
||||||
self._rgb = None
|
self._rgb = None
|
||||||
|
@ -101,7 +72,7 @@ class MySensorsLight(mysensors.MySensorsDeviceEntity, Light):
|
||||||
"""Turn on light child device."""
|
"""Turn on light child device."""
|
||||||
set_req = self.gateway.const.SetReq
|
set_req = self.gateway.const.SetReq
|
||||||
|
|
||||||
if self._state or set_req.V_LIGHT not in self._values:
|
if self._state:
|
||||||
return
|
return
|
||||||
self.gateway.set_child_value(
|
self.gateway.set_child_value(
|
||||||
self.node_id, self.child_id, set_req.V_LIGHT, 1)
|
self.node_id, self.child_id, set_req.V_LIGHT, 1)
|
||||||
|
@ -110,7 +81,6 @@ class MySensorsLight(mysensors.MySensorsDeviceEntity, Light):
|
||||||
# optimistically assume that light has changed state
|
# optimistically assume that light has changed state
|
||||||
self._state = True
|
self._state = True
|
||||||
self._values[set_req.V_LIGHT] = STATE_ON
|
self._values[set_req.V_LIGHT] = STATE_ON
|
||||||
self.schedule_update_ha_state()
|
|
||||||
|
|
||||||
def _turn_on_dimmer(self, **kwargs):
|
def _turn_on_dimmer(self, **kwargs):
|
||||||
"""Turn on dimmer child device."""
|
"""Turn on dimmer child device."""
|
||||||
|
@ -130,7 +100,6 @@ class MySensorsLight(mysensors.MySensorsDeviceEntity, Light):
|
||||||
# optimistically assume that light has changed state
|
# optimistically assume that light has changed state
|
||||||
self._brightness = brightness
|
self._brightness = brightness
|
||||||
self._values[set_req.V_DIMMER] = percent
|
self._values[set_req.V_DIMMER] = percent
|
||||||
self.schedule_update_ha_state()
|
|
||||||
|
|
||||||
def _turn_on_rgb_and_w(self, hex_template, **kwargs):
|
def _turn_on_rgb_and_w(self, hex_template, **kwargs):
|
||||||
"""Turn on RGB or RGBW child device."""
|
"""Turn on RGB or RGBW child device."""
|
||||||
|
@ -144,16 +113,11 @@ class MySensorsLight(mysensors.MySensorsDeviceEntity, Light):
|
||||||
return
|
return
|
||||||
if new_rgb is not None:
|
if new_rgb is not None:
|
||||||
rgb = list(new_rgb)
|
rgb = list(new_rgb)
|
||||||
if rgb is None:
|
|
||||||
return
|
|
||||||
if hex_template == '%02x%02x%02x%02x':
|
if hex_template == '%02x%02x%02x%02x':
|
||||||
if new_white is not None:
|
if new_white is not None:
|
||||||
rgb.append(new_white)
|
rgb.append(new_white)
|
||||||
elif white is not None:
|
|
||||||
rgb.append(white)
|
|
||||||
else:
|
else:
|
||||||
_LOGGER.error("White value is not updated for RGBW light")
|
rgb.append(white)
|
||||||
return
|
|
||||||
hex_color = hex_template % tuple(rgb)
|
hex_color = hex_template % tuple(rgb)
|
||||||
if len(rgb) > 3:
|
if len(rgb) > 3:
|
||||||
white = rgb.pop()
|
white = rgb.pop()
|
||||||
|
@ -164,104 +128,40 @@ class MySensorsLight(mysensors.MySensorsDeviceEntity, Light):
|
||||||
# optimistically assume that light has changed state
|
# optimistically assume that light has changed state
|
||||||
self._rgb = rgb
|
self._rgb = rgb
|
||||||
self._white = white
|
self._white = white
|
||||||
if hex_color:
|
self._values[self.value_type] = hex_color
|
||||||
self._values[self.value_type] = hex_color
|
|
||||||
self.schedule_update_ha_state()
|
|
||||||
|
|
||||||
def _turn_off_light(self, value_type=None, value=None):
|
def turn_off(self):
|
||||||
"""Turn off light child device."""
|
|
||||||
set_req = self.gateway.const.SetReq
|
|
||||||
value_type = (
|
|
||||||
set_req.V_LIGHT
|
|
||||||
if set_req.V_LIGHT in self._values else value_type)
|
|
||||||
value = 0 if set_req.V_LIGHT in self._values else value
|
|
||||||
return {ATTR_VALUE_TYPE: value_type, ATTR_VALUE: value}
|
|
||||||
|
|
||||||
def _turn_off_dimmer(self, value_type=None, value=None):
|
|
||||||
"""Turn off dimmer child device."""
|
|
||||||
set_req = self.gateway.const.SetReq
|
|
||||||
value_type = (
|
|
||||||
set_req.V_DIMMER
|
|
||||||
if set_req.V_DIMMER in self._values else value_type)
|
|
||||||
value = 0 if set_req.V_DIMMER in self._values else value
|
|
||||||
return {ATTR_VALUE_TYPE: value_type, ATTR_VALUE: value}
|
|
||||||
|
|
||||||
def _turn_off_rgb_or_w(self, value_type=None, value=None):
|
|
||||||
"""Turn off RGB or RGBW child device."""
|
|
||||||
if float(self.gateway.protocol_version) >= 1.5:
|
|
||||||
set_req = self.gateway.const.SetReq
|
|
||||||
if self.value_type == set_req.V_RGB:
|
|
||||||
value = '000000'
|
|
||||||
elif self.value_type == set_req.V_RGBW:
|
|
||||||
value = '00000000'
|
|
||||||
return {ATTR_VALUE_TYPE: self.value_type, ATTR_VALUE: value}
|
|
||||||
|
|
||||||
def _turn_off_main(self, value_type=None, value=None):
|
|
||||||
"""Turn the device off."""
|
"""Turn the device off."""
|
||||||
set_req = self.gateway.const.SetReq
|
value_type = self.gateway.const.SetReq.V_LIGHT
|
||||||
if value_type is None or value is None:
|
|
||||||
_LOGGER.warning(
|
|
||||||
"%s: value_type %s, value = %s, None is not valid argument "
|
|
||||||
"when setting child value", self._name, value_type, value)
|
|
||||||
return
|
|
||||||
self.gateway.set_child_value(
|
self.gateway.set_child_value(
|
||||||
self.node_id, self.child_id, value_type, value)
|
self.node_id, self.child_id, value_type, 0)
|
||||||
if self.gateway.optimistic:
|
if self.gateway.optimistic:
|
||||||
# optimistically assume that light has changed state
|
# optimistically assume that light has changed state
|
||||||
self._state = False
|
self._state = False
|
||||||
self._values[value_type] = (
|
self._values[value_type] = STATE_OFF
|
||||||
STATE_OFF if set_req.V_LIGHT in self._values else value)
|
|
||||||
self.schedule_update_ha_state()
|
self.schedule_update_ha_state()
|
||||||
|
|
||||||
def _update_light(self):
|
def _update_light(self):
|
||||||
"""Update the controller with values from light child."""
|
"""Update the controller with values from light child."""
|
||||||
value_type = self.gateway.const.SetReq.V_LIGHT
|
value_type = self.gateway.const.SetReq.V_LIGHT
|
||||||
if value_type in self._values:
|
self._state = self._values[value_type] == STATE_ON
|
||||||
self._values[value_type] = (
|
|
||||||
STATE_ON if int(self._values[value_type]) == 1 else STATE_OFF)
|
|
||||||
self._state = self._values[value_type] == STATE_ON
|
|
||||||
|
|
||||||
def _update_dimmer(self):
|
def _update_dimmer(self):
|
||||||
"""Update the controller with values from dimmer child."""
|
"""Update the controller with values from dimmer child."""
|
||||||
set_req = self.gateway.const.SetReq
|
value_type = self.gateway.const.SetReq.V_DIMMER
|
||||||
value_type = set_req.V_DIMMER
|
|
||||||
if value_type in self._values:
|
if value_type in self._values:
|
||||||
self._brightness = round(255 * int(self._values[value_type]) / 100)
|
self._brightness = round(255 * int(self._values[value_type]) / 100)
|
||||||
if self._brightness == 0:
|
if self._brightness == 0:
|
||||||
self._state = False
|
self._state = False
|
||||||
if set_req.V_LIGHT not in self._values:
|
|
||||||
self._state = self._brightness > 0
|
|
||||||
|
|
||||||
def _update_rgb_or_w(self):
|
def _update_rgb_or_w(self):
|
||||||
"""Update the controller with values from RGB or RGBW child."""
|
"""Update the controller with values from RGB or RGBW child."""
|
||||||
set_req = self.gateway.const.SetReq
|
|
||||||
value = self._values[self.value_type]
|
value = self._values[self.value_type]
|
||||||
if len(value) != 6 and len(value) != 8:
|
|
||||||
_LOGGER.error(
|
|
||||||
"Wrong value %s for %s", value, set_req(self.value_type).name)
|
|
||||||
return
|
|
||||||
color_list = rgb_hex_to_rgb_list(value)
|
color_list = rgb_hex_to_rgb_list(value)
|
||||||
if set_req.V_LIGHT not in self._values and \
|
|
||||||
set_req.V_DIMMER not in self._values:
|
|
||||||
self._state = max(color_list) > 0
|
|
||||||
if len(color_list) > 3:
|
if len(color_list) > 3:
|
||||||
if set_req.V_RGBW != self.value_type:
|
|
||||||
_LOGGER.error(
|
|
||||||
"Wrong value %s for %s",
|
|
||||||
value, set_req(self.value_type).name)
|
|
||||||
return
|
|
||||||
self._white = color_list.pop()
|
self._white = color_list.pop()
|
||||||
self._rgb = color_list
|
self._rgb = color_list
|
||||||
|
|
||||||
def _update_main(self):
|
|
||||||
"""Update the controller with the latest value from a sensor."""
|
|
||||||
node = self.gateway.sensors[self.node_id]
|
|
||||||
child = node.children[self.child_id]
|
|
||||||
for value_type, value in child.values.items():
|
|
||||||
_LOGGER.debug(
|
|
||||||
"%s: value_type %s, value = %s", self._name, value_type, value)
|
|
||||||
self._values[value_type] = value
|
|
||||||
|
|
||||||
|
|
||||||
class MySensorsLightDimmer(MySensorsLight):
|
class MySensorsLightDimmer(MySensorsLight):
|
||||||
"""Dimmer child class to MySensorsLight."""
|
"""Dimmer child class to MySensorsLight."""
|
||||||
|
@ -270,18 +170,12 @@ class MySensorsLightDimmer(MySensorsLight):
|
||||||
"""Turn the device on."""
|
"""Turn the device on."""
|
||||||
self._turn_on_light()
|
self._turn_on_light()
|
||||||
self._turn_on_dimmer(**kwargs)
|
self._turn_on_dimmer(**kwargs)
|
||||||
|
if self.gateway.optimistic:
|
||||||
def turn_off(self, **kwargs):
|
self.schedule_update_ha_state()
|
||||||
"""Turn the device off."""
|
|
||||||
ret = self._turn_off_dimmer()
|
|
||||||
ret = self._turn_off_light(
|
|
||||||
value_type=ret[ATTR_VALUE_TYPE], value=ret[ATTR_VALUE])
|
|
||||||
self._turn_off_main(
|
|
||||||
value_type=ret[ATTR_VALUE_TYPE], value=ret[ATTR_VALUE])
|
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
"""Update the controller with the latest value from a sensor."""
|
"""Update the controller with the latest value from a sensor."""
|
||||||
self._update_main()
|
super().update()
|
||||||
self._update_light()
|
self._update_light()
|
||||||
self._update_dimmer()
|
self._update_dimmer()
|
||||||
|
|
||||||
|
@ -294,20 +188,12 @@ class MySensorsLightRGB(MySensorsLight):
|
||||||
self._turn_on_light()
|
self._turn_on_light()
|
||||||
self._turn_on_dimmer(**kwargs)
|
self._turn_on_dimmer(**kwargs)
|
||||||
self._turn_on_rgb_and_w('%02x%02x%02x', **kwargs)
|
self._turn_on_rgb_and_w('%02x%02x%02x', **kwargs)
|
||||||
|
if self.gateway.optimistic:
|
||||||
def turn_off(self, **kwargs):
|
self.schedule_update_ha_state()
|
||||||
"""Turn the device off."""
|
|
||||||
ret = self._turn_off_rgb_or_w()
|
|
||||||
ret = self._turn_off_dimmer(
|
|
||||||
value_type=ret[ATTR_VALUE_TYPE], value=ret[ATTR_VALUE])
|
|
||||||
ret = self._turn_off_light(
|
|
||||||
value_type=ret[ATTR_VALUE_TYPE], value=ret[ATTR_VALUE])
|
|
||||||
self._turn_off_main(
|
|
||||||
value_type=ret[ATTR_VALUE_TYPE], value=ret[ATTR_VALUE])
|
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
"""Update the controller with the latest value from a sensor."""
|
"""Update the controller with the latest value from a sensor."""
|
||||||
self._update_main()
|
super().update()
|
||||||
self._update_light()
|
self._update_light()
|
||||||
self._update_dimmer()
|
self._update_dimmer()
|
||||||
self._update_rgb_or_w()
|
self._update_rgb_or_w()
|
||||||
|
@ -316,8 +202,12 @@ class MySensorsLightRGB(MySensorsLight):
|
||||||
class MySensorsLightRGBW(MySensorsLightRGB):
|
class MySensorsLightRGBW(MySensorsLightRGB):
|
||||||
"""RGBW child class to MySensorsLightRGB."""
|
"""RGBW child class to MySensorsLightRGB."""
|
||||||
|
|
||||||
|
# pylint: disable=too-many-ancestors
|
||||||
|
|
||||||
def turn_on(self, **kwargs):
|
def turn_on(self, **kwargs):
|
||||||
"""Turn the device on."""
|
"""Turn the device on."""
|
||||||
self._turn_on_light()
|
self._turn_on_light()
|
||||||
self._turn_on_dimmer(**kwargs)
|
self._turn_on_dimmer(**kwargs)
|
||||||
self._turn_on_rgb_and_w('%02x%02x%02x%02x', **kwargs)
|
self._turn_on_rgb_and_w('%02x%02x%02x%02x', **kwargs)
|
||||||
|
if self.gateway.optimistic:
|
||||||
|
self.schedule_update_ha_state()
|
||||||
|
|
|
@ -4,30 +4,37 @@ Connect to a MySensors gateway via pymysensors API.
|
||||||
For more details about this platform, please refer to the documentation at
|
For more details about this platform, please refer to the documentation at
|
||||||
https://home-assistant.io/components/sensor.mysensors/
|
https://home-assistant.io/components/sensor.mysensors/
|
||||||
"""
|
"""
|
||||||
|
import asyncio
|
||||||
|
from collections import defaultdict
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import socket
|
import socket
|
||||||
import sys
|
import sys
|
||||||
|
from timeit import default_timer as timer
|
||||||
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
import homeassistant.helpers.config_validation as cv
|
|
||||||
from homeassistant.setup import setup_component
|
|
||||||
from homeassistant.components.mqtt import (
|
from homeassistant.components.mqtt import (
|
||||||
valid_publish_topic, valid_subscribe_topic)
|
valid_publish_topic, valid_subscribe_topic)
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
ATTR_BATTERY_LEVEL, CONF_NAME, CONF_OPTIMISTIC, EVENT_HOMEASSISTANT_START,
|
ATTR_BATTERY_LEVEL, CONF_NAME, CONF_OPTIMISTIC, EVENT_HOMEASSISTANT_START,
|
||||||
EVENT_HOMEASSISTANT_STOP, STATE_OFF, STATE_ON)
|
EVENT_HOMEASSISTANT_STOP, STATE_OFF, STATE_ON)
|
||||||
from homeassistant.helpers import discovery
|
from homeassistant.helpers import discovery
|
||||||
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
from homeassistant.helpers.dispatcher import (
|
||||||
|
async_dispatcher_connect, dispatcher_send)
|
||||||
|
from homeassistant.helpers.entity import Entity
|
||||||
from homeassistant.loader import get_component
|
from homeassistant.loader import get_component
|
||||||
|
from homeassistant.setup import setup_component
|
||||||
|
|
||||||
REQUIREMENTS = ['pymysensors==0.10.0']
|
REQUIREMENTS = ['pymysensors==0.11.0']
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
ATTR_CHILD_ID = 'child_id'
|
ATTR_CHILD_ID = 'child_id'
|
||||||
ATTR_DESCRIPTION = 'description'
|
ATTR_DESCRIPTION = 'description'
|
||||||
ATTR_DEVICE = 'device'
|
ATTR_DEVICE = 'device'
|
||||||
|
ATTR_DEVICES = 'devices'
|
||||||
ATTR_NODE_ID = 'node_id'
|
ATTR_NODE_ID = 'node_id'
|
||||||
|
|
||||||
CONF_BAUD_RATE = 'baud_rate'
|
CONF_BAUD_RATE = 'baud_rate'
|
||||||
|
@ -44,11 +51,16 @@ CONF_VERSION = 'version'
|
||||||
|
|
||||||
DEFAULT_BAUD_RATE = 115200
|
DEFAULT_BAUD_RATE = 115200
|
||||||
DEFAULT_TCP_PORT = 5003
|
DEFAULT_TCP_PORT = 5003
|
||||||
DEFAULT_VERSION = 1.4
|
DEFAULT_VERSION = '1.4'
|
||||||
DOMAIN = 'mysensors'
|
DOMAIN = 'mysensors'
|
||||||
|
|
||||||
MQTT_COMPONENT = 'mqtt'
|
MQTT_COMPONENT = 'mqtt'
|
||||||
MYSENSORS_GATEWAYS = 'mysensors_gateways'
|
MYSENSORS_GATEWAYS = 'mysensors_gateways'
|
||||||
|
MYSENSORS_PLATFORM_DEVICES = 'mysensors_devices_{}'
|
||||||
|
PLATFORM = 'platform'
|
||||||
|
SCHEMA = 'schema'
|
||||||
|
SIGNAL_CALLBACK = 'mysensors_callback_{}_{}_{}_{}'
|
||||||
|
TYPE = 'type'
|
||||||
|
|
||||||
|
|
||||||
def is_socket_address(value):
|
def is_socket_address(value):
|
||||||
|
@ -144,11 +156,127 @@ CONFIG_SCHEMA = vol.Schema({
|
||||||
vol.Optional(CONF_OPTIMISTIC, default=False): cv.boolean,
|
vol.Optional(CONF_OPTIMISTIC, default=False): cv.boolean,
|
||||||
vol.Optional(CONF_PERSISTENCE, default=True): cv.boolean,
|
vol.Optional(CONF_PERSISTENCE, default=True): cv.boolean,
|
||||||
vol.Optional(CONF_RETAIN, default=True): cv.boolean,
|
vol.Optional(CONF_RETAIN, default=True): cv.boolean,
|
||||||
vol.Optional(CONF_VERSION, default=DEFAULT_VERSION): vol.Coerce(float),
|
vol.Optional(CONF_VERSION, default=DEFAULT_VERSION): cv.string,
|
||||||
}))
|
}))
|
||||||
}, extra=vol.ALLOW_EXTRA)
|
}, extra=vol.ALLOW_EXTRA)
|
||||||
|
|
||||||
|
|
||||||
|
# mysensors const schemas
|
||||||
|
BINARY_SENSOR_SCHEMA = {PLATFORM: 'binary_sensor', TYPE: 'V_TRIPPED'}
|
||||||
|
CLIMATE_SCHEMA = {PLATFORM: 'climate', TYPE: 'V_HVAC_FLOW_STATE'}
|
||||||
|
LIGHT_DIMMER_SCHEMA = {
|
||||||
|
PLATFORM: 'light', TYPE: 'V_DIMMER',
|
||||||
|
SCHEMA: {'V_DIMMER': cv.string, 'V_LIGHT': cv.string}}
|
||||||
|
LIGHT_PERCENTAGE_SCHEMA = {
|
||||||
|
PLATFORM: 'light', TYPE: 'V_PERCENTAGE',
|
||||||
|
SCHEMA: {'V_PERCENTAGE': cv.string, 'V_STATUS': cv.string}}
|
||||||
|
LIGHT_RGB_SCHEMA = {
|
||||||
|
PLATFORM: 'light', TYPE: 'V_RGB', SCHEMA: {
|
||||||
|
'V_RGB': cv.string, 'V_STATUS': cv.string}}
|
||||||
|
LIGHT_RGBW_SCHEMA = {
|
||||||
|
PLATFORM: 'light', TYPE: 'V_RGBW', SCHEMA: {
|
||||||
|
'V_RGBW': cv.string, 'V_STATUS': cv.string}}
|
||||||
|
NOTIFY_SCHEMA = {PLATFORM: 'notify', TYPE: 'V_TEXT'}
|
||||||
|
DEVICE_TRACKER_SCHEMA = {PLATFORM: 'device_tracker', TYPE: 'V_POSITION'}
|
||||||
|
DUST_SCHEMA = [
|
||||||
|
{PLATFORM: 'sensor', TYPE: 'V_DUST_LEVEL'},
|
||||||
|
{PLATFORM: 'sensor', TYPE: 'V_LEVEL'}]
|
||||||
|
SWITCH_LIGHT_SCHEMA = {PLATFORM: 'switch', TYPE: 'V_LIGHT'}
|
||||||
|
SWITCH_STATUS_SCHEMA = {PLATFORM: 'switch', TYPE: 'V_STATUS'}
|
||||||
|
MYSENSORS_CONST_SCHEMA = {
|
||||||
|
'S_DOOR': [BINARY_SENSOR_SCHEMA, {PLATFORM: 'switch', TYPE: 'V_ARMED'}],
|
||||||
|
'S_MOTION': [BINARY_SENSOR_SCHEMA, {PLATFORM: 'switch', TYPE: 'V_ARMED'}],
|
||||||
|
'S_SMOKE': [BINARY_SENSOR_SCHEMA, {PLATFORM: 'switch', TYPE: 'V_ARMED'}],
|
||||||
|
'S_SPRINKLER': [
|
||||||
|
BINARY_SENSOR_SCHEMA, {PLATFORM: 'switch', TYPE: 'V_STATUS'}],
|
||||||
|
'S_WATER_LEAK': [
|
||||||
|
BINARY_SENSOR_SCHEMA, {PLATFORM: 'switch', TYPE: 'V_ARMED'}],
|
||||||
|
'S_SOUND': [
|
||||||
|
BINARY_SENSOR_SCHEMA, {PLATFORM: 'sensor', TYPE: 'V_LEVEL'},
|
||||||
|
{PLATFORM: 'switch', TYPE: 'V_ARMED'}],
|
||||||
|
'S_VIBRATION': [
|
||||||
|
BINARY_SENSOR_SCHEMA, {PLATFORM: 'sensor', TYPE: 'V_LEVEL'},
|
||||||
|
{PLATFORM: 'switch', TYPE: 'V_ARMED'}],
|
||||||
|
'S_MOISTURE': [
|
||||||
|
BINARY_SENSOR_SCHEMA, {PLATFORM: 'sensor', TYPE: 'V_LEVEL'},
|
||||||
|
{PLATFORM: 'switch', TYPE: 'V_ARMED'}],
|
||||||
|
'S_HVAC': [CLIMATE_SCHEMA],
|
||||||
|
'S_COVER': [
|
||||||
|
{PLATFORM: 'cover', TYPE: 'V_DIMMER'},
|
||||||
|
{PLATFORM: 'cover', TYPE: 'V_PERCENTAGE'},
|
||||||
|
{PLATFORM: 'cover', TYPE: 'V_LIGHT'},
|
||||||
|
{PLATFORM: 'cover', TYPE: 'V_STATUS'}],
|
||||||
|
'S_DIMMER': [LIGHT_DIMMER_SCHEMA, LIGHT_PERCENTAGE_SCHEMA],
|
||||||
|
'S_RGB_LIGHT': [LIGHT_RGB_SCHEMA],
|
||||||
|
'S_RGBW_LIGHT': [LIGHT_RGBW_SCHEMA],
|
||||||
|
'S_INFO': [NOTIFY_SCHEMA, {PLATFORM: 'sensor', TYPE: 'V_TEXT'}],
|
||||||
|
'S_GPS': [
|
||||||
|
DEVICE_TRACKER_SCHEMA, {PLATFORM: 'sensor', TYPE: 'V_POSITION'}],
|
||||||
|
'S_TEMP': [{PLATFORM: 'sensor', TYPE: 'V_TEMP'}],
|
||||||
|
'S_HUM': [{PLATFORM: 'sensor', TYPE: 'V_HUM'}],
|
||||||
|
'S_BARO': [
|
||||||
|
{PLATFORM: 'sensor', TYPE: 'V_PRESSURE'},
|
||||||
|
{PLATFORM: 'sensor', TYPE: 'V_FORECAST'}],
|
||||||
|
'S_WIND': [
|
||||||
|
{PLATFORM: 'sensor', TYPE: 'V_WIND'},
|
||||||
|
{PLATFORM: 'sensor', TYPE: 'V_GUST'},
|
||||||
|
{PLATFORM: 'sensor', TYPE: 'V_DIRECTION'}],
|
||||||
|
'S_RAIN': [
|
||||||
|
{PLATFORM: 'sensor', TYPE: 'V_RAIN'},
|
||||||
|
{PLATFORM: 'sensor', TYPE: 'V_RAINRATE'}],
|
||||||
|
'S_UV': [{PLATFORM: 'sensor', TYPE: 'V_UV'}],
|
||||||
|
'S_WEIGHT': [
|
||||||
|
{PLATFORM: 'sensor', TYPE: 'V_WEIGHT'},
|
||||||
|
{PLATFORM: 'sensor', TYPE: 'V_IMPEDANCE'}],
|
||||||
|
'S_POWER': [
|
||||||
|
{PLATFORM: 'sensor', TYPE: 'V_WATT'},
|
||||||
|
{PLATFORM: 'sensor', TYPE: 'V_KWH'},
|
||||||
|
{PLATFORM: 'sensor', TYPE: 'V_VAR'},
|
||||||
|
{PLATFORM: 'sensor', TYPE: 'V_VA'},
|
||||||
|
{PLATFORM: 'sensor', TYPE: 'V_POWER_FACTOR'}],
|
||||||
|
'S_DISTANCE': [{PLATFORM: 'sensor', TYPE: 'V_DISTANCE'}],
|
||||||
|
'S_LIGHT_LEVEL': [
|
||||||
|
{PLATFORM: 'sensor', TYPE: 'V_LIGHT_LEVEL'},
|
||||||
|
{PLATFORM: 'sensor', TYPE: 'V_LEVEL'}],
|
||||||
|
'S_IR': [
|
||||||
|
{PLATFORM: 'sensor', TYPE: 'V_IR_RECEIVE'},
|
||||||
|
{PLATFORM: 'switch', TYPE: 'V_IR_SEND',
|
||||||
|
SCHEMA: {'V_IR_SEND': cv.string, 'V_LIGHT': cv.string}}],
|
||||||
|
'S_WATER': [
|
||||||
|
{PLATFORM: 'sensor', TYPE: 'V_FLOW'},
|
||||||
|
{PLATFORM: 'sensor', TYPE: 'V_VOLUME'}],
|
||||||
|
'S_CUSTOM': [
|
||||||
|
{PLATFORM: 'sensor', TYPE: 'V_VAR1'},
|
||||||
|
{PLATFORM: 'sensor', TYPE: 'V_VAR2'},
|
||||||
|
{PLATFORM: 'sensor', TYPE: 'V_VAR3'},
|
||||||
|
{PLATFORM: 'sensor', TYPE: 'V_VAR4'},
|
||||||
|
{PLATFORM: 'sensor', TYPE: 'V_VAR5'},
|
||||||
|
{PLATFORM: 'sensor', TYPE: 'V_CUSTOM'}],
|
||||||
|
'S_SCENE_CONTROLLER': [
|
||||||
|
{PLATFORM: 'sensor', TYPE: 'V_SCENE_ON'},
|
||||||
|
{PLATFORM: 'sensor', TYPE: 'V_SCENE_OFF'}],
|
||||||
|
'S_COLOR_SENSOR': [{PLATFORM: 'sensor', TYPE: 'V_RGB'}],
|
||||||
|
'S_MULTIMETER': [
|
||||||
|
{PLATFORM: 'sensor', TYPE: 'V_VOLTAGE'},
|
||||||
|
{PLATFORM: 'sensor', TYPE: 'V_CURRENT'},
|
||||||
|
{PLATFORM: 'sensor', TYPE: 'V_IMPEDANCE'}],
|
||||||
|
'S_GAS': [
|
||||||
|
{PLATFORM: 'sensor', TYPE: 'V_FLOW'},
|
||||||
|
{PLATFORM: 'sensor', TYPE: 'V_VOLUME'}],
|
||||||
|
'S_WATER_QUALITY': [
|
||||||
|
{PLATFORM: 'sensor', TYPE: 'V_TEMP'},
|
||||||
|
{PLATFORM: 'sensor', TYPE: 'V_PH'},
|
||||||
|
{PLATFORM: 'sensor', TYPE: 'V_ORP'},
|
||||||
|
{PLATFORM: 'sensor', TYPE: 'V_EC'},
|
||||||
|
{PLATFORM: 'switch', TYPE: 'V_STATUS'}],
|
||||||
|
'S_AIR_QUALITY': DUST_SCHEMA,
|
||||||
|
'S_DUST': DUST_SCHEMA,
|
||||||
|
'S_LIGHT': [SWITCH_LIGHT_SCHEMA],
|
||||||
|
'S_BINARY': [SWITCH_STATUS_SCHEMA],
|
||||||
|
'S_LOCK': [{PLATFORM: 'switch', TYPE: 'V_LOCK_STATUS'}],
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
def setup(hass, config):
|
def setup(hass, config):
|
||||||
"""Set up the MySensors component."""
|
"""Set up the MySensors component."""
|
||||||
import mysensors.mysensors as mysensors
|
import mysensors.mysensors as mysensors
|
||||||
|
@ -197,20 +325,14 @@ def setup(hass, config):
|
||||||
# invalid ip address
|
# invalid ip address
|
||||||
return
|
return
|
||||||
gateway.metric = hass.config.units.is_metric
|
gateway.metric = hass.config.units.is_metric
|
||||||
optimistic = config[DOMAIN].get(CONF_OPTIMISTIC)
|
gateway.optimistic = config[DOMAIN].get(CONF_OPTIMISTIC)
|
||||||
gateway = GatewayWrapper(gateway, optimistic, device)
|
gateway.device = device
|
||||||
# pylint: disable=attribute-defined-outside-init
|
gateway.event_callback = gw_callback_factory(hass)
|
||||||
gateway.event_callback = gateway.callback_factory()
|
|
||||||
|
|
||||||
def gw_start(event):
|
def gw_start(event):
|
||||||
"""Trigger to start of the gateway and any persistence."""
|
"""Trigger to start of the gateway and any persistence."""
|
||||||
if persistence:
|
if persistence:
|
||||||
for node_id in gateway.sensors:
|
discover_persistent_devices(hass, gateway)
|
||||||
node = gateway.sensors[node_id]
|
|
||||||
for child_id in node.children:
|
|
||||||
msg = mysensors.Message().modify(
|
|
||||||
node_id=node_id, child_id=child_id)
|
|
||||||
gateway.event_callback(msg)
|
|
||||||
gateway.start()
|
gateway.start()
|
||||||
hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP,
|
hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP,
|
||||||
lambda event: gateway.stop())
|
lambda event: gateway.stop())
|
||||||
|
@ -219,15 +341,8 @@ def setup(hass, config):
|
||||||
|
|
||||||
return gateway
|
return gateway
|
||||||
|
|
||||||
gateways = hass.data.get(MYSENSORS_GATEWAYS)
|
|
||||||
if gateways is not None:
|
|
||||||
_LOGGER.error(
|
|
||||||
"%s already exists in %s, will not setup %s component",
|
|
||||||
MYSENSORS_GATEWAYS, hass.data, DOMAIN)
|
|
||||||
return False
|
|
||||||
|
|
||||||
# Setup all devices from config
|
# Setup all devices from config
|
||||||
gateways = []
|
gateways = {}
|
||||||
conf_gateways = config[DOMAIN][CONF_GATEWAYS]
|
conf_gateways = config[DOMAIN][CONF_GATEWAYS]
|
||||||
|
|
||||||
for index, gway in enumerate(conf_gateways):
|
for index, gway in enumerate(conf_gateways):
|
||||||
|
@ -243,7 +358,7 @@ def setup(hass, config):
|
||||||
device, persistence_file, baud_rate, tcp_port, in_prefix,
|
device, persistence_file, baud_rate, tcp_port, in_prefix,
|
||||||
out_prefix)
|
out_prefix)
|
||||||
if ready_gateway is not None:
|
if ready_gateway is not None:
|
||||||
gateways.append(ready_gateway)
|
gateways[id(ready_gateway)] = ready_gateway
|
||||||
|
|
||||||
if not gateways:
|
if not gateways:
|
||||||
_LOGGER.error(
|
_LOGGER.error(
|
||||||
|
@ -252,115 +367,187 @@ def setup(hass, config):
|
||||||
|
|
||||||
hass.data[MYSENSORS_GATEWAYS] = gateways
|
hass.data[MYSENSORS_GATEWAYS] = gateways
|
||||||
|
|
||||||
for component in ['sensor', 'switch', 'light', 'binary_sensor', 'climate',
|
|
||||||
'cover']:
|
|
||||||
discovery.load_platform(hass, component, DOMAIN, {}, config)
|
|
||||||
|
|
||||||
discovery.load_platform(
|
|
||||||
hass, 'device_tracker', DOMAIN, {}, config)
|
|
||||||
|
|
||||||
discovery.load_platform(
|
|
||||||
hass, 'notify', DOMAIN, {CONF_NAME: DOMAIN}, config)
|
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
def pf_callback_factory(map_sv_types, devices, entity_class, add_devices=None):
|
def validate_child(gateway, node_id, child):
|
||||||
"""Return a new callback for the platform."""
|
"""Validate that a child has the correct values according to schema.
|
||||||
def mysensors_callback(gateway, msg):
|
|
||||||
"""Run when a message from the gateway arrives."""
|
Return a dict of platform with a list of device ids for validated devices.
|
||||||
if gateway.sensors[msg.node_id].sketch_name is None:
|
"""
|
||||||
_LOGGER.debug("No sketch_name: node %s", msg.node_id)
|
validated = defaultdict(list)
|
||||||
return
|
|
||||||
child = gateway.sensors[msg.node_id].children.get(msg.child_id)
|
if not child.values:
|
||||||
|
_LOGGER.debug(
|
||||||
|
"No child values for node %s child %s", node_id, child.id)
|
||||||
|
return validated
|
||||||
|
if gateway.sensors[node_id].sketch_name is None:
|
||||||
|
_LOGGER.debug("Node %s is missing sketch name", node_id)
|
||||||
|
return validated
|
||||||
|
pres = gateway.const.Presentation
|
||||||
|
set_req = gateway.const.SetReq
|
||||||
|
s_name = next(
|
||||||
|
(member.name for member in pres if member.value == child.type), None)
|
||||||
|
if s_name not in MYSENSORS_CONST_SCHEMA:
|
||||||
|
_LOGGER.warning("Child type %s is not supported", s_name)
|
||||||
|
return validated
|
||||||
|
child_schemas = MYSENSORS_CONST_SCHEMA[s_name]
|
||||||
|
|
||||||
|
def msg(name):
|
||||||
|
"""Return a message for an invalid schema."""
|
||||||
|
return "{} requires value_type {}".format(
|
||||||
|
pres(child.type).name, set_req[name].name)
|
||||||
|
|
||||||
|
for schema in child_schemas:
|
||||||
|
platform = schema[PLATFORM]
|
||||||
|
v_name = schema[TYPE]
|
||||||
|
value_type = next(
|
||||||
|
(member.value for member in set_req if member.name == v_name),
|
||||||
|
None)
|
||||||
|
if value_type is None:
|
||||||
|
continue
|
||||||
|
_child_schema = child.get_schema(gateway.protocol_version)
|
||||||
|
vol_schema = _child_schema.extend(
|
||||||
|
{vol.Required(set_req[key].value, msg=msg(key)):
|
||||||
|
_child_schema.schema.get(set_req[key].value, val)
|
||||||
|
for key, val in schema.get(SCHEMA, {v_name: cv.string}).items()},
|
||||||
|
extra=vol.ALLOW_EXTRA)
|
||||||
|
try:
|
||||||
|
vol_schema(child.values)
|
||||||
|
except vol.Invalid as exc:
|
||||||
|
level = (logging.WARNING if value_type in child.values
|
||||||
|
else logging.DEBUG)
|
||||||
|
_LOGGER.log(
|
||||||
|
level,
|
||||||
|
"Invalid values: %s: %s platform: node %s child %s: %s",
|
||||||
|
child.values, platform, node_id, child.id, exc)
|
||||||
|
continue
|
||||||
|
dev_id = id(gateway), node_id, child.id, value_type
|
||||||
|
validated[platform].append(dev_id)
|
||||||
|
return validated
|
||||||
|
|
||||||
|
|
||||||
|
def discover_mysensors_platform(hass, platform, new_devices):
|
||||||
|
"""Discover a mysensors platform."""
|
||||||
|
discovery.load_platform(
|
||||||
|
hass, platform, DOMAIN, {ATTR_DEVICES: new_devices, CONF_NAME: DOMAIN})
|
||||||
|
|
||||||
|
|
||||||
|
def discover_persistent_devices(hass, gateway):
|
||||||
|
"""Discover platforms for devices loaded via persistence file."""
|
||||||
|
new_devices = defaultdict(list)
|
||||||
|
for node_id in gateway.sensors:
|
||||||
|
node = gateway.sensors[node_id]
|
||||||
|
for child in node.children.values():
|
||||||
|
validated = validate_child(gateway, node_id, child)
|
||||||
|
for platform, dev_ids in validated.items():
|
||||||
|
new_devices[platform].extend(dev_ids)
|
||||||
|
for platform, dev_ids in new_devices.items():
|
||||||
|
discover_mysensors_platform(hass, platform, dev_ids)
|
||||||
|
|
||||||
|
|
||||||
|
def get_mysensors_devices(hass, domain):
|
||||||
|
"""Return mysensors devices for a platform."""
|
||||||
|
if MYSENSORS_PLATFORM_DEVICES.format(domain) not in hass.data:
|
||||||
|
hass.data[MYSENSORS_PLATFORM_DEVICES.format(domain)] = {}
|
||||||
|
return hass.data[MYSENSORS_PLATFORM_DEVICES.format(domain)]
|
||||||
|
|
||||||
|
|
||||||
|
def gw_callback_factory(hass):
|
||||||
|
"""Return a new callback for the gateway."""
|
||||||
|
def mysensors_callback(msg):
|
||||||
|
"""Default callback for a mysensors gateway."""
|
||||||
|
start = timer()
|
||||||
|
_LOGGER.debug(
|
||||||
|
"Node update: node %s child %s", msg.node_id, msg.child_id)
|
||||||
|
|
||||||
|
child = msg.gateway.sensors[msg.node_id].children.get(msg.child_id)
|
||||||
if child is None:
|
if child is None:
|
||||||
|
_LOGGER.debug(
|
||||||
|
"Not a child update for node %s", msg.node_id)
|
||||||
return
|
return
|
||||||
for value_type in child.values:
|
|
||||||
key = msg.node_id, child.id, value_type
|
signals = []
|
||||||
if child.type not in map_sv_types or \
|
|
||||||
value_type not in map_sv_types[child.type]:
|
# Update all platforms for the device via dispatcher.
|
||||||
continue
|
# Add/update entity if schema validates to true.
|
||||||
if key in devices:
|
validated = validate_child(msg.gateway, msg.node_id, child)
|
||||||
if add_devices:
|
for platform, dev_ids in validated.items():
|
||||||
devices[key].schedule_update_ha_state(True)
|
devices = get_mysensors_devices(hass, platform)
|
||||||
else:
|
for idx, dev_id in enumerate(list(dev_ids)):
|
||||||
devices[key].update()
|
if dev_id in devices:
|
||||||
continue
|
dev_ids.pop(idx)
|
||||||
name = '{} {} {}'.format(
|
signals.append(SIGNAL_CALLBACK.format(*dev_id))
|
||||||
gateway.sensors[msg.node_id].sketch_name, msg.node_id,
|
if dev_ids:
|
||||||
child.id)
|
discover_mysensors_platform(hass, platform, dev_ids)
|
||||||
if isinstance(entity_class, dict):
|
for signal in set(signals):
|
||||||
device_class = entity_class[child.type]
|
# Only one signal per device is needed.
|
||||||
else:
|
# A device can have multiple platforms, ie multiple schemas.
|
||||||
device_class = entity_class
|
# FOR LATER: Add timer to not signal if another update comes in.
|
||||||
devices[key] = device_class(
|
dispatcher_send(hass, signal)
|
||||||
gateway, msg.node_id, child.id, name, value_type)
|
end = timer()
|
||||||
if add_devices:
|
if end - start > 0.1:
|
||||||
_LOGGER.info("Adding new devices: %s", [devices[key]])
|
_LOGGER.debug(
|
||||||
add_devices([devices[key]], True)
|
"Callback for node %s child %s took %.3f seconds",
|
||||||
else:
|
msg.node_id, msg.child_id, end - start)
|
||||||
devices[key].update()
|
|
||||||
return mysensors_callback
|
return mysensors_callback
|
||||||
|
|
||||||
|
|
||||||
class GatewayWrapper(object):
|
def get_mysensors_name(gateway, node_id, child_id):
|
||||||
"""Gateway wrapper class."""
|
"""Return a name for a node child."""
|
||||||
|
return '{} {} {}'.format(
|
||||||
def __init__(self, gateway, optimistic, device):
|
gateway.sensors[node_id].sketch_name, node_id, child_id)
|
||||||
"""Set up the class attributes on instantiation.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
gateway (mysensors.SerialGateway): Gateway to wrap.
|
|
||||||
optimistic (bool): Send values to actuators without feedback state.
|
|
||||||
device (str): Path to serial port, ip adress or mqtt.
|
|
||||||
|
|
||||||
Attributes:
|
|
||||||
_wrapped_gateway (mysensors.SerialGateway): Wrapped gateway.
|
|
||||||
platform_callbacks (list): Callback functions, one per platform.
|
|
||||||
optimistic (bool): Send values to actuators without feedback state.
|
|
||||||
device (str): Device configured as gateway.
|
|
||||||
__initialised (bool): True if GatewayWrapper is initialised.
|
|
||||||
|
|
||||||
"""
|
|
||||||
self._wrapped_gateway = gateway
|
|
||||||
self.platform_callbacks = []
|
|
||||||
self.optimistic = optimistic
|
|
||||||
self.device = device
|
|
||||||
self.__initialised = True
|
|
||||||
|
|
||||||
def __getattr__(self, name):
|
|
||||||
"""See if this object has attribute name."""
|
|
||||||
# Do not use hasattr, it goes into infinite recurrsion
|
|
||||||
if name in self.__dict__:
|
|
||||||
# This object has the attribute.
|
|
||||||
return getattr(self, name)
|
|
||||||
# The wrapped object has the attribute.
|
|
||||||
return getattr(self._wrapped_gateway, name)
|
|
||||||
|
|
||||||
def __setattr__(self, name, value):
|
|
||||||
"""See if this object has attribute name then set to value."""
|
|
||||||
if '_GatewayWrapper__initialised' not in self.__dict__:
|
|
||||||
return object.__setattr__(self, name, value)
|
|
||||||
elif name in self.__dict__:
|
|
||||||
object.__setattr__(self, name, value)
|
|
||||||
else:
|
|
||||||
object.__setattr__(self._wrapped_gateway, name, value)
|
|
||||||
|
|
||||||
def callback_factory(self):
|
|
||||||
"""Return a new callback function."""
|
|
||||||
def node_update(msg):
|
|
||||||
"""Handle node updates from the MySensors gateway."""
|
|
||||||
_LOGGER.debug(
|
|
||||||
"Update: node %s, child %s sub_type %s",
|
|
||||||
msg.node_id, msg.child_id, msg.sub_type)
|
|
||||||
for callback in self.platform_callbacks:
|
|
||||||
callback(self, msg)
|
|
||||||
|
|
||||||
return node_update
|
|
||||||
|
|
||||||
|
|
||||||
class MySensorsDeviceEntity(object):
|
def get_mysensors_gateway(hass, gateway_id):
|
||||||
"""Representation of a MySensors entity."""
|
"""Return gateway."""
|
||||||
|
if MYSENSORS_GATEWAYS not in hass.data:
|
||||||
|
hass.data[MYSENSORS_GATEWAYS] = {}
|
||||||
|
gateways = hass.data.get(MYSENSORS_GATEWAYS)
|
||||||
|
return gateways.get(gateway_id)
|
||||||
|
|
||||||
|
|
||||||
|
def setup_mysensors_platform(
|
||||||
|
hass, domain, discovery_info, device_class, device_args=None,
|
||||||
|
add_devices=None):
|
||||||
|
"""Set up a mysensors platform."""
|
||||||
|
# Only act if called via mysensors by discovery event.
|
||||||
|
# Otherwise gateway is not setup.
|
||||||
|
if not discovery_info:
|
||||||
|
return
|
||||||
|
if device_args is None:
|
||||||
|
device_args = ()
|
||||||
|
new_devices = []
|
||||||
|
new_dev_ids = discovery_info[ATTR_DEVICES]
|
||||||
|
for dev_id in new_dev_ids:
|
||||||
|
devices = get_mysensors_devices(hass, domain)
|
||||||
|
if dev_id in devices:
|
||||||
|
continue
|
||||||
|
gateway_id, node_id, child_id, value_type = dev_id
|
||||||
|
gateway = get_mysensors_gateway(hass, gateway_id)
|
||||||
|
if not gateway:
|
||||||
|
continue
|
||||||
|
device_class_copy = device_class
|
||||||
|
if isinstance(device_class, dict):
|
||||||
|
child = gateway.sensors[node_id].children[child_id]
|
||||||
|
s_type = gateway.const.Presentation(child.type).name
|
||||||
|
device_class_copy = device_class[s_type]
|
||||||
|
name = get_mysensors_name(gateway, node_id, child_id)
|
||||||
|
|
||||||
|
# python 3.4 cannot unpack inside tuple, but combining tuples works
|
||||||
|
args_copy = device_args + (
|
||||||
|
gateway, node_id, child_id, name, value_type)
|
||||||
|
devices[dev_id] = device_class_copy(*args_copy)
|
||||||
|
new_devices.append(devices[dev_id])
|
||||||
|
if new_devices:
|
||||||
|
_LOGGER.info("Adding new devices: %s", new_devices)
|
||||||
|
if add_devices is not None:
|
||||||
|
add_devices(new_devices, True)
|
||||||
|
return new_devices
|
||||||
|
|
||||||
|
|
||||||
|
class MySensorsDevice(object):
|
||||||
|
"""Representation of a MySensors device."""
|
||||||
|
|
||||||
def __init__(self, gateway, node_id, child_id, name, value_type):
|
def __init__(self, gateway, node_id, child_id, name, value_type):
|
||||||
"""Set up the MySensors device."""
|
"""Set up the MySensors device."""
|
||||||
|
@ -373,11 +560,6 @@ class MySensorsDeviceEntity(object):
|
||||||
self.child_type = child.type
|
self.child_type = child.type
|
||||||
self._values = {}
|
self._values = {}
|
||||||
|
|
||||||
@property
|
|
||||||
def should_poll(self):
|
|
||||||
"""Mysensor gateway pushes its state to HA."""
|
|
||||||
return False
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
"""Return the name of this entity."""
|
"""Return the name of this entity."""
|
||||||
|
@ -399,18 +581,9 @@ class MySensorsDeviceEntity(object):
|
||||||
set_req = self.gateway.const.SetReq
|
set_req = self.gateway.const.SetReq
|
||||||
|
|
||||||
for value_type, value in self._values.items():
|
for value_type, value in self._values.items():
|
||||||
try:
|
attr[set_req(value_type).name] = value
|
||||||
attr[set_req(value_type).name] = value
|
|
||||||
except ValueError:
|
|
||||||
_LOGGER.error("Value_type %s is not valid for mysensors "
|
|
||||||
"version %s", value_type,
|
|
||||||
self.gateway.protocol_version)
|
|
||||||
return attr
|
|
||||||
|
|
||||||
@property
|
return attr
|
||||||
def available(self):
|
|
||||||
"""Return true if entity is available."""
|
|
||||||
return self.value_type in self._values
|
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
"""Update the controller with the latest value from a sensor."""
|
"""Update the controller with the latest value from a sensor."""
|
||||||
|
@ -419,7 +592,8 @@ class MySensorsDeviceEntity(object):
|
||||||
set_req = self.gateway.const.SetReq
|
set_req = self.gateway.const.SetReq
|
||||||
for value_type, value in child.values.items():
|
for value_type, value in child.values.items():
|
||||||
_LOGGER.debug(
|
_LOGGER.debug(
|
||||||
"%s: value_type %s, value = %s", self._name, value_type, value)
|
"Entity update: %s: value_type %s, value = %s",
|
||||||
|
self._name, value_type, value)
|
||||||
if value_type in (set_req.V_ARMED, set_req.V_LIGHT,
|
if value_type in (set_req.V_ARMED, set_req.V_LIGHT,
|
||||||
set_req.V_LOCK_STATUS, set_req.V_TRIPPED):
|
set_req.V_LOCK_STATUS, set_req.V_TRIPPED):
|
||||||
self._values[value_type] = (
|
self._values[value_type] = (
|
||||||
|
@ -428,3 +602,29 @@ class MySensorsDeviceEntity(object):
|
||||||
self._values[value_type] = int(value)
|
self._values[value_type] = int(value)
|
||||||
else:
|
else:
|
||||||
self._values[value_type] = value
|
self._values[value_type] = value
|
||||||
|
|
||||||
|
|
||||||
|
class MySensorsEntity(MySensorsDevice, Entity):
|
||||||
|
"""Representation of a MySensors entity."""
|
||||||
|
|
||||||
|
@property
|
||||||
|
def should_poll(self):
|
||||||
|
"""Mysensor gateway pushes its state to HA."""
|
||||||
|
return False
|
||||||
|
|
||||||
|
@property
|
||||||
|
def available(self):
|
||||||
|
"""Return true if entity is available."""
|
||||||
|
return self.value_type in self._values
|
||||||
|
|
||||||
|
def _async_update_callback(self):
|
||||||
|
"""Update the entity."""
|
||||||
|
self.hass.async_add_job(self.async_update_ha_state(True))
|
||||||
|
|
||||||
|
@asyncio.coroutine
|
||||||
|
def async_added_to_hass(self):
|
||||||
|
"""Register update callback."""
|
||||||
|
dev_id = id(self.gateway), self.node_id, self.child_id, self.value_type
|
||||||
|
async_dispatcher_connect(
|
||||||
|
self.hass, SIGNAL_CALLBACK.format(*dev_id),
|
||||||
|
self._async_update_callback)
|
||||||
|
|
|
@ -6,35 +6,19 @@ https://home-assistant.io/components/notify.mysensors/
|
||||||
"""
|
"""
|
||||||
from homeassistant.components import mysensors
|
from homeassistant.components import mysensors
|
||||||
from homeassistant.components.notify import (
|
from homeassistant.components.notify import (
|
||||||
ATTR_TARGET, BaseNotificationService)
|
ATTR_TARGET, DOMAIN, BaseNotificationService)
|
||||||
|
|
||||||
|
|
||||||
def get_service(hass, config, discovery_info=None):
|
def get_service(hass, config, discovery_info=None):
|
||||||
"""Get the MySensors notification service."""
|
"""Get the MySensors notification service."""
|
||||||
if discovery_info is None:
|
new_devices = mysensors.setup_mysensors_platform(
|
||||||
|
hass, DOMAIN, discovery_info, MySensorsNotificationDevice)
|
||||||
|
if not new_devices:
|
||||||
return
|
return
|
||||||
platform_devices = []
|
return MySensorsNotificationService(hass)
|
||||||
gateways = hass.data.get(mysensors.MYSENSORS_GATEWAYS)
|
|
||||||
if not gateways:
|
|
||||||
return
|
|
||||||
|
|
||||||
for gateway in gateways:
|
|
||||||
if float(gateway.protocol_version) < 2.0:
|
|
||||||
continue
|
|
||||||
pres = gateway.const.Presentation
|
|
||||||
set_req = gateway.const.SetReq
|
|
||||||
map_sv_types = {
|
|
||||||
pres.S_INFO: [set_req.V_TEXT],
|
|
||||||
}
|
|
||||||
devices = {}
|
|
||||||
gateway.platform_callbacks.append(mysensors.pf_callback_factory(
|
|
||||||
map_sv_types, devices, MySensorsNotificationDevice))
|
|
||||||
platform_devices.append(devices)
|
|
||||||
|
|
||||||
return MySensorsNotificationService(platform_devices)
|
|
||||||
|
|
||||||
|
|
||||||
class MySensorsNotificationDevice(mysensors.MySensorsDeviceEntity):
|
class MySensorsNotificationDevice(mysensors.MySensorsDevice):
|
||||||
"""Represent a MySensors Notification device."""
|
"""Represent a MySensors Notification device."""
|
||||||
|
|
||||||
def send_msg(self, msg):
|
def send_msg(self, msg):
|
||||||
|
@ -44,24 +28,25 @@ class MySensorsNotificationDevice(mysensors.MySensorsDeviceEntity):
|
||||||
self.gateway.set_child_value(
|
self.gateway.set_child_value(
|
||||||
self.node_id, self.child_id, self.value_type, sub_msg)
|
self.node_id, self.child_id, self.value_type, sub_msg)
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
"""Return the representation."""
|
||||||
|
return "<MySensorsNotificationDevice {}>".format(self.name)
|
||||||
|
|
||||||
|
|
||||||
class MySensorsNotificationService(BaseNotificationService):
|
class MySensorsNotificationService(BaseNotificationService):
|
||||||
"""Implement MySensors notification service."""
|
"""Implement a MySensors notification service."""
|
||||||
|
|
||||||
# pylint: disable=too-few-public-methods
|
# pylint: disable=too-few-public-methods
|
||||||
|
|
||||||
def __init__(self, platform_devices):
|
def __init__(self, hass):
|
||||||
"""Initialize the service."""
|
"""Initialize the service."""
|
||||||
self.platform_devices = platform_devices
|
self.devices = mysensors.get_mysensors_devices(hass, DOMAIN)
|
||||||
|
|
||||||
def send_message(self, message="", **kwargs):
|
def send_message(self, message="", **kwargs):
|
||||||
"""Send a message to a user."""
|
"""Send a message to a user."""
|
||||||
target_devices = kwargs.get(ATTR_TARGET)
|
target_devices = kwargs.get(ATTR_TARGET)
|
||||||
devices = []
|
devices = [device for device in self.devices.values()
|
||||||
for gw_devs in self.platform_devices:
|
if target_devices is None or device.name in target_devices]
|
||||||
for device in gw_devs.values():
|
|
||||||
if target_devices is None or device.name in target_devices:
|
|
||||||
devices.append(device)
|
|
||||||
|
|
||||||
for device in devices:
|
for device in devices:
|
||||||
device.send_msg(message)
|
device.send_msg(message)
|
||||||
|
|
|
@ -4,89 +4,18 @@ Support for MySensors sensors.
|
||||||
For more details about this platform, please refer to the documentation at
|
For more details about this platform, please refer to the documentation at
|
||||||
https://home-assistant.io/components/sensor.mysensors/
|
https://home-assistant.io/components/sensor.mysensors/
|
||||||
"""
|
"""
|
||||||
import logging
|
|
||||||
|
|
||||||
from homeassistant.components import mysensors
|
from homeassistant.components import mysensors
|
||||||
|
from homeassistant.components.sensor import DOMAIN
|
||||||
from homeassistant.const import TEMP_CELSIUS, TEMP_FAHRENHEIT
|
from homeassistant.const import TEMP_CELSIUS, TEMP_FAHRENHEIT
|
||||||
from homeassistant.helpers.entity import Entity
|
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
|
|
||||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||||
"""Set up the MySensors platform for sensors."""
|
"""Set up the MySensors platform for sensors."""
|
||||||
# Only act if loaded via mysensors by discovery event.
|
mysensors.setup_mysensors_platform(
|
||||||
# Otherwise gateway is not setup.
|
hass, DOMAIN, discovery_info, MySensorsSensor, add_devices=add_devices)
|
||||||
if discovery_info is None:
|
|
||||||
return
|
|
||||||
|
|
||||||
gateways = hass.data.get(mysensors.MYSENSORS_GATEWAYS)
|
|
||||||
if not gateways:
|
|
||||||
return
|
|
||||||
|
|
||||||
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_TEMP: [set_req.V_TEMP],
|
|
||||||
pres.S_HUM: [set_req.V_HUM],
|
|
||||||
pres.S_BARO: [set_req.V_PRESSURE, set_req.V_FORECAST],
|
|
||||||
pres.S_WIND: [set_req.V_WIND, set_req.V_GUST, set_req.V_DIRECTION],
|
|
||||||
pres.S_RAIN: [set_req.V_RAIN, set_req.V_RAINRATE],
|
|
||||||
pres.S_UV: [set_req.V_UV],
|
|
||||||
pres.S_WEIGHT: [set_req.V_WEIGHT, set_req.V_IMPEDANCE],
|
|
||||||
pres.S_POWER: [set_req.V_WATT, set_req.V_KWH],
|
|
||||||
pres.S_DISTANCE: [set_req.V_DISTANCE],
|
|
||||||
pres.S_LIGHT_LEVEL: [set_req.V_LIGHT_LEVEL],
|
|
||||||
pres.S_IR: [set_req.V_IR_RECEIVE],
|
|
||||||
pres.S_WATER: [set_req.V_FLOW, set_req.V_VOLUME],
|
|
||||||
pres.S_CUSTOM: [set_req.V_VAR1,
|
|
||||||
set_req.V_VAR2,
|
|
||||||
set_req.V_VAR3,
|
|
||||||
set_req.V_VAR4,
|
|
||||||
set_req.V_VAR5],
|
|
||||||
pres.S_SCENE_CONTROLLER: [set_req.V_SCENE_ON,
|
|
||||||
set_req.V_SCENE_OFF],
|
|
||||||
}
|
|
||||||
if float(gateway.protocol_version) < 1.5:
|
|
||||||
map_sv_types.update({
|
|
||||||
pres.S_AIR_QUALITY: [set_req.V_DUST_LEVEL],
|
|
||||||
pres.S_DUST: [set_req.V_DUST_LEVEL],
|
|
||||||
})
|
|
||||||
if float(gateway.protocol_version) >= 1.5:
|
|
||||||
map_sv_types.update({
|
|
||||||
pres.S_COLOR_SENSOR: [set_req.V_RGB],
|
|
||||||
pres.S_MULTIMETER: [set_req.V_VOLTAGE,
|
|
||||||
set_req.V_CURRENT,
|
|
||||||
set_req.V_IMPEDANCE],
|
|
||||||
pres.S_SOUND: [set_req.V_LEVEL],
|
|
||||||
pres.S_VIBRATION: [set_req.V_LEVEL],
|
|
||||||
pres.S_MOISTURE: [set_req.V_LEVEL],
|
|
||||||
pres.S_AIR_QUALITY: [set_req.V_LEVEL],
|
|
||||||
pres.S_DUST: [set_req.V_LEVEL],
|
|
||||||
})
|
|
||||||
map_sv_types[pres.S_LIGHT_LEVEL].append(set_req.V_LEVEL)
|
|
||||||
|
|
||||||
if float(gateway.protocol_version) >= 2.0:
|
|
||||||
map_sv_types.update({
|
|
||||||
pres.S_INFO: [set_req.V_TEXT],
|
|
||||||
pres.S_GAS: [set_req.V_FLOW, set_req.V_VOLUME],
|
|
||||||
pres.S_GPS: [set_req.V_POSITION],
|
|
||||||
pres.S_WATER_QUALITY: [set_req.V_TEMP, set_req.V_PH,
|
|
||||||
set_req.V_ORP, set_req.V_EC]
|
|
||||||
})
|
|
||||||
map_sv_types[pres.S_CUSTOM].append(set_req.V_CUSTOM)
|
|
||||||
map_sv_types[pres.S_POWER].extend(
|
|
||||||
[set_req.V_VAR, set_req.V_VA, set_req.V_POWER_FACTOR])
|
|
||||||
|
|
||||||
devices = {}
|
|
||||||
gateway.platform_callbacks.append(mysensors.pf_callback_factory(
|
|
||||||
map_sv_types, devices, MySensorsSensor, add_devices))
|
|
||||||
|
|
||||||
|
|
||||||
class MySensorsSensor(mysensors.MySensorsDeviceEntity, Entity):
|
class MySensorsSensor(mysensors.MySensorsEntity):
|
||||||
"""Representation of a MySensors Sensor child node."""
|
"""Representation of a MySensors Sensor child node."""
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
|
|
@ -4,7 +4,6 @@ Support for MySensors switches.
|
||||||
For more details about this platform, please refer to the documentation at
|
For more details about this platform, please refer to the documentation at
|
||||||
https://home-assistant.io/components/switch.mysensors/
|
https://home-assistant.io/components/switch.mysensors/
|
||||||
"""
|
"""
|
||||||
import logging
|
|
||||||
import os
|
import os
|
||||||
|
|
||||||
import voluptuous as vol
|
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.config import load_yaml_config_file
|
||||||
from homeassistant.const import ATTR_ENTITY_ID, STATE_OFF, STATE_ON
|
from homeassistant.const import ATTR_ENTITY_ID, STATE_OFF, STATE_ON
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
|
||||||
DEPENDENCIES = []
|
|
||||||
|
|
||||||
ATTR_IR_CODE = 'V_IR_SEND'
|
ATTR_IR_CODE = 'V_IR_SEND'
|
||||||
SERVICE_SEND_IR_CODE = 'mysensors_send_ir_code'
|
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):
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||||
"""Set up the mysensors platform for switches."""
|
"""Set up the mysensors platform for switches."""
|
||||||
# Only act if loaded via mysensors by discovery event.
|
device_class_map = {
|
||||||
# Otherwise gateway is not setup.
|
'S_DOOR': MySensorsSwitch,
|
||||||
if discovery_info is None:
|
'S_MOTION': MySensorsSwitch,
|
||||||
return
|
'S_SMOKE': MySensorsSwitch,
|
||||||
|
'S_LIGHT': MySensorsSwitch,
|
||||||
gateways = hass.data.get(mysensors.MYSENSORS_GATEWAYS)
|
'S_LOCK': MySensorsSwitch,
|
||||||
if not gateways:
|
'S_IR': MySensorsIRSwitch,
|
||||||
return
|
'S_BINARY': MySensorsSwitch,
|
||||||
|
'S_SPRINKLER': MySensorsSwitch,
|
||||||
platform_devices = []
|
'S_WATER_LEAK': MySensorsSwitch,
|
||||||
|
'S_SOUND': MySensorsSwitch,
|
||||||
for gateway in gateways:
|
'S_VIBRATION': MySensorsSwitch,
|
||||||
# Define the S_TYPES and V_TYPES that the platform should handle as
|
'S_MOISTURE': MySensorsSwitch,
|
||||||
# states. Map them in a dict of lists.
|
'S_WATER_QUALITY': MySensorsSwitch,
|
||||||
pres = gateway.const.Presentation
|
}
|
||||||
set_req = gateway.const.SetReq
|
mysensors.setup_mysensors_platform(
|
||||||
map_sv_types = {
|
hass, DOMAIN, discovery_info, device_class_map,
|
||||||
pres.S_DOOR: [set_req.V_ARMED],
|
add_devices=add_devices)
|
||||||
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)
|
|
||||||
|
|
||||||
def send_ir_code_service(service):
|
def send_ir_code_service(service):
|
||||||
"""Set IR code as device state attribute."""
|
"""Set IR code as device state attribute."""
|
||||||
entity_ids = service.data.get(ATTR_ENTITY_ID)
|
entity_ids = service.data.get(ATTR_ENTITY_ID)
|
||||||
ir_code = service.data.get(ATTR_IR_CODE)
|
ir_code = service.data.get(ATTR_IR_CODE)
|
||||||
|
devices = mysensors.get_mysensors_devices(hass, DOMAIN)
|
||||||
|
|
||||||
if entity_ids:
|
if entity_ids:
|
||||||
_devices = [device for gw_devs in platform_devices
|
_devices = [device for device in devices.values()
|
||||||
for device in gw_devs.values()
|
|
||||||
if isinstance(device, MySensorsIRSwitch) and
|
if isinstance(device, MySensorsIRSwitch) and
|
||||||
device.entity_id in entity_ids]
|
device.entity_id in entity_ids]
|
||||||
else:
|
else:
|
||||||
_devices = [device for gw_devs in platform_devices
|
_devices = [device for device in devices.values()
|
||||||
for device in gw_devs.values()
|
|
||||||
if isinstance(device, MySensorsIRSwitch)]
|
if isinstance(device, MySensorsIRSwitch)]
|
||||||
|
|
||||||
kwargs = {ATTR_IR_CODE: ir_code}
|
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)
|
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."""
|
"""Representation of the value of a MySensors Switch child node."""
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
@ -131,9 +82,7 @@ class MySensorsSwitch(mysensors.MySensorsDeviceEntity, SwitchDevice):
|
||||||
@property
|
@property
|
||||||
def is_on(self):
|
def is_on(self):
|
||||||
"""Return True if switch is on."""
|
"""Return True if switch is on."""
|
||||||
if self.value_type in self._values:
|
return self._values.get(self.value_type) == STATE_ON
|
||||||
return self._values[self.value_type] == STATE_ON
|
|
||||||
return False
|
|
||||||
|
|
||||||
def turn_on(self, **kwargs):
|
def turn_on(self, **kwargs):
|
||||||
"""Turn the switch on."""
|
"""Turn the switch on."""
|
||||||
|
@ -159,24 +108,18 @@ class MySensorsIRSwitch(MySensorsSwitch):
|
||||||
|
|
||||||
def __init__(self, *args):
|
def __init__(self, *args):
|
||||||
"""Set up instance attributes."""
|
"""Set up instance attributes."""
|
||||||
MySensorsSwitch.__init__(self, *args)
|
super().__init__(*args)
|
||||||
self._ir_code = None
|
self._ir_code = None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_on(self):
|
def is_on(self):
|
||||||
"""Return True if switch is on."""
|
"""Return True if switch is on."""
|
||||||
set_req = self.gateway.const.SetReq
|
set_req = self.gateway.const.SetReq
|
||||||
if set_req.V_LIGHT in self._values:
|
return self._values.get(set_req.V_LIGHT) == STATE_ON
|
||||||
return self._values[set_req.V_LIGHT] == STATE_ON
|
|
||||||
return False
|
|
||||||
|
|
||||||
def turn_on(self, **kwargs):
|
def turn_on(self, **kwargs):
|
||||||
"""Turn the IR switch on."""
|
"""Turn the IR switch on."""
|
||||||
set_req = self.gateway.const.SetReq
|
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:
|
if ATTR_IR_CODE in kwargs:
|
||||||
self._ir_code = kwargs[ATTR_IR_CODE]
|
self._ir_code = kwargs[ATTR_IR_CODE]
|
||||||
self.gateway.set_child_value(
|
self.gateway.set_child_value(
|
||||||
|
@ -194,10 +137,6 @@ class MySensorsIRSwitch(MySensorsSwitch):
|
||||||
def turn_off(self, **kwargs):
|
def turn_off(self, **kwargs):
|
||||||
"""Turn the IR switch off."""
|
"""Turn the IR switch off."""
|
||||||
set_req = self.gateway.const.SetReq
|
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.gateway.set_child_value(
|
||||||
self.node_id, self.child_id, set_req.V_LIGHT, 0)
|
self.node_id, self.child_id, set_req.V_LIGHT, 0)
|
||||||
if self.gateway.optimistic:
|
if self.gateway.optimistic:
|
||||||
|
@ -207,6 +146,5 @@ class MySensorsIRSwitch(MySensorsSwitch):
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
"""Update the controller with the latest value from a sensor."""
|
"""Update the controller with the latest value from a sensor."""
|
||||||
MySensorsSwitch.update(self)
|
super().update()
|
||||||
if self.value_type in self._values:
|
self._ir_code = self._values.get(self.value_type)
|
||||||
self._ir_code = self._values[self.value_type]
|
|
||||||
|
|
|
@ -655,7 +655,7 @@ pymodbus==1.3.1
|
||||||
pymyq==0.0.8
|
pymyq==0.0.8
|
||||||
|
|
||||||
# homeassistant.components.mysensors
|
# homeassistant.components.mysensors
|
||||||
pymysensors==0.10.0
|
pymysensors==0.11.0
|
||||||
|
|
||||||
# homeassistant.components.lock.nello
|
# homeassistant.components.lock.nello
|
||||||
pynello==1.5
|
pynello==1.5
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue