Skip to content

Commit

Permalink
Pre-Commit fixs
Browse files Browse the repository at this point in the history
  • Loading branch information
MiguelAngelLV committed Dec 25, 2024
1 parent d9a43ff commit fd95335
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 16 deletions.
2 changes: 1 addition & 1 deletion custom_components/aliexpress_openplatform/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -20,6 +20,7 @@

_LOGGER = logging.getLogger(__name__)


class AliexpressOpenPlatformCoordinator(DataUpdateCoordinator):
"""Coordinator for managing Aliexpress order data updates."""

Expand All @@ -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]

Expand Down
34 changes: 20 additions & 14 deletions custom_components/aliexpress_openplatform/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -16,23 +15,25 @@
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
from homeassistant.helpers.device_registry import DeviceInfo
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]
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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


Expand All @@ -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()


Expand All @@ -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,
Expand All @@ -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()


Expand All @@ -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:
Expand All @@ -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()


Expand All @@ -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()


Expand All @@ -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
Expand Down

0 comments on commit fd95335

Please sign in to comment.