Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Newbie help running Python script under pyscript as a service #587

Open
royf007 opened this issue Feb 29, 2024 · 2 comments
Open

Newbie help running Python script under pyscript as a service #587

royf007 opened this issue Feb 29, 2024 · 2 comments

Comments

@royf007
Copy link

royf007 commented Feb 29, 2024

I'm asking if anyone can help me get a Python script that runs from the command line into a service that can run under pyscript? The script is a Telnet client to wake up my Tivo box.

I've added the @service to the main() method, and I'm not seeing pyscript.main in the developer tools as a runnable script, most likely because of a missing dependency (telnetlib3).

My configuration.yaml:

pyscript:
  allow_all_imports: true
  hass_is_global: true

My script uses the telnetlib3, asyncio, and time libraries. I'm not really sure how to install telnetlib3 into HA. Does it allow something like below in the shell?

pip install telnetlib3

This error originated from a custom integration.

Logger: custom_components.pyscript.file.TivoCommandClient
Source: custom_components/pyscript/global_ctx.py:341
Integration: Pyscript Python scripting (documentation, issues)
First occurred: 11:22:53 PM (1 occurrences)
Last logged: 11:22:53 PM

Exception in </config/pyscript/TivoCommandClient.py> line 2: import telnetlib3 ^ ModuleNotFoundError: No module named 'telnetlib3'

Also, I'm not sure my script will run as is. Here is the code:

import asyncio
import telnetlib3
import time

async def shell(reader, writer):
    rules = [
            ('COMMAND_TIMEOUT', 'IRCODE LIVETV'),
            ]

    ruleiter = iter(rules)
    expect, send = next(ruleiter)
    writer.write('IRCODE TIVO')
    writer.write('\r\n')
    time.sleep(10)  # Makes Python wait for 10 seconds

    while True:
        outp = await reader.read(1024)
        print(outp, flush=True)
        if not outp:
            break

        if expect in outp:
            writer.write(send)
            writer.write('\r\n')
            try:
                expect, send = next(ruleiter)
            except StopIteration:
                break

        # display all server output
        print(outp, flush=True)

    # EOF
    print()

@service
async def main():
    reader, writer = await telnetlib3.open_connection('192.168.186.191', 31339, shell=shell)
    await writer.protocol.waiter_closed


if __name__ == '__main__':
    asyncio.run(main())

@royf007 royf007 changed the title Newbie Help with Running Python Script to Run under pyscript as a Service Newbie help running Python script under pyscript as a service Feb 29, 2024
@SVH-Powel
Copy link

SVH-Powel commented Feb 29, 2024

You have to list all imports in the requirements.txt file as described here: https://hacs-pyscript.readthedocs.io/en/stable/reference.html#importing

@royf007
Copy link
Author

royf007 commented Mar 1, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants