mirror of
https://github.com/esphome/esphome.git
synced 2025-11-07 02:21:51 +00:00
Compare commits
28 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ddff92c88b | ||
|
|
ed9fd173a9 | ||
|
|
175f00f41b | ||
|
|
676b37e6b0 | ||
|
|
28a3cddde3 | ||
|
|
687f5ca633 | ||
|
|
ff97639f79 | ||
|
|
8e4b9c3c1e | ||
|
|
1aa49c8956 | ||
|
|
711faab329 | ||
|
|
1204b4f1bd | ||
|
|
cadbf7463e | ||
|
|
3f40e32eba | ||
|
|
b421fccc08 | ||
|
|
10ca05b686 | ||
|
|
d0ac202a3f | ||
|
|
1c4b06700f | ||
|
|
47d42afda3 | ||
|
|
1a9f66e630 | ||
|
|
8fb6b8f1a2 | ||
|
|
22eef036c7 | ||
|
|
625ce2b8eb | ||
|
|
e5e3b253bc | ||
|
|
c369443263 | ||
|
|
1e061582d3 | ||
|
|
445b13dbc6 | ||
|
|
255483de63 | ||
|
|
4ac49907ca |
@@ -38,6 +38,7 @@ RUN \
|
|||||||
openssh-client=1:9.2p1-2+deb12u1 \
|
openssh-client=1:9.2p1-2+deb12u1 \
|
||||||
python3-cffi=1.15.1-5 \
|
python3-cffi=1.15.1-5 \
|
||||||
libcairo2=1.16.0-7 \
|
libcairo2=1.16.0-7 \
|
||||||
|
libmagic1=1:5.44-3 \
|
||||||
patch=2.7.6-7; \
|
patch=2.7.6-7; \
|
||||||
if [ "$TARGETARCH$TARGETVARIANT" = "armv7" ]; then \
|
if [ "$TARGETARCH$TARGETVARIANT" = "armv7" ]; then \
|
||||||
apt-get install -y --no-install-recommends \
|
apt-get install -y --no-install-recommends \
|
||||||
@@ -48,6 +49,8 @@ RUN \
|
|||||||
libfreetype-dev=2.12.1+dfsg-5 \
|
libfreetype-dev=2.12.1+dfsg-5 \
|
||||||
libssl-dev=3.0.11-1~deb12u2 \
|
libssl-dev=3.0.11-1~deb12u2 \
|
||||||
libffi-dev=3.4.4-1 \
|
libffi-dev=3.4.4-1 \
|
||||||
|
libopenjp2-7=2.5.0-2 \
|
||||||
|
libtiff6=4.5.0-6 \
|
||||||
cargo=0.66.0+ds1-1 \
|
cargo=0.66.0+ds1-1 \
|
||||||
pkg-config=1.8.1-1 \
|
pkg-config=1.8.1-1 \
|
||||||
gcc-arm-linux-gnueabihf=4:12.2.0-3; \
|
gcc-arm-linux-gnueabihf=4:12.2.0-3; \
|
||||||
@@ -68,7 +71,7 @@ ENV \
|
|||||||
# See: https://unix.stackexchange.com/questions/553743/correct-way-to-add-lib-ld-linux-so-3-in-debian
|
# See: https://unix.stackexchange.com/questions/553743/correct-way-to-add-lib-ld-linux-so-3-in-debian
|
||||||
RUN \
|
RUN \
|
||||||
if [ "$TARGETARCH$TARGETVARIANT" = "armv7" ]; then \
|
if [ "$TARGETARCH$TARGETVARIANT" = "armv7" ]; then \
|
||||||
ln -s /lib/arm-linux-gnueabihf/ld-linux.so.3 /lib/ld-linux.so.3; \
|
ln -s /lib/arm-linux-gnueabihf/ld-linux-armhf.so.3 /lib/ld-linux.so.3; \
|
||||||
fi
|
fi
|
||||||
|
|
||||||
RUN \
|
RUN \
|
||||||
|
|||||||
@@ -3,23 +3,26 @@ from typing import Union, Optional
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
|
import esphome.final_validate as fv
|
||||||
|
|
||||||
from esphome.helpers import copy_file_if_changed, write_file_if_changed, mkdir_p
|
from esphome.helpers import copy_file_if_changed, write_file_if_changed, mkdir_p
|
||||||
from esphome.const import (
|
from esphome.const import (
|
||||||
|
CONF_ADVANCED,
|
||||||
CONF_BOARD,
|
CONF_BOARD,
|
||||||
CONF_COMPONENTS,
|
CONF_COMPONENTS,
|
||||||
|
CONF_ESPHOME,
|
||||||
CONF_FRAMEWORK,
|
CONF_FRAMEWORK,
|
||||||
|
CONF_IGNORE_EFUSE_MAC_CRC,
|
||||||
CONF_NAME,
|
CONF_NAME,
|
||||||
|
CONF_PATH,
|
||||||
|
CONF_PLATFORMIO_OPTIONS,
|
||||||
|
CONF_REF,
|
||||||
|
CONF_REFRESH,
|
||||||
CONF_SOURCE,
|
CONF_SOURCE,
|
||||||
CONF_TYPE,
|
CONF_TYPE,
|
||||||
|
CONF_URL,
|
||||||
CONF_VARIANT,
|
CONF_VARIANT,
|
||||||
CONF_VERSION,
|
CONF_VERSION,
|
||||||
CONF_ADVANCED,
|
|
||||||
CONF_REFRESH,
|
|
||||||
CONF_PATH,
|
|
||||||
CONF_URL,
|
|
||||||
CONF_REF,
|
|
||||||
CONF_IGNORE_EFUSE_MAC_CRC,
|
|
||||||
KEY_CORE,
|
KEY_CORE,
|
||||||
KEY_FRAMEWORK_VERSION,
|
KEY_FRAMEWORK_VERSION,
|
||||||
KEY_NAME,
|
KEY_NAME,
|
||||||
@@ -327,6 +330,32 @@ def _detect_variant(value):
|
|||||||
return value
|
return value
|
||||||
|
|
||||||
|
|
||||||
|
def final_validate(config):
|
||||||
|
if CONF_PLATFORMIO_OPTIONS not in fv.full_config.get()[CONF_ESPHOME]:
|
||||||
|
return config
|
||||||
|
|
||||||
|
pio_flash_size_key = "board_upload.flash_size"
|
||||||
|
pio_partitions_key = "board_build.partitions"
|
||||||
|
if (
|
||||||
|
CONF_PARTITIONS in config
|
||||||
|
and pio_partitions_key
|
||||||
|
in fv.full_config.get()[CONF_ESPHOME][CONF_PLATFORMIO_OPTIONS]
|
||||||
|
):
|
||||||
|
raise cv.Invalid(
|
||||||
|
f"Do not specify '{pio_partitions_key}' in '{CONF_PLATFORMIO_OPTIONS}' with '{CONF_PARTITIONS}' in esp32"
|
||||||
|
)
|
||||||
|
|
||||||
|
if (
|
||||||
|
pio_flash_size_key
|
||||||
|
in fv.full_config.get()[CONF_ESPHOME][CONF_PLATFORMIO_OPTIONS]
|
||||||
|
):
|
||||||
|
raise cv.Invalid(
|
||||||
|
f"Please specify {CONF_FLASH_SIZE} within esp32 configuration only"
|
||||||
|
)
|
||||||
|
|
||||||
|
return config
|
||||||
|
|
||||||
|
|
||||||
CONF_PLATFORM_VERSION = "platform_version"
|
CONF_PLATFORM_VERSION = "platform_version"
|
||||||
|
|
||||||
ARDUINO_FRAMEWORK_SCHEMA = cv.All(
|
ARDUINO_FRAMEWORK_SCHEMA = cv.All(
|
||||||
@@ -387,6 +416,7 @@ FRAMEWORK_SCHEMA = cv.typed_schema(
|
|||||||
|
|
||||||
|
|
||||||
FLASH_SIZES = [
|
FLASH_SIZES = [
|
||||||
|
"2MB",
|
||||||
"4MB",
|
"4MB",
|
||||||
"8MB",
|
"8MB",
|
||||||
"16MB",
|
"16MB",
|
||||||
@@ -394,6 +424,7 @@ FLASH_SIZES = [
|
|||||||
]
|
]
|
||||||
|
|
||||||
CONF_FLASH_SIZE = "flash_size"
|
CONF_FLASH_SIZE = "flash_size"
|
||||||
|
CONF_PARTITIONS = "partitions"
|
||||||
CONFIG_SCHEMA = cv.All(
|
CONFIG_SCHEMA = cv.All(
|
||||||
cv.Schema(
|
cv.Schema(
|
||||||
{
|
{
|
||||||
@@ -401,6 +432,7 @@ CONFIG_SCHEMA = cv.All(
|
|||||||
cv.Optional(CONF_FLASH_SIZE, default="4MB"): cv.one_of(
|
cv.Optional(CONF_FLASH_SIZE, default="4MB"): cv.one_of(
|
||||||
*FLASH_SIZES, upper=True
|
*FLASH_SIZES, upper=True
|
||||||
),
|
),
|
||||||
|
cv.Optional(CONF_PARTITIONS): cv.file_,
|
||||||
cv.Optional(CONF_VARIANT): cv.one_of(*VARIANTS, upper=True),
|
cv.Optional(CONF_VARIANT): cv.one_of(*VARIANTS, upper=True),
|
||||||
cv.Optional(CONF_FRAMEWORK, default={}): FRAMEWORK_SCHEMA,
|
cv.Optional(CONF_FRAMEWORK, default={}): FRAMEWORK_SCHEMA,
|
||||||
}
|
}
|
||||||
@@ -410,6 +442,9 @@ CONFIG_SCHEMA = cv.All(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
FINAL_VALIDATE_SCHEMA = cv.Schema(final_validate)
|
||||||
|
|
||||||
|
|
||||||
async def to_code(config):
|
async def to_code(config):
|
||||||
cg.add_platformio_option("board", config[CONF_BOARD])
|
cg.add_platformio_option("board", config[CONF_BOARD])
|
||||||
cg.add_platformio_option("board_upload.flash_size", config[CONF_FLASH_SIZE])
|
cg.add_platformio_option("board_upload.flash_size", config[CONF_FLASH_SIZE])
|
||||||
@@ -462,7 +497,10 @@ async def to_code(config):
|
|||||||
add_idf_sdkconfig_option("CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU0", False)
|
add_idf_sdkconfig_option("CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU0", False)
|
||||||
add_idf_sdkconfig_option("CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU1", False)
|
add_idf_sdkconfig_option("CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU1", False)
|
||||||
|
|
||||||
cg.add_platformio_option("board_build.partitions", "partitions.csv")
|
if CONF_PARTITIONS in config:
|
||||||
|
cg.add_platformio_option("board_build.partitions", config[CONF_PARTITIONS])
|
||||||
|
else:
|
||||||
|
cg.add_platformio_option("board_build.partitions", "partitions.csv")
|
||||||
|
|
||||||
for name, value in conf[CONF_SDKCONFIG_OPTIONS].items():
|
for name, value in conf[CONF_SDKCONFIG_OPTIONS].items():
|
||||||
add_idf_sdkconfig_option(name, RawSdkconfigValue(value))
|
add_idf_sdkconfig_option(name, RawSdkconfigValue(value))
|
||||||
@@ -507,7 +545,10 @@ async def to_code(config):
|
|||||||
[f"platformio/framework-arduinoespressif32@{conf[CONF_SOURCE]}"],
|
[f"platformio/framework-arduinoespressif32@{conf[CONF_SOURCE]}"],
|
||||||
)
|
)
|
||||||
|
|
||||||
cg.add_platformio_option("board_build.partitions", "partitions.csv")
|
if CONF_PARTITIONS in config:
|
||||||
|
cg.add_platformio_option("board_build.partitions", config[CONF_PARTITIONS])
|
||||||
|
else:
|
||||||
|
cg.add_platformio_option("board_build.partitions", "partitions.csv")
|
||||||
|
|
||||||
cg.add_define(
|
cg.add_define(
|
||||||
"USE_ARDUINO_VERSION_CODE",
|
"USE_ARDUINO_VERSION_CODE",
|
||||||
@@ -518,6 +559,7 @@ async def to_code(config):
|
|||||||
|
|
||||||
|
|
||||||
APP_PARTITION_SIZES = {
|
APP_PARTITION_SIZES = {
|
||||||
|
"2MB": 0x0C0000, # 768 KB
|
||||||
"4MB": 0x1C0000, # 1792 KB
|
"4MB": 0x1C0000, # 1792 KB
|
||||||
"8MB": 0x3C0000, # 3840 KB
|
"8MB": 0x3C0000, # 3840 KB
|
||||||
"16MB": 0x7C0000, # 7936 KB
|
"16MB": 0x7C0000, # 7936 KB
|
||||||
|
|||||||
@@ -220,6 +220,8 @@ size_t I2SAudioSpeaker::play(const uint8_t *data, size_t length) {
|
|||||||
return index;
|
return index;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool I2SAudioSpeaker::has_buffered_data() const { return uxQueueMessagesWaiting(this->buffer_queue_) > 0; }
|
||||||
|
|
||||||
} // namespace i2s_audio
|
} // namespace i2s_audio
|
||||||
} // namespace esphome
|
} // namespace esphome
|
||||||
|
|
||||||
|
|||||||
@@ -56,6 +56,8 @@ class I2SAudioSpeaker : public Component, public speaker::Speaker, public I2SAud
|
|||||||
|
|
||||||
size_t play(const uint8_t *data, size_t length) override;
|
size_t play(const uint8_t *data, size_t length) override;
|
||||||
|
|
||||||
|
bool has_buffered_data() const override;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void start_();
|
void start_();
|
||||||
// void stop_();
|
// void stop_();
|
||||||
|
|||||||
@@ -1,15 +1,23 @@
|
|||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
import hashlib
|
||||||
import io
|
import io
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
import re
|
import re
|
||||||
import requests
|
import requests
|
||||||
|
from magic import Magic
|
||||||
|
|
||||||
|
from PIL import Image
|
||||||
|
|
||||||
from esphome import core
|
from esphome import core
|
||||||
from esphome.components import font
|
from esphome.components import font
|
||||||
|
from esphome import external_files
|
||||||
import esphome.config_validation as cv
|
import esphome.config_validation as cv
|
||||||
import esphome.codegen as cg
|
import esphome.codegen as cg
|
||||||
from esphome.const import (
|
from esphome.const import (
|
||||||
|
__version__,
|
||||||
CONF_DITHER,
|
CONF_DITHER,
|
||||||
CONF_FILE,
|
CONF_FILE,
|
||||||
CONF_ICON,
|
CONF_ICON,
|
||||||
@@ -19,6 +27,7 @@ from esphome.const import (
|
|||||||
CONF_RESIZE,
|
CONF_RESIZE,
|
||||||
CONF_SOURCE,
|
CONF_SOURCE,
|
||||||
CONF_TYPE,
|
CONF_TYPE,
|
||||||
|
CONF_URL,
|
||||||
)
|
)
|
||||||
from esphome.core import CORE, HexInt
|
from esphome.core import CORE, HexInt
|
||||||
|
|
||||||
@@ -43,34 +52,74 @@ IMAGE_TYPE = {
|
|||||||
CONF_USE_TRANSPARENCY = "use_transparency"
|
CONF_USE_TRANSPARENCY = "use_transparency"
|
||||||
|
|
||||||
# If the MDI file cannot be downloaded within this time, abort.
|
# If the MDI file cannot be downloaded within this time, abort.
|
||||||
MDI_DOWNLOAD_TIMEOUT = 30 # seconds
|
IMAGE_DOWNLOAD_TIMEOUT = 30 # seconds
|
||||||
|
|
||||||
SOURCE_LOCAL = "local"
|
SOURCE_LOCAL = "local"
|
||||||
SOURCE_MDI = "mdi"
|
SOURCE_MDI = "mdi"
|
||||||
|
SOURCE_WEB = "web"
|
||||||
|
|
||||||
|
|
||||||
Image_ = image_ns.class_("Image")
|
Image_ = image_ns.class_("Image")
|
||||||
|
|
||||||
|
|
||||||
def _compute_local_icon_path(value) -> Path:
|
def _compute_local_icon_path(value: dict) -> Path:
|
||||||
base_dir = Path(CORE.data_dir) / DOMAIN / "mdi"
|
base_dir = external_files.compute_local_file_dir(DOMAIN) / "mdi"
|
||||||
return base_dir / f"{value[CONF_ICON]}.svg"
|
return base_dir / f"{value[CONF_ICON]}.svg"
|
||||||
|
|
||||||
|
|
||||||
def download_mdi(value):
|
def _compute_local_image_path(value: dict) -> Path:
|
||||||
mdi_id = value[CONF_ICON]
|
url = value[CONF_URL]
|
||||||
path = _compute_local_icon_path(value)
|
h = hashlib.new("sha256")
|
||||||
if path.is_file():
|
h.update(url.encode())
|
||||||
return value
|
key = h.hexdigest()[:8]
|
||||||
url = f"https://raw.githubusercontent.com/Templarian/MaterialDesign/master/svg/{mdi_id}.svg"
|
base_dir = external_files.compute_local_file_dir(DOMAIN)
|
||||||
_LOGGER.debug("Downloading %s MDI image from %s", mdi_id, url)
|
return base_dir / key
|
||||||
|
|
||||||
|
|
||||||
|
def download_content(url: str, path: Path) -> None:
|
||||||
|
if not external_files.has_remote_file_changed(url, path):
|
||||||
|
_LOGGER.debug("Remote file has not changed %s", url)
|
||||||
|
return
|
||||||
|
|
||||||
|
_LOGGER.debug(
|
||||||
|
"Remote file has changed, downloading from %s to %s",
|
||||||
|
url,
|
||||||
|
path,
|
||||||
|
)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
req = requests.get(url, timeout=MDI_DOWNLOAD_TIMEOUT)
|
req = requests.get(
|
||||||
|
url,
|
||||||
|
timeout=IMAGE_DOWNLOAD_TIMEOUT,
|
||||||
|
headers={"User-agent": f"ESPHome/{__version__} (https://esphome.io)"},
|
||||||
|
)
|
||||||
req.raise_for_status()
|
req.raise_for_status()
|
||||||
except requests.exceptions.RequestException as e:
|
except requests.exceptions.RequestException as e:
|
||||||
raise cv.Invalid(f"Could not download MDI image {mdi_id} from {url}: {e}")
|
raise cv.Invalid(f"Could not download from {url}: {e}")
|
||||||
|
|
||||||
path.parent.mkdir(parents=True, exist_ok=True)
|
path.parent.mkdir(parents=True, exist_ok=True)
|
||||||
path.write_bytes(req.content)
|
path.write_bytes(req.content)
|
||||||
|
|
||||||
|
|
||||||
|
def download_mdi(value):
|
||||||
|
validate_cairosvg_installed(value)
|
||||||
|
|
||||||
|
mdi_id = value[CONF_ICON]
|
||||||
|
path = _compute_local_icon_path(value)
|
||||||
|
|
||||||
|
url = f"https://raw.githubusercontent.com/Templarian/MaterialDesign/master/svg/{mdi_id}.svg"
|
||||||
|
|
||||||
|
download_content(url, path)
|
||||||
|
|
||||||
|
return value
|
||||||
|
|
||||||
|
|
||||||
|
def download_image(value):
|
||||||
|
url = value[CONF_URL]
|
||||||
|
path = _compute_local_image_path(value)
|
||||||
|
|
||||||
|
download_content(url, path)
|
||||||
|
|
||||||
return value
|
return value
|
||||||
|
|
||||||
|
|
||||||
@@ -139,6 +188,13 @@ def validate_file_shorthand(value):
|
|||||||
CONF_ICON: icon,
|
CONF_ICON: icon,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
if value.startswith("http://") or value.startswith("https://"):
|
||||||
|
return FILE_SCHEMA(
|
||||||
|
{
|
||||||
|
CONF_SOURCE: SOURCE_WEB,
|
||||||
|
CONF_URL: value,
|
||||||
|
}
|
||||||
|
)
|
||||||
return FILE_SCHEMA(
|
return FILE_SCHEMA(
|
||||||
{
|
{
|
||||||
CONF_SOURCE: SOURCE_LOCAL,
|
CONF_SOURCE: SOURCE_LOCAL,
|
||||||
@@ -160,10 +216,18 @@ MDI_SCHEMA = cv.All(
|
|||||||
download_mdi,
|
download_mdi,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
WEB_SCHEMA = cv.All(
|
||||||
|
{
|
||||||
|
cv.Required(CONF_URL): cv.string,
|
||||||
|
},
|
||||||
|
download_image,
|
||||||
|
)
|
||||||
|
|
||||||
TYPED_FILE_SCHEMA = cv.typed_schema(
|
TYPED_FILE_SCHEMA = cv.typed_schema(
|
||||||
{
|
{
|
||||||
SOURCE_LOCAL: LOCAL_SCHEMA,
|
SOURCE_LOCAL: LOCAL_SCHEMA,
|
||||||
SOURCE_MDI: MDI_SCHEMA,
|
SOURCE_MDI: MDI_SCHEMA,
|
||||||
|
SOURCE_WEB: WEB_SCHEMA,
|
||||||
},
|
},
|
||||||
key=CONF_SOURCE,
|
key=CONF_SOURCE,
|
||||||
)
|
)
|
||||||
@@ -201,9 +265,7 @@ IMAGE_SCHEMA = cv.Schema(
|
|||||||
CONFIG_SCHEMA = cv.All(font.validate_pillow_installed, IMAGE_SCHEMA)
|
CONFIG_SCHEMA = cv.All(font.validate_pillow_installed, IMAGE_SCHEMA)
|
||||||
|
|
||||||
|
|
||||||
def load_svg_image(file: str, resize: tuple[int, int]):
|
def load_svg_image(file: bytes, resize: tuple[int, int]):
|
||||||
from PIL import Image
|
|
||||||
|
|
||||||
# This import is only needed in case of SVG images; adding it
|
# This import is only needed in case of SVG images; adding it
|
||||||
# to the top would force configurations not using SVG to also have it
|
# to the top would force configurations not using SVG to also have it
|
||||||
# installed for no reason.
|
# installed for no reason.
|
||||||
@@ -212,19 +274,17 @@ def load_svg_image(file: str, resize: tuple[int, int]):
|
|||||||
if resize:
|
if resize:
|
||||||
req_width, req_height = resize
|
req_width, req_height = resize
|
||||||
svg_image = svg2png(
|
svg_image = svg2png(
|
||||||
url=file,
|
file,
|
||||||
output_width=req_width,
|
output_width=req_width,
|
||||||
output_height=req_height,
|
output_height=req_height,
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
svg_image = svg2png(url=file)
|
svg_image = svg2png(file)
|
||||||
|
|
||||||
return Image.open(io.BytesIO(svg_image))
|
return Image.open(io.BytesIO(svg_image))
|
||||||
|
|
||||||
|
|
||||||
async def to_code(config):
|
async def to_code(config):
|
||||||
from PIL import Image
|
|
||||||
|
|
||||||
conf_file = config[CONF_FILE]
|
conf_file = config[CONF_FILE]
|
||||||
|
|
||||||
if conf_file[CONF_SOURCE] == SOURCE_LOCAL:
|
if conf_file[CONF_SOURCE] == SOURCE_LOCAL:
|
||||||
@@ -233,17 +293,26 @@ async def to_code(config):
|
|||||||
elif conf_file[CONF_SOURCE] == SOURCE_MDI:
|
elif conf_file[CONF_SOURCE] == SOURCE_MDI:
|
||||||
path = _compute_local_icon_path(conf_file).as_posix()
|
path = _compute_local_icon_path(conf_file).as_posix()
|
||||||
|
|
||||||
|
elif conf_file[CONF_SOURCE] == SOURCE_WEB:
|
||||||
|
path = _compute_local_image_path(conf_file).as_posix()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
resize = config.get(CONF_RESIZE)
|
with open(path, "rb") as f:
|
||||||
if path.lower().endswith(".svg"):
|
file_contents = f.read()
|
||||||
image = load_svg_image(path, resize)
|
|
||||||
else:
|
|
||||||
image = Image.open(path)
|
|
||||||
if resize:
|
|
||||||
image.thumbnail(resize)
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise core.EsphomeError(f"Could not load image file {path}: {e}")
|
raise core.EsphomeError(f"Could not load image file {path}: {e}")
|
||||||
|
|
||||||
|
mime = Magic(mime=True)
|
||||||
|
file_type = mime.from_buffer(file_contents)
|
||||||
|
|
||||||
|
resize = config.get(CONF_RESIZE)
|
||||||
|
if "svg" in file_type:
|
||||||
|
image = load_svg_image(file_contents, resize)
|
||||||
|
else:
|
||||||
|
image = Image.open(io.BytesIO(file_contents))
|
||||||
|
if resize:
|
||||||
|
image.thumbnail(resize)
|
||||||
|
|
||||||
width, height = image.size
|
width, height = image.size
|
||||||
|
|
||||||
if CONF_RESIZE not in config and (width > 500 or height > 500):
|
if CONF_RESIZE not in config and (width > 500 or height > 500):
|
||||||
|
|||||||
@@ -40,6 +40,8 @@ const EntityBase *MQTTLockComponent::get_entity() const { return this->lock_; }
|
|||||||
void MQTTLockComponent::send_discovery(JsonObject root, mqtt::SendDiscoveryConfig &config) {
|
void MQTTLockComponent::send_discovery(JsonObject root, mqtt::SendDiscoveryConfig &config) {
|
||||||
if (this->lock_->traits.get_assumed_state())
|
if (this->lock_->traits.get_assumed_state())
|
||||||
root[MQTT_OPTIMISTIC] = true;
|
root[MQTT_OPTIMISTIC] = true;
|
||||||
|
if (this->lock_->traits.get_supports_open())
|
||||||
|
root[MQTT_PAYLOAD_OPEN] = "OPEN";
|
||||||
}
|
}
|
||||||
bool MQTTLockComponent::send_initial_state() { return this->publish_state(); }
|
bool MQTTLockComponent::send_initial_state() { return this->publish_state(); }
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
#include "my9231.h"
|
#include "my9231.h"
|
||||||
#include "esphome/core/log.h"
|
#include "esphome/core/log.h"
|
||||||
|
#include "esphome/core/helpers.h"
|
||||||
|
|
||||||
namespace esphome {
|
namespace esphome {
|
||||||
namespace my9231 {
|
namespace my9231 {
|
||||||
@@ -51,7 +52,11 @@ void MY9231OutputComponent::setup() {
|
|||||||
MY9231_CMD_SCATTER_APDM | MY9231_CMD_FREQUENCY_DIVIDE_1 | MY9231_CMD_REACTION_FAST | MY9231_CMD_ONE_SHOT_DISABLE;
|
MY9231_CMD_SCATTER_APDM | MY9231_CMD_FREQUENCY_DIVIDE_1 | MY9231_CMD_REACTION_FAST | MY9231_CMD_ONE_SHOT_DISABLE;
|
||||||
ESP_LOGV(TAG, " Command: 0x%02X", command);
|
ESP_LOGV(TAG, " Command: 0x%02X", command);
|
||||||
|
|
||||||
this->init_chips_(command);
|
{
|
||||||
|
InterruptLock lock;
|
||||||
|
this->send_dcki_pulses_(32 * this->num_chips_);
|
||||||
|
this->init_chips_(command);
|
||||||
|
}
|
||||||
ESP_LOGV(TAG, " Chips initialized.");
|
ESP_LOGV(TAG, " Chips initialized.");
|
||||||
}
|
}
|
||||||
void MY9231OutputComponent::dump_config() {
|
void MY9231OutputComponent::dump_config() {
|
||||||
@@ -66,11 +71,14 @@ void MY9231OutputComponent::loop() {
|
|||||||
if (!this->update_)
|
if (!this->update_)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
for (auto pwm_amount : this->pwm_amounts_) {
|
{
|
||||||
this->write_word_(pwm_amount, this->bit_depth_);
|
InterruptLock lock;
|
||||||
|
for (auto pwm_amount : this->pwm_amounts_) {
|
||||||
|
this->write_word_(pwm_amount, this->bit_depth_);
|
||||||
|
}
|
||||||
|
// Send 8 DI pulses. After 8 falling edges, the duty data are store.
|
||||||
|
this->send_di_pulses_(8);
|
||||||
}
|
}
|
||||||
// Send 8 DI pulses. After 8 falling edges, the duty data are store.
|
|
||||||
this->send_di_pulses_(8);
|
|
||||||
this->update_ = false;
|
this->update_ = false;
|
||||||
}
|
}
|
||||||
void MY9231OutputComponent::set_channel_value_(uint8_t channel, uint16_t value) {
|
void MY9231OutputComponent::set_channel_value_(uint8_t channel, uint16_t value) {
|
||||||
@@ -92,6 +100,7 @@ void MY9231OutputComponent::init_chips_(uint8_t command) {
|
|||||||
// Send 16 DI pulse. After 14 falling edges, the command data are
|
// Send 16 DI pulse. After 14 falling edges, the command data are
|
||||||
// stored and after 16 falling edges the duty mode is activated.
|
// stored and after 16 falling edges the duty mode is activated.
|
||||||
this->send_di_pulses_(16);
|
this->send_di_pulses_(16);
|
||||||
|
delayMicroseconds(12);
|
||||||
}
|
}
|
||||||
void MY9231OutputComponent::write_word_(uint16_t value, uint8_t bits) {
|
void MY9231OutputComponent::write_word_(uint16_t value, uint8_t bits) {
|
||||||
for (uint8_t i = bits; i > 0; i--) {
|
for (uint8_t i = bits; i > 0; i--) {
|
||||||
@@ -106,6 +115,13 @@ void MY9231OutputComponent::send_di_pulses_(uint8_t count) {
|
|||||||
this->pin_di_->digital_write(false);
|
this->pin_di_->digital_write(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
void MY9231OutputComponent::send_dcki_pulses_(uint8_t count) {
|
||||||
|
delayMicroseconds(12);
|
||||||
|
for (uint8_t i = 0; i < count; i++) {
|
||||||
|
this->pin_dcki_->digital_write(true);
|
||||||
|
this->pin_dcki_->digital_write(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace my9231
|
} // namespace my9231
|
||||||
} // namespace esphome
|
} // namespace esphome
|
||||||
|
|||||||
@@ -49,6 +49,7 @@ class MY9231OutputComponent : public Component {
|
|||||||
void init_chips_(uint8_t command);
|
void init_chips_(uint8_t command);
|
||||||
void write_word_(uint16_t value, uint8_t bits);
|
void write_word_(uint16_t value, uint8_t bits);
|
||||||
void send_di_pulses_(uint8_t count);
|
void send_di_pulses_(uint8_t count);
|
||||||
|
void send_dcki_pulses_(uint8_t count);
|
||||||
|
|
||||||
GPIOPin *pin_di_;
|
GPIOPin *pin_di_;
|
||||||
GPIOPin *pin_dcki_;
|
GPIOPin *pin_dcki_;
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
#include "remote_base.h"
|
#include "remote_base.h"
|
||||||
#include "esphome/core/log.h"
|
#include "esphome/core/log.h"
|
||||||
|
|
||||||
|
#include <cinttypes>
|
||||||
|
|
||||||
namespace esphome {
|
namespace esphome {
|
||||||
namespace remote_base {
|
namespace remote_base {
|
||||||
|
|
||||||
|
|||||||
@@ -18,6 +18,8 @@ class Speaker {
|
|||||||
virtual void start() = 0;
|
virtual void start() = 0;
|
||||||
virtual void stop() = 0;
|
virtual void stop() = 0;
|
||||||
|
|
||||||
|
virtual bool has_buffered_data() const = 0;
|
||||||
|
|
||||||
bool is_running() const { return this->state_ == STATE_RUNNING; }
|
bool is_running() const { return this->state_ == STATE_RUNNING; }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|||||||
@@ -18,20 +18,27 @@ DEPENDENCIES = ["api", "microphone"]
|
|||||||
|
|
||||||
CODEOWNERS = ["@jesserockz"]
|
CODEOWNERS = ["@jesserockz"]
|
||||||
|
|
||||||
CONF_SILENCE_DETECTION = "silence_detection"
|
|
||||||
CONF_ON_LISTENING = "on_listening"
|
|
||||||
CONF_ON_START = "on_start"
|
|
||||||
CONF_ON_WAKE_WORD_DETECTED = "on_wake_word_detected"
|
|
||||||
CONF_ON_STT_END = "on_stt_end"
|
|
||||||
CONF_ON_TTS_START = "on_tts_start"
|
|
||||||
CONF_ON_TTS_END = "on_tts_end"
|
|
||||||
CONF_ON_END = "on_end"
|
CONF_ON_END = "on_end"
|
||||||
CONF_ON_ERROR = "on_error"
|
CONF_ON_ERROR = "on_error"
|
||||||
|
CONF_ON_INTENT_END = "on_intent_end"
|
||||||
|
CONF_ON_INTENT_START = "on_intent_start"
|
||||||
|
CONF_ON_LISTENING = "on_listening"
|
||||||
|
CONF_ON_START = "on_start"
|
||||||
|
CONF_ON_STT_END = "on_stt_end"
|
||||||
|
CONF_ON_STT_VAD_END = "on_stt_vad_end"
|
||||||
|
CONF_ON_STT_VAD_START = "on_stt_vad_start"
|
||||||
|
CONF_ON_TTS_END = "on_tts_end"
|
||||||
|
CONF_ON_TTS_START = "on_tts_start"
|
||||||
|
CONF_ON_TTS_STREAM_START = "on_tts_stream_start"
|
||||||
|
CONF_ON_TTS_STREAM_END = "on_tts_stream_end"
|
||||||
|
CONF_ON_WAKE_WORD_DETECTED = "on_wake_word_detected"
|
||||||
|
|
||||||
|
CONF_SILENCE_DETECTION = "silence_detection"
|
||||||
CONF_USE_WAKE_WORD = "use_wake_word"
|
CONF_USE_WAKE_WORD = "use_wake_word"
|
||||||
CONF_VAD_THRESHOLD = "vad_threshold"
|
CONF_VAD_THRESHOLD = "vad_threshold"
|
||||||
|
|
||||||
CONF_NOISE_SUPPRESSION_LEVEL = "noise_suppression_level"
|
|
||||||
CONF_AUTO_GAIN = "auto_gain"
|
CONF_AUTO_GAIN = "auto_gain"
|
||||||
|
CONF_NOISE_SUPPRESSION_LEVEL = "noise_suppression_level"
|
||||||
CONF_VOLUME_MULTIPLIER = "volume_multiplier"
|
CONF_VOLUME_MULTIPLIER = "volume_multiplier"
|
||||||
|
|
||||||
|
|
||||||
@@ -50,6 +57,20 @@ StopAction = voice_assistant_ns.class_(
|
|||||||
IsRunningCondition = voice_assistant_ns.class_(
|
IsRunningCondition = voice_assistant_ns.class_(
|
||||||
"IsRunningCondition", automation.Condition, cg.Parented.template(VoiceAssistant)
|
"IsRunningCondition", automation.Condition, cg.Parented.template(VoiceAssistant)
|
||||||
)
|
)
|
||||||
|
ConnectedCondition = voice_assistant_ns.class_(
|
||||||
|
"ConnectedCondition", automation.Condition, cg.Parented.template(VoiceAssistant)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def tts_stream_validate(config):
|
||||||
|
if CONF_SPEAKER not in config and (
|
||||||
|
CONF_ON_TTS_STREAM_START in config or CONF_ON_TTS_STREAM_END in config
|
||||||
|
):
|
||||||
|
raise cv.Invalid(
|
||||||
|
f"{CONF_SPEAKER} is required when using {CONF_ON_TTS_STREAM_START} and/or {CONF_ON_TTS_STREAM_END}"
|
||||||
|
)
|
||||||
|
return config
|
||||||
|
|
||||||
|
|
||||||
CONFIG_SCHEMA = cv.All(
|
CONFIG_SCHEMA = cv.All(
|
||||||
cv.Schema(
|
cv.Schema(
|
||||||
@@ -88,8 +109,27 @@ CONFIG_SCHEMA = cv.All(
|
|||||||
cv.Optional(CONF_ON_CLIENT_DISCONNECTED): automation.validate_automation(
|
cv.Optional(CONF_ON_CLIENT_DISCONNECTED): automation.validate_automation(
|
||||||
single=True
|
single=True
|
||||||
),
|
),
|
||||||
|
cv.Optional(CONF_ON_INTENT_START): automation.validate_automation(
|
||||||
|
single=True
|
||||||
|
),
|
||||||
|
cv.Optional(CONF_ON_INTENT_END): automation.validate_automation(
|
||||||
|
single=True
|
||||||
|
),
|
||||||
|
cv.Optional(CONF_ON_STT_VAD_START): automation.validate_automation(
|
||||||
|
single=True
|
||||||
|
),
|
||||||
|
cv.Optional(CONF_ON_STT_VAD_END): automation.validate_automation(
|
||||||
|
single=True
|
||||||
|
),
|
||||||
|
cv.Optional(CONF_ON_TTS_STREAM_START): automation.validate_automation(
|
||||||
|
single=True
|
||||||
|
),
|
||||||
|
cv.Optional(CONF_ON_TTS_STREAM_END): automation.validate_automation(
|
||||||
|
single=True
|
||||||
|
),
|
||||||
}
|
}
|
||||||
).extend(cv.COMPONENT_SCHEMA),
|
).extend(cv.COMPONENT_SCHEMA),
|
||||||
|
tts_stream_validate,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -177,6 +217,48 @@ async def to_code(config):
|
|||||||
config[CONF_ON_CLIENT_DISCONNECTED],
|
config[CONF_ON_CLIENT_DISCONNECTED],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if CONF_ON_INTENT_START in config:
|
||||||
|
await automation.build_automation(
|
||||||
|
var.get_intent_start_trigger(),
|
||||||
|
[],
|
||||||
|
config[CONF_ON_INTENT_START],
|
||||||
|
)
|
||||||
|
|
||||||
|
if CONF_ON_INTENT_END in config:
|
||||||
|
await automation.build_automation(
|
||||||
|
var.get_intent_end_trigger(),
|
||||||
|
[],
|
||||||
|
config[CONF_ON_INTENT_END],
|
||||||
|
)
|
||||||
|
|
||||||
|
if CONF_ON_STT_VAD_START in config:
|
||||||
|
await automation.build_automation(
|
||||||
|
var.get_stt_vad_start_trigger(),
|
||||||
|
[],
|
||||||
|
config[CONF_ON_STT_VAD_START],
|
||||||
|
)
|
||||||
|
|
||||||
|
if CONF_ON_STT_VAD_END in config:
|
||||||
|
await automation.build_automation(
|
||||||
|
var.get_stt_vad_end_trigger(),
|
||||||
|
[],
|
||||||
|
config[CONF_ON_STT_VAD_END],
|
||||||
|
)
|
||||||
|
|
||||||
|
if CONF_ON_TTS_STREAM_START in config:
|
||||||
|
await automation.build_automation(
|
||||||
|
var.get_tts_stream_start_trigger(),
|
||||||
|
[],
|
||||||
|
config[CONF_ON_TTS_STREAM_START],
|
||||||
|
)
|
||||||
|
|
||||||
|
if CONF_ON_TTS_STREAM_END in config:
|
||||||
|
await automation.build_automation(
|
||||||
|
var.get_tts_stream_end_trigger(),
|
||||||
|
[],
|
||||||
|
config[CONF_ON_TTS_STREAM_END],
|
||||||
|
)
|
||||||
|
|
||||||
cg.add_define("USE_VOICE_ASSISTANT")
|
cg.add_define("USE_VOICE_ASSISTANT")
|
||||||
|
|
||||||
|
|
||||||
@@ -219,3 +301,12 @@ async def voice_assistant_is_running_to_code(config, condition_id, template_arg,
|
|||||||
var = cg.new_Pvariable(condition_id, template_arg)
|
var = cg.new_Pvariable(condition_id, template_arg)
|
||||||
await cg.register_parented(var, config[CONF_ID])
|
await cg.register_parented(var, config[CONF_ID])
|
||||||
return var
|
return var
|
||||||
|
|
||||||
|
|
||||||
|
@register_condition(
|
||||||
|
"voice_assistant.connected", ConnectedCondition, VOICE_ASSISTANT_ACTION_SCHEMA
|
||||||
|
)
|
||||||
|
async def voice_assistant_connected_to_code(config, condition_id, template_arg, args):
|
||||||
|
var = cg.new_Pvariable(condition_id, template_arg)
|
||||||
|
await cg.register_parented(var, config[CONF_ID])
|
||||||
|
return var
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ void VoiceAssistant::setup() {
|
|||||||
|
|
||||||
this->socket_ = socket::socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
|
this->socket_ = socket::socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
|
||||||
if (socket_ == nullptr) {
|
if (socket_ == nullptr) {
|
||||||
ESP_LOGW(TAG, "Could not create socket.");
|
ESP_LOGW(TAG, "Could not create socket");
|
||||||
this->mark_failed();
|
this->mark_failed();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -69,7 +69,7 @@ void VoiceAssistant::setup() {
|
|||||||
ExternalRAMAllocator<uint8_t> speaker_allocator(ExternalRAMAllocator<uint8_t>::ALLOW_FAILURE);
|
ExternalRAMAllocator<uint8_t> speaker_allocator(ExternalRAMAllocator<uint8_t>::ALLOW_FAILURE);
|
||||||
this->speaker_buffer_ = speaker_allocator.allocate(SPEAKER_BUFFER_SIZE);
|
this->speaker_buffer_ = speaker_allocator.allocate(SPEAKER_BUFFER_SIZE);
|
||||||
if (this->speaker_buffer_ == nullptr) {
|
if (this->speaker_buffer_ == nullptr) {
|
||||||
ESP_LOGW(TAG, "Could not allocate speaker buffer.");
|
ESP_LOGW(TAG, "Could not allocate speaker buffer");
|
||||||
this->mark_failed();
|
this->mark_failed();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -79,7 +79,7 @@ void VoiceAssistant::setup() {
|
|||||||
ExternalRAMAllocator<int16_t> allocator(ExternalRAMAllocator<int16_t>::ALLOW_FAILURE);
|
ExternalRAMAllocator<int16_t> allocator(ExternalRAMAllocator<int16_t>::ALLOW_FAILURE);
|
||||||
this->input_buffer_ = allocator.allocate(INPUT_BUFFER_SIZE);
|
this->input_buffer_ = allocator.allocate(INPUT_BUFFER_SIZE);
|
||||||
if (this->input_buffer_ == nullptr) {
|
if (this->input_buffer_ == nullptr) {
|
||||||
ESP_LOGW(TAG, "Could not allocate input buffer.");
|
ESP_LOGW(TAG, "Could not allocate input buffer");
|
||||||
this->mark_failed();
|
this->mark_failed();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -89,7 +89,7 @@ void VoiceAssistant::setup() {
|
|||||||
|
|
||||||
this->ring_buffer_ = rb_create(BUFFER_SIZE, sizeof(int16_t));
|
this->ring_buffer_ = rb_create(BUFFER_SIZE, sizeof(int16_t));
|
||||||
if (this->ring_buffer_ == nullptr) {
|
if (this->ring_buffer_ == nullptr) {
|
||||||
ESP_LOGW(TAG, "Could not allocate ring buffer.");
|
ESP_LOGW(TAG, "Could not allocate ring buffer");
|
||||||
this->mark_failed();
|
this->mark_failed();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -98,7 +98,7 @@ void VoiceAssistant::setup() {
|
|||||||
ExternalRAMAllocator<uint8_t> send_allocator(ExternalRAMAllocator<uint8_t>::ALLOW_FAILURE);
|
ExternalRAMAllocator<uint8_t> send_allocator(ExternalRAMAllocator<uint8_t>::ALLOW_FAILURE);
|
||||||
this->send_buffer_ = send_allocator.allocate(SEND_BUFFER_SIZE);
|
this->send_buffer_ = send_allocator.allocate(SEND_BUFFER_SIZE);
|
||||||
if (send_buffer_ == nullptr) {
|
if (send_buffer_ == nullptr) {
|
||||||
ESP_LOGW(TAG, "Could not allocate send buffer.");
|
ESP_LOGW(TAG, "Could not allocate send buffer");
|
||||||
this->mark_failed();
|
this->mark_failed();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -221,8 +221,8 @@ void VoiceAssistant::loop() {
|
|||||||
msg.audio_settings = audio_settings;
|
msg.audio_settings = audio_settings;
|
||||||
|
|
||||||
if (this->api_client_ == nullptr || !this->api_client_->send_voice_assistant_request(msg)) {
|
if (this->api_client_ == nullptr || !this->api_client_->send_voice_assistant_request(msg)) {
|
||||||
ESP_LOGW(TAG, "Could not request start.");
|
ESP_LOGW(TAG, "Could not request start");
|
||||||
this->error_trigger_->trigger("not-connected", "Could not request start.");
|
this->error_trigger_->trigger("not-connected", "Could not request start");
|
||||||
this->continuous_ = false;
|
this->continuous_ = false;
|
||||||
this->set_state_(State::IDLE, State::IDLE);
|
this->set_state_(State::IDLE, State::IDLE);
|
||||||
break;
|
break;
|
||||||
@@ -273,28 +273,27 @@ void VoiceAssistant::loop() {
|
|||||||
bool playing = false;
|
bool playing = false;
|
||||||
#ifdef USE_SPEAKER
|
#ifdef USE_SPEAKER
|
||||||
if (this->speaker_ != nullptr) {
|
if (this->speaker_ != nullptr) {
|
||||||
|
ssize_t received_len = 0;
|
||||||
if (this->speaker_buffer_index_ + RECEIVE_SIZE < SPEAKER_BUFFER_SIZE) {
|
if (this->speaker_buffer_index_ + RECEIVE_SIZE < SPEAKER_BUFFER_SIZE) {
|
||||||
auto len = this->socket_->read(this->speaker_buffer_ + this->speaker_buffer_index_, RECEIVE_SIZE);
|
received_len = this->socket_->read(this->speaker_buffer_ + this->speaker_buffer_index_, RECEIVE_SIZE);
|
||||||
if (len > 0) {
|
if (received_len > 0) {
|
||||||
this->speaker_buffer_index_ += len;
|
this->speaker_buffer_index_ += received_len;
|
||||||
this->speaker_buffer_size_ += len;
|
this->speaker_buffer_size_ += received_len;
|
||||||
|
this->speaker_bytes_received_ += received_len;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
ESP_LOGW(TAG, "Receive buffer full.");
|
ESP_LOGD(TAG, "Receive buffer full");
|
||||||
}
|
|
||||||
if (this->speaker_buffer_size_ > 0) {
|
|
||||||
size_t written = this->speaker_->play(this->speaker_buffer_, this->speaker_buffer_size_);
|
|
||||||
if (written > 0) {
|
|
||||||
memmove(this->speaker_buffer_, this->speaker_buffer_ + written, this->speaker_buffer_size_ - written);
|
|
||||||
this->speaker_buffer_size_ -= written;
|
|
||||||
this->speaker_buffer_index_ -= written;
|
|
||||||
this->set_timeout("speaker-timeout", 2000, [this]() { this->speaker_->stop(); });
|
|
||||||
} else {
|
|
||||||
ESP_LOGW(TAG, "Speaker buffer full.");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
// Build a small buffer of audio before sending to the speaker
|
||||||
|
if (this->speaker_bytes_received_ > RECEIVE_SIZE * 4)
|
||||||
|
this->write_speaker_();
|
||||||
if (this->wait_for_stream_end_) {
|
if (this->wait_for_stream_end_) {
|
||||||
this->cancel_timeout("playing");
|
this->cancel_timeout("playing");
|
||||||
|
if (this->stream_ended_ && received_len < 0) {
|
||||||
|
ESP_LOGD(TAG, "End of audio stream received");
|
||||||
|
this->cancel_timeout("speaker-timeout");
|
||||||
|
this->set_state_(State::RESPONSE_FINISHED, State::RESPONSE_FINISHED);
|
||||||
|
}
|
||||||
break; // We dont want to timeout here as the STREAM_END event will take care of that.
|
break; // We dont want to timeout here as the STREAM_END event will take care of that.
|
||||||
}
|
}
|
||||||
playing = this->speaker_->is_running();
|
playing = this->speaker_->is_running();
|
||||||
@@ -316,14 +315,26 @@ void VoiceAssistant::loop() {
|
|||||||
case State::RESPONSE_FINISHED: {
|
case State::RESPONSE_FINISHED: {
|
||||||
#ifdef USE_SPEAKER
|
#ifdef USE_SPEAKER
|
||||||
if (this->speaker_ != nullptr) {
|
if (this->speaker_ != nullptr) {
|
||||||
|
if (this->speaker_buffer_size_ > 0) {
|
||||||
|
this->write_speaker_();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (this->speaker_->has_buffered_data() || this->speaker_->is_running()) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
ESP_LOGD(TAG, "Speaker has finished outputting all audio");
|
||||||
this->speaker_->stop();
|
this->speaker_->stop();
|
||||||
this->cancel_timeout("speaker-timeout");
|
this->cancel_timeout("speaker-timeout");
|
||||||
this->cancel_timeout("playing");
|
this->cancel_timeout("playing");
|
||||||
this->speaker_buffer_size_ = 0;
|
this->speaker_buffer_size_ = 0;
|
||||||
this->speaker_buffer_index_ = 0;
|
this->speaker_buffer_index_ = 0;
|
||||||
|
this->speaker_bytes_received_ = 0;
|
||||||
memset(this->speaker_buffer_, 0, SPEAKER_BUFFER_SIZE);
|
memset(this->speaker_buffer_, 0, SPEAKER_BUFFER_SIZE);
|
||||||
|
this->wait_for_stream_end_ = false;
|
||||||
|
this->stream_ended_ = false;
|
||||||
|
|
||||||
|
this->tts_stream_end_trigger_->trigger();
|
||||||
}
|
}
|
||||||
this->wait_for_stream_end_ = false;
|
|
||||||
#endif
|
#endif
|
||||||
this->set_state_(State::IDLE, State::IDLE);
|
this->set_state_(State::IDLE, State::IDLE);
|
||||||
break;
|
break;
|
||||||
@@ -333,6 +344,22 @@ void VoiceAssistant::loop() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef USE_SPEAKER
|
||||||
|
void VoiceAssistant::write_speaker_() {
|
||||||
|
if (this->speaker_buffer_size_ > 0) {
|
||||||
|
size_t written = this->speaker_->play(this->speaker_buffer_, this->speaker_buffer_size_);
|
||||||
|
if (written > 0) {
|
||||||
|
memmove(this->speaker_buffer_, this->speaker_buffer_ + written, this->speaker_buffer_size_ - written);
|
||||||
|
this->speaker_buffer_size_ -= written;
|
||||||
|
this->speaker_buffer_index_ -= written;
|
||||||
|
this->set_timeout("speaker-timeout", 5000, [this]() { this->speaker_->stop(); });
|
||||||
|
} else {
|
||||||
|
ESP_LOGD(TAG, "Speaker buffer full, trying again next loop");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
void VoiceAssistant::client_subscription(api::APIConnection *client, bool subscribe) {
|
void VoiceAssistant::client_subscription(api::APIConnection *client, bool subscribe) {
|
||||||
if (!subscribe) {
|
if (!subscribe) {
|
||||||
if (this->api_client_ == nullptr || client != this->api_client_) {
|
if (this->api_client_ == nullptr || client != this->api_client_) {
|
||||||
@@ -503,21 +530,20 @@ void VoiceAssistant::on_event(const api::VoiceAssistantEventResponse &msg) {
|
|||||||
switch (msg.event_type) {
|
switch (msg.event_type) {
|
||||||
case api::enums::VOICE_ASSISTANT_RUN_START:
|
case api::enums::VOICE_ASSISTANT_RUN_START:
|
||||||
ESP_LOGD(TAG, "Assist Pipeline running");
|
ESP_LOGD(TAG, "Assist Pipeline running");
|
||||||
this->start_trigger_->trigger();
|
this->defer([this]() { this->start_trigger_->trigger(); });
|
||||||
break;
|
break;
|
||||||
case api::enums::VOICE_ASSISTANT_WAKE_WORD_START:
|
case api::enums::VOICE_ASSISTANT_WAKE_WORD_START:
|
||||||
break;
|
break;
|
||||||
case api::enums::VOICE_ASSISTANT_WAKE_WORD_END: {
|
case api::enums::VOICE_ASSISTANT_WAKE_WORD_END: {
|
||||||
ESP_LOGD(TAG, "Wake word detected");
|
ESP_LOGD(TAG, "Wake word detected");
|
||||||
this->wake_word_detected_trigger_->trigger();
|
this->defer([this]() { this->wake_word_detected_trigger_->trigger(); });
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case api::enums::VOICE_ASSISTANT_STT_START:
|
case api::enums::VOICE_ASSISTANT_STT_START:
|
||||||
ESP_LOGD(TAG, "STT Started");
|
ESP_LOGD(TAG, "STT started");
|
||||||
this->listening_trigger_->trigger();
|
this->defer([this]() { this->listening_trigger_->trigger(); });
|
||||||
break;
|
break;
|
||||||
case api::enums::VOICE_ASSISTANT_STT_END: {
|
case api::enums::VOICE_ASSISTANT_STT_END: {
|
||||||
this->set_state_(State::STOP_MICROPHONE, State::AWAITING_RESPONSE);
|
|
||||||
std::string text;
|
std::string text;
|
||||||
for (auto arg : msg.data) {
|
for (auto arg : msg.data) {
|
||||||
if (arg.name == "text") {
|
if (arg.name == "text") {
|
||||||
@@ -525,19 +551,24 @@ void VoiceAssistant::on_event(const api::VoiceAssistantEventResponse &msg) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (text.empty()) {
|
if (text.empty()) {
|
||||||
ESP_LOGW(TAG, "No text in STT_END event.");
|
ESP_LOGW(TAG, "No text in STT_END event");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
ESP_LOGD(TAG, "Speech recognised as: \"%s\"", text.c_str());
|
ESP_LOGD(TAG, "Speech recognised as: \"%s\"", text.c_str());
|
||||||
this->stt_end_trigger_->trigger(text);
|
this->defer([this, text]() { this->stt_end_trigger_->trigger(text); });
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
case api::enums::VOICE_ASSISTANT_INTENT_START:
|
||||||
|
ESP_LOGD(TAG, "Intent started");
|
||||||
|
this->defer([this]() { this->intent_start_trigger_->trigger(); });
|
||||||
|
break;
|
||||||
case api::enums::VOICE_ASSISTANT_INTENT_END: {
|
case api::enums::VOICE_ASSISTANT_INTENT_END: {
|
||||||
for (auto arg : msg.data) {
|
for (auto arg : msg.data) {
|
||||||
if (arg.name == "conversation_id") {
|
if (arg.name == "conversation_id") {
|
||||||
this->conversation_id_ = std::move(arg.value);
|
this->conversation_id_ = std::move(arg.value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
this->defer([this]() { this->intent_end_trigger_->trigger(); });
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case api::enums::VOICE_ASSISTANT_TTS_START: {
|
case api::enums::VOICE_ASSISTANT_TTS_START: {
|
||||||
@@ -548,14 +579,16 @@ void VoiceAssistant::on_event(const api::VoiceAssistantEventResponse &msg) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (text.empty()) {
|
if (text.empty()) {
|
||||||
ESP_LOGW(TAG, "No text in TTS_START event.");
|
ESP_LOGW(TAG, "No text in TTS_START event");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
ESP_LOGD(TAG, "Response: \"%s\"", text.c_str());
|
ESP_LOGD(TAG, "Response: \"%s\"", text.c_str());
|
||||||
this->tts_start_trigger_->trigger(text);
|
this->defer([this, text]() {
|
||||||
|
this->tts_start_trigger_->trigger(text);
|
||||||
#ifdef USE_SPEAKER
|
#ifdef USE_SPEAKER
|
||||||
this->speaker_->start();
|
this->speaker_->start();
|
||||||
#endif
|
#endif
|
||||||
|
});
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case api::enums::VOICE_ASSISTANT_TTS_END: {
|
case api::enums::VOICE_ASSISTANT_TTS_END: {
|
||||||
@@ -566,18 +599,20 @@ void VoiceAssistant::on_event(const api::VoiceAssistantEventResponse &msg) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (url.empty()) {
|
if (url.empty()) {
|
||||||
ESP_LOGW(TAG, "No url in TTS_END event.");
|
ESP_LOGW(TAG, "No url in TTS_END event");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
ESP_LOGD(TAG, "Response URL: \"%s\"", url.c_str());
|
ESP_LOGD(TAG, "Response URL: \"%s\"", url.c_str());
|
||||||
|
this->defer([this, url]() {
|
||||||
#ifdef USE_MEDIA_PLAYER
|
#ifdef USE_MEDIA_PLAYER
|
||||||
if (this->media_player_ != nullptr) {
|
if (this->media_player_ != nullptr) {
|
||||||
this->media_player_->make_call().set_media_url(url).perform();
|
this->media_player_->make_call().set_media_url(url).perform();
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
this->tts_end_trigger_->trigger(url);
|
||||||
|
});
|
||||||
State new_state = this->local_output_ ? State::STREAMING_RESPONSE : State::IDLE;
|
State new_state = this->local_output_ ? State::STREAMING_RESPONSE : State::IDLE;
|
||||||
this->set_state_(new_state, new_state);
|
this->set_state_(new_state, new_state);
|
||||||
this->tts_end_trigger_->trigger(url);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case api::enums::VOICE_ASSISTANT_RUN_END: {
|
case api::enums::VOICE_ASSISTANT_RUN_END: {
|
||||||
@@ -594,7 +629,7 @@ void VoiceAssistant::on_event(const api::VoiceAssistantEventResponse &msg) {
|
|||||||
this->set_state_(State::IDLE, State::IDLE);
|
this->set_state_(State::IDLE, State::IDLE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this->end_trigger_->trigger();
|
this->defer([this]() { this->end_trigger_->trigger(); });
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case api::enums::VOICE_ASSISTANT_ERROR: {
|
case api::enums::VOICE_ASSISTANT_ERROR: {
|
||||||
@@ -612,8 +647,10 @@ void VoiceAssistant::on_event(const api::VoiceAssistantEventResponse &msg) {
|
|||||||
return;
|
return;
|
||||||
} else if (code == "wake-provider-missing" || code == "wake-engine-missing") {
|
} else if (code == "wake-provider-missing" || code == "wake-engine-missing") {
|
||||||
// Wake word is not set up or not ready on Home Assistant so stop and do not retry until user starts again.
|
// Wake word is not set up or not ready on Home Assistant so stop and do not retry until user starts again.
|
||||||
this->request_stop();
|
this->defer([this, code, message]() {
|
||||||
this->error_trigger_->trigger(code, message);
|
this->request_stop();
|
||||||
|
this->error_trigger_->trigger(code, message);
|
||||||
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
ESP_LOGE(TAG, "Error: %s - %s", code.c_str(), message.c_str());
|
ESP_LOGE(TAG, "Error: %s - %s", code.c_str(), message.c_str());
|
||||||
@@ -621,19 +658,33 @@ void VoiceAssistant::on_event(const api::VoiceAssistantEventResponse &msg) {
|
|||||||
this->signal_stop_();
|
this->signal_stop_();
|
||||||
this->set_state_(State::STOP_MICROPHONE, State::IDLE);
|
this->set_state_(State::STOP_MICROPHONE, State::IDLE);
|
||||||
}
|
}
|
||||||
this->error_trigger_->trigger(code, message);
|
this->defer([this, code, message]() { this->error_trigger_->trigger(code, message); });
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case api::enums::VOICE_ASSISTANT_TTS_STREAM_START: {
|
case api::enums::VOICE_ASSISTANT_TTS_STREAM_START: {
|
||||||
#ifdef USE_SPEAKER
|
#ifdef USE_SPEAKER
|
||||||
this->wait_for_stream_end_ = true;
|
this->wait_for_stream_end_ = true;
|
||||||
|
ESP_LOGD(TAG, "TTS stream start");
|
||||||
|
this->defer([this] { this->tts_stream_start_trigger_->trigger(); });
|
||||||
#endif
|
#endif
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case api::enums::VOICE_ASSISTANT_TTS_STREAM_END: {
|
case api::enums::VOICE_ASSISTANT_TTS_STREAM_END: {
|
||||||
this->set_state_(State::RESPONSE_FINISHED, State::IDLE);
|
#ifdef USE_SPEAKER
|
||||||
|
this->stream_ended_ = true;
|
||||||
|
ESP_LOGD(TAG, "TTS stream end");
|
||||||
|
#endif
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
case api::enums::VOICE_ASSISTANT_STT_VAD_START:
|
||||||
|
ESP_LOGD(TAG, "Starting STT by VAD");
|
||||||
|
this->defer([this]() { this->stt_vad_start_trigger_->trigger(); });
|
||||||
|
break;
|
||||||
|
case api::enums::VOICE_ASSISTANT_STT_VAD_END:
|
||||||
|
ESP_LOGD(TAG, "STT by VAD end");
|
||||||
|
this->set_state_(State::STOP_MICROPHONE, State::AWAITING_RESPONSE);
|
||||||
|
this->defer([this]() { this->stt_vad_end_trigger_->trigger(); });
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
ESP_LOGD(TAG, "Unhandled event type: %d", msg.event_type);
|
ESP_LOGD(TAG, "Unhandled event type: %d", msg.event_type);
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -100,13 +100,21 @@ class VoiceAssistant : public Component {
|
|||||||
void set_auto_gain(uint8_t auto_gain) { this->auto_gain_ = auto_gain; }
|
void set_auto_gain(uint8_t auto_gain) { this->auto_gain_ = auto_gain; }
|
||||||
void set_volume_multiplier(float volume_multiplier) { this->volume_multiplier_ = volume_multiplier; }
|
void set_volume_multiplier(float volume_multiplier) { this->volume_multiplier_ = volume_multiplier; }
|
||||||
|
|
||||||
|
Trigger<> *get_intent_end_trigger() const { return this->intent_end_trigger_; }
|
||||||
|
Trigger<> *get_intent_start_trigger() const { return this->intent_start_trigger_; }
|
||||||
Trigger<> *get_listening_trigger() const { return this->listening_trigger_; }
|
Trigger<> *get_listening_trigger() const { return this->listening_trigger_; }
|
||||||
|
Trigger<> *get_end_trigger() const { return this->end_trigger_; }
|
||||||
Trigger<> *get_start_trigger() const { return this->start_trigger_; }
|
Trigger<> *get_start_trigger() const { return this->start_trigger_; }
|
||||||
|
Trigger<> *get_stt_vad_end_trigger() const { return this->stt_vad_end_trigger_; }
|
||||||
|
Trigger<> *get_stt_vad_start_trigger() const { return this->stt_vad_start_trigger_; }
|
||||||
|
#ifdef USE_SPEAKER
|
||||||
|
Trigger<> *get_tts_stream_start_trigger() const { return this->tts_stream_start_trigger_; }
|
||||||
|
Trigger<> *get_tts_stream_end_trigger() const { return this->tts_stream_end_trigger_; }
|
||||||
|
#endif
|
||||||
Trigger<> *get_wake_word_detected_trigger() const { return this->wake_word_detected_trigger_; }
|
Trigger<> *get_wake_word_detected_trigger() const { return this->wake_word_detected_trigger_; }
|
||||||
Trigger<std::string> *get_stt_end_trigger() const { return this->stt_end_trigger_; }
|
Trigger<std::string> *get_stt_end_trigger() const { return this->stt_end_trigger_; }
|
||||||
Trigger<std::string> *get_tts_start_trigger() const { return this->tts_start_trigger_; }
|
|
||||||
Trigger<std::string> *get_tts_end_trigger() const { return this->tts_end_trigger_; }
|
Trigger<std::string> *get_tts_end_trigger() const { return this->tts_end_trigger_; }
|
||||||
Trigger<> *get_end_trigger() const { return this->end_trigger_; }
|
Trigger<std::string> *get_tts_start_trigger() const { return this->tts_start_trigger_; }
|
||||||
Trigger<std::string, std::string> *get_error_trigger() const { return this->error_trigger_; }
|
Trigger<std::string, std::string> *get_error_trigger() const { return this->error_trigger_; }
|
||||||
|
|
||||||
Trigger<> *get_client_connected_trigger() const { return this->client_connected_trigger_; }
|
Trigger<> *get_client_connected_trigger() const { return this->client_connected_trigger_; }
|
||||||
@@ -124,13 +132,21 @@ class VoiceAssistant : public Component {
|
|||||||
std::unique_ptr<socket::Socket> socket_ = nullptr;
|
std::unique_ptr<socket::Socket> socket_ = nullptr;
|
||||||
struct sockaddr_storage dest_addr_;
|
struct sockaddr_storage dest_addr_;
|
||||||
|
|
||||||
|
Trigger<> *intent_end_trigger_ = new Trigger<>();
|
||||||
|
Trigger<> *intent_start_trigger_ = new Trigger<>();
|
||||||
Trigger<> *listening_trigger_ = new Trigger<>();
|
Trigger<> *listening_trigger_ = new Trigger<>();
|
||||||
|
Trigger<> *end_trigger_ = new Trigger<>();
|
||||||
Trigger<> *start_trigger_ = new Trigger<>();
|
Trigger<> *start_trigger_ = new Trigger<>();
|
||||||
|
Trigger<> *stt_vad_start_trigger_ = new Trigger<>();
|
||||||
|
Trigger<> *stt_vad_end_trigger_ = new Trigger<>();
|
||||||
|
#ifdef USE_SPEAKER
|
||||||
|
Trigger<> *tts_stream_start_trigger_ = new Trigger<>();
|
||||||
|
Trigger<> *tts_stream_end_trigger_ = new Trigger<>();
|
||||||
|
#endif
|
||||||
Trigger<> *wake_word_detected_trigger_ = new Trigger<>();
|
Trigger<> *wake_word_detected_trigger_ = new Trigger<>();
|
||||||
Trigger<std::string> *stt_end_trigger_ = new Trigger<std::string>();
|
Trigger<std::string> *stt_end_trigger_ = new Trigger<std::string>();
|
||||||
Trigger<std::string> *tts_start_trigger_ = new Trigger<std::string>();
|
|
||||||
Trigger<std::string> *tts_end_trigger_ = new Trigger<std::string>();
|
Trigger<std::string> *tts_end_trigger_ = new Trigger<std::string>();
|
||||||
Trigger<> *end_trigger_ = new Trigger<>();
|
Trigger<std::string> *tts_start_trigger_ = new Trigger<std::string>();
|
||||||
Trigger<std::string, std::string> *error_trigger_ = new Trigger<std::string, std::string>();
|
Trigger<std::string, std::string> *error_trigger_ = new Trigger<std::string, std::string>();
|
||||||
|
|
||||||
Trigger<> *client_connected_trigger_ = new Trigger<>();
|
Trigger<> *client_connected_trigger_ = new Trigger<>();
|
||||||
@@ -140,11 +156,14 @@ class VoiceAssistant : public Component {
|
|||||||
|
|
||||||
microphone::Microphone *mic_{nullptr};
|
microphone::Microphone *mic_{nullptr};
|
||||||
#ifdef USE_SPEAKER
|
#ifdef USE_SPEAKER
|
||||||
|
void write_speaker_();
|
||||||
speaker::Speaker *speaker_{nullptr};
|
speaker::Speaker *speaker_{nullptr};
|
||||||
uint8_t *speaker_buffer_;
|
uint8_t *speaker_buffer_;
|
||||||
size_t speaker_buffer_index_{0};
|
size_t speaker_buffer_index_{0};
|
||||||
size_t speaker_buffer_size_{0};
|
size_t speaker_buffer_size_{0};
|
||||||
|
size_t speaker_bytes_received_{0};
|
||||||
bool wait_for_stream_end_{false};
|
bool wait_for_stream_end_{false};
|
||||||
|
bool stream_ended_{false};
|
||||||
#endif
|
#endif
|
||||||
#ifdef USE_MEDIA_PLAYER
|
#ifdef USE_MEDIA_PLAYER
|
||||||
media_player::MediaPlayer *media_player_{nullptr};
|
media_player::MediaPlayer *media_player_{nullptr};
|
||||||
@@ -203,6 +222,11 @@ template<typename... Ts> class IsRunningCondition : public Condition<Ts...>, pub
|
|||||||
bool check(Ts... x) override { return this->parent_->is_running() || this->parent_->is_continuous(); }
|
bool check(Ts... x) override { return this->parent_->is_running() || this->parent_->is_continuous(); }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template<typename... Ts> class ConnectedCondition : public Condition<Ts...>, public Parented<VoiceAssistant> {
|
||||||
|
public:
|
||||||
|
bool check(Ts... x) override { return this->parent_->get_api_connection() != nullptr; }
|
||||||
|
};
|
||||||
|
|
||||||
extern VoiceAssistant *global_voice_assistant; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
extern VoiceAssistant *global_voice_assistant; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
||||||
|
|
||||||
} // namespace voice_assistant
|
} // namespace voice_assistant
|
||||||
|
|||||||
@@ -674,6 +674,11 @@ void WiFiComponent::wifi_process_event_(IDFWiFiEvent *data) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (it.number == 0) {
|
||||||
|
// no results
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
uint16_t number = it.number;
|
uint16_t number = it.number;
|
||||||
std::vector<wifi_ap_record_t> records(number);
|
std::vector<wifi_ap_record_t> records(number);
|
||||||
err = esp_wifi_scan_get_ap_records(&number, records.data());
|
err = esp_wifi_scan_get_ap_records(&number, records.data());
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
"""Constants used by esphome."""
|
"""Constants used by esphome."""
|
||||||
|
|
||||||
__version__ = "2023.11.0"
|
__version__ = "2023.11.6"
|
||||||
|
|
||||||
ALLOWED_NAME_CHARS = "abcdefghijklmnopqrstuvwxyz0123456789-_"
|
ALLOWED_NAME_CHARS = "abcdefghijklmnopqrstuvwxyz0123456789-_"
|
||||||
VALID_SUBSTITUTIONS_CHARACTERS = (
|
VALID_SUBSTITUTIONS_CHARACTERS = (
|
||||||
|
|||||||
@@ -973,6 +973,7 @@ class MDNSStatusThread(threading.Thread):
|
|||||||
self.host_name_to_filename: dict[str, str] = {}
|
self.host_name_to_filename: dict[str, str] = {}
|
||||||
# This is a set of host names to track (i.e no_mdns = false)
|
# This is a set of host names to track (i.e no_mdns = false)
|
||||||
self.host_name_with_mdns_enabled: set[set] = set()
|
self.host_name_with_mdns_enabled: set[set] = set()
|
||||||
|
self.zc: EsphomeZeroconf | None = None
|
||||||
self._refresh_hosts()
|
self._refresh_hosts()
|
||||||
|
|
||||||
def _refresh_hosts(self):
|
def _refresh_hosts(self):
|
||||||
@@ -996,7 +997,14 @@ class MDNSStatusThread(threading.Thread):
|
|||||||
# If we just adopted/imported this host, we likely
|
# If we just adopted/imported this host, we likely
|
||||||
# already have a state for it, so we should make sure
|
# already have a state for it, so we should make sure
|
||||||
# to set it so the dashboard shows it as online
|
# to set it so the dashboard shows it as online
|
||||||
if name in host_mdns_state:
|
if self.zc and (
|
||||||
|
entry.loaded_integrations and "api" not in entry.loaded_integrations
|
||||||
|
):
|
||||||
|
# No api available so we have to poll since
|
||||||
|
# the device won't respond to a request to ._esphomelib._tcp.local.
|
||||||
|
PING_RESULT[filename] = bool(self.zc.resolve_host(entry.name))
|
||||||
|
elif name in host_mdns_state:
|
||||||
|
# We already have a state for this host
|
||||||
PING_RESULT[filename] = host_mdns_state[name]
|
PING_RESULT[filename] = host_mdns_state[name]
|
||||||
|
|
||||||
# Make sure the mapping is up to date
|
# Make sure the mapping is up to date
|
||||||
@@ -1007,7 +1015,8 @@ class MDNSStatusThread(threading.Thread):
|
|||||||
def run(self):
|
def run(self):
|
||||||
global IMPORT_RESULT
|
global IMPORT_RESULT
|
||||||
|
|
||||||
zc = EsphomeZeroconf()
|
self.zc = EsphomeZeroconf()
|
||||||
|
zc = self.zc
|
||||||
host_mdns_state = self.host_mdns_state
|
host_mdns_state = self.host_mdns_state
|
||||||
host_name_to_filename = self.host_name_to_filename
|
host_name_to_filename = self.host_name_to_filename
|
||||||
host_name_with_mdns_enabled = self.host_name_with_mdns_enabled
|
host_name_with_mdns_enabled = self.host_name_with_mdns_enabled
|
||||||
|
|||||||
75
esphome/external_files.py
Normal file
75
esphome/external_files.py
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import logging
|
||||||
|
from pathlib import Path
|
||||||
|
import os
|
||||||
|
from datetime import datetime
|
||||||
|
import requests
|
||||||
|
import esphome.config_validation as cv
|
||||||
|
from esphome.core import CORE, TimePeriodSeconds
|
||||||
|
|
||||||
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
CODEOWNERS = ["@landonr"]
|
||||||
|
|
||||||
|
NETWORK_TIMEOUT = 30
|
||||||
|
|
||||||
|
IF_MODIFIED_SINCE = "If-Modified-Since"
|
||||||
|
CACHE_CONTROL = "Cache-Control"
|
||||||
|
CACHE_CONTROL_MAX_AGE = "max-age="
|
||||||
|
CONTENT_DISPOSITION = "content-disposition"
|
||||||
|
TEMP_DIR = "temp"
|
||||||
|
|
||||||
|
|
||||||
|
def has_remote_file_changed(url, local_file_path):
|
||||||
|
if os.path.exists(local_file_path):
|
||||||
|
_LOGGER.debug("has_remote_file_changed: File exists at %s", local_file_path)
|
||||||
|
try:
|
||||||
|
local_modification_time = os.path.getmtime(local_file_path)
|
||||||
|
local_modification_time_str = datetime.utcfromtimestamp(
|
||||||
|
local_modification_time
|
||||||
|
).strftime("%a, %d %b %Y %H:%M:%S GMT")
|
||||||
|
|
||||||
|
headers = {
|
||||||
|
IF_MODIFIED_SINCE: local_modification_time_str,
|
||||||
|
CACHE_CONTROL: CACHE_CONTROL_MAX_AGE + "3600",
|
||||||
|
}
|
||||||
|
response = requests.head(url, headers=headers, timeout=NETWORK_TIMEOUT)
|
||||||
|
|
||||||
|
_LOGGER.debug(
|
||||||
|
"has_remote_file_changed: File %s, Local modified %s, response code %d",
|
||||||
|
local_file_path,
|
||||||
|
local_modification_time_str,
|
||||||
|
response.status_code,
|
||||||
|
)
|
||||||
|
|
||||||
|
if response.status_code == 304:
|
||||||
|
_LOGGER.debug(
|
||||||
|
"has_remote_file_changed: File not modified since %s",
|
||||||
|
local_modification_time_str,
|
||||||
|
)
|
||||||
|
return False
|
||||||
|
_LOGGER.debug("has_remote_file_changed: File modified")
|
||||||
|
return True
|
||||||
|
except requests.exceptions.RequestException as e:
|
||||||
|
raise cv.Invalid(
|
||||||
|
f"Could not check if {url} has changed, please check if file exists "
|
||||||
|
f"({e})"
|
||||||
|
)
|
||||||
|
|
||||||
|
_LOGGER.debug("has_remote_file_changed: File doesn't exists at %s", local_file_path)
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def is_file_recent(file_path: str, refresh: TimePeriodSeconds) -> bool:
|
||||||
|
if os.path.exists(file_path):
|
||||||
|
creation_time = os.path.getctime(file_path)
|
||||||
|
current_time = datetime.now().timestamp()
|
||||||
|
return current_time - creation_time <= refresh.total_seconds
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def compute_local_file_dir(domain: str) -> Path:
|
||||||
|
base_directory = Path(CORE.data_dir) / domain
|
||||||
|
base_directory.mkdir(parents=True, exist_ok=True)
|
||||||
|
|
||||||
|
return base_directory
|
||||||
@@ -150,7 +150,11 @@ class EsphomeZeroconf(Zeroconf):
|
|||||||
def resolve_host(self, host: str, timeout: float = 3.0) -> str | None:
|
def resolve_host(self, host: str, timeout: float = 3.0) -> str | None:
|
||||||
"""Resolve a host name to an IP address."""
|
"""Resolve a host name to an IP address."""
|
||||||
name = host.partition(".")[0]
|
name = host.partition(".")[0]
|
||||||
info = HostResolver(ESPHOME_SERVICE_TYPE, f"{name}.{ESPHOME_SERVICE_TYPE}")
|
info = HostResolver(
|
||||||
|
ESPHOME_SERVICE_TYPE,
|
||||||
|
f"{name}.{ESPHOME_SERVICE_TYPE}",
|
||||||
|
server=f"{name}.local.",
|
||||||
|
)
|
||||||
if (
|
if (
|
||||||
info.load_from_cache(self)
|
info.load_from_cache(self)
|
||||||
or (timeout and info.request(self, timeout * 1000))
|
or (timeout and info.request(self, timeout * 1000))
|
||||||
|
|||||||
@@ -10,8 +10,9 @@ platformio==6.1.11 # When updating platformio, also update Dockerfile
|
|||||||
esptool==4.6.2
|
esptool==4.6.2
|
||||||
click==8.1.7
|
click==8.1.7
|
||||||
esphome-dashboard==20231107.0
|
esphome-dashboard==20231107.0
|
||||||
aioesphomeapi==18.4.0
|
aioesphomeapi==18.5.2
|
||||||
zeroconf==0.123.0
|
zeroconf==0.123.0
|
||||||
|
python-magic==0.4.27
|
||||||
|
|
||||||
# esp-idf requires this, but doesn't bundle it by default
|
# esp-idf requires this, but doesn't bundle it by default
|
||||||
# https://github.com/espressif/esp-idf/blob/220590d599e134d7a5e7f1e683cc4550349ffbf8/requirements.txt#L24
|
# https://github.com/espressif/esp-idf/blob/220590d599e134d7a5e7f1e683cc4550349ffbf8/requirements.txt#L24
|
||||||
|
|||||||
@@ -752,6 +752,18 @@ image:
|
|||||||
file: pnglogo.png
|
file: pnglogo.png
|
||||||
type: RGB565
|
type: RGB565
|
||||||
use_transparency: no
|
use_transparency: no
|
||||||
|
- id: web_svg_image
|
||||||
|
file: https://raw.githubusercontent.com/esphome/esphome-docs/a62d7ab193c1a464ed791670170c7d518189109b/images/logo.svg
|
||||||
|
resize: 256x48
|
||||||
|
type: TRANSPARENT_BINARY
|
||||||
|
- id: web_tiff_image
|
||||||
|
file: https://upload.wikimedia.org/wikipedia/commons/b/b6/SIPI_Jelly_Beans_4.1.07.tiff
|
||||||
|
type: RGB24
|
||||||
|
resize: 48x48
|
||||||
|
- id: web_redirect_image
|
||||||
|
file: https://avatars.githubusercontent.com/u/3060199?s=48&v=4
|
||||||
|
type: RGB24
|
||||||
|
resize: 48x48
|
||||||
|
|
||||||
- id: mdi_alert
|
- id: mdi_alert
|
||||||
file: mdi:alert-circle-outline
|
file: mdi:alert-circle-outline
|
||||||
|
|||||||
Reference in New Issue
Block a user