Exemple #1
0
def includes(config):
    ret = []
    for include in config.get(CONF_INCLUDES, []):
        path = CORE.relative_path(include)
        res = os.path.relpath(path, CORE.relative_build_path('src'))
        ret.append(u'#include "{}"'.format(res))
    return ret
Exemple #2
0
def progmem_array(id, rhs):
    rhs = safe_exp(rhs)
    obj = MockObj(id, u'.')
    assignment = ProgmemAssignmentExpression(id.type, id, rhs, obj)
    CORE.add(assignment)
    CORE.register_variable(id, obj)
    obj.requires.append(assignment)
    return obj
Exemple #3
0
def clean_build():
    pioenvs = CORE.relative_pioenvs_path()
    if os.path.isdir(pioenvs):
        _LOGGER.info("Deleting %s", pioenvs)
        shutil.rmtree(pioenvs)
    piolibdeps = CORE.relative_piolibdeps_path()
    if os.path.isdir(piolibdeps):
        _LOGGER.info("Deleting %s", piolibdeps)
        shutil.rmtree(piolibdeps)
Exemple #4
0
def run_platformio_cli(*args, **kwargs):
    os.environ["PLATFORMIO_FORCE_COLOR"] = "true"
    os.environ["PLATFORMIO_BUILD_DIR"] = os.path.abspath(CORE.relative_pioenvs_path())
    os.environ["PLATFORMIO_LIBDEPS_DIR"] = os.path.abspath(CORE.relative_piolibdeps_path())
    cmd = ['platformio'] + list(args)

    if os.environ.get('PI4HOME_USE_SUBPROCESS') is None:
        import platformio.__main__
        return run_external_command(platformio.__main__.main,
                                    *cmd, **kwargs)

    return run_external_process(*cmd, **kwargs)
Exemple #5
0
def write_cpp(config):
    _LOGGER.info("Generating C++ source...")

    CORE.add_job(core_config.to_code, config[CONF_PI4HOME], domain='pi4home')
    for domain in PRE_INITIALIZE:
        if domain == CONF_PI4HOME or domain not in config:
            continue
        CORE.add_job(get_component(domain).to_code,
                     config[domain],
                     domain=domain)

    for domain, component, conf in iter_components(config):
        if domain in PRE_INITIALIZE or not hasattr(component, 'to_code'):
            continue
        CORE.add_job(component.to_code, conf, domain=domain)

    CORE.flush_tasks()
    add(RawStatement(''))
    add(RawStatement(''))
    all_code = []
    for exp in CORE.expressions:
        if not config[CONF_PI4HOME][CONF_USE_CUSTOM_CODE]:
            if isinstance(exp, Expression) and not exp.required:
                continue
        all_code.append(text_type(statement(exp)))

    writer.write_platformio_project()

    code_s = indent('\n'.join(line.rstrip() for line in all_code))
    writer.write_cpp(code_s)
    return 0
Exemple #6
0
def variable(
        id,  # type: ID
        rhs,  # type: Expression
        type=None  # type: MockObj
):
    # type: (...) -> MockObj
    rhs = safe_exp(rhs)
    obj = MockObj(id, u'.')
    id.type = type or id.type
    assignment = AssignmentExpression(id.type, '', id, rhs, obj)
    CORE.add(assignment)
    CORE.register_variable(id, obj)
    obj.requires.append(assignment)
    return obj
