BMW code cleanup (#14424)

* Some cleanup for BMW sensors

* Changed dict sort

* Updates based on review and Travis
This commit is contained in:
Gerard authored and Sebastian Muszynski committed 2018-05-15 20:47:32 +02:00
1 parent e49e0b5a13
commit 2e7b5dcd19
2 files changed
+31 -33

No files matched your search

@@ -115,14 +115,7 @@ class BMWConnectedDriveSensor(BinarySensorDevice):
result['lights_parking'] = vehicle_state.parking_lights.value
elif self._attribute == 'condition_based_services':
for report in vehicle_state.condition_based_services:
service_type = report.service_type.lower().replace('_', ' ')
result['{} status'.format(service_type)] = report.state.value
if report.due_date is not None:
result['{} date'.format(service_type)] = \
report.due_date.strftime('%Y-%m-%d')
if report.due_distance is not None:
result['{} distance'.format(service_type)] = \
'{} km'.format(report.due_distance)
result.update(self._format_cbs_report(report))
elif self._attribute == 'check_control_messages':
check_control_messages = vehicle_state.check_control_messages
if not check_control_messages:
@@ -139,7 +132,7 @@ class BMWConnectedDriveSensor(BinarySensorDevice):
result['connection_status'] = \
vehicle_state._attributes['connectionStatus']
return result
return sorted(result.items())
def update(self):
"""Read new state data from the library."""
@@ -177,6 +170,19 @@ class BMWConnectedDriveSensor(BinarySensorDevice):
self._state = (vehicle_state._attributes['connectionStatus'] ==
'CONNECTED')
@staticmethod
def _format_cbs_report(report):
result = {}
service_type = report.service_type.lower().replace('_', ' ')
result['{} status'.format(service_type)] = report.state.value
if report.due_date is not None:
result['{} date'.format(service_type)] = \
report.due_date.strftime('%Y-%m-%d')
if report.due_distance is not None:
result['{} distance'.format(service_type)] = \
'{} km'.format(report.due_distance)
return result
def update_callback(self):
"""Schedule a state update."""
self.schedule_update_ha_state(True)