Skip to content

Commit

Permalink
Fix mysensors version errors
Browse files Browse the repository at this point in the history
* The application of the version check in unit_of_measurement was
	messed up after the last refactor. Fix that again.
* An error could occur in device_state_attributes if there was a
	mismatch between used value_type in the device and mysensors
	version in config. Add try... except to handle that.
  • Loading branch information
MartinHjelmare committed Feb 6, 2016
1 parent 614034d commit b700ec4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
13 changes: 8 additions & 5 deletions homeassistant/components/sensor/mysensors.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,23 +151,26 @@ def unit_of_measurement(self):
self.gateway.const.SetReq.V_VOLTAGE: 'V',
self.gateway.const.SetReq.V_CURRENT: 'A',
}
unit_map_v15 = {
self.gateway.const.SetReq.V_PERCENTAGE: '%',
}
if float(self.gateway.version) >= 1.5:
if self.gateway.const.SetReq.V_UNIT_PREFIX in self._values:
return self._values[
self.gateway.const.SetReq.V_UNIT_PREFIX]
unit_map.update(unit_map_v15)
unit_map.update({self.gateway.const.SetReq.V_PERCENTAGE: '%'})
return unit_map.get(self.value_type)

@property
def device_state_attributes(self):
"""Return device specific state attributes."""
set_req = self.gateway.const.SetReq
device_attr = {}
for value_type, value in self._values.items():
if value_type != self.value_type:
device_attr[self.gateway.const.SetReq(value_type).name] = value
try:
device_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.version)
return device_attr

@property
Expand Down
8 changes: 7 additions & 1 deletion homeassistant/components/switch/mysensors.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,16 @@ def name(self):
@property
def device_state_attributes(self):
"""Return device specific state attributes."""
set_req = self.gateway.const.SetReq
device_attr = {}
for value_type, value in self._values.items():
if value_type != self.value_type:
device_attr[self.gateway.const.SetReq(value_type).name] = value
try:
device_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.version)
return device_attr

@property
Expand Down

0 comments on commit b700ec4

Please sign in to comment.