Using "import" in a custom_component

I’m trying to write a component that does stuff that uses the “subprocess” module.

The import function seems to cause trouble.

For example, if I take the hello_service:

The domain of your component. Should be equal to the name of your component.

DOMAIN = ‘hello_service’

ATTR_NAME = ‘name’
DEFAULT_NAME = ‘World’

import subprocess

def setup(hass, config):

Then adding that import line breaks it. Where am I going wrong?

Actually this is rubbish! But it’s not working…

DOMAIN = ‘hello_service’
ATTR_NAME = ‘name’
DEFAULT_NAME = ‘World’

import subprocess

def setup(hass, config):
“”“Set up is called when Home Assistant is loading our component.”“”

def handle_hello(call):
    name = call.data.get(ATTR_NAME, DEFAULT_NAME)
    cmd = """osascript -e 'tell app "Finder" to sleep'"""
    p = subprocess.Popen(cmd, shell=True)
    p.terminate()
    hass.states.set('hello_service.hello', name)

def handle_sleep(call):
    cmd = """osascript -e 'tell app "Finder" to sleep'"""
    p = subprocess.Popen(cmd, shell=True)
    p.terminate()

hass.services.register(DOMAIN, 'hello', handle_hello)
hass.services.register(DOMAIN, 'sleep', handle_sleep)

# Return boolean to indicate that initialization was successfully.
return True

…and the reason it’s not working is that p.terminate() stops it. Get rid of that and you’re OK, but maybe better to pause? Not very familiar with Popen!