From fd9533560ef822462915014d17b889eb426878bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Wed, 25 Dec 2024 20:08:00 +0100 Subject: [PATCH] Pre-Commit fixs --- .../aliexpress_openplatform/__init__.py | 2 +- .../aliexpress_coordinator.py | 4 ++- .../aliexpress_openplatform/sensor.py | 34 +++++++++++-------- 3 files changed, 24 insertions(+), 16 deletions(-) diff --git a/custom_components/aliexpress_openplatform/__init__.py b/custom_components/aliexpress_openplatform/__init__.py index 26580a9..c0120ce 100644 --- a/custom_components/aliexpress_openplatform/__init__.py +++ b/custom_components/aliexpress_openplatform/__init__.py @@ -9,8 +9,8 @@ from homeassistant.const import Platform -from .const import DOMAIN from .aliexpress_coordinator import AliexpressOpenPlatformCoordinator +from .const import DOMAIN if TYPE_CHECKING: from homeassistant.config_entries import ConfigEntry diff --git a/custom_components/aliexpress_openplatform/aliexpress_coordinator.py b/custom_components/aliexpress_openplatform/aliexpress_coordinator.py index 50d283d..b591fc3 100644 --- a/custom_components/aliexpress_openplatform/aliexpress_coordinator.py +++ b/custom_components/aliexpress_openplatform/aliexpress_coordinator.py @@ -2,8 +2,8 @@ from __future__ import annotations -import logging from datetime import datetime, timedelta, timezone +import logging from typing import TYPE_CHECKING, Any from homeassistant.helpers.update_coordinator import ( @@ -20,6 +20,7 @@ _LOGGER = logging.getLogger(__name__) + class AliexpressOpenPlatformCoordinator(DataUpdateCoordinator): """Coordinator for managing Aliexpress order data updates.""" @@ -44,6 +45,7 @@ def __init__(self, hass: HomeAssistant, config_entry: ConfigEntry) -> None: } def get_value(self, name: str) -> Any | None: + """Return the value from coordinator data or none.""" if name in self.data: return self.data[name] diff --git a/custom_components/aliexpress_openplatform/sensor.py b/custom_components/aliexpress_openplatform/sensor.py index 39df81a..afdea29 100644 --- a/custom_components/aliexpress_openplatform/sensor.py +++ b/custom_components/aliexpress_openplatform/sensor.py @@ -3,7 +3,6 @@ from __future__ import annotations import logging -from datetime import date, datetime from typing import TYPE_CHECKING, Any, Mapping from homeassistant.components.sensor import ( @@ -16,10 +15,10 @@ CoordinatorEntity, ) -from .aliexpress_coordinator import AliexpressOpenPlatformCoordinator -from .const import DOMAIN, DEVICE_INFO +from .const import DEVICE_INFO, DOMAIN if TYPE_CHECKING: + from datetime import date, datetime from decimal import Decimal from homeassistant.config_entries import ConfigEntry @@ -27,12 +26,14 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.typing import StateType + from .aliexpress_coordinator import AliexpressOpenPlatformCoordinator + _LOGGER = logging.getLogger(__name__) CURRENCY_USD = "$" async def async_setup_entry( - hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback + hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback ) -> None: """Initialize Aliexpress sensors from a configuration entry.""" coordinator = hass.data[DOMAIN][entry.entry_id] @@ -49,8 +50,10 @@ async def async_setup_entry( class AliexpressSensor(SensorEntity, CoordinatorEntity): + """Aliexpress Base sensor class.""" - def __init__(self, coordinator: AliexpressOpenPlatformCoordinator): + def __init__(self, coordinator: AliexpressOpenPlatformCoordinator) -> None: + """Initialize the values.""" super().__init__(coordinator) self.coordinator = coordinator self._state = None @@ -63,6 +66,7 @@ def device_info(self) -> DeviceInfo | None: @property def native_value(self) -> StateType | date | datetime | Decimal: + """Return the sensor's state.""" return self._state @callback @@ -73,6 +77,7 @@ def _handle_coordinator_update(self) -> None: @property def last_reset(self) -> datetime | None: + """Return the last reset sensor's date.""" return self._last_reset @@ -90,10 +95,11 @@ def __init__(self, coordinator: AliexpressOpenPlatformCoordinator) -> None: state_class=SensorStateClass.TOTAL, native_unit_of_measurement=CURRENCY_USD, ) + @callback def _handle_coordinator_update(self) -> None: super()._handle_coordinator_update() - self._state = self.coordinator.data['total_commissions'] + self._state = self.coordinator.data["total_commissions"] self.async_write_ha_state() @@ -106,7 +112,7 @@ def __init__(self, coordinator: AliexpressOpenPlatformCoordinator) -> None: self._attr_unique_id = "aliexpress_affiliate_commissions" self.entity_description = SensorEntityDescription( name="Affiliate Commissions", - key=f"aliexpress_affiliate_commissions", + key="aliexpress_affiliate_commissions", icon="mdi:cash-multiple", state_class=SensorStateClass.TOTAL, native_unit_of_measurement=CURRENCY_USD, @@ -115,7 +121,7 @@ def __init__(self, coordinator: AliexpressOpenPlatformCoordinator) -> None: @callback def _handle_coordinator_update(self) -> None: super()._handle_coordinator_update() - self._state = self.coordinator.data['affiliate_commissions'] + self._state = self.coordinator.data["affiliate_commissions"] self.async_write_ha_state() @@ -137,11 +143,11 @@ def __init__(self, coordinator: AliexpressOpenPlatformCoordinator) -> None: @callback def _handle_coordinator_update(self) -> None: super()._handle_coordinator_update() - self._state = self.coordinator.data['influencer_commissions'] + self._state = self.coordinator.data["influencer_commissions"] self.async_write_ha_state() -class AliexpressOrderCountSensor(SensorEntity, CoordinatorEntity): +class AliexpressOrderCountSensor(AliexpressSensor): """Sensor for tracking total number of orders.""" def __init__(self, coordinator: AliexpressOpenPlatformCoordinator) -> None: @@ -157,9 +163,9 @@ def __init__(self, coordinator: AliexpressOpenPlatformCoordinator) -> None: @callback def _handle_coordinator_update(self) -> None: - """Get the update from coordinator""" + """Get the update from coordinator.""" super()._handle_coordinator_update() - self._state = self.coordinator.data['total_orders'] + self._state = self.coordinator.data["total_orders"] self.async_write_ha_state() @@ -181,7 +187,7 @@ def __init__(self, coordinator: AliexpressOpenPlatformCoordinator) -> None: @callback def _handle_coordinator_update(self) -> None: super()._handle_coordinator_update() - self._state = self.coordinator.data['total_paid'] + self._state = self.coordinator.data["total_paid"] self.async_write_ha_state() @@ -202,7 +208,7 @@ def __init__(self, coordinator: AliexpressOpenPlatformCoordinator) -> None: @callback def _handle_coordinator_update(self) -> None: - self._state = self.coordinator.data['last_order']['total_commission'] + self._state = self.coordinator.data["last_order"]["total_commission"] self.async_write_ha_state() @property