Skip to content

Commit

Permalink
Include warehouse name in sensor
Browse files Browse the repository at this point in the history
  • Loading branch information
krasnoukhov committed Oct 20, 2023
1 parent 6bd788a commit cfe4747
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
18 changes: 15 additions & 3 deletions custom_components/nova_poshta/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import httpx
from novaposhta.client import NovaPoshtaApi, InvalidAPIKeyError, APIRequestError
from transliterate import translit

from .const import (
API_KEY,
Expand Down Expand Up @@ -82,12 +83,23 @@ def parcels(self) -> list[dict]:
return self.data["data"][0]["result"]

@property
def warehouses(self) -> list[str]:
"""Retrieve unique warehouse ids."""
def warehouses(self) -> list[dict]:
"""Retrieve unique warehouses."""
return list(
set(
map(
lambda x: x["SettlmentAddressData"]["RecipientWarehouseNumber"],
lambda x: frozenset(
{
"id": x["SettlmentAddressData"]["RecipientWarehouseNumber"],
"name": translit(
x["SettlmentAddressData"][
"RecipientSettlementDescription"
],
"uk",
reversed=True,
).replace("Kyyiv", "Kyiv"),
}.items()
),
self.parcels,
)
)
Expand Down
4 changes: 2 additions & 2 deletions custom_components/nova_poshta/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
"integration_type": "service",
"iot_class": "cloud_polling",
"issue_tracker": "https://github.com/krasnoukhov/homeassistant-nova-poshta/issues",
"requirements": ["git+https://github.com/krasnoukhov/novaposhta-python-client.git@errors#novaposhta-python-client==0.1.2"],
"version": "1.0.0-beta.0"
"requirements": ["transliterate==1.10.2", "git+https://github.com/krasnoukhov/novaposhta-python-client.git@errors#novaposhta-python-client==0.1.2"],
"version": "1.0.0-beta.1"
}
14 changes: 8 additions & 6 deletions custom_components/nova_poshta/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import logging
from typing import cast
from stringcase import snakecase

from homeassistant.components.sensor import (
SensorEntity,
Expand Down Expand Up @@ -35,8 +36,8 @@ async def async_setup_entry(

async_add_entities(
[
NovaPoshtaSensor(coordinator, entry, warehouse_id)
for warehouse_id in coordinator.warehouses
NovaPoshtaSensor(coordinator, entry, warehouse)
for warehouse in coordinator.warehouses
]
)

Expand All @@ -50,16 +51,17 @@ def __init__(
self,
coordinator: NovaPoshtaCoordinator,
entry: ConfigEntry,
warehouse_id: str,
warehouse: frozenset,
) -> None:
"""Initialize a Nova Poshta entity."""
super().__init__(coordinator)

self._parcels = self.coordinator.delivered_by_warehouse(warehouse_id)
warehouse_info = dict(warehouse)
self._parcels = self.coordinator.delivered_by_warehouse(warehouse_info["id"])

self.entity_description = SensorEntityDescription(
key=f"delivered_parcels_{warehouse_id}",
name=f"Delivered parcels in {warehouse_id}",
key=f"delivered_parcels_{snakecase(warehouse_info['name'])}_{warehouse_info['id']}",
name=f"Delivered parcels in {warehouse_info['name']}@{warehouse_info['id']}",
state_class=SensorStateClass.TOTAL,
)
self._attr_unique_id = self.entity_description.key
Expand Down

0 comments on commit cfe4747

Please sign in to comment.