thomasloven / hass-lovelace_gen

🔹 Improve the lovelace yaml parser for Home Assistant
MIT License
209 stars 22 forks source link

Script validations fails when installed #2

Closed johntdyer closed 4 years ago

johntdyer commented 4 years ago

HASSIO 0.97.1 lovelace_gen: version 4

When ever I run the script validation I get an error that the plugin isnt found

root@ubuntu:/usr/share/hassio/homeassistant/includes# hass --script check_config -c  /usr/share/hassio/homeassistant/
Testing configuration at /usr/share/hassio/homeassistant/
Failed config
  General Errors:
    - Component not found: lovelace_gen

Successful config (partial)

I have tried reinstalling it but that doesnt seem to help

thomasloven commented 4 years ago

That always happens with new custom integrations They aren't found until after a restart, so the safest way to do things is actually:

johntdyer commented 4 years ago

I installed via HACS and I've restarted multiple times since....

thomasloven commented 4 years ago

Then I'm not sure what could be the problem. Make sure the files in custom_components look correct.

johntdyer commented 4 years ago

I am not 100% what you consider correct but here is a dir, sha and cat of the files

ls -la 
total 20
drwxr-xr-x  3 homeassistant homeassistant 4096 Aug 19 17:30 .
drwxr-xr-x 38 homeassistant homeassistant 4096 Aug 23 10:19 ..
-rw-r--r--  1 homeassistant homeassistant 1994 Aug 19 17:26 __init__.py
-rw-r--r--  1 homeassistant homeassistant  162 Aug 19 17:26 manifest.json
drwxr-xr-x  2 homeassistant homeassistant 4096 Aug 21 07:51 __pycache__
root@ubuntu:/usr/share/hassio/homeassistant/custom_components/lovelace_gen# sha256sum *
9703e204f785193b9ab1d3fd61ab99f3703131a68b4ca5810a7680980ef6ab35  __init__.py
cda43fd3a0118aa2007580c49026792726984098a24ee3718c60e1fdef35e9b4  manifest.json
sha256sum: __pycache__: Is a directory
root@ubuntu:/usr/share/hassio/homeassistant/custom_components/lovelace_gen# 
root@ubuntu:/usr/share/hassio/homeassistant/custom_components/lovelace_gen# cat __
__init__.py  __pycache__/
root@ubuntu:/usr/share/hassio/homeassistant/custom_components/lovelace_gen# cat __init__.py
import os
import logging
import json
import io
import time
from collections import OrderedDict

import jinja2

from homeassistant.util.yaml import loader
from homeassistant.exceptions import HomeAssistantError

_LOGGER = logging.getLogger(__name__)

jinja = jinja2.Environment(loader=jinja2.FileSystemLoader("/"))

def load_yaml(fname, args={}):
    try:
        ll_gen = False
        with open(fname, encoding="utf-8") as f:
            if f.readline().lower().startswith("# lovelace_gen"):
                ll_gen = True

        if ll_gen:
            stream = io.StringIO(jinja.get_template(fname).render(args))
            stream.name = fname
            return loader.yaml.load(stream, Loader=loader.SafeLineLoader) or OrderedDict()
        else:
            with open(fname, encoding="utf-8") as config_file:
                return loader.yaml.load(config_file, Loader=loader.SafeLineLoader) or OrderedDict()
    except loader.yaml.YAMLError as exc:
        _LOGGER.error(str(exc))
        raise HomeAssistantError(exc)
    except UnicodeDecodeError as exc:
        _LOGGER.error("Unable to read file %s: %s", fname, exc)
        raise HomeAssistantError(exc)

def _include_yaml(ldr, node):
    args = {}
    if isinstance(node.value, str):
        fn = node.value
    else:
        fn, args, *_ = ldr.construct_sequence(node)
    fname = os.path.join(os.path.dirname(ldr.name), fn)
    try:
        return loader._add_reference(load_yaml(fname, args), ldr, node)
    except FileNotFoundError as exc:
        _LOGGER.error("Unable to include file %s: %s", fname, exc);
        raise HomeAssistantError(exc)

def _uncache_file(ldr, node):
    path = node.value
    timestamp = str(time.time())
    if '?' in path:
        return f"{path}&{timestamp}"
    return f"{path}?{timestamp}"

loader.load_yaml = load_yaml
loader.yaml.SafeLoader.add_constructor("!include", _include_yaml)
loader.yaml.SafeLoader.add_constructor("!file", _uncache_file)

async def async_setup(*_):
    return True
root@ubuntu:/usr/share/hassio/homeassistant/custom_components/lovelace_gen#
thomasloven commented 4 years ago

Is this still a problem?

johntdyer commented 4 years ago

nah, we can close, ty