From e6bba49bcd9e7b05a56958a8507d27fa15467ce2 Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Wed, 9 Oct 2024 19:29:42 +0200 Subject: [PATCH] Add strict typing for govee_ble (#128044) --- .strict-typing | 1 + homeassistant/components/govee_ble/binary_sensor.py | 6 +++--- homeassistant/components/govee_ble/coordinator.py | 2 ++ homeassistant/components/govee_ble/sensor.py | 13 +++++++++---- mypy.ini | 10 ++++++++++ 5 files changed, 25 insertions(+), 7 deletions(-) diff --git a/.strict-typing b/.strict-typing index 214cdac7a66..c0b65c0f3da 100644 --- a/.strict-typing +++ b/.strict-typing @@ -214,6 +214,7 @@ homeassistant.components.google_assistant_sdk.* homeassistant.components.google_cloud.* homeassistant.components.google_photos.* homeassistant.components.google_sheets.* +homeassistant.components.govee_ble.* homeassistant.components.gpsd.* homeassistant.components.greeneye_monitor.* homeassistant.components.group.* diff --git a/homeassistant/components/govee_ble/binary_sensor.py b/homeassistant/components/govee_ble/binary_sensor.py index e5966124216..bd92093c29c 100644 --- a/homeassistant/components/govee_ble/binary_sensor.py +++ b/homeassistant/components/govee_ble/binary_sensor.py @@ -44,7 +44,7 @@ BINARY_SENSOR_DESCRIPTIONS = { def sensor_update_to_bluetooth_data_update( sensor_update: SensorUpdate, -) -> PassiveBluetoothDataUpdate: +) -> PassiveBluetoothDataUpdate[bool | None]: """Convert a sensor update to a bluetooth data update.""" return PassiveBluetoothDataUpdate( devices={ @@ -95,13 +95,13 @@ class GoveeBluetoothBinarySensorEntity( ): """Representation of a govee-ble binary sensor.""" - processor: GoveeBLEPassiveBluetoothDataProcessor + processor: GoveeBLEPassiveBluetoothDataProcessor[bool | None] @property def available(self) -> bool: """Return False if sensor is in error.""" coordinator = self.processor.coordinator - return self.processor.entity_data.get(self.entity_key) != ERROR and ( + return self.processor.entity_data.get(self.entity_key) != ERROR and ( # type: ignore[comparison-overlap] ((model_info := coordinator.model_info) and model_info.sleepy) or super().available ) diff --git a/homeassistant/components/govee_ble/coordinator.py b/homeassistant/components/govee_ble/coordinator.py index 011a89e565b..4408b7f3199 100644 --- a/homeassistant/components/govee_ble/coordinator.py +++ b/homeassistant/components/govee_ble/coordinator.py @@ -1,5 +1,7 @@ """The govee Bluetooth integration.""" +from __future__ import annotations + from collections.abc import Callable from logging import Logger diff --git a/homeassistant/components/govee_ble/sensor.py b/homeassistant/components/govee_ble/sensor.py index a94610ef0e1..383f50e5c46 100644 --- a/homeassistant/components/govee_ble/sensor.py +++ b/homeassistant/components/govee_ble/sensor.py @@ -2,6 +2,9 @@ from __future__ import annotations +from datetime import date, datetime +from decimal import Decimal + from govee_ble import DeviceClass, SensorUpdate, Units from govee_ble.parser import ERROR @@ -29,6 +32,8 @@ from homeassistant.helpers.sensor import sensor_device_info_to_hass_device_info from .coordinator import GoveeBLEConfigEntry, GoveeBLEPassiveBluetoothDataProcessor from .device import device_key_to_bluetooth_entity_key +type _SensorValueType = str | int | float | date | datetime | Decimal | None + SENSOR_DESCRIPTIONS = { (DeviceClass.TEMPERATURE, Units.TEMP_CELSIUS): SensorEntityDescription( key=f"{DeviceClass.TEMPERATURE}_{Units.TEMP_CELSIUS}", @@ -72,7 +77,7 @@ SENSOR_DESCRIPTIONS = { def sensor_update_to_bluetooth_data_update( sensor_update: SensorUpdate, -) -> PassiveBluetoothDataUpdate: +) -> PassiveBluetoothDataUpdate[_SensorValueType]: """Convert a sensor update to a bluetooth data update.""" return PassiveBluetoothDataUpdate( devices={ @@ -117,13 +122,13 @@ async def async_setup_entry( class GoveeBluetoothSensorEntity( PassiveBluetoothProcessorEntity[ - PassiveBluetoothDataProcessor[float | int | str | None, SensorUpdate] + PassiveBluetoothDataProcessor[_SensorValueType, SensorUpdate] ], SensorEntity, ): """Representation of a govee ble sensor.""" - processor: GoveeBLEPassiveBluetoothDataProcessor + processor: GoveeBLEPassiveBluetoothDataProcessor[_SensorValueType] @property def available(self) -> bool: @@ -135,6 +140,6 @@ class GoveeBluetoothSensorEntity( ) @property - def native_value(self) -> float | int | str | None: + def native_value(self) -> _SensorValueType: # pylint: disable=hass-return-type """Return the native value.""" return self.processor.entity_data.get(self.entity_key) diff --git a/mypy.ini b/mypy.ini index f04bcf4b9a8..700bcb23f2a 100644 --- a/mypy.ini +++ b/mypy.ini @@ -1895,6 +1895,16 @@ disallow_untyped_defs = true warn_return_any = true warn_unreachable = true +[mypy-homeassistant.components.govee_ble.*] +check_untyped_defs = true +disallow_incomplete_defs = true +disallow_subclassing_any = true +disallow_untyped_calls = true +disallow_untyped_decorators = true +disallow_untyped_defs = true +warn_return_any = true +warn_unreachable = true + [mypy-homeassistant.components.gpsd.*] check_untyped_defs = true disallow_incomplete_defs = true