dinki / View-Assist

View Assist provides visual feedback for the Home Assistant Assist Voice Assistant
76 stars 5 forks source link

Wikipedia Search Enhancement #16

Open ADHDSquir opened 3 weeks ago

ADHDSquir commented 3 weeks ago

For Wikipedia search, implement the following:

dinki commented 2 weeks ago

I am interested in using Pyscript for this and have asked for and received some help on this in the forum:

https://community.home-assistant.io/t/pyscript-new-integration-for-easy-and-powerful-python-scripting/215967/322

Unfortunately errors still are going with the latest version:

import wikipedia
@service
def search_wikipedia(searchterm=None, return_response=True):
  #summary = task.executor(wikipedia.summary, searchterm, {"sentences": 2})
  funcArgs = {"query": searchterm, "sentences": 2, "auto_suggest": False}
  summary = task.executor(wikipedia.summary, **funcArgs)
  response_variable = { "summary": summary }
  return response_variable

With the error:

  2024-06-08 08:51:14.622 ERROR (MainThread) [custom_components.pyscript.file.wiki.search_wikipedia] Exception in <file.wiki.search_wikipedia> line 6:
      summary = task.executor(wikipedia.summary, **funcArgs)

I am fine with other solutions for sure but also want to keep things simple so I can provide support and to use things like this for other similar tasks of querying and pulling data.

dinki commented 2 weeks ago

@ADHDSquir Received this answer on HA forum which absolutely works great:

import wikipedia
@service(supports_response="optional")
def search_wikipedia(searchterm=None, return_response=True):
    """yaml
    name: Search Wikipedia
    description: hello_world service example using pyscript.
    fields:
        searchterm:
            description: What to search for?
            example: Madonna
            required: true
            selector:
                text:
    """
    funcArgs = {"sentences": 2, "auto_suggest": False}
    summary = task.executor(wikipedia.summary, searchterm, **funcArgs)
    response_variable = { "summary": summary }
    return response_variable

image

The cool thing about pyscript is it can be installed via HACS and the scripts can be dropped into a directory, HA scans it immediately, and then the service is available. I will try to extend this to other APIs if I can wrap my head around this. Hopefully the requests module will work with task.executor too