Exemple #7
0
def to_code(config):
    from PIL import Image

    path = CORE.relative_path(config[CONF_FILE])
    try:
        image = Image.open(path)
    except Exception as e:
        raise core.pi4homeError(u"Could not load image file {}: {}".format(path, e))

    if CONF_RESIZE in config:
        image.thumbnail(config[CONF_RESIZE])

    image = image.convert('1', dither=Image.NONE)
    width, height = image.size
    if width > 500 or height > 500:
        _LOGGER.warning("The image you requested is very big. Please consider using the resize "
                        "parameter")
    width8 = ((width + 7) // 8) * 8
    data = [0 for _ in range(height * width8 // 8)]
    for y in range(height):
        for x in range(width):
            if image.getpixel((x, y)):
                continue
            pos = x + y * width8
            data[pos // 8] |= 0x80 >> (pos % 8)

    rhs = safe_exp([HexInt(x) for x in data])
    prog_arr = progmem_array(config[CONF_RAW_DATA_ID], rhs)

    rhs = App.make_image(prog_arr, width, height)
    Pvariable(config[CONF_ID], rhs)
Exemple #8
0
def update_pi4home_core_repo():
    if CONF_REPOSITORY not in CORE.pi4home_core_version:
        return

    if CONF_BRANCH not in CORE.pi4home_core_version:
        # Git commit hash or tag cannot be updated
        return

    pi4home_core_path = CORE.relative_piolibdeps_path('pi4home-core')

    rc, _, _ = run_system_command('git', '-C', pi4home_core_path, '--help')
    if rc != 0:
        # git not installed or repo not downloaded yet
        return
    rc, _, _ = run_system_command('git', '-C', pi4home_core_path, 'diff-index',
                                  '--quiet', 'HEAD', '--')
    if rc != 0:
        # local changes, cannot update
        _LOGGER.warning(
            "Local changes in pi4home-core copy from git. Will not auto-update."
        )
        return
    _LOGGER.info("Updating pi4home-core copy from git (%s)", pi4home_core_path)
    rc, stdout, _ = run_system_command('git', '-c', 'color.ui=always', '-C',
                                       pi4home_core_path, 'pull', '--stat')
    if rc != 0:
        _LOGGER.warning("Couldn't auto-update local git copy of pi4home-core.")
        return
    if IS_PY3:
        stdout = stdout.decode('utf-8', 'backslashreplace')
    safe_print(stdout.strip())
Exemple #9
0
def write_cpp(code_s):
    path = CORE.relative_build_path('src', 'main.cpp')
    if os.path.isfile(path):
        try:
            with codecs.open(path, 'r', encoding='utf-8') as f_handle:
                text = f_handle.read()
        except OSError:
            raise pi4homeError(u"Could not read C++ file at {}".format(path))
        prev_file = text
        code_format = find_begin_end(text, CPP_AUTO_GENERATE_BEGIN,
                                     CPP_AUTO_GENERATE_END)
        code_format_ = find_begin_end(code_format[0], CPP_INCLUDE_BEGIN,
                                      CPP_INCLUDE_END)
        code_format = (code_format_[0], code_format_[1], code_format[1])
    else:
        prev_file = None
        mkdir_p(os.path.dirname(path))
        code_format = CPP_BASE_FORMAT

    include_s = get_include_text()

    full_file = code_format[
        0] + CPP_INCLUDE_BEGIN + u'\n' + include_s + CPP_INCLUDE_END
    full_file += code_format[
        1] + CPP_AUTO_GENERATE_BEGIN + u'\n' + code_s + CPP_AUTO_GENERATE_END
    full_file += code_format[2]
    if prev_file == full_file:
        return
    with codecs.open(path, 'w+', encoding='utf-8') as f_handle:
        f_handle.write(full_file)
Exemple #10
0
def write_platformio_project():
    mkdir_p(CORE.build_path)

    platformio_ini = CORE.relative_build_path('platformio.ini')
    content = get_ini_content()
    write_gitignore()
    write_platformio_ini(content, platformio_ini)
Exemple #11
0
def get_ini_content():
    lib_deps = gather_lib_deps()
    build_flags = gather_build_flags()

    data = {
        'platform': CORE.arduino_version,
        'board': CORE.board,
        'framework': 'arduino',
        'lib_deps': lib_deps + ['${common.lib_deps}'],
        'build_flags': build_flags + ['${common.build_flags}'],
        'upload_speed': UPLOAD_SPEED_OVERRIDE.get(CORE.board, 115200),
    }

    if CORE.is_esp32:
        data['board_build.partitions'] = "partitions.csv"
        partitions_csv = CORE.relative_build_path('partitions.csv')
        if not os.path.isfile(partitions_csv):
            with open(partitions_csv, "w") as f:
                f.write("nvs,      data, nvs,     0x009000, 0x005000,\n")
                f.write("otadata,  data, ota,     0x00e000, 0x002000,\n")
                f.write("app0,     app,  ota_0,   0x010000, 0x190000,\n")
                f.write("app1,     app,  ota_1,   0x200000, 0x190000,\n")
                f.write("eeprom,   data, 0x99,    0x390000, 0x001000,\n")
                f.write("spiffs,   data, spiffs,  0x391000, 0x00F000\n")

    if CONF_BOARD_FLASH_MODE in CORE.config[CONF_PI4HOME]:
        flash_mode = CORE.config[CONF_PI4HOME][CONF_BOARD_FLASH_MODE]
        data['board_build.flash_mode'] = flash_mode

    if not CORE.config[CONF_PI4HOME][CONF_USE_CUSTOM_CODE]:
        # Ignore libraries that are not explicitly used, but may
        # be added by LDF
        data['lib_ldf_mode'] = 'chain'
        REMOVABLE_LIBRARIES = [
            'ArduinoOTA',
            'Update',
            'Wire',
            'FastLED',
            'NeoPixelBus',
            'ESP Async WebServer',
            'AsyncMqttClient',
            'AsyncTCP',
            'ESPAsyncTCP',
        ]
        ignore = []
        for x in REMOVABLE_LIBRARIES:
            for o in lib_deps:
                if o.startswith(x):
                    break
            else:
                ignore.append(x)
        if ignore:
            data['lib_ignore'] = ignore

    data.update(CORE.config[CONF_PI4HOME].get(CONF_PLATFORMIO_OPTIONS, {}))

    content = u'[env:{}]\n'.format(CORE.name)
    content += format_ini(data)

    return content
Exemple #12
0
def process_lambda(
        value,  # type: Lambda
        parameters,  # type: List[Tuple[Expression, str]]
        capture='=',  # type: str
        return_type=None  # type: Optional[Expression]
):
    # type: (...) -> Generator[LambdaExpression]
    from pi4home.components.globals import GlobalVariableComponent

    if value is None:
        yield
        return
    parts = value.parts[:]
    for i, id in enumerate(value.requires_ids):
        for full_id, var in CORE.get_variable_with_full_id(id):
            yield
        if full_id is not None and isinstance(full_id.type, MockObjClass) and \
                full_id.type.inherits_from(GlobalVariableComponent):
            parts[i * 3 + 1] = var.value()
            continue

        if parts[i * 3 + 2] == '.':
            parts[i * 3 + 1] = var._
        else:
            parts[i * 3 + 1] = var
        parts[i * 3 + 2] = ''
    yield LambdaExpression(parts, parameters, capture, return_type)
Exemple #13
0
def gather_lib_deps():
    lib_deps = set()
    if CONF_REPOSITORY in CORE.pi4home_core_version:
        repo = CORE.pi4home_core_version[CONF_REPOSITORY]
        ref = next((CORE.pi4home_core_version[x]
                    for x in (CONF_COMMIT, CONF_BRANCH, CONF_TAG)
                    if x in CORE.pi4home_core_version), None)
        if CONF_TAG in CORE.pi4home_core_version and repo == LIBRARY_URI_REPO:
            this_version = GITHUB_ARCHIVE_ZIP.format(ref)
        elif ref is not None:
            this_version = repo + '#' + ref
        lib_deps.add(this_version)
    elif CORE.is_local_pi4home_core_copy:
        src_path = CORE.relative_path(CORE.pi4home_core_version[CONF_LOCAL])
        # Manually add lib_deps because platformio seems to ignore them inside libs/
        library_json_path = os.path.join(src_path, 'library.json')
        with codecs.open(library_json_path, 'r', encoding='utf-8') as f_handle:
            library_json_text = f_handle.read()

        library_json = json.loads(library_json_text)
        for dep in library_json.get('dependencies', []):
            if 'version' in dep and VERSION_REGEX.match(
                    dep['version']) is not None:
                lib_deps.add(dep['name'] + '@' + dep['version'])
            else:
                lib_deps.add(dep['version'])
    else:
        lib_deps.add(CORE.pi4home_core_version)

    lib_deps |= get_build_flags('LIB_DEPS')
    lib_deps |= get_build_flags('lib_deps')
    if CORE.is_esp32:
        lib_deps |= {
            'Preferences',  # Preferences helper
            '[email protected]',  # Pin AsyncTCP version
        }

        # Manual fix for AsyncTCP
        if CORE.arduino_version == ARDUINO_VERSION_ESP32_1_0_0:
            lib_deps.discard('[email protected]')
            lib_deps.add('[email protected]')
        lib_deps.add('ESPmDNS')
    elif CORE.is_esp8266:
        lib_deps.add('[email protected]')
        lib_deps.add('ESP8266mDNS')

    # avoid changing build flags order
    lib_deps_l = list(lib_deps)
    lib_deps_l.sort()

    # Move AsyncTCP to front, see https://github.com/platformio/platformio-core/issues/2115
    if '[email protected]' in lib_deps_l:
        lib_deps_l.insert(0,
                          lib_deps_l.pop(lib_deps_l.index('[email protected]')))
    if '[email protected]' in lib_deps_l:
        lib_deps_l.insert(0,
                          lib_deps_l.pop(lib_deps_l.index('[email protected]')))

    return lib_deps_l
Exemple #14
0
def validate_local_pi4home_core_version(value):
    value = cv.directory(value)
    path = CORE.relative_path(value)
    library_json = os.path.join(path, 'library.json')
    if not os.path.exists(library_json):
        raise vol.Invalid(u"Could not find '{}' file. '{}' does not seem to point to an "
                          u"pi4home-core copy.".format(library_json, value))
    return value
Exemple #15
0
def file_(value):
    value = string(value)
    path = CORE.relative_path(value)
    if not os.path.exists(path):
        raise vol.Invalid(u"Could not find file '{}'. Please make sure it exists.".format(
            path))
    if not os.path.isfile(path):
        raise vol.Invalid(u"Path '{}' is not a file.".format(path))
    return value
Exemple #16
0
def directory(value):
    value = string(value)
    path = CORE.relative_path(value)
    if not os.path.exists(path):
        raise vol.Invalid(u"Could not find directory '{}'. Please make sure it exists.".format(
            path))
    if not os.path.isdir(path):
        raise vol.Invalid(u"Path '{}' is not a directory.".format(path))
    return value
Exemple #17
0
def Pvariable(
        id,  # type: ID
        rhs,  # type: Expression
        has_side_effects=True,  # type: bool
        type=None  # type: MockObj
):
    # type: (...) -> MockObj
    rhs = safe_exp(rhs)
    if not has_side_effects and hasattr(rhs, '_has_side_effects'):
        # pylint: disable=attribute-defined-outside-init, protected-access
        rhs._has_side_effects = False
    obj = MockObj(id, u'->', has_side_effects=has_side_effects)
    id.type = type or id.type
    assignment = AssignmentExpression(id.type, '*', id, rhs, obj)
    CORE.add(assignment)
    CORE.register_variable(id, obj)
    obj.requires.append(assignment)
    return obj
Exemple #18
0
def symlink_pi4home_core_version(pi4home_core_version):
    lib_path = CORE.relative_build_path('lib')
    dst_path = CORE.relative_build_path('lib', 'pi4home-core')
    if CORE.is_local_pi4home_core_copy:
        src_path = CORE.relative_path(pi4home_core_version[CONF_LOCAL])
        do_write = True
        if os.path.islink(dst_path):
            old_path = os.path.join(os.readlink(dst_path), lib_path)
            if old_path != lib_path:
                os.unlink(dst_path)
            else:
                do_write = False
        if do_write:
            mkdir_p(lib_path)
            symlink(src_path, dst_path)
    else:
        # Remove symlink when changing back from local version
        if os.path.islink(dst_path):
            os.unlink(dst_path)
Exemple #19
0
def generic_gpio_pin_expression_(conf, mock_obj, default_mode):
    if conf is None:
        return
    number = conf[CONF_NUMBER]
    inverted = conf.get(CONF_INVERTED)
    if CONF_PCF8574 in conf:
        from pi4home.components import pcf8574

        for hub in CORE.get_variable(conf[CONF_PCF8574]):
            yield None

        if default_mode == u'INPUT':
            mode = pcf8574.PCF8675_GPIO_MODES[conf.get(CONF_MODE, u'INPUT')]
            yield hub.make_input_pin(number, mode, inverted)
            return
        if default_mode == u'OUTPUT':
            yield hub.make_output_pin(number, inverted)
            return

        raise pi4homeError(u"Unknown default mode {}".format(default_mode))
    if CONF_MCP23017 in conf:
        from pi4home.components import mcp23017

        for hub in CORE.get_variable(conf[CONF_MCP23017]):
            yield None

        if default_mode == u'INPUT':
            mode = mcp23017.MCP23017_GPIO_MODES[conf.get(CONF_MODE, u'INPUT')]
            yield hub.make_input_pin(number, mode, inverted)
            return
        if default_mode == u'OUTPUT':
            yield hub.make_output_pin(number, inverted)
            return

        raise pi4homeError(u"Unknown default mode {}".format(default_mode))
    if len(conf) == 1:
        yield IntLiteral(number)
        return
    mode = RawExpression(conf.get(CONF_MODE, default_mode))
    yield mock_obj(number, mode, inverted)
Exemple #20
0
def migrate_src_version_0_to_1():
    main_cpp = CORE.relative_build_path('src', 'main.cpp')
    if not os.path.isfile(main_cpp):
        return

    with codecs.open(main_cpp, 'r', encoding='utf-8') as f_handle:
        content = orig_content = f_handle.read()

    if CPP_INCLUDE_BEGIN in content:
        return

    content, count = replace_file_content(content, r'\s*delay\((?:16|20)\);',
                                          '')
    if count != 0:
        _LOGGER.info("Migration: Removed %s occurrence of 'delay(16);' in %s",
                     count, main_cpp)

    content, count = replace_file_content(content,
                                          r'using namespace pi4homelib;', '')
    if count != 0:
        _LOGGER.info(
            "Migration: Removed %s occurrence of 'using namespace pi4homelib;' "
            "in %s", count, main_cpp)

    if CPP_INCLUDE_BEGIN not in content:
        content, count = replace_file_content(
            content, r'#include "pi4homelib/application.h"',
            CPP_INCLUDE_BEGIN + u'\n' + CPP_INCLUDE_END)
        if count == 0:
            _LOGGER.error(
                "Migration failed. PI4Home 1.10.0 needs to have a new auto-generated "
                "include section in the %s file. Please remove %s and let it be "
                "auto-generated again.", main_cpp, main_cpp)
        _LOGGER.info("Migration: Added include section to %s", main_cpp)

    if orig_content == content:
        return
    with codecs.open(main_cpp, 'w', encoding='utf-8') as f_handle:
        f_handle.write(content)
Exemple #21
0
def preload_core_config(config):
    if 'pi4homeyaml' in config:
        _LOGGER.warning("The pi4homeyaml section has been renamed to pi4home in 1.11.0. "
                        "Please replace 'pi4homeyaml:' in your configuration with 'pi4home:'.")
        config[CONF_PI4HOME] = config.pop('pi4homeyaml')
    if CONF_PI4HOME not in config:
        raise pi4homeError(u"No pi4home section in config")
    core_conf = config[CONF_PI4HOME]
    if CONF_PLATFORM not in core_conf:
        raise pi4homeError("pi4home.platform not specified.")
    if CONF_BOARD not in core_conf:
        raise pi4homeError("pi4home.board not specified.")
    if CONF_NAME not in core_conf:
        raise pi4homeError("pi4home.name not specified.")

    try:
        CORE.esp_platform = validate_platform(core_conf[CONF_PLATFORM])
        CORE.board = validate_board(core_conf[CONF_BOARD])
        CORE.name = cv.valid_name(core_conf[CONF_NAME])
        CORE.build_path = CORE.relative_path(
            cv.string(core_conf.get(CONF_BUILD_PATH, default_build_path())))
    except vol.Invalid as e:
        raise pi4homeError(text_type(e))
Exemple #22
0
def add(
        expression,  # type: Union[Expression, Statement]
        require=True  # type: bool
):
    # type: (...) -> None
    CORE.add(expression, require=require)
Exemple #23
0
def get_variable(id):  # type: (ID) -> Generator[MockObj]
    for var in CORE.get_variable(id):
        yield None
    yield var
Exemple #24
0
def setup_stepper(stepper_var, config):
    CORE.add_job(setup_stepper_core_, stepper_var, config)
Exemple #25
0
def setup_fan(fan_obj, config):
    fan_var = Pvariable(config[CONF_ID], fan_obj, has_side_effects=False)
    CORE.add_job(setup_fan_core_, fan_var, config)
Exemple #26
0
def setup_output_platform(obj, config, skip_power_supply=False):
    CORE.add_job(setup_output_platform_, obj, config, skip_power_supply)
Exemple #27
0
def setup_time(time_var, config):
    CORE.add_job(setup_time_core_, time_var, config)
Exemple #28
0
def setup_light(light_obj, config):
    light_var = Pvariable(config[CONF_ID], light_obj, has_side_effects=False)
    CORE.add_job(setup_light_core_, light_var, config)
Exemple #29
0
def register_output(var, config):
    output_var = Pvariable(config[CONF_ID], var, has_side_effects=True)
    CORE.add_job(setup_output_platform_, output_var, config)
Exemple #30
0
def storage_path():  # type: () -> str
    return CORE.relative_path('.pi4home',
                              '{}.json'.format(CORE.config_filename))