From ab25e32509798e8564bad1b556f8ad552f13880f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Trzci=C5=84ski?= Date: Wed, 20 Dec 2023 11:33:05 +0100 Subject: [PATCH 1/3] image: allow the image to by auto-loaded by animation (#5139) --- esphome/__main__.py | 4 ++-- esphome/components/image/__init__.py | 1 + esphome/config.py | 20 ++++++++++++++++++-- esphome/loader.py | 4 ++++ esphome/writer.py | 8 ++++---- tests/test8.yaml | 10 ++++++++++ 6 files changed, 39 insertions(+), 8 deletions(-) diff --git a/esphome/__main__.py b/esphome/__main__.py index 0796dead43..baa5ecde47 100644 --- a/esphome/__main__.py +++ b/esphome/__main__.py @@ -12,7 +12,7 @@ import argcomplete from esphome import const, writer, yaml_util import esphome.codegen as cg -from esphome.config import iter_components, read_config, strip_default_ids +from esphome.config import iter_component_configs, read_config, strip_default_ids from esphome.const import ( ALLOWED_NAME_CHARS, CONF_BAUD_RATE, @@ -196,7 +196,7 @@ def write_cpp(config): def generate_cpp_contents(config): _LOGGER.info("Generating C++ source...") - for name, component, conf in iter_components(CORE.config): + for name, component, conf in iter_component_configs(CORE.config): if component.to_code is not None: coro = wrap_to_code(name, component) CORE.add_job(coro, conf) diff --git a/esphome/components/image/__init__.py b/esphome/components/image/__init__.py index c11021fc9c..73dc73aa45 100644 --- a/esphome/components/image/__init__.py +++ b/esphome/components/image/__init__.py @@ -36,6 +36,7 @@ _LOGGER = logging.getLogger(__name__) DOMAIN = "image" DEPENDENCIES = ["display"] MULTI_CONF = True +MULTI_CONF_NO_DEFAULT = True image_ns = cg.esphome_ns.namespace("image") diff --git a/esphome/config.py b/esphome/config.py index 745883c2ef..e9433d537e 100644 --- a/esphome/config.py +++ b/esphome/config.py @@ -39,6 +39,17 @@ _LOGGER = logging.getLogger(__name__) def iter_components(config): + for domain, conf in config.items(): + component = get_component(domain) + yield domain, component + if component.is_platform_component: + for p_config in conf: + p_name = f"{domain}.{p_config[CONF_PLATFORM]}" + platform = get_platform(domain, p_config[CONF_PLATFORM]) + yield p_name, platform + + +def iter_component_configs(config): for domain, conf in config.items(): component = get_component(domain) if component.multi_conf: @@ -303,8 +314,10 @@ class LoadValidationStep(ConfigValidationStep): # Ignore top-level keys starting with a dot return result.add_output_path([self.domain], self.domain) - result[self.domain] = self.conf component = get_component(self.domain) + if component.multi_conf_no_default and isinstance(self.conf, core.AutoLoad): + self.conf = [] + result[self.domain] = self.conf path = [self.domain] if component is None: result.add_str_error(f"Component not found: {self.domain}", path) @@ -424,7 +437,10 @@ class MetadataValidationStep(ConfigValidationStep): def run(self, result: Config) -> None: if self.conf is None: - result[self.domain] = self.conf = {} + if self.comp.multi_conf and self.comp.multi_conf_no_default: + result[self.domain] = self.conf = [] + else: + result[self.domain] = self.conf = {} success = True for dependency in self.comp.dependencies: diff --git a/esphome/loader.py b/esphome/loader.py index cd21e5a509..40a38d0a14 100644 --- a/esphome/loader.py +++ b/esphome/loader.py @@ -57,6 +57,10 @@ class ComponentManifest: def multi_conf(self) -> bool: return getattr(self.module, "MULTI_CONF", False) + @property + def multi_conf_no_default(self) -> bool: + return getattr(self.module, "MULTI_CONF_NO_DEFAULT", False) + @property def to_code(self) -> Optional[Callable[[Any], None]]: return getattr(self.module, "to_code", None) diff --git a/esphome/writer.py b/esphome/writer.py index 83e95614a6..3ad0e60d31 100644 --- a/esphome/writer.py +++ b/esphome/writer.py @@ -4,7 +4,7 @@ import re from pathlib import Path from typing import Union -from esphome.config import iter_components +from esphome.config import iter_components, iter_component_configs from esphome.const import ( HEADER_FILE_EXTENSIONS, SOURCE_FILE_EXTENSIONS, @@ -70,14 +70,14 @@ UPLOAD_SPEED_OVERRIDE = { def get_flags(key): flags = set() - for _, component, conf in iter_components(CORE.config): + for _, component, conf in iter_component_configs(CORE.config): flags |= getattr(component, key)(conf) return flags def get_include_text(): include_text = '#include "esphome.h"\nusing namespace esphome;\n' - for _, component, conf in iter_components(CORE.config): + for _, component, conf in iter_component_configs(CORE.config): if not hasattr(component, "includes"): continue includes = component.includes @@ -232,7 +232,7 @@ the custom_components folder or the external_components feature. def copy_src_tree(): source_files: list[loader.FileResource] = [] - for _, component, _ in iter_components(CORE.config): + for _, component in iter_components(CORE.config): source_files += component.resources source_files_map = { Path(x.package.replace(".", "/") + "/" + x.resource): x for x in source_files diff --git a/tests/test8.yaml b/tests/test8.yaml index 558e86e1f9..fafdb76e12 100644 --- a/tests/test8.yaml +++ b/tests/test8.yaml @@ -92,3 +92,13 @@ sensor: name: "Loop Time" psram: name: "PSRAM Free" + +# Purposely test that `animation:` does auto-load `image:` +# Keep the `image:` undefined. +# image: + +animation: + - id: rgb565_animation + file: pnglogo.png + type: RGB565 + use_transparency: no From 6a9e85438f95b14be54bdf9a961897bdd78224b9 Mon Sep 17 00:00:00 2001 From: Jesse Hills <3060199+jesserockz@users.noreply.github.com> Date: Wed, 20 Dec 2023 19:42:27 +0900 Subject: [PATCH 2/3] Fix pin reuse error with pin expanders (#5973) Co-authored-by: Clyde Stubbs <2366188+clydebarrow@users.noreply.github.com> --- esphome/pins.py | 20 +++++++++++--------- tests/test1.yaml | 4 ---- tests/test3.1.yaml | 2 -- 3 files changed, 11 insertions(+), 15 deletions(-) diff --git a/esphome/pins.py b/esphome/pins.py index e2fd8e98e2..87f7084d4f 100644 --- a/esphome/pins.py +++ b/esphome/pins.py @@ -1,7 +1,7 @@ import operator from functools import reduce import esphome.config_validation as cv -from esphome.core import CORE, ID +from esphome.core import CORE from esphome.const import ( CONF_INPUT, @@ -25,15 +25,16 @@ class PinRegistry(dict): def reset(self): self.pins_used = {} - def get_count(self, key, number): + def get_count(self, key, id, number): """ Get the number of places a given pin is used. - :param key: The ID of the defining component + :param key: The key of the registered pin schema. + :param id: The ID of the defining component :param number: The pin number :return: The number of places the pin is used. """ - pin_key = (key, number) - return self.pins_used[pin_key] if pin_key in self.pins_used else 0 + pin_key = (key, id, number) + return len(self.pins_used[pin_key]) if pin_key in self.pins_used else 0 def register(self, name, schema, final_validate=None): """ @@ -65,9 +66,10 @@ class PinRegistry(dict): result = self[key][1](conf) if CONF_NUMBER in result: # key maps to the pin schema - if isinstance(key, ID): - key = key.id - pin_key = (key, result[CONF_NUMBER]) + if key != CORE.target_platform: + pin_key = (key, conf[key], result[CONF_NUMBER]) + else: + pin_key = (key, key, result[CONF_NUMBER]) if pin_key not in self.pins_used: self.pins_used[pin_key] = [] # client_id identifies the instance of the providing component @@ -101,7 +103,7 @@ class PinRegistry(dict): Run the final validation for all pins, and check for reuse :param fconf: The full config """ - for (key, _), pin_list in self.pins_used.items(): + for (key, _, _), pin_list in self.pins_used.items(): count = len(pin_list) # number of places same pin used. final_val_fun = self[key][2] # final validation function for pin_path, client_id, pin_config in pin_list: diff --git a/tests/test1.yaml b/tests/test1.yaml index f7b433cce2..3f8ae0151f 100644 --- a/tests/test1.yaml +++ b/tests/test1.yaml @@ -1667,7 +1667,6 @@ binary_sensor: mcp23xxx: mcp23s08_hub # Use pin number 1 number: 1 - allow_other_uses: true # One of INPUT or INPUT_PULLUP mode: INPUT_PULLUP inverted: false @@ -2149,7 +2148,6 @@ output: pin: mcp23xxx: mcp23017_hub number: 0 - allow_other_uses: true mode: OUTPUT inverted: false - platform: gpio @@ -2157,7 +2155,6 @@ output: pin: mcp23xxx: mcp23008_hub number: 0 - allow_other_uses: true mode: OUTPUT inverted: false - platform: gpio @@ -2597,7 +2594,6 @@ switch: mcp23xxx: mcp23s08_hub # Use pin number 0 number: 0 - allow_other_uses: true mode: OUTPUT inverted: false - platform: gpio diff --git a/tests/test3.1.yaml b/tests/test3.1.yaml index 63ef4e8ce0..b5428abbfa 100644 --- a/tests/test3.1.yaml +++ b/tests/test3.1.yaml @@ -401,7 +401,6 @@ switch: pin: mcp23xxx: mcp23017_hub number: 0 - allow_other_uses: true mode: OUTPUT interlock: &interlock [gpio_switch1, gpio_switch2, gpio_switch3] - platform: gpio @@ -409,7 +408,6 @@ switch: pin: mcp23xxx: mcp23008_hub number: 0 - allow_other_uses: true mode: OUTPUT interlock: *interlock - platform: gpio From bec1ad9396806086f61eae699ca6d3d8210682a7 Mon Sep 17 00:00:00 2001 From: Jesse Hills <3060199+jesserockz@users.noreply.github.com> Date: Wed, 20 Dec 2023 20:59:46 +0900 Subject: [PATCH 3/3] Bump version to 2023.12.0b6 --- esphome/const.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/esphome/const.py b/esphome/const.py index 0df9c27233..475f28c35e 100644 --- a/esphome/const.py +++ b/esphome/const.py @@ -1,6 +1,6 @@ """Constants used by esphome.""" -__version__ = "2023.12.0b5" +__version__ = "2023.12.0b6" ALLOWED_NAME_CHARS = "abcdefghijklmnopqrstuvwxyz0123456789-_" VALID_SUBSTITUTIONS_CHARACTERS = (