Fix for battery device: new_device referenced before assignment. (#23793)

* Fix for battery device: new_device referenced before assignment.

* Fix buttons and switches mixup

* Update __init__.py

* Update binary_sensor.py

* Update __init__.py

* Update __init__.py

* Update binary_sensor.py

* Update __init__.py

* Update binary_sensor.py

* typo and indentation fixes

* low_bat and lowbat to uppercase.
This commit is contained in:
sander76 2019-05-13 20:52:55 +02:00 committed by Pascal Vizeli
parent 990a9e80a2
commit b2a1204bc5
2 changed files with 30 additions and 40 deletions

View File

@ -27,10 +27,9 @@ DISCOVER_BINARY_SENSORS = 'homematic.binary_sensor'
DISCOVER_COVER = 'homematic.cover' DISCOVER_COVER = 'homematic.cover'
DISCOVER_CLIMATE = 'homematic.climate' DISCOVER_CLIMATE = 'homematic.climate'
DISCOVER_LOCKS = 'homematic.locks' DISCOVER_LOCKS = 'homematic.locks'
DISCOVER_BUTTONS = 'homematic.binary_sensor' DISCOVER_BATTERY = 'homematic.battery'
ATTR_DISCOVER_DEVICES = 'devices' ATTR_DISCOVER_DEVICES = 'devices'
ATTR_BATTERY_DEVICES = 'battery_devices'
ATTR_PARAM = 'param' ATTR_PARAM = 'param'
ATTR_CHANNEL = 'channel' ATTR_CHANNEL = 'channel'
ATTR_ADDRESS = 'address' ATTR_ADDRESS = 'address'
@ -43,6 +42,9 @@ ATTR_TIME = 'time'
ATTR_UNIQUE_ID = 'unique_id' ATTR_UNIQUE_ID = 'unique_id'
ATTR_PARAMSET_KEY = 'paramset_key' ATTR_PARAMSET_KEY = 'paramset_key'
ATTR_PARAMSET = 'paramset' ATTR_PARAMSET = 'paramset'
ATTR_DISCOVERY_TYPE = 'discovery_type'
ATTR_LOW_BAT = 'LOW_BAT'
ATTR_LOWBAT = 'LOWBAT'
EVENT_KEYPRESS = 'homematic.keypress' EVENT_KEYPRESS = 'homematic.keypress'
@ -87,8 +89,7 @@ HM_DEVICE_TYPES = {
'SmartwareMotion', 'IPWeatherSensorPlus', 'MotionIPV2', 'WaterIP', 'SmartwareMotion', 'IPWeatherSensorPlus', 'MotionIPV2', 'WaterIP',
'IPMultiIO', 'TiltIP', 'IPShutterContactSabotage'], 'IPMultiIO', 'TiltIP', 'IPShutterContactSabotage'],
DISCOVER_COVER: ['Blind', 'KeyBlind', 'IPKeyBlind', 'IPKeyBlindTilt'], DISCOVER_COVER: ['Blind', 'KeyBlind', 'IPKeyBlind', 'IPKeyBlindTilt'],
DISCOVER_LOCKS: ['KeyMatic'], DISCOVER_LOCKS: ['KeyMatic']
DISCOVER_BUTTONS: ['HmIP-WRC6', 'HmIP-RC8']
} }
HM_IGNORE_DISCOVERY_NODE = [ HM_IGNORE_DISCOVERY_NODE = [
@ -465,7 +466,7 @@ def _system_callback_handler(hass, config, src, *args):
('sensor', DISCOVER_SENSORS), ('sensor', DISCOVER_SENSORS),
('climate', DISCOVER_CLIMATE), ('climate', DISCOVER_CLIMATE),
('lock', DISCOVER_LOCKS), ('lock', DISCOVER_LOCKS),
('binary_sensor', DISCOVER_SWITCHES)): ('binary_sensor', DISCOVER_BATTERY)):
# Get all devices of a specific type # Get all devices of a specific type
found_devices = _get_devices( found_devices = _get_devices(
hass, discovery_type, addresses, interface) hass, discovery_type, addresses, interface)
@ -473,21 +474,10 @@ def _system_callback_handler(hass, config, src, *args):
# When devices of this type are found # When devices of this type are found
# they are setup in HASS and a discovery event is fired # they are setup in HASS and a discovery event is fired
if found_devices: if found_devices:
discovery_info = {ATTR_DISCOVER_DEVICES: found_devices, discovery.load_platform(hass, component_name, DOMAIN, {
ATTR_BATTERY_DEVICES: False} ATTR_DISCOVER_DEVICES: found_devices,
ATTR_DISCOVERY_TYPE: discovery_type,
# Switches are skipped as a component. They will only }, config)
# 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)
# Homegear error message # Homegear error message
elif src == 'error': elif src == 'error':
@ -509,7 +499,8 @@ def _get_devices(hass, discovery_type, keys, interface):
metadata = {} metadata = {}
# Class not supported by discovery type # 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 continue
# Load metadata needed to generate a parameter list # Load metadata needed to generate a parameter list
@ -517,6 +508,15 @@ def _get_devices(hass, discovery_type, keys, interface):
metadata.update(device.SENSORNODE) metadata.update(device.SENSORNODE)
elif discovery_type == DISCOVER_BINARY_SENSORS: elif discovery_type == DISCOVER_BINARY_SENSORS:
metadata.update(device.BINARYNODE) 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: else:
metadata.update({None: device.ELEMENT}) metadata.update({None: device.ELEMENT})

View File

@ -2,16 +2,14 @@
import logging import logging
from homeassistant.components.binary_sensor import BinarySensorDevice from homeassistant.components.binary_sensor import BinarySensorDevice
from homeassistant.components.homematic import ATTR_BATTERY_DEVICES from homeassistant.components.homematic import (
from homeassistant.const import STATE_UNKNOWN, DEVICE_CLASS_BATTERY ATTR_DISCOVERY_TYPE, DISCOVER_BATTERY)
from homeassistant.const import DEVICE_CLASS_BATTERY
from . import ATTR_DISCOVER_DEVICES, HMDevice from . import ATTR_DISCOVER_DEVICES, HMDevice
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
ATTR_LOW_BAT = 'LOW_BAT'
ATTR_LOWBAT = 'LOWBAT'
SENSOR_TYPES_CLASS = { SENSOR_TYPES_CLASS = {
'IPShutterContact': 'opening', 'IPShutterContact': 'opening',
'MaxShutterContact': 'opening', 'MaxShutterContact': 'opening',
@ -34,16 +32,11 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
return return
devices = [] devices = []
battery_devices = discovery_info[ATTR_BATTERY_DEVICES]
for conf in discovery_info[ATTR_DISCOVER_DEVICES]: for conf in discovery_info[ATTR_DISCOVER_DEVICES]:
if battery_devices: if discovery_info[ATTR_DISCOVERY_TYPE] == DISCOVER_BATTERY:
battery_device = conf.get(ATTR_LOWBAT) or conf.get(ATTR_LOW_BAT) devices.append(HMBatterySensor(conf))
if battery_device:
new_device = HMBatterySensor(conf)
else: else:
new_device = HMBinarySensor(conf) devices.append(HMBinarySensor(conf))
devices.append(new_device)
add_entities(devices) add_entities(devices)
@ -70,7 +63,7 @@ class HMBinarySensor(HMDevice, BinarySensorDevice):
"""Generate the data dictionary (self._data) from metadata.""" """Generate the data dictionary (self._data) from metadata."""
# Add state to data struct # Add state to data struct
if self._state: if self._state:
self._data.update({self._state: STATE_UNKNOWN}) self._data.update({self._state: None})
class HMBatterySensor(HMDevice, BinarySensorDevice): class HMBatterySensor(HMDevice, BinarySensorDevice):
@ -84,13 +77,10 @@ class HMBatterySensor(HMDevice, BinarySensorDevice):
@property @property
def is_on(self): def is_on(self):
"""Return True if battery is low.""" """Return True if battery is low."""
is_on = self._data.get(ATTR_LOW_BAT, False) or self._data.get( return bool(self._hm_get_state())
ATTR_LOWBAT, False
)
return is_on
def _init_data_struct(self): def _init_data_struct(self):
"""Generate the data dictionary (self._data) from metadata.""" """Generate the data dictionary (self._data) from metadata."""
# Add state to data struct # Add state to data struct
if self._state: if self._state:
self._data.update({self._state: STATE_UNKNOWN}) self._data.update({self._state: None})