diff --git a/homeassistant/components/homematic/__init__.py b/homeassistant/components/homematic/__init__.py index 7f6f9a6d522..b301e22597e 100644 --- a/homeassistant/components/homematic/__init__.py +++ b/homeassistant/components/homematic/__init__.py @@ -27,10 +27,9 @@ DISCOVER_BINARY_SENSORS = 'homematic.binary_sensor' DISCOVER_COVER = 'homematic.cover' DISCOVER_CLIMATE = 'homematic.climate' DISCOVER_LOCKS = 'homematic.locks' -DISCOVER_BUTTONS = 'homematic.binary_sensor' +DISCOVER_BATTERY = 'homematic.battery' ATTR_DISCOVER_DEVICES = 'devices' -ATTR_BATTERY_DEVICES = 'battery_devices' ATTR_PARAM = 'param' ATTR_CHANNEL = 'channel' ATTR_ADDRESS = 'address' @@ -43,6 +42,9 @@ ATTR_TIME = 'time' ATTR_UNIQUE_ID = 'unique_id' ATTR_PARAMSET_KEY = 'paramset_key' ATTR_PARAMSET = 'paramset' +ATTR_DISCOVERY_TYPE = 'discovery_type' +ATTR_LOW_BAT = 'LOW_BAT' +ATTR_LOWBAT = 'LOWBAT' EVENT_KEYPRESS = 'homematic.keypress' @@ -87,8 +89,7 @@ HM_DEVICE_TYPES = { 'SmartwareMotion', 'IPWeatherSensorPlus', 'MotionIPV2', 'WaterIP', 'IPMultiIO', 'TiltIP', 'IPShutterContactSabotage'], DISCOVER_COVER: ['Blind', 'KeyBlind', 'IPKeyBlind', 'IPKeyBlindTilt'], - DISCOVER_LOCKS: ['KeyMatic'], - DISCOVER_BUTTONS: ['HmIP-WRC6', 'HmIP-RC8'] + DISCOVER_LOCKS: ['KeyMatic'] } HM_IGNORE_DISCOVERY_NODE = [ @@ -465,7 +466,7 @@ def _system_callback_handler(hass, config, src, *args): ('sensor', DISCOVER_SENSORS), ('climate', DISCOVER_CLIMATE), ('lock', DISCOVER_LOCKS), - ('binary_sensor', DISCOVER_SWITCHES)): + ('binary_sensor', DISCOVER_BATTERY)): # Get all devices of a specific type found_devices = _get_devices( hass, discovery_type, addresses, interface) @@ -473,21 +474,10 @@ def _system_callback_handler(hass, config, src, *args): # When devices of this type are found # they are setup in HASS and a discovery event is fired if found_devices: - discovery_info = {ATTR_DISCOVER_DEVICES: found_devices, - ATTR_BATTERY_DEVICES: False} - - # Switches are skipped as a component. They will only - # appear in hass as a battery device. - if not discovery_type == DISCOVER_SWITCHES: - discovery.load_platform(hass, component_name, DOMAIN, - discovery_info, config) - - # Pass all devices to binary sensor discovery, - # check whether they are battery operated and - # add them as a battery operated binary sensor device. - discovery_info[ATTR_BATTERY_DEVICES] = True - discovery.load_platform(hass, 'binary_sensor', DOMAIN, - discovery_info, config) + discovery.load_platform(hass, component_name, DOMAIN, { + ATTR_DISCOVER_DEVICES: found_devices, + ATTR_DISCOVERY_TYPE: discovery_type, + }, config) # Homegear error message elif src == 'error': @@ -509,7 +499,8 @@ def _get_devices(hass, discovery_type, keys, interface): metadata = {} # Class not supported by discovery type - if class_name not in HM_DEVICE_TYPES[discovery_type]: + if discovery_type != DISCOVER_BATTERY and \ + class_name not in HM_DEVICE_TYPES[discovery_type]: continue # Load metadata needed to generate a parameter list @@ -517,6 +508,15 @@ def _get_devices(hass, discovery_type, keys, interface): metadata.update(device.SENSORNODE) elif discovery_type == DISCOVER_BINARY_SENSORS: metadata.update(device.BINARYNODE) + elif discovery_type == DISCOVER_BATTERY: + if ATTR_LOWBAT in device.ATTRIBUTENODE: + metadata.update( + {ATTR_LOWBAT: device.ATTRIBUTENODE[ATTR_LOWBAT]}) + elif ATTR_LOW_BAT in device.ATTRIBUTENODE: + metadata.update( + {ATTR_LOW_BAT: device.ATTRIBUTENODE[ATTR_LOW_BAT]}) + else: + continue else: metadata.update({None: device.ELEMENT}) diff --git a/homeassistant/components/homematic/binary_sensor.py b/homeassistant/components/homematic/binary_sensor.py index 91960cd8570..9d47f74df92 100644 --- a/homeassistant/components/homematic/binary_sensor.py +++ b/homeassistant/components/homematic/binary_sensor.py @@ -2,16 +2,14 @@ import logging from homeassistant.components.binary_sensor import BinarySensorDevice -from homeassistant.components.homematic import ATTR_BATTERY_DEVICES -from homeassistant.const import STATE_UNKNOWN, DEVICE_CLASS_BATTERY +from homeassistant.components.homematic import ( + ATTR_DISCOVERY_TYPE, DISCOVER_BATTERY) +from homeassistant.const import DEVICE_CLASS_BATTERY from . import ATTR_DISCOVER_DEVICES, HMDevice _LOGGER = logging.getLogger(__name__) -ATTR_LOW_BAT = 'LOW_BAT' -ATTR_LOWBAT = 'LOWBAT' - SENSOR_TYPES_CLASS = { 'IPShutterContact': 'opening', 'MaxShutterContact': 'opening', @@ -34,16 +32,11 @@ def setup_platform(hass, config, add_entities, discovery_info=None): return devices = [] - battery_devices = discovery_info[ATTR_BATTERY_DEVICES] - for conf in discovery_info[ATTR_DISCOVER_DEVICES]: - if battery_devices: - battery_device = conf.get(ATTR_LOWBAT) or conf.get(ATTR_LOW_BAT) - if battery_device: - new_device = HMBatterySensor(conf) + if discovery_info[ATTR_DISCOVERY_TYPE] == DISCOVER_BATTERY: + devices.append(HMBatterySensor(conf)) else: - new_device = HMBinarySensor(conf) - devices.append(new_device) + devices.append(HMBinarySensor(conf)) add_entities(devices) @@ -70,7 +63,7 @@ class HMBinarySensor(HMDevice, BinarySensorDevice): """Generate the data dictionary (self._data) from metadata.""" # Add state to data struct if self._state: - self._data.update({self._state: STATE_UNKNOWN}) + self._data.update({self._state: None}) class HMBatterySensor(HMDevice, BinarySensorDevice): @@ -84,13 +77,10 @@ class HMBatterySensor(HMDevice, BinarySensorDevice): @property def is_on(self): """Return True if battery is low.""" - is_on = self._data.get(ATTR_LOW_BAT, False) or self._data.get( - ATTR_LOWBAT, False - ) - return is_on + return bool(self._hm_get_state()) def _init_data_struct(self): """Generate the data dictionary (self._data) from metadata.""" # Add state to data struct if self._state: - self._data.update({self._state: STATE_UNKNOWN}) + self._data.update({self._state: None})