Add gateway wrapper, fix discovery and callbacks
* Add gateway wrapper by subclassing serial gateway. * Fix platform setup with discovery service. * Fix platform callback functions with callback factory.
This commit is contained in:
parent
be25ea4f09
commit
69ed6fe6e7
5 changed files with 244 additions and 216 deletions
|
@ -7,6 +7,7 @@ For more details about this platform, please refer to the documentation at
|
|||
https://home-assistant.io/components/sensor.mysensors/
|
||||
"""
|
||||
import logging
|
||||
from collections import defaultdict
|
||||
|
||||
from homeassistant.helpers.entity import Entity
|
||||
|
||||
|
@ -20,74 +21,71 @@ import homeassistant.components.mysensors as mysensors
|
|||
_LOGGER = logging.getLogger(__name__)
|
||||
DEPENDENCIES = []
|
||||
|
||||
ADD_DEVICES = None
|
||||
S_TYPES = None
|
||||
V_TYPES = None
|
||||
|
||||
|
||||
@mysensors.mysensors_update
|
||||
def sensor_update(gateway, port, devices, nid):
|
||||
"""Internal callback for sensor updates."""
|
||||
return (S_TYPES, V_TYPES, MySensorsSensor, ADD_DEVICES)
|
||||
|
||||
|
||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||
"""Setup the mysensors platform for sensors."""
|
||||
# Define the S_TYPES and V_TYPES that the platform should handle as states.
|
||||
global ADD_DEVICES
|
||||
ADD_DEVICES = add_devices
|
||||
global S_TYPES
|
||||
S_TYPES = [
|
||||
mysensors.CONST.Presentation.S_TEMP,
|
||||
mysensors.CONST.Presentation.S_HUM,
|
||||
mysensors.CONST.Presentation.S_BARO,
|
||||
mysensors.CONST.Presentation.S_WIND,
|
||||
mysensors.CONST.Presentation.S_RAIN,
|
||||
mysensors.CONST.Presentation.S_UV,
|
||||
mysensors.CONST.Presentation.S_WEIGHT,
|
||||
mysensors.CONST.Presentation.S_POWER,
|
||||
mysensors.CONST.Presentation.S_DISTANCE,
|
||||
mysensors.CONST.Presentation.S_LIGHT_LEVEL,
|
||||
mysensors.CONST.Presentation.S_IR,
|
||||
mysensors.CONST.Presentation.S_WATER,
|
||||
mysensors.CONST.Presentation.S_AIR_QUALITY,
|
||||
mysensors.CONST.Presentation.S_CUSTOM,
|
||||
mysensors.CONST.Presentation.S_DUST,
|
||||
mysensors.CONST.Presentation.S_SCENE_CONTROLLER,
|
||||
]
|
||||
not_v_types = [
|
||||
mysensors.CONST.SetReq.V_ARMED,
|
||||
mysensors.CONST.SetReq.V_LIGHT,
|
||||
mysensors.CONST.SetReq.V_LOCK_STATUS,
|
||||
]
|
||||
if float(mysensors.VERSION) >= 1.5:
|
||||
S_TYPES.extend([
|
||||
mysensors.CONST.Presentation.S_COLOR_SENSOR,
|
||||
mysensors.CONST.Presentation.S_MULTIMETER,
|
||||
])
|
||||
not_v_types.extend([mysensors.CONST.SetReq.V_STATUS, ])
|
||||
global V_TYPES
|
||||
V_TYPES = [member for member in mysensors.CONST.SetReq
|
||||
if member.value not in not_v_types]
|
||||
# Only act if loaded via mysensors by discovery event.
|
||||
# Otherwise gateway is not setup.
|
||||
if discovery_info is None:
|
||||
return
|
||||
|
||||
for gateway in mysensors.GATEWAYS.values():
|
||||
# Define the S_TYPES and V_TYPES that the platform should handle as
|
||||
# states.
|
||||
s_types = [
|
||||
gateway.const.Presentation.S_TEMP,
|
||||
gateway.const.Presentation.S_HUM,
|
||||
gateway.const.Presentation.S_BARO,
|
||||
gateway.const.Presentation.S_WIND,
|
||||
gateway.const.Presentation.S_RAIN,
|
||||
gateway.const.Presentation.S_UV,
|
||||
gateway.const.Presentation.S_WEIGHT,
|
||||
gateway.const.Presentation.S_POWER,
|
||||
gateway.const.Presentation.S_DISTANCE,
|
||||
gateway.const.Presentation.S_LIGHT_LEVEL,
|
||||
gateway.const.Presentation.S_IR,
|
||||
gateway.const.Presentation.S_WATER,
|
||||
gateway.const.Presentation.S_AIR_QUALITY,
|
||||
gateway.const.Presentation.S_CUSTOM,
|
||||
gateway.const.Presentation.S_DUST,
|
||||
gateway.const.Presentation.S_SCENE_CONTROLLER,
|
||||
]
|
||||
not_v_types = [
|
||||
gateway.const.SetReq.V_ARMED,
|
||||
gateway.const.SetReq.V_LIGHT,
|
||||
gateway.const.SetReq.V_LOCK_STATUS,
|
||||
]
|
||||
if float(gateway.version) >= 1.5:
|
||||
s_types.extend([
|
||||
gateway.const.Presentation.S_COLOR_SENSOR,
|
||||
gateway.const.Presentation.S_MULTIMETER,
|
||||
])
|
||||
not_v_types.extend([gateway.const.SetReq.V_STATUS, ])
|
||||
v_types = [member for member in gateway.const.SetReq
|
||||
if member.value not in not_v_types]
|
||||
|
||||
devices = defaultdict(list)
|
||||
gateway.platform_callbacks.append(mysensors.pf_callback_factory(
|
||||
s_types, v_types, devices, add_devices, MySensorsSensor))
|
||||
|
||||
|
||||
class MySensorsSensor(Entity):
|
||||
"""Represent the value of a MySensors child node."""
|
||||
|
||||
# pylint: disable=too-many-arguments, too-many-instance-attributes
|
||||
# pylint: disable=too-many-arguments
|
||||
|
||||
def __init__(self, port, node_id, child_id, name, value_type):
|
||||
def __init__(self, gateway, node_id, child_id, name, value_type):
|
||||
"""Setup class attributes on instantiation.
|
||||
|
||||
Args:
|
||||
port (str): Gateway port.
|
||||
gateway (str): Gateway.
|
||||
node_id (str): Id of node.
|
||||
child_id (str): Id of child.
|
||||
name (str): Entity name.
|
||||
value_type (str): Value type of child. Value is entity state.
|
||||
|
||||
Attributes:
|
||||
port (str): Gateway port.
|
||||
gateway (str): Gateway.
|
||||
node_id (str): Id of node.
|
||||
child_id (str): Id of child.
|
||||
_name (str): Entity name.
|
||||
|
@ -95,7 +93,7 @@ class MySensorsSensor(Entity):
|
|||
battery_level (int): Node battery level.
|
||||
_values (dict): Child values. Non state values set as state attributes.
|
||||
"""
|
||||
self.port = port
|
||||
self.gateway = gateway
|
||||
self.node_id = node_id
|
||||
self.child_id = child_id
|
||||
self._name = name
|
||||
|
@ -124,25 +122,25 @@ class MySensorsSensor(Entity):
|
|||
def unit_of_measurement(self):
|
||||
"""Unit of measurement of this entity."""
|
||||
# pylint:disable=too-many-return-statements
|
||||
if self.value_type == mysensors.CONST.SetReq.V_TEMP:
|
||||
return TEMP_CELCIUS if mysensors.IS_METRIC else TEMP_FAHRENHEIT
|
||||
elif self.value_type == mysensors.CONST.SetReq.V_HUM or \
|
||||
self.value_type == mysensors.CONST.SetReq.V_DIMMER or \
|
||||
self.value_type == mysensors.CONST.SetReq.V_PERCENTAGE or \
|
||||
self.value_type == mysensors.CONST.SetReq.V_LIGHT_LEVEL:
|
||||
if self.value_type == self.gateway.const.SetReq.V_TEMP:
|
||||
return TEMP_CELCIUS if self.gateway.metric else TEMP_FAHRENHEIT
|
||||
elif self.value_type == self.gateway.const.SetReq.V_HUM or \
|
||||
self.value_type == self.gateway.const.SetReq.V_DIMMER or \
|
||||
self.value_type == self.gateway.const.SetReq.V_PERCENTAGE or \
|
||||
self.value_type == self.gateway.const.SetReq.V_LIGHT_LEVEL:
|
||||
return '%'
|
||||
elif self.value_type == mysensors.CONST.SetReq.V_WATT:
|
||||
elif self.value_type == self.gateway.const.SetReq.V_WATT:
|
||||
return 'W'
|
||||
elif self.value_type == mysensors.CONST.SetReq.V_KWH:
|
||||
elif self.value_type == self.gateway.const.SetReq.V_KWH:
|
||||
return 'kWh'
|
||||
elif self.value_type == mysensors.CONST.SetReq.V_VOLTAGE:
|
||||
elif self.value_type == self.gateway.const.SetReq.V_VOLTAGE:
|
||||
return 'V'
|
||||
elif self.value_type == mysensors.CONST.SetReq.V_CURRENT:
|
||||
elif self.value_type == self.gateway.const.SetReq.V_CURRENT:
|
||||
return 'A'
|
||||
elif self.value_type == mysensors.CONST.SetReq.V_IMPEDANCE:
|
||||
elif self.value_type == self.gateway.const.SetReq.V_IMPEDANCE:
|
||||
return 'ohm'
|
||||
elif mysensors.CONST.SetReq.V_UNIT_PREFIX in self._values:
|
||||
return self._values[mysensors.CONST.SetReq.V_UNIT_PREFIX]
|
||||
elif self.gateway.const.SetReq.V_UNIT_PREFIX in self._values:
|
||||
return self._values[self.gateway.const.SetReq.V_UNIT_PREFIX]
|
||||
return None
|
||||
|
||||
@property
|
||||
|
@ -168,16 +166,17 @@ class MySensorsSensor(Entity):
|
|||
|
||||
return data
|
||||
|
||||
def update_sensor(self, values, battery_level):
|
||||
def update(self):
|
||||
"""Update the controller with the latest values from a sensor."""
|
||||
for value_type, value in values.items():
|
||||
node = self.gateway.sensors[self.node_id]
|
||||
child = node.children[self.child_id]
|
||||
for value_type, value in child.values.items():
|
||||
_LOGGER.info(
|
||||
"%s: value_type %s, value = %s", self._name, value_type, value)
|
||||
if value_type == mysensors.CONST.SetReq.V_TRIPPED:
|
||||
if value_type == self.gateway.const.SetReq.V_TRIPPED:
|
||||
self._values[value_type] = STATE_ON if int(
|
||||
value) == 1 else STATE_OFF
|
||||
else:
|
||||
self._values[value_type] = value
|
||||
|
||||
self.battery_level = battery_level
|
||||
self.update_ha_state()
|
||||
self.battery_level = node.battery_level
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue