Add reload service to python_script (#9512)
* Add reload service * add reload test * Use global variable * remove white space .... * adjust as suggested * remove annoying white space.... * fix travis * fix travis, again * rename Load_scripts to Discover_scripts Travis complains that "Load_scripts" is an invalid name (I don't know why) * Update python_script.py
This commit is contained in:
parent
58cc3a2d7a
commit
c26fb9906f
2 changed files with 60 additions and 2 deletions
|
@ -6,6 +6,7 @@ import datetime
|
|||
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.const import SERVICE_RELOAD
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
from homeassistant.loader import bind_hass
|
||||
from homeassistant.util import sanitize_filename
|
||||
|
@ -36,6 +37,24 @@ def setup(hass, config):
|
|||
"""Initialize the python_script component."""
|
||||
path = hass.config.path(FOLDER)
|
||||
|
||||
if not os.path.isdir(path):
|
||||
_LOGGER.warning('Folder %s not found in config folder', FOLDER)
|
||||
return False
|
||||
|
||||
discover_scripts(hass)
|
||||
|
||||
def reload_scripts_handler(call):
|
||||
"""Handle reload service calls."""
|
||||
discover_scripts(hass)
|
||||
hass.services.register(DOMAIN, SERVICE_RELOAD, reload_scripts_handler)
|
||||
|
||||
return True
|
||||
|
||||
|
||||
def discover_scripts(hass):
|
||||
"""Discover python scripts in folder."""
|
||||
path = hass.config.path(FOLDER)
|
||||
|
||||
if not os.path.isdir(path):
|
||||
_LOGGER.warning('Folder %s not found in config folder', FOLDER)
|
||||
return False
|
||||
|
@ -44,12 +63,16 @@ def setup(hass, config):
|
|||
"""Handle python script service calls."""
|
||||
execute_script(hass, call.service, call.data)
|
||||
|
||||
existing = hass.services.services.get(DOMAIN, {}).keys()
|
||||
for existing_service in existing:
|
||||
if existing_service == SERVICE_RELOAD:
|
||||
continue
|
||||
hass.services.remove(DOMAIN, existing_service)
|
||||
|
||||
for fil in glob.iglob(os.path.join(path, '*.py')):
|
||||
name = os.path.splitext(os.path.basename(fil))[0]
|
||||
hass.services.register(DOMAIN, name, python_script_service_handler)
|
||||
|
||||
return True
|
||||
|
||||
|
||||
@bind_hass
|
||||
def execute_script(hass, name, data=None):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue