From 0293db343fd364ed64d7b6c31d1683f7bdcc8087 Mon Sep 17 00:00:00 2001 From: rikroe <42204099+rikroe@users.noreply.github.com> Date: Fri, 12 Aug 2022 15:45:28 +0200 Subject: [PATCH] Allow only known attrs for BMW binary sensors (#76663) Co-authored-by: Paulus Schoutsen Co-authored-by: rikroe --- .../bmw_connected_drive/binary_sensor.py | 41 ++++++++++++++++--- 1 file changed, 36 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/bmw_connected_drive/binary_sensor.py b/homeassistant/components/bmw_connected_drive/binary_sensor.py index 94d8902fe23..96e8a1fc0e4 100644 --- a/homeassistant/components/bmw_connected_drive/binary_sensor.py +++ b/homeassistant/components/bmw_connected_drive/binary_sensor.py @@ -28,11 +28,32 @@ from .coordinator import BMWDataUpdateCoordinator _LOGGER = logging.getLogger(__name__) +ALLOWED_CONDITION_BASED_SERVICE_KEYS = { + "BRAKE_FLUID", + "ENGINE_OIL", + "OIL", + "TIRE_WEAR_FRONT", + "TIRE_WEAR_REAR", + "VEHICLE_CHECK", + "VEHICLE_TUV", +} + +ALLOWED_CHECK_CONTROL_MESSAGE_KEYS = {"ENGINE_OIL", "TIRE_PRESSURE"} + + def _condition_based_services( vehicle: MyBMWVehicle, unit_system: UnitSystem ) -> dict[str, Any]: extra_attributes = {} for report in vehicle.condition_based_services.messages: + if report.service_type not in ALLOWED_CONDITION_BASED_SERVICE_KEYS: + _LOGGER.warning( + "'%s' not an allowed condition based service (%s)", + report.service_type, + report, + ) + continue + extra_attributes.update(_format_cbs_report(report, unit_system)) return extra_attributes @@ -40,7 +61,17 @@ def _condition_based_services( def _check_control_messages(vehicle: MyBMWVehicle) -> dict[str, Any]: extra_attributes: dict[str, Any] = {} for message in vehicle.check_control_messages.messages: - extra_attributes.update({message.description_short: message.state.value}) + if message.description_short not in ALLOWED_CHECK_CONTROL_MESSAGE_KEYS: + _LOGGER.warning( + "'%s' not an allowed check control message (%s)", + message.description_short, + message, + ) + continue + + extra_attributes.update( + {message.description_short.lower(): message.state.value} + ) return extra_attributes @@ -48,10 +79,10 @@ def _format_cbs_report( report: ConditionBasedService, unit_system: UnitSystem ) -> dict[str, Any]: result: dict[str, Any] = {} - service_type = report.service_type.lower().replace("_", " ") - result[f"{service_type} status"] = report.state.value + service_type = report.service_type.lower() + result[service_type] = report.state.value if report.due_date is not None: - result[f"{service_type} date"] = report.due_date.strftime("%Y-%m-%d") + result[f"{service_type}_date"] = report.due_date.strftime("%Y-%m-%d") if report.due_distance.value and report.due_distance.unit: distance = round( unit_system.length( @@ -59,7 +90,7 @@ def _format_cbs_report( UNIT_MAP.get(report.due_distance.unit, report.due_distance.unit), ) ) - result[f"{service_type} distance"] = f"{distance} {unit_system.length_unit}" + result[f"{service_type}_distance"] = f"{distance} {unit_system.length_unit}" return result