I have the following script called edit_myhouse_theme.py in the config/pyscripts folder:
import fileinput
import sys
import os
import time
@service
def changefilepyscript():
if '/config/pyscript_modules' not in sys.path:
sys.path.append('/config/pyscript_modules')
path = '/config/themes/myhouse-mode/myhouse-mode.yaml'
time.sleep(10)
file_name = state.get('input_text.background_full_filename')
task.executor(theme_module.changetheme, file_name)
The script was to replace the name of the lovelace_theme value in the theme file. As this involves open commands, there is a script in config/pyscript_modules called theme_module.py that i call using task.executor. This carries out the file changes. The code is below:
import fileinput
import sys
import os
def changetheme(file_selected=None):
if len(file_selected) > 8:
path = '/config/themes/myhouse-mode/myhouse-mode.yaml'
with open(path) as file:
data = file.readlines()
data[13] = f' lovelace-background: "center / cover no-repeat url(\'local/images/backgrounds/{file_selected}\')"\n'
with open(path, 'w') as file:
file.writelines(data)
I have run the created pyscript service changefilepyscript and get the following error:
This error originated from a custom integration.
Logger: custom_components.pyscript.file.edit_myhouse_theme.changefilepyscript
Source: custom_components/pyscript/eval.py:510
integration: Pyscript Python scripting (documentation, issues)
First occurred: 13:45:10 (1 occurrences)
Last logged: 13:45:10
Exception in <file.edit_myhouse_theme.changefilepyscript> line 18: task.executor(theme_module.changetheme, file_name) ^ NameError: name 'theme_module.changetheme' is not defined
I am not sure what i have failed to do as i think i have set the module path up correctly in the calling pyscript but may be wrong. Any help appreciated
UPDATE:
I have moved on a bit by using @pyscript_compile rather than a 2nd native python script.
import fileinput
import sys
import os
import time
@service
def changefilepyscript():
# if 'config/pyscript_modules' not in sys.path:
# sys.path.append('config/pyscript_modules')
# path = 'config/themes/myhouse-mode/myhouse-mode.yaml'
# time.sleep(10)
file_name = state.get('input_text.background_full_filename')
contents, exception = task.executor(changetheme, file_name)
if exception:
raise exception
log.info(f"contents = {contents}")
@pyscript_compile
def changetheme(file_selected=None):
# if len(file_selected) > 8:
path = '/homeassistant/themes/myhouse-mode/myhouse-mode.yaml'
with open(path) as file:
data = file.readlines()
data[13] = f' lovelace-background: "center / cover no-repeat url(\'local/images/backgrounds/{file_selected}\')"\n'
with open(path, 'w') as filew:
filew.writelines(data)
However, the path name for the yaml file is not finding the file even though i have used ssh to confirm and tried various variations on the name (config, homeassistant etc).
The current definition is
path = '/homeassistant/themes/myhouse-mode/myhouse-mode.yaml'
Readlink using the full python text above returns the same correct path so the format is at least correct.
I am at a total loss with this.