mirror of
https://github.com/home-assistant/core.git
synced 2025-07-13 16:27:08 +00:00
Fix avoid event bus for updates
This commit is contained in:
parent
77959341a3
commit
be25ea4f09
@ -26,23 +26,6 @@ mysensors:
|
||||
debug: true
|
||||
persistence: true
|
||||
version: '1.5'
|
||||
|
||||
mysensors:
|
||||
port:
|
||||
- '/dev/ttyUSB0'
|
||||
- '/dev/ttyACM1'
|
||||
debug: true
|
||||
persistence: true
|
||||
persistence_file:
|
||||
- 'path/to/.homeassistant/mysensors.json'
|
||||
- 'path/to/.homeassistant/mysensors2.json'
|
||||
version: '1.5'
|
||||
|
||||
sensor:
|
||||
platform: mysensors
|
||||
|
||||
switch:
|
||||
platform: mysensors
|
||||
"""
|
||||
import logging
|
||||
|
||||
@ -70,11 +53,8 @@ REQUIREMENTS = [
|
||||
'https://github.com/theolind/pymysensors/archive/'
|
||||
'2aa8f32908e8c5bb3e5c77c5851db778f8635792.zip#pymysensors==0.3']
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
ATTR_PORT = 'port'
|
||||
ATTR_DEVICES = 'devices'
|
||||
ATTR_NODE_ID = 'node_id'
|
||||
ATTR_CHILD_ID = 'child_id'
|
||||
ATTR_UPDATE_TYPE = 'update_type'
|
||||
|
||||
COMPONENTS_WITH_MYSENSORS_PLATFORM = [
|
||||
'sensor',
|
||||
@ -84,11 +64,11 @@ COMPONENTS_WITH_MYSENSORS_PLATFORM = [
|
||||
IS_METRIC = None
|
||||
CONST = None
|
||||
GATEWAYS = None
|
||||
EVENT_MYSENSORS_NODE_UPDATE = 'MYSENSORS_NODE_UPDATE'
|
||||
|
||||
|
||||
def setup(hass, config):
|
||||
"""Setup the MySensors component."""
|
||||
# pylint: disable=too-many-locals
|
||||
import mysensors.mysensors as mysensors
|
||||
|
||||
if not validate_config(config,
|
||||
@ -119,18 +99,17 @@ def setup(hass, config):
|
||||
if not bootstrap.setup_component(hass, component, mysensors_config):
|
||||
return False
|
||||
|
||||
def callback_factory(port, devices):
|
||||
import homeassistant.components.sensor.mysensors as mysensors_sensor
|
||||
import homeassistant.components.switch.mysensors as mysensors_switch
|
||||
|
||||
def callback_factory(gateway, port, devices):
|
||||
"""Return a new callback function. Run once per gateway setup."""
|
||||
def node_update(update_type, nid):
|
||||
"""Callback for node updates from the MySensors gateway."""
|
||||
_LOGGER.info('update %s: node %s', update_type, nid)
|
||||
|
||||
hass.bus.fire(EVENT_MYSENSORS_NODE_UPDATE, {
|
||||
ATTR_PORT: port,
|
||||
ATTR_DEVICES: devices,
|
||||
ATTR_UPDATE_TYPE: update_type,
|
||||
ATTR_NODE_ID: nid
|
||||
})
|
||||
mysensors_sensor.sensor_update(gateway, port, devices, nid)
|
||||
mysensors_switch.sensor_update(gateway, port, devices, nid)
|
||||
|
||||
return node_update
|
||||
|
||||
@ -141,7 +120,7 @@ def setup(hass, config):
|
||||
persistence=persistence,
|
||||
persistence_file=persistence_file,
|
||||
protocol_version=VERSION)
|
||||
gateway.event_callback = callback_factory(port, devices)
|
||||
gateway.event_callback = callback_factory(gateway, port, devices)
|
||||
gateway.metric = IS_METRIC
|
||||
gateway.debug = config[DOMAIN].get(CONF_DEBUG, False)
|
||||
gateway.start()
|
||||
@ -212,18 +191,3 @@ def mysensors_update(platform_type):
|
||||
node[child_id][value_type].update_sensor(
|
||||
child.values, gateway.sensors[nid].battery_level)
|
||||
return wrapper
|
||||
|
||||
|
||||
def event_update(update):
|
||||
"""Decorator for callback function for mysensor event updates."""
|
||||
def wrapper(event):
|
||||
"""Wrapper function in the decorator."""
|
||||
_LOGGER.info(
|
||||
'update %s: node %s', event.data[ATTR_UPDATE_TYPE],
|
||||
event.data[ATTR_NODE_ID])
|
||||
sensor_update = update(event)
|
||||
sensor_update(GATEWAYS[event.data[ATTR_PORT]],
|
||||
event.data[ATTR_PORT],
|
||||
event.data[ATTR_DEVICES],
|
||||
event.data[ATTR_NODE_ID])
|
||||
return wrapper
|
||||
|
@ -20,11 +20,24 @@ 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.
|
||||
s_types = [
|
||||
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,
|
||||
@ -48,26 +61,15 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||
mysensors.CONST.SetReq.V_LOCK_STATUS,
|
||||
]
|
||||
if float(mysensors.VERSION) >= 1.5:
|
||||
s_types.extend([
|
||||
S_TYPES.extend([
|
||||
mysensors.CONST.Presentation.S_COLOR_SENSOR,
|
||||
mysensors.CONST.Presentation.S_MULTIMETER,
|
||||
])
|
||||
not_v_types.extend([mysensors.CONST.SetReq.V_STATUS, ])
|
||||
v_types = [member for member in mysensors.CONST.SetReq
|
||||
global V_TYPES
|
||||
V_TYPES = [member for member in mysensors.CONST.SetReq
|
||||
if member.value not in not_v_types]
|
||||
|
||||
@mysensors.mysensors_update
|
||||
def _sensor_update(gateway, port, devices, nid):
|
||||
"""Internal callback for sensor updates."""
|
||||
return (s_types, v_types, MySensorsSensor, add_devices)
|
||||
|
||||
@mysensors.event_update
|
||||
def event_update(event):
|
||||
"""Callback for event updates from the MySensors component."""
|
||||
return _sensor_update
|
||||
|
||||
hass.bus.listen(mysensors.EVENT_MYSENSORS_NODE_UPDATE, event_update)
|
||||
|
||||
|
||||
class MySensorsSensor(Entity):
|
||||
"""Represent the value of a MySensors child node."""
|
||||
@ -101,18 +103,6 @@ class MySensorsSensor(Entity):
|
||||
self.battery_level = 0
|
||||
self._values = {}
|
||||
|
||||
def as_dict(self):
|
||||
"""Return a dict representation of this entity."""
|
||||
return {
|
||||
'port': self.port,
|
||||
'name': self._name,
|
||||
'node_id': self.node_id,
|
||||
'child_id': self.child_id,
|
||||
'battery_level': self.battery_level,
|
||||
'value_type': self.value_type,
|
||||
'values': self._values,
|
||||
}
|
||||
|
||||
@property
|
||||
def should_poll(self):
|
||||
"""MySensor gateway pushes its state to HA."""
|
||||
|
@ -19,11 +19,24 @@ 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, MySensorsSwitch, ADD_DEVICES)
|
||||
|
||||
|
||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||
"""Setup the mysensors platform for switches."""
|
||||
# Define the S_TYPES and V_TYPES that the platform should handle as states.
|
||||
s_types = [
|
||||
global ADD_DEVICES
|
||||
ADD_DEVICES = add_devices
|
||||
global S_TYPES
|
||||
S_TYPES = [
|
||||
mysensors.CONST.Presentation.S_DOOR,
|
||||
mysensors.CONST.Presentation.S_MOTION,
|
||||
mysensors.CONST.Presentation.S_SMOKE,
|
||||
@ -31,32 +44,21 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||
mysensors.CONST.Presentation.S_BINARY,
|
||||
mysensors.CONST.Presentation.S_LOCK,
|
||||
]
|
||||
v_types = [
|
||||
global V_TYPES
|
||||
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([
|
||||
S_TYPES.extend([
|
||||
mysensors.CONST.Presentation.S_SPRINKLER,
|
||||
mysensors.CONST.Presentation.S_WATER_LEAK,
|
||||
mysensors.CONST.Presentation.S_SOUND,
|
||||
mysensors.CONST.Presentation.S_VIBRATION,
|
||||
mysensors.CONST.Presentation.S_MOISTURE,
|
||||
])
|
||||
v_types.extend([mysensors.CONST.SetReq.V_STATUS, ])
|
||||
|
||||
@mysensors.mysensors_update
|
||||
def _sensor_update(gateway, port, devices, nid):
|
||||
"""Internal callback for sensor updates."""
|
||||
return (s_types, v_types, MySensorsSwitch, add_devices)
|
||||
|
||||
@mysensors.event_update
|
||||
def event_update(event):
|
||||
"""Callback for event updates from the MySensors component."""
|
||||
return _sensor_update
|
||||
|
||||
hass.bus.listen(mysensors.EVENT_MYSENSORS_NODE_UPDATE, event_update)
|
||||
V_TYPES.extend([mysensors.CONST.SetReq.V_STATUS, ])
|
||||
|
||||
|
||||
class MySensorsSwitch(SwitchDevice):
|
||||
@ -91,18 +93,6 @@ class MySensorsSwitch(SwitchDevice):
|
||||
self.battery_level = 0
|
||||
self._values = {}
|
||||
|
||||
def as_dict(self):
|
||||
"""Return a dict representation of this entity."""
|
||||
return {
|
||||
'port': self.port,
|
||||
'name': self._name,
|
||||
'node_id': self.node_id,
|
||||
'child_id': self.child_id,
|
||||
'battery_level': self.battery_level,
|
||||
'value_type': self.value_type,
|
||||
'values': self._values,
|
||||
}
|
||||
|
||||
@property
|
||||
def should_poll(self):
|
||||
"""MySensor gateway pushes its state to HA."""
|
||||
|
Loading…
x
Reference in New Issue
Block a user