Ejemplo n.º 1
0
    def getList(self) -> str:
        uuid = self.skillInstance.getConfig('uuid')
        uuidList = self.skillInstance.getConfig('listUuid')
        bringList = BringApi(uuid, uuidList)
        translation = BringApi.loadTranslations(
            self.LanguageManager.activeLanguageAndCountryCode)

        items = bringList.get_items()['purchase']
        details = bringList.get_items_detail()
        itemList = [{
            "text": self.translate(item['name'], translation),
            "image": self.get_image(details, item['name'])
        } for item in items]

        return json.dumps(itemList)
class BringDataUpdateCoordinator(DataUpdateCoordinator[int]):
    """Class to manage fetching shopping list data from endpoint."""
    def __init__(self, hass, username, password, list_id, name, locale):
        """Initialize Bring data updater."""
        self.list_id = list_id
        self.locale = locale
        self.name = name
        self.api = BringApi(username, password, True)

        self.purchase = []
        self.recently = []

        super().__init__(
            hass,
            _LOGGER,
            name=name,
            update_interval=SCAN_INTERVAL,
        )

    async def _async_update_data(self) -> Optional[int]:
        """Fetch new state data for the sensor.
        This is the only method that should fetch new data for Home Assistant.
        """

        catalog = self.api.loadCatalog(self.locale)
        details = self.api.get_items_detail()
        data = self.api.get_items(self.locale)

        purchase = data["purchase"]
        recently = data["recently"]

        self.purchase = self._get_list(purchase, details, catalog)
        self.recently = self._get_list(recently, details, catalog)

        return len(purchase)

    def _get_list(self, source=None, details=None, articles=None):
        if articles is None:
            articles = []
        items = []
        for p in source:
            found = False
            for d in details:
                if p["name"] == d["itemId"]:
                    found = True
                    break

            item = {
                "image": p["name"],
                "name": p["name"],
                "specification": p["specificitemation"]
            }

            if found:
                item["image"] = d["userIconItemId"]

            item["key"] = item["image"]

            if item["name"] in articles:
                item["name"] = articles[item["name"]]
            else:
                if found == 0:
                    item["image"] = item["name"][0]

            item["image"] = self._purge(item["image"])

            if "+" in item["specification"]:
                specs = item["specification"].split("+")

                for spec in specs:
                    temp = dict(item.items())
                    temp["specification"] = spec.strip()
                    items.append(temp)

            else:
                items.append(item)

        return items

    @staticmethod
    def _purge(item):
        return item.lower() \
            .replace("é", "e") \
            .replace("ä", "ae") \
            .replace("-", "_") \
            .replace("ö", "oe") \
            .replace("ü", "ue") \
            .replace(" ", "_")