async def post(self) -> JSONResponse: """POST /item/{id}/states""" identifier = self.data["id"] item = self.core.item_manager.items.get(identifier) if not item: return self.error( ERROR_ITEM_NOT_FOUND, f"No item found with identifier {identifier}", 404) try: content = (await self.request.content.read()).decode() commit = STATE_COMMIT_SCHEMA( json.loads(content) if content else {}) # pylint: disable=broad-except except Exception as e: return JSONResponse(error=e) if not commit.keys() & item.states.states.keys() == commit.keys(): return self.error( ERROR_INVALID_ITEM_STATES, f"The given states {set(commit.keys())} do not comply with " f"accepted states {set(item.states.states.keys())}") if not item.status == ItemStatus.ONLINE: return JSONResponse( error=ItemNotOnlineError( f"The item {item.identifier} is not online")) return JSONResponse(dict(ChainMap( *[await item.states.set(state, value) for state, value in commit.items()])))
async def post(self) -> JSONResponse: """POST /item/{id}/states/{state_name}""" identifier = self.data["id"] state_name = self.data["state_name"] item = self.core.item_manager.items.get(identifier) if not item: return self.error( ERROR_ITEM_NOT_FOUND, f"No item found with identifier {identifier}", 404) if state_name not in item.states.states.keys(): return self.error( ERROR_INVALID_ITEM_STATE, f"The given state '{state_name}' does not exist") if not item.status == ItemStatus.ONLINE: return self.error(ItemNotOnlineError( f"The item {item.identifier} is not online")) try: content = (await self.request.content.read()).decode() value = json.loads(content) if content else {} result = await item.states.set(state_name, value) # pylint: disable=broad-except except (Exception, vol.Error) as e: return self.error(e) return self.json(result)
async def set(self, value) -> dict: """Sets a state""" if self.state_engine.item.status != ItemStatus.ONLINE: raise ItemNotOnlineError(self.state_engine.item.identifier) if self.schema: # Apply schema to new value value = self.schema(value) if self.setter: result: dict = await self.setter(value) for state, change in result.items(): self.state_engine.states[state].value = change self.state_engine.core.event_engine.broadcast( "state_change", item=self.state_engine.item, changes=result) LOGGER.debug("State change: %s %s", self.state_engine.item.identifier, result) return result return {}
async def set_item_states(request: web.Request) -> JSONResponse: identifier = request.match_info["id"] item = self.core.item_manager.items.get(identifier) if not item: return JSONResponse(error={ "type": ERROR_ITEM_NOT_FOUND, "message": f"No item found with identifier {identifier}" }, status_code=404) try: content = (await request.content.read()).decode() commit = STATE_COMMIT_SCHEMA( json.loads(content) if content else {}) # pylint: disable=broad-except except Exception as e: return JSONResponse(error=e) if not commit.keys() & item.states.states.keys() == commit.keys(): return JSONResponse( error={ "type": ERROR_INVALID_ITEM_STATES, "message": f"The given states {set(commit.keys())} do not" f"comply with accepted states " f"{set(item.states.states.keys())}" }) if not item.status == ItemStatus.ONLINE: return JSONResponse(error=ItemNotOnlineError( f"The item {item.identifier} is not online")) return JSONResponse( dict( ChainMap(*[ await item.states.set(state, value) for state, value in commit.items() ])))
async def set_item_state(request: web.Request) -> JSONResponse: identifier = request.match_info["id"] item = self.core.item_manager.items.get(identifier) if not item: return JSONResponse(error={ "type": ERROR_ITEM_NOT_FOUND, "message": f"No item found with identifier {identifier}" }, status_code=404) state_name = request.match_info["state_name"] if state_name not in item.states.states.keys(): return JSONResponse( error={ "type": ERROR_INVALID_ITEM_STATE, "message": f"The given state '{state_name}' does not exist" }) if not item.status == ItemStatus.ONLINE: return JSONResponse(error=ItemNotOnlineError( f"The item {item.identifier} is not online")) try: content = (await request.content.read()).decode() value = json.loads(content) if content else {} result = await item.states.set(state_name, value) # pylint: disable=broad-except except (Exception, vol.error.Error) as e: return JSONResponse(error=e) return JSONResponse(result)