From b60934b10db745762294c9f455482fc4f88657f5 Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Tue, 26 Oct 2021 20:27:26 +0200 Subject: [PATCH] Enable type checking - bmw_connected_drive (#58310) --- .../bmw_connected_drive/__init__.py | 5 ++-- .../components/bmw_connected_drive/sensor.py | 28 ++++++++++++++++--- mypy.ini | 3 -- script/hassfest/mypy_config.py | 1 - 4 files changed, 27 insertions(+), 10 deletions(-) diff --git a/homeassistant/components/bmw_connected_drive/__init__.py b/homeassistant/components/bmw_connected_drive/__init__.py index 722809dff5c..0dad3e1fce1 100644 --- a/homeassistant/components/bmw_connected_drive/__init__.py +++ b/homeassistant/components/bmw_connected_drive/__init__.py @@ -1,6 +1,7 @@ """Reads vehicle status from BMW connected drive portal.""" from __future__ import annotations +from collections.abc import Callable import logging from bimmer_connected.account import ConnectedDriveAccount @@ -282,7 +283,7 @@ class BMWConnectedDriveAccount: self.read_only = read_only self.account = ConnectedDriveAccount(username, password, region) self.name = name - self._update_listeners = [] + self._update_listeners: list[Callable[[], None]] = [] # Set observer position once for older cars to be in range for # GPS position (pre-7/2014, <2km) and get new data from API @@ -311,7 +312,7 @@ class BMWConnectedDriveAccount: ) _LOGGER.exception(exception) - def add_update_listener(self, listener): + def add_update_listener(self, listener: Callable[[], None]) -> None: """Add a listener for update notifications.""" self._update_listeners.append(listener) diff --git a/homeassistant/components/bmw_connected_drive/sensor.py b/homeassistant/components/bmw_connected_drive/sensor.py index daf7569bc77..d2b434f96cd 100644 --- a/homeassistant/components/bmw_connected_drive/sensor.py +++ b/homeassistant/components/bmw_connected_drive/sensor.py @@ -388,12 +388,18 @@ async def async_setup_entry( if service == SERVICE_LAST_TRIP: entities.extend( [ + # mypy issues will be fixed in next release + # https://github.com/python/mypy/issues/9096 BMWConnectedDriveSensor( - account, vehicle, description, unit_system, service + account, + vehicle, + description, # type: ignore[arg-type] + unit_system, + service, ) for attribute_name in vehicle.state.last_trip.available_attributes if attribute_name != "date" - and (description := SENSOR_TYPES.get(attribute_name)) + and (description := SENSOR_TYPES.get(attribute_name)) # type: ignore[no-redef] ] ) if "date" in vehicle.state.last_trip.available_attributes: @@ -534,7 +540,14 @@ class BMWConnectedDriveSensor(BMWConnectedDriveBaseEntity, SensorEntity): vehicle_last_trip = self._vehicle.state.last_trip if sensor_key == "date_utc": date_str = getattr(vehicle_last_trip, "date") - self._attr_native_value = dt_util.parse_datetime(date_str).isoformat() + if parsed_date := dt_util.parse_datetime(date_str): + self._attr_native_value = parsed_date.isoformat() + else: + _LOGGER.debug( + "Could not parse date string for 'date_utc' sensor: %s", + date_str, + ) + self._attr_native_value = None else: self._attr_native_value = getattr(vehicle_last_trip, sensor_key) elif self._service == SERVICE_ALL_TRIPS: @@ -553,7 +566,14 @@ class BMWConnectedDriveSensor(BMWConnectedDriveBaseEntity, SensorEntity): return if sensor_key == "reset_date_utc": date_str = getattr(vehicle_all_trips, "reset_date") - self._attr_native_value = dt_util.parse_datetime(date_str).isoformat() + if parsed_date := dt_util.parse_datetime(date_str): + self._attr_native_value = parsed_date.isoformat() + else: + _LOGGER.debug( + "Could not parse date string for 'reset_date_utc' sensor: %s", + date_str, + ) + self._attr_native_value = None else: self._attr_native_value = getattr(vehicle_all_trips, sensor_key) diff --git a/mypy.ini b/mypy.ini index 058d587ec22..315ee094b40 100644 --- a/mypy.ini +++ b/mypy.ini @@ -1546,9 +1546,6 @@ ignore_errors = true [mypy-homeassistant.components.blueprint.*] ignore_errors = true -[mypy-homeassistant.components.bmw_connected_drive.*] -ignore_errors = true - [mypy-homeassistant.components.climacell.*] ignore_errors = true diff --git a/script/hassfest/mypy_config.py b/script/hassfest/mypy_config.py index 6ffd2e0da42..7d289984335 100644 --- a/script/hassfest/mypy_config.py +++ b/script/hassfest/mypy_config.py @@ -16,7 +16,6 @@ from .model import Config, Integration IGNORED_MODULES: Final[list[str]] = [ "homeassistant.components.awair.*", "homeassistant.components.blueprint.*", - "homeassistant.components.bmw_connected_drive.*", "homeassistant.components.climacell.*", "homeassistant.components.cloud.*", "homeassistant.components.config.*", -- GitLab