2023-11-22 07:40:19 +00:00
|
|
|
"""Definition of Picnic shopping cart."""
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
import logging
|
|
|
|
from typing import Any, cast
|
|
|
|
|
2023-11-24 12:06:32 +00:00
|
|
|
from homeassistant.components.todo import (
|
|
|
|
TodoItem,
|
|
|
|
TodoItemStatus,
|
|
|
|
TodoListEntity,
|
|
|
|
TodoListEntityFeature,
|
|
|
|
)
|
2023-11-22 07:40:19 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
from homeassistant.helpers.update_coordinator import (
|
|
|
|
CoordinatorEntity,
|
|
|
|
DataUpdateCoordinator,
|
|
|
|
)
|
|
|
|
|
|
|
|
from .const import CONF_COORDINATOR, DOMAIN
|
2023-11-24 12:06:32 +00:00
|
|
|
from .services import product_search
|
2023-11-22 07:40:19 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
|
|
|
"""Set up the Picnic shopping cart todo platform config entry."""
|
|
|
|
picnic_coordinator = hass.data[DOMAIN][config_entry.entry_id][CONF_COORDINATOR]
|
|
|
|
|
2023-11-24 12:06:32 +00:00
|
|
|
async_add_entities([PicnicCart(hass, picnic_coordinator, config_entry)])
|
2023-11-22 07:40:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
class PicnicCart(TodoListEntity, CoordinatorEntity):
|
|
|
|
"""A Picnic Shopping Cart TodoListEntity."""
|
|
|
|
|
|
|
|
_attr_has_entity_name = True
|
|
|
|
_attr_icon = "mdi:cart"
|
2023-11-24 12:06:32 +00:00
|
|
|
_attr_supported_features = TodoListEntityFeature.CREATE_TODO_ITEM
|
|
|
|
_attr_translation_key = "shopping_cart"
|
2023-11-22 07:40:19 +00:00
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
2023-11-24 12:06:32 +00:00
|
|
|
hass: HomeAssistant,
|
2023-11-22 07:40:19 +00:00
|
|
|
coordinator: DataUpdateCoordinator[Any],
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
) -> None:
|
|
|
|
"""Initialize PicnicCart."""
|
|
|
|
super().__init__(coordinator)
|
|
|
|
self._attr_device_info = DeviceInfo(
|
|
|
|
entry_type=DeviceEntryType.SERVICE,
|
|
|
|
identifiers={(DOMAIN, cast(str, config_entry.unique_id))},
|
|
|
|
manufacturer="Picnic",
|
|
|
|
model=config_entry.unique_id,
|
|
|
|
)
|
2023-11-24 12:06:32 +00:00
|
|
|
self.hass = hass
|
2023-11-22 07:40:19 +00:00
|
|
|
self._attr_unique_id = f"{config_entry.unique_id}-cart"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def todo_items(self) -> list[TodoItem] | None:
|
|
|
|
"""Get the current set of items in cart items."""
|
|
|
|
if self.coordinator.data is None:
|
|
|
|
return None
|
|
|
|
|
|
|
|
_LOGGER.debug(self.coordinator.data["cart_data"]["items"])
|
|
|
|
|
|
|
|
items = []
|
|
|
|
for item in self.coordinator.data["cart_data"]["items"]:
|
|
|
|
for article in item["items"]:
|
|
|
|
items.append(
|
|
|
|
TodoItem(
|
|
|
|
summary=f"{article['name']} ({article['unit_quantity']})",
|
|
|
|
uid=f"{item['id']}-{article['id']}",
|
|
|
|
status=TodoItemStatus.NEEDS_ACTION, # We set 'NEEDS_ACTION' so they count as state
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
return items
|
2023-11-24 12:06:32 +00:00
|
|
|
|
|
|
|
async def async_create_todo_item(self, item: TodoItem) -> None:
|
|
|
|
"""Add item to shopping cart."""
|
|
|
|
product_id = await self.hass.async_add_executor_job(
|
|
|
|
product_search, self.coordinator.picnic_api_client, item.summary
|
|
|
|
)
|
|
|
|
|
|
|
|
if not product_id:
|
|
|
|
raise ValueError("No product found or no product ID given")
|
|
|
|
|
|
|
|
await self.hass.async_add_executor_job(
|
|
|
|
self.coordinator.picnic_api_client.add_product, product_id, 1
|
|
|
|
)
|
|
|
|
|
|
|
|
await self.coordinator.async_refresh()
|