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(" ", "_")
class BringlistSkill(MycroftSkill): def __init__(self): MycroftSkill.__init__(self) self._bring = None self._regex = {} def initialize(self): self.settings_change_callback = self.on_websettings_changed self.setup() def on_websettings_changed(self): self.setup() def setup(self): # handle credentials uuid = None uuidlist = None credentials = self._load_credentials_store() if credentials is not None: uuid = credentials['uuid'] uuidlist = credentials['list'] else: login = self.settings.get("login", "") password = self.settings.get("password", "") if login: uuid, uuidlist = BringApi.login(login, password) if uuid is None: self.speak_dialog('bring.error.connect') self.log.warning( "Loading credentials failed, please check your credentials") return self.log.info("Loaded credentials") self._bring = BringApi(uuid, uuidlist) if self._bring is None: self.speak_dialog('bring.error.connect') self.log.warning("API connect failed") self.log.info("API connect succeeded") return @intent_handler( IntentBuilder("AddToBringlist").require("bring.list").require( "bring.add")) def handle_bringlist_add(self, message): self.log.info("Bringlist add") if self._bring is None: self.speak_dialog('bring.error.connect') item, desc = self._get_item(message.data.get('utterance'), 'bring.add.regex') if item is not None: self._bring.purchase_item(item.capitalize(), desc) self.speak_dialog('bring.success.add', data={"Item": item}) return self.speak_dialog('bring.error.add', data={"Item": item}) @intent_handler( IntentBuilder("RemoveFromBringlist").require("bring.list").require( "bring.remove")) def handle_bringlist_remove(self, message): self.log.info("Bringlist remove") if self._bring is None: self.speak_dialog('bring.error.connect') item, desc = self._get_item(message.data.get('utterance'), 'bring.remove.regex') if item: self._bring.recent_item(item.capitalize()) self.speak_dialog('bring.success.remove', data={"Item": item}) return self.speak_dialog('bring.error.remove', data={"Item": item}) @intent_handler( IntentBuilder("ClearBringlist").require("bring.list").require( "bring.clear")) def handle_bringlist_clear(self, message): self.log.info("Bringlist clear") if self._bring is None: self.speak_dialog('bring.error.connect') items = self._bring.get_items()['purchase'] if items: for item in items: self._bring.recent_item(item['name']) self.speak_dialog('bring.success.clear', data={"Count": len(items)}) return self.speak_dialog('bring.error.clear') def _load_credentials_store(self): credentials = None credentials_file = 'credentials.store' if self.file_system.exists(credentials_file): with self.file_system.open(credentials_file, 'rb') as f: credentials = pickle.load(f) return credentials def _get_item(self, text, regfile): match = self._get_regex(regfile).match(text) if match: return match.group('Item'), match.group('Desc') if match.group( 'Desc') is not None else "" else: return None, None def _get_regex(self, regfile): if regfile in self._regex: return self._regex[regfile] with open(self.find_resource(regfile, 'regex')) as f: matcher = f.readline().rstrip('\n') regex = re.compile(matcher) self._regex[regfile] = regex return regex