Compare commits

..

5 Commits

Author SHA1 Message Date
J. Nick Koston 1d2f0793d7 Bump habluetooth to 6.8.0 (#172577) 2026-05-29 18:11:51 +02:00
epenet 14fcb6c2d6 Import notify domain in notify tests (#172572) 2026-05-29 18:10:59 +02:00
Jan Bouwhuis 5763829b4b Silent migrate MQTT protocol version to version 5 if the broker supports it or raise an issue (#172500)
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
2026-05-29 17:33:11 +02:00
dependabot[bot] 7dfec6ef3d Bump docker/setup-buildx-action from 4.0.0 to 4.1.0 (#172526) 2026-05-29 16:22:05 +02:00
dependabot[bot] efe55f247a Bump docker/metadata-action from 6.0.0 to 6.1.0 (#172528) 2026-05-29 16:20:53 +02:00
19 changed files with 287 additions and 424 deletions
+2 -2
View File
@@ -380,7 +380,7 @@ jobs:
# 2025.12.0.dev202511250240 -> tags: 2025.12.0.dev202511250240, dev
- name: Generate Docker metadata
id: meta
uses: docker/metadata-action@030e881283bb7a6894de51c315a6bfe6a94e05cf # v6.0.0
uses: docker/metadata-action@80c7e94dd9b9319bd5eb7a0e0fe9291e23a2a2e9 # v6.1.0
with:
images: ${{ matrix.registry }}/home-assistant
sep-tags: ","
@@ -394,7 +394,7 @@ jobs:
type=semver,pattern={{major}}.{{minor}},value=${{ needs.init.outputs.version }},enable=${{ !contains(needs.init.outputs.version, 'd') && !contains(needs.init.outputs.version, 'b') }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@4d04d5d9486b7bd6fa91e7baf45bbb4f8b9deedd # v3.7.1
uses: docker/setup-buildx-action@d7f5e7f509e45cec5c76c4d5afdd7de93d0b3df5 # v3.7.1
- name: Copy architecture images to DockerHub
if: matrix.registry == 'docker.io/homeassistant'
@@ -11,6 +11,7 @@ from homeassistant.components.sensor import (
SensorStateClass,
)
from homeassistant.const import (
CONCENTRATION_PARTS_PER_CUBIC_METER,
PERCENTAGE,
UV_INDEX,
UnitOfIrradiance,
@@ -46,8 +47,6 @@ from .coordinator import (
PARALLEL_UPDATES = 1
PARTS_PER_CUBIC_METER = "p/m³"
@dataclass(frozen=True, kw_only=True)
class AccuWeatherSensorDescription(SensorEntityDescription):
@@ -82,7 +81,7 @@ FORECAST_SENSOR_TYPES: tuple[AccuWeatherSensorDescription, ...] = (
AccuWeatherSensorDescription(
key="Grass",
entity_registry_enabled_default=False,
native_unit_of_measurement=PARTS_PER_CUBIC_METER,
native_unit_of_measurement=CONCENTRATION_PARTS_PER_CUBIC_METER,
value_fn=lambda data: cast(int, data[ATTR_VALUE]),
attr_fn=lambda data: {
ATTR_LEVEL: POLLEN_CATEGORY_MAP[data[ATTR_CATEGORY_VALUE]]
@@ -108,7 +107,7 @@ FORECAST_SENSOR_TYPES: tuple[AccuWeatherSensorDescription, ...] = (
AccuWeatherSensorDescription(
key="Mold",
entity_registry_enabled_default=False,
native_unit_of_measurement=PARTS_PER_CUBIC_METER,
native_unit_of_measurement=CONCENTRATION_PARTS_PER_CUBIC_METER,
value_fn=lambda data: cast(int, data[ATTR_VALUE]),
attr_fn=lambda data: {
ATTR_LEVEL: POLLEN_CATEGORY_MAP[data[ATTR_CATEGORY_VALUE]]
@@ -117,7 +116,7 @@ FORECAST_SENSOR_TYPES: tuple[AccuWeatherSensorDescription, ...] = (
),
AccuWeatherSensorDescription(
key="Ragweed",
native_unit_of_measurement=PARTS_PER_CUBIC_METER,
native_unit_of_measurement=CONCENTRATION_PARTS_PER_CUBIC_METER,
entity_registry_enabled_default=False,
value_fn=lambda data: cast(int, data[ATTR_VALUE]),
attr_fn=lambda data: {
@@ -185,7 +184,7 @@ FORECAST_SENSOR_TYPES: tuple[AccuWeatherSensorDescription, ...] = (
),
AccuWeatherSensorDescription(
key="Tree",
native_unit_of_measurement=PARTS_PER_CUBIC_METER,
native_unit_of_measurement=CONCENTRATION_PARTS_PER_CUBIC_METER,
entity_registry_enabled_default=False,
value_fn=lambda data: cast(int, data[ATTR_VALUE]),
attr_fn=lambda data: {
@@ -21,6 +21,6 @@
"bluetooth-auto-recovery==1.6.4",
"bluetooth-data-tools==1.29.18",
"dbus-fast==5.0.16",
"habluetooth==6.7.9"
"habluetooth==6.8.0"
]
}
+43 -20
View File
@@ -14,6 +14,7 @@ from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
CONF_DISCOVERY,
CONF_PLATFORM,
CONF_PORT,
CONF_PROTOCOL,
SERVICE_RELOAD,
)
@@ -50,6 +51,7 @@ from .client import (
async_subscribe_internal,
publish,
subscribe,
try_connection,
)
from .config import MQTT_BASE_SCHEMA, MQTT_RO_SCHEMA, MQTT_RW_SCHEMA
from .config_integration import CONFIG_SCHEMA_BASE
@@ -79,14 +81,15 @@ from .const import (
CONFIG_ENTRY_VERSION,
DEFAULT_DISCOVERY,
DEFAULT_ENCODING,
DEFAULT_PORT,
DEFAULT_PREFIX,
DEFAULT_PROTOCOL,
DEFAULT_QOS,
DEFAULT_RETAIN,
DOMAIN,
ENTITY_PLATFORMS,
ENTRY_OPTION_FIELDS,
MQTT_CONNECTION_STATE,
PROTOCOL_5,
PROTOCOL_311,
TEMPLATE_ERRORS,
Platform,
@@ -496,25 +499,45 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Load a config entry."""
mqtt_data: MqttData
if (protocol := entry.data.get(CONF_PROTOCOL, PROTOCOL_311)) != DEFAULT_PROTOCOL:
broker: str = entry.data[CONF_BROKER]
async_create_issue(
hass,
DOMAIN,
"protocol_5_migration",
issue_domain=DOMAIN,
is_fixable=True,
breaks_in_ha_version="2027.1.0",
severity=IssueSeverity.WARNING,
learn_more_url="https://www.home-assistant.io/integrations/mqtt/#mqtt-protocol",
data={
"entry_id": entry.entry_id,
"broker": broker,
"protocol": protocol,
},
translation_placeholders={"broker": broker, "protocol": protocol},
translation_key="protocol_5_migration",
)
if (protocol := entry.data.get(CONF_PROTOCOL, PROTOCOL_311)) != PROTOCOL_5:
# Automatically migrate the broker protocol to v5 if possible
# Can be removed with HA Core 2027.1
new_entry_data = entry.data.copy()
new_entry_data[CONF_PROTOCOL] = PROTOCOL_5
# Try the connection with protocol version 5
# And update the protocol if successful
if await hass.async_add_executor_job(
try_connection,
{CONF_PORT: DEFAULT_PORT} | new_entry_data,
):
hass.config_entries.async_update_entry(
entry,
data=new_entry_data,
)
ir.async_delete_issue(hass, DOMAIN, "protocol_5_migration")
_LOGGER.info(
"The MQTT protocol version was successfully updated to version 5"
)
else:
broker: str = entry.data[CONF_BROKER]
async_create_issue(
hass,
DOMAIN,
"protocol_5_migration",
issue_domain=DOMAIN,
is_fixable=False,
breaks_in_ha_version="2027.1.0",
severity=IssueSeverity.WARNING,
learn_more_url="https://www.home-assistant.io/integrations/mqtt/"
"#mqtt-protocol",
data={
"entry_id": entry.entry_id,
"broker": broker,
"protocol": protocol,
},
translation_placeholders={"broker": broker, "protocol": protocol},
translation_key="protocol_5_migration",
)
async def _setup_client() -> tuple[MqttData, dict[str, Any]]:
"""Set up the MQTT client."""
+37
View File
@@ -9,6 +9,7 @@ from functools import lru_cache, partial
from itertools import chain, groupby
import logging
from operator import attrgetter
import queue
import socket
import ssl
import time
@@ -92,6 +93,8 @@ from .util import EnsureJobAfterCooldown, get_file_path, mqtt_config_entry_enabl
_LOGGER = logging.getLogger(__name__)
MQTT_TIMEOUT = 5
MIN_BUFFER_SIZE = 131072 # Minimum buffer size to use if preferred size fails
PREFERRED_BUFFER_SIZE = 8 * 1024 * 1024 # Set receive buffer size to 8MiB
@@ -433,6 +436,40 @@ class MqttClientSetup:
return self._client
def try_connection(
user_input: dict[str, Any],
) -> bool:
"""Test if we can connect to an MQTT broker."""
mqtt_client_setup = MqttClientSetup(user_input)
mqtt_client_setup.setup()
client = mqtt_client_setup.client
result: queue.Queue[bool] = queue.Queue(maxsize=1)
def on_connect(
_mqttc: mqtt.Client,
_userdata: None,
_connect_flags: mqtt.ConnectFlags,
reason_code: mqtt.ReasonCode,
_properties: mqtt.Properties | None = None,
) -> None:
"""Handle connection result."""
result.put(not reason_code.is_failure)
client.on_connect = on_connect
client.connect_async(user_input[CONF_BROKER], user_input[CONF_PORT])
client.loop_start()
try:
return result.get(timeout=MQTT_TIMEOUT)
except queue.Empty:
return False
finally:
client.disconnect()
client.loop_stop()
class MQTT:
"""Home Assistant MQTT client."""
+1 -39
View File
@@ -8,7 +8,6 @@ from dataclasses import dataclass
from enum import IntEnum
import json
import logging
import queue
from ssl import PROTOCOL_TLS_CLIENT, SSLContext, SSLError
from types import MappingProxyType
from typing import TYPE_CHECKING, Any, cast
@@ -22,7 +21,6 @@ from cryptography.hazmat.primitives.serialization import (
load_pem_private_key,
)
from cryptography.x509 import load_der_x509_certificate, load_pem_x509_certificate
import paho.mqtt.client as mqtt
import voluptuous as vol
import yaml
@@ -143,7 +141,7 @@ from homeassistant.util.json import JSON_DECODE_EXCEPTIONS, json_loads
from homeassistant.util.unit_conversion import TemperatureConverter
from .addon import get_addon_manager
from .client import MqttClientSetup
from .client import try_connection
from .const import (
ALARM_CONTROL_PANEL_SUPPORTED_FEATURES,
ATTR_PAYLOAD,
@@ -444,8 +442,6 @@ ADDON_SETUP_TIMEOUT_ROUNDS = 5
CONF_CLIENT_KEY_PASSWORD = "client_key_password"
MQTT_TIMEOUT = 5
ADVANCED_OPTIONS = "advanced_options"
SET_CA_CERT = "set_ca_cert"
SET_CLIENT_CERT = "set_client_cert"
@@ -5581,40 +5577,6 @@ async def async_get_broker_settings(
return False
def try_connection(
user_input: dict[str, Any],
) -> bool:
"""Test if we can connect to an MQTT broker."""
mqtt_client_setup = MqttClientSetup(user_input)
mqtt_client_setup.setup()
client = mqtt_client_setup.client
result: queue.Queue[bool] = queue.Queue(maxsize=1)
def on_connect(
_mqttc: mqtt.Client,
_userdata: None,
_connect_flags: mqtt.ConnectFlags,
reason_code: mqtt.ReasonCode,
_properties: mqtt.Properties | None = None,
) -> None:
"""Handle connection result."""
result.put(not reason_code.is_failure)
client.on_connect = on_connect
client.connect_async(user_input[CONF_BROKER], user_input[CONF_PORT])
client.loop_start()
try:
return result.get(timeout=MQTT_TIMEOUT)
except queue.Empty:
return False
finally:
client.disconnect()
client.loop_stop()
def check_certicate_chain() -> str | None:
"""Check the MQTT certificates."""
if client_certificate := get_file_path(CONF_CLIENT_CERT):
+1 -56
View File
@@ -5,12 +5,10 @@ from typing import TYPE_CHECKING
import voluptuous as vol
from homeassistant.components.repairs import RepairsFlow, RepairsFlowResult
from homeassistant.const import CONF_PORT, CONF_PROTOCOL
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr
from .config_flow import try_connection
from .const import DEFAULT_PORT, DOMAIN, PROTOCOL_5
from .const import DOMAIN
URL_MQTT_BROKER_CONFIGURATION = (
"https://www.home-assistant.io/integrations/mqtt/#broker-configuration"
@@ -55,55 +53,6 @@ class MQTTDeviceEntryMigration(RepairsFlow):
)
class MQTTProtocolV5Migration(RepairsFlow):
"""Handler to migrate to MQTT protocol version 5."""
def __init__(self, entry_id: str, broker: str, protocol: str) -> None:
"""Initialize the flow."""
self.entry_id = entry_id
self.broker = broker
self.protocol = protocol
async def async_step_init(
self, user_input: dict[str, str] | None = None
) -> RepairsFlowResult:
"""Handle the first step of a fix flow."""
return await self.async_step_confirm()
async def async_step_confirm(
self, user_input: dict[str, str] | None = None
) -> RepairsFlowResult:
"""Handle the confirm step of a fix flow."""
if user_input is not None:
entry = self.hass.config_entries.async_get_entry(self.entry_id)
if TYPE_CHECKING:
assert entry is not None
new_entry_data = entry.data.copy()
new_entry_data[CONF_PROTOCOL] = PROTOCOL_5
# Try the connection with protocol version 5
if await self.hass.async_add_executor_job(
try_connection,
{CONF_PORT: DEFAULT_PORT} | new_entry_data,
):
self.hass.config_entries.async_update_entry(entry, data=new_entry_data)
return self.async_create_entry(data={})
return self.async_abort(
reason="mqtt_broker_migration_to_v5_failed",
description_placeholders={
"broker": self.broker,
"protocol": self.protocol,
"url_mqtt_broker_configuration": URL_MQTT_BROKER_CONFIGURATION,
},
)
return self.async_show_form(
step_id="confirm",
data_schema=vol.Schema({}),
description_placeholders={"broker": self.broker, "protocol": self.protocol},
)
async def async_create_fix_flow(
hass: HomeAssistant,
issue_id: str,
@@ -113,10 +62,6 @@ async def async_create_fix_flow(
if TYPE_CHECKING:
assert data is not None
entry_id: str = data["entry_id"] # type: ignore[assignment]
if issue_id == "protocol_5_migration":
broker: str = data["broker"] # type: ignore[assignment]
protocol: str = data["protocol"] # type: ignore[assignment]
return MQTTProtocolV5Migration(entry_id, broker, protocol)
subentry_id: str = data["subentry_id"] # type: ignore[assignment]
name: str = data["name"] # type: ignore[assignment]
return MQTTDeviceEntryMigration(
+3 -13
View File
@@ -56,7 +56,7 @@
"keepalive": "A value less than 90 seconds is advised.",
"password": "The password to log in to your MQTT broker.",
"port": "The port your MQTT broker listens to. For example 1883.",
"protocol": "The MQTT protocol your broker operates at. For example 3.1.1.",
"protocol": "The MQTT protocol version that is used. Note that Home Assistant will silently change to version 5 if your broker supports it.",
"set_ca_cert": "Select **Auto** for automatic CA validation, or **Custom** and select **Next** to set a custom CA certificate, to allow validating your MQTT broker's certificate.",
"set_client_cert": "Enable and select **Next** to set a client certificate and private key to authenticate against your MQTT broker.",
"tls_insecure": "Option to ignore validation of your MQTT broker's certificate.",
@@ -1134,18 +1134,8 @@
"title": "Invalid config found for MQTT {domain} item"
},
"protocol_5_migration": {
"fix_flow": {
"abort": {
"mqtt_broker_migration_to_v5_failed": "Migrating the broker ({broker}) protocol version from {protocol} to 5 failed, and the migration has been aborted.\n\nYour broker may not support MQTT protocol version 5.\n\nPlease [reconfigure your MQTT broker settings]({url_mqtt_broker_configuration}) or upgrade your broker to support MQTT protocol version 5 to fix this issue."
},
"step": {
"confirm": {
"description": "Home Assistant needs to migrate to MQTT protocol version 5. The currently configured protocol version for broker {broker} is {protocol}. This protocol version is deprecated, and support for it will be removed.\n\nSubmitting this form will attempt to migrate your MQTT broker configuration to use protocol version 5 to fix this issue. If the broker cannot be reached using MQTT protocol version 5, for example because it does not support it, the migration will be aborted.",
"title": "MQTT protocol change required"
}
}
},
"title": "Deprecated MQTT protocol {protocol} in use"
"description": "The automatic migration to MQTT protocol version 5 failed. The currently configured protocol version for MQTT broker {broker} is {protocol}, but this protocol version is deprecated, and support for it will be removed.\n\nMake sure your broker supports protocol version 5. Update your MQTT broker's connection settings, and restart Home Assistant to fix this issue.",
"title": "MQTT protocol migration failed"
},
"subentry_migration_discovery": {
"fix_flow": {
+1 -17
View File
@@ -1,16 +1,9 @@
"""Constants used by Home Assistant components."""
from enum import StrEnum
from functools import partial
from typing import TYPE_CHECKING, Final
from .generated.entity_platforms import EntityPlatforms
from .helpers.deprecation import (
DeprecatedConstant,
all_with_deprecated_constants,
check_if_deprecated_constant,
dir_with_deprecated_constants,
)
from .util.event_type import EventType
from .util.hass_dict import HassKey
from .util.signal_type import SignalType
@@ -765,9 +758,7 @@ CONCENTRATION_GRAMS_PER_CUBIC_METER: Final = "g/m³"
CONCENTRATION_MILLIGRAMS_PER_CUBIC_METER: Final = "mg/m³"
CONCENTRATION_MICROGRAMS_PER_CUBIC_METER: Final = "μg/m³"
CONCENTRATION_MICROGRAMS_PER_CUBIC_FOOT: Final = "μg/ft³"
_DEPRECATED_CONCENTRATION_PARTS_PER_CUBIC_METER = DeprecatedConstant(
"p/m³", "p/m³", "2027.7"
)
CONCENTRATION_PARTS_PER_CUBIC_METER: Final = "p/m³"
CONCENTRATION_PARTS_PER_MILLION: Final = "ppm"
CONCENTRATION_PARTS_PER_BILLION: Final = "ppb"
@@ -1001,10 +992,3 @@ FORMAT_DATETIME: Final = f"{FORMAT_DATE} {FORMAT_TIME}"
# This is not a hard limit, but caches and other
# data structures will be pre-allocated to this size
MAX_EXPECTED_ENTITY_IDS: Final = 16384
# These can be removed if no deprecated constants are in this module anymore
__getattr__ = partial(check_if_deprecated_constant, module_globals=globals())
__dir__ = partial(
dir_with_deprecated_constants, module_globals_keys=[*globals().keys()]
)
__all__ = all_with_deprecated_constants(globals())
+1 -1
View File
@@ -35,7 +35,7 @@ file-read-backwards==2.0.0
fnv-hash-fast==2.0.3
go2rtc-client==0.4.0
ha-ffmpeg==3.2.2
habluetooth==6.7.9
habluetooth==6.8.0
hass-nabucasa==2.2.0
hassil==3.5.0
home-assistant-bluetooth==2.0.0
+1 -1
View File
@@ -1213,7 +1213,7 @@ ha-xthings-cloud==1.0.5
habiticalib==0.4.7
# homeassistant.components.bluetooth
habluetooth==6.7.9
habluetooth==6.8.0
# homeassistant.components.hanna
hanna-cloud==0.0.7
+26 -27
View File
@@ -6,11 +6,10 @@ from unittest.mock import MagicMock, patch
import pytest
from homeassistant.components.notify import DOMAIN as NOTIFY_DOMAIN
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
BASE_COMPONENT = "notify"
@pytest.fixture(autouse=True)
def reset_log_level():
@@ -26,25 +25,25 @@ async def test_apprise_config_load_fail01(hass: HomeAssistant) -> None:
"""Test apprise configuration failures 1."""
config = {
BASE_COMPONENT: {"name": "test", "platform": "apprise", "config": "/path/"}
NOTIFY_DOMAIN: {"name": "test", "platform": "apprise", "config": "/path/"}
}
with patch(
"homeassistant.components.apprise.notify.apprise.AppriseConfig.add",
return_value=False,
):
assert await async_setup_component(hass, BASE_COMPONENT, config)
assert await async_setup_component(hass, NOTIFY_DOMAIN, config)
await hass.async_block_till_done()
# Test that our service failed to load
assert not hass.services.has_service(BASE_COMPONENT, "test")
assert not hass.services.has_service(NOTIFY_DOMAIN, "test")
async def test_apprise_config_load_fail02(hass: HomeAssistant) -> None:
"""Test apprise configuration failures 2."""
config = {
BASE_COMPONENT: {"name": "test", "platform": "apprise", "config": "/path/"}
NOTIFY_DOMAIN: {"name": "test", "platform": "apprise", "config": "/path/"}
}
with (
@@ -57,11 +56,11 @@ async def test_apprise_config_load_fail02(hass: HomeAssistant) -> None:
return_value=True,
),
):
assert await async_setup_component(hass, BASE_COMPONENT, config)
assert await async_setup_component(hass, NOTIFY_DOMAIN, config)
await hass.async_block_till_done()
# Test that our service failed to load
assert not hass.services.has_service(BASE_COMPONENT, "test")
assert not hass.services.has_service(NOTIFY_DOMAIN, "test")
async def test_apprise_config_load_okay(hass: HomeAssistant, tmp_path: Path) -> None:
@@ -73,20 +72,20 @@ async def test_apprise_config_load_okay(hass: HomeAssistant, tmp_path: Path) ->
f = d / "apprise"
f.write_text("mailto://user:pass@example.com/")
config = {BASE_COMPONENT: {"name": "test", "platform": "apprise", "config": str(f)}}
config = {NOTIFY_DOMAIN: {"name": "test", "platform": "apprise", "config": str(f)}}
assert await async_setup_component(hass, BASE_COMPONENT, config)
assert await async_setup_component(hass, NOTIFY_DOMAIN, config)
await hass.async_block_till_done()
# Valid configuration was loaded; our service is good
assert hass.services.has_service(BASE_COMPONENT, "test")
assert hass.services.has_service(NOTIFY_DOMAIN, "test")
async def test_apprise_url_load_fail(hass: HomeAssistant) -> None:
"""Test apprise url failure."""
config = {
BASE_COMPONENT: {
NOTIFY_DOMAIN: {
"name": "test",
"platform": "apprise",
"url": "mailto://user:pass@example.com",
@@ -96,18 +95,18 @@ async def test_apprise_url_load_fail(hass: HomeAssistant) -> None:
"homeassistant.components.apprise.notify.apprise.Apprise.add",
return_value=False,
):
assert await async_setup_component(hass, BASE_COMPONENT, config)
assert await async_setup_component(hass, NOTIFY_DOMAIN, config)
await hass.async_block_till_done()
# Test that our service failed to load
assert not hass.services.has_service(BASE_COMPONENT, "test")
assert not hass.services.has_service(NOTIFY_DOMAIN, "test")
async def test_apprise_notification(hass: HomeAssistant) -> None:
"""Test apprise notification."""
config = {
BASE_COMPONENT: {
NOTIFY_DOMAIN: {
"name": "test",
"platform": "apprise",
"url": "mailto://user:pass@example.com",
@@ -124,18 +123,18 @@ async def test_apprise_notification(hass: HomeAssistant) -> None:
obj.add.return_value = True
obj.notify.return_value = True
mock_apprise.return_value = obj
assert await async_setup_component(hass, BASE_COMPONENT, config)
assert await async_setup_component(hass, NOTIFY_DOMAIN, config)
await hass.async_block_till_done()
# Test the existence of our service
assert hass.services.has_service(BASE_COMPONENT, "test")
assert hass.services.has_service(NOTIFY_DOMAIN, "test")
# Test the call to our underlining notify() call
await hass.services.async_call(BASE_COMPONENT, "test", data)
await hass.services.async_call(NOTIFY_DOMAIN, "test", data)
await hass.async_block_till_done()
# Validate calls were made under the hood correctly
obj.add.assert_called_once_with(config[BASE_COMPONENT]["url"])
obj.add.assert_called_once_with(config[NOTIFY_DOMAIN]["url"])
obj.notify.assert_called_once_with(
body=data["message"], title=data["title"], tag=None
)
@@ -145,7 +144,7 @@ async def test_apprise_multiple_notification(hass: HomeAssistant) -> None:
"""Test apprise notification."""
config = {
BASE_COMPONENT: {
NOTIFY_DOMAIN: {
"name": "test",
"platform": "apprise",
"url": [
@@ -165,14 +164,14 @@ async def test_apprise_multiple_notification(hass: HomeAssistant) -> None:
obj.add.return_value = True
obj.notify.return_value = True
mock_apprise.return_value = obj
assert await async_setup_component(hass, BASE_COMPONENT, config)
assert await async_setup_component(hass, NOTIFY_DOMAIN, config)
await hass.async_block_till_done()
# Test the existence of our service
assert hass.services.has_service(BASE_COMPONENT, "test")
assert hass.services.has_service(NOTIFY_DOMAIN, "test")
# Test the call to our underlining notify() call
await hass.services.async_call(BASE_COMPONENT, "test", data)
await hass.services.async_call(NOTIFY_DOMAIN, "test", data)
await hass.async_block_till_done()
# Validate 2 calls were made under the hood
@@ -196,7 +195,7 @@ async def test_apprise_notification_with_target(
f.write_text("devops=mailto://user:pass@example.com/\r\n")
f.write_text("system,alert=syslog://\r\n")
config = {BASE_COMPONENT: {"name": "test", "platform": "apprise", "config": str(f)}}
config = {NOTIFY_DOMAIN: {"name": "test", "platform": "apprise", "config": str(f)}}
# Our Message, only notify the services tagged with "devops"
data = {"title": "Test Title", "message": "Test Message", "target": ["devops"]}
@@ -208,14 +207,14 @@ async def test_apprise_notification_with_target(
apprise_obj.add.return_value = True
apprise_obj.notify.return_value = True
mock_apprise.return_value = apprise_obj
assert await async_setup_component(hass, BASE_COMPONENT, config)
assert await async_setup_component(hass, NOTIFY_DOMAIN, config)
await hass.async_block_till_done()
# Test the existence of our service
assert hass.services.has_service(BASE_COMPONENT, "test")
assert hass.services.has_service(NOTIFY_DOMAIN, "test")
# Test the call to our underlining notify() call
await hass.services.async_call(BASE_COMPONENT, "test", data)
await hass.services.async_call(NOTIFY_DOMAIN, "test", data)
await hass.async_block_till_done()
# Validate calls were made under the hood correctly
+19 -1
View File
@@ -5,7 +5,7 @@ from collections.abc import AsyncGenerator, Generator
from pathlib import Path
from random import getrandbits
from typing import Any
from unittest.mock import AsyncMock, patch
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
@@ -33,6 +33,24 @@ def patch_hass_config(mock_hass_config: None) -> None:
"""Patch configuration.yaml."""
@pytest.fixture
def mock_v5_protocol_check() -> bool:
"""Fixture to mock a v5 protocol test result."""
return True
@pytest.fixture(autouse=True)
def mock_try_connection_protocol_check(
hass: HomeAssistant, mock_v5_protocol_check: bool
) -> Generator[MagicMock]:
"""Patch try_connection."""
with patch(
"homeassistant.components.mqtt.try_connection",
return_value=mock_v5_protocol_check,
) as mock_try_connection:
yield mock_try_connection
@pytest.fixture
def temp_dir_prefix() -> str:
"""Set an alternate temp dir prefix."""
+11 -4
View File
@@ -230,6 +230,7 @@ async def test_publish(
assert publish_mock.call_args[0][4].json() == {"MessageExpiryInterval": 60}
@pytest.mark.parametrize("mock_v5_protocol_check", [False])
@pytest.mark.parametrize(
("mqtt_config_entry_options", "mqtt_config_entry_data", "protocol"),
[
@@ -1257,7 +1258,12 @@ async def test_restore_subscriptions_on_reconnect(
@pytest.mark.parametrize(
("mqtt_config_entry_data", "mqtt_config_entry_options"),
[({mqtt.CONF_BROKER: "mock-broker"}, {mqtt.CONF_DISCOVERY: False})],
[
(
{mqtt.CONF_BROKER: "mock-broker", mqtt.CONF_PROTOCOL: "5"},
{mqtt.CONF_DISCOVERY: False},
)
],
)
async def test_restore_all_active_subscriptions_on_reconnect(
hass: HomeAssistant,
@@ -1277,7 +1283,7 @@ async def test_restore_all_active_subscriptions_on_reconnect(
# the subscription with the highest QoS should survive
expected = [
call([("test/state", 2)], properties=None),
call([("test/state", 2)], properties=ANY),
]
assert mqtt_client_mock.subscribe.mock_calls == expected
@@ -1291,7 +1297,7 @@ async def test_restore_all_active_subscriptions_on_reconnect(
# wait for cooldown
await mock_debouncer.wait()
expected.append(call([("test/state", 1)], properties=None))
expected.append(call([("test/state", 1)], properties=ANY))
for expected_call in expected:
assert mqtt_client_mock.subscribe.hass_call(expected_call)
@@ -1549,6 +1555,7 @@ async def test_handle_message_callback(
assert callbacks[0].payload == "test-payload"
@pytest.mark.parametrize("mock_v5_protocol_check", [False])
@pytest.mark.parametrize(
("mqtt_config_entry_data", "protocol", "clean_session"),
[
@@ -1582,7 +1589,6 @@ async def test_handle_message_callback(
async def test_setup_mqtt_client_clean_session_and_protocol(
hass: HomeAssistant,
mqtt_mock_entry: MqttMockHAClientGenerator,
mqtt_client_mock: MqttMockPahoClient,
protocol: int,
clean_session: bool | None,
) -> None:
@@ -1597,6 +1603,7 @@ async def test_setup_mqtt_client_clean_session_and_protocol(
assert mock_client.call_args[1]["protocol"] == protocol
@pytest.mark.parametrize("mock_v5_protocol_check", [False])
@pytest.mark.parametrize(
("mqtt_config_entry_data", "connect_args"),
[
+3 -1
View File
@@ -130,6 +130,7 @@ MOCK_ENCRYPTED_CLIENT_KEY_DER = b"## mock DER formatted encrypted key file ##\n"
MOCK_ENTRY_DATA = {
mqtt.CONF_BROKER: "test-broker",
CONF_PROTOCOL: "5",
CONF_PORT: 1234,
CONF_USERNAME: "user",
CONF_PASSWORD: "pass",
@@ -273,7 +274,7 @@ def mock_try_connection_time_out() -> Generator[MagicMock]:
# Patch prevent waiting 5 sec for a timeout
with (
patch("homeassistant.components.mqtt.client.AsyncMQTTClient") as mock_client,
patch("homeassistant.components.mqtt.config_flow.MQTT_TIMEOUT", 0),
patch("homeassistant.components.mqtt.client.MQTT_TIMEOUT", 0),
):
mock_client().loop_start = lambda *args: 1
yield mock_client()
@@ -1756,6 +1757,7 @@ async def test_step_hassio_reauth(
mock_try_connection.assert_called_once_with(
{
"broker": "core-mosquitto",
CONF_PROTOCOL: "5",
"port": 1883,
"username": "mock-user",
"password": "mock-pass",
+125
View File
@@ -5,6 +5,7 @@ from copy import deepcopy
from datetime import datetime, timedelta
from functools import partial
import json
import logging
import time
from typing import Any, TypedDict
from unittest.mock import ANY, MagicMock, Mock, mock_open, patch
@@ -28,6 +29,7 @@ from homeassistant.components.sensor import SensorDeviceClass
from homeassistant.config_entries import ConfigEntryDisabler, ConfigEntryState
from homeassistant.const import (
ATTR_ASSUMED_STATE,
CONF_PORT,
CONF_PROTOCOL,
SERVICE_RELOAD,
STATE_UNAVAILABLE,
@@ -53,6 +55,7 @@ from tests.common import (
MockEntity,
MockEntityPlatform,
MockMqttReasonCode,
async_capture_events,
async_fire_mqtt_message,
async_fire_time_changed,
mock_restore_cache,
@@ -2470,3 +2473,125 @@ async def test_yaml_config_with_active_mqtt_config_entry(
state = hass.states.get("sensor.mqtt_sensor")
assert state is not None
assert issue is None
@pytest.mark.parametrize(
"mqtt_config_entry_data",
[
{
mqtt.CONF_BROKER: "mock-broker",
},
{
mqtt.CONF_BROKER: "mock-broker",
CONF_PORT: 1883,
},
{
mqtt.CONF_BROKER: "mock-broker",
CONF_PROTOCOL: "3.1.1",
CONF_PORT: 1883,
},
{
mqtt.CONF_BROKER: "mock-broker",
CONF_PROTOCOL: "3.1",
CONF_PORT: 1883,
},
],
ids=[
"entry_without_protocol_without_port",
"entry_without_protocol_with_port",
"entry_with_protocol_3.1.1",
"entry_with_protocol_3.1",
],
)
async def test_mqtt_protocol_successful_migration_to_v5(
hass: HomeAssistant,
mqtt_mock_entry: MqttMockHAClientGenerator,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test the silent MQTT protocol migration is successful."""
assert await async_setup_component(hass, "repairs", {})
events = async_capture_events(hass, ir.EVENT_REPAIRS_ISSUE_REGISTRY_UPDATED)
with caplog.at_level(logging.INFO):
await mqtt_mock_entry()
assert len(events) == 0
assert (
"The MQTT protocol version was successfully updated to version 5"
in caplog.text
)
entry = hass.config_entries.async_entries(mqtt.DOMAIN)[0]
assert entry.data[mqtt.CONF_PROTOCOL] == mqtt.PROTOCOL_5
@pytest.mark.parametrize("mock_v5_protocol_check", [False])
@pytest.mark.parametrize(
("mqtt_config_entry_data", "current_protocol"),
[
(
{
mqtt.CONF_BROKER: "mock-broker",
},
"3.1.1",
),
(
{
mqtt.CONF_BROKER: "mock-broker",
CONF_PORT: 1883,
},
"3.1.1",
),
(
{
mqtt.CONF_BROKER: "mock-broker",
CONF_PROTOCOL: "3.1.1",
CONF_PORT: 1883,
},
"3.1.1",
),
(
{
mqtt.CONF_BROKER: "mock-broker",
CONF_PROTOCOL: "3.1",
CONF_PORT: 1883,
},
"3.1",
),
],
ids=[
"entry_without_protocol_without_port",
"entry_without_protocol_with_port",
"entry_with_protocol_3.1.1",
"entry_with_protocol_3.1",
],
)
async def test_mqtt_protocol_failed_migration_to_v5(
hass: HomeAssistant,
mqtt_mock_entry: MqttMockHAClientGenerator,
caplog: pytest.LogCaptureFixture,
current_protocol: str,
) -> None:
"""Test failed silent MQTT protocol migration creates a repair issue."""
assert await async_setup_component(hass, "repairs", {})
events = async_capture_events(hass, ir.EVENT_REPAIRS_ISSUE_REGISTRY_UPDATED)
await mqtt_mock_entry()
assert len(events) == 1
assert events[0].data["issue_id"] == "protocol_5_migration"
issue_registry = ir.async_get(hass)
assert len(issue_registry.issues) == 1
issue = issue_registry.async_get_issue(mqtt.DOMAIN, "protocol_5_migration")
assert issue is not None
assert issue.translation_key == "protocol_5_migration"
assert issue.translation_placeholders == {
"broker": "mock-broker",
"protocol": current_protocol,
}
assert (
"The MQTT protocol version was successfully updated to version 5"
not in caplog.text
)
entry = hass.config_entries.async_entries(mqtt.DOMAIN)[0]
assert entry.data.get(mqtt.CONF_PROTOCOL, mqtt.PROTOCOL_311) == current_protocol
+3 -191
View File
@@ -1,19 +1,18 @@
"""Test repairs for MQTT."""
from collections.abc import Coroutine, Generator
from collections.abc import Coroutine
from copy import deepcopy
from typing import Any
from unittest.mock import MagicMock, patch
from unittest.mock import patch
import pytest
from homeassistant.components import mqtt
from homeassistant.config_entries import ConfigSubentry, ConfigSubentryData
from homeassistant.const import CONF_PORT, CONF_PROTOCOL, SERVICE_RELOAD
from homeassistant.const import SERVICE_RELOAD
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
from homeassistant.helpers import device_registry as dr, issue_registry as ir
from homeassistant.setup import async_setup_component
from homeassistant.util.yaml import parse_yaml
from .common import MOCK_NOTIFY_SUBENTRY_DATA_MULTI, async_fire_mqtt_message
@@ -28,13 +27,6 @@ from tests.conftest import ClientSessionGenerator
from tests.typing import MqttMockHAClientGenerator
@pytest.fixture
def mock_try_connection() -> Generator[MagicMock]:
"""Mock the try connection method."""
with patch("homeassistant.components.mqtt.repairs.try_connection") as mock_try:
yield mock_try
async def help_setup_yaml(hass: HomeAssistant, config: dict[str, str]) -> None:
"""Help to set up an exported MQTT device via YAML."""
with patch(
@@ -185,183 +177,3 @@ async def test_subentry_reconfigure_export_settings(
device = device_registry.async_get_device(identifiers={(mqtt.DOMAIN, subentry_id)})
assert device.config_entries_subentries[config_entry.entry_id] == {None}
assert device is not None
@pytest.mark.parametrize(
("mqtt_config_entry_data", "current_protocol"),
[
(
{
mqtt.CONF_BROKER: "mock-broker",
},
"3.1.1",
),
(
{
mqtt.CONF_BROKER: "mock-broker",
CONF_PORT: 1883,
},
"3.1.1",
),
(
{
mqtt.CONF_BROKER: "mock-broker",
CONF_PROTOCOL: "3.1.1",
CONF_PORT: 1883,
},
"3.1.1",
),
(
{
mqtt.CONF_BROKER: "mock-broker",
CONF_PROTOCOL: "3.1",
CONF_PORT: 1883,
},
"3.1",
),
],
ids=[
"entry_without_protocol_without_port",
"entry_without_protocol_with_port",
"entry_with_protocol_3.1.1",
"entry_with_protocol_3.1",
],
)
async def test_mqtt_protocol_successful_migration_to_v5(
hass: HomeAssistant,
mqtt_mock_entry: MqttMockHAClientGenerator,
hass_client: ClientSessionGenerator,
mqtt_config_entry_data: dict[str, Any],
current_protocol: str,
mock_try_connection: MagicMock,
) -> None:
"""Test the MQTT protocol migration repair flow is successful."""
assert await async_setup_component(hass, "repairs", {})
events = async_capture_events(hass, ir.EVENT_REPAIRS_ISSUE_REGISTRY_UPDATED)
await mqtt_mock_entry()
assert len(events) == 1
assert events[0].data["issue_id"] == "protocol_5_migration"
issue_registry = ir.async_get(hass)
assert len(issue_registry.issues) == 1
issue = issue_registry.async_get_issue(mqtt.DOMAIN, "protocol_5_migration")
assert issue is not None
assert issue.translation_key == "protocol_5_migration"
assert issue.translation_placeholders == {
"broker": "mock-broker",
"protocol": current_protocol,
}
await async_process_repairs_platforms(hass)
client = await hass_client()
data = await start_repair_fix_flow(client, mqtt.DOMAIN, "protocol_5_migration")
flow_id = data["flow_id"]
assert data["description_placeholders"] == {
"broker": "mock-broker",
"protocol": current_protocol,
}
assert data["step_id"] == "confirm"
mock_try_connection.side_effect = lambda x: True
data = await process_repair_fix_flow(client, flow_id)
assert data["type"] == "create_entry"
expected_entry_data: dict[str, Any] = mqtt_config_entry_data | {CONF_PROTOCOL: "5"}
mock_try_connection.assert_called_once_with(expected_entry_data | {CONF_PORT: 1883})
entry = hass.config_entries.async_entries(mqtt.DOMAIN)[0]
assert entry.data == expected_entry_data
await hass.async_block_till_done(wait_background_tasks=True)
@pytest.mark.parametrize(
("mqtt_config_entry_data", "current_protocol"),
[
(
{
mqtt.CONF_BROKER: "mock-broker",
},
"3.1.1",
),
(
{
mqtt.CONF_BROKER: "mock-broker",
CONF_PORT: 1883,
},
"3.1.1",
),
(
{
mqtt.CONF_BROKER: "mock-broker",
CONF_PROTOCOL: "3.1.1",
CONF_PORT: 1883,
},
"3.1.1",
),
(
{
mqtt.CONF_BROKER: "mock-broker",
CONF_PROTOCOL: "3.1",
CONF_PORT: 1883,
},
"3.1",
),
],
ids=[
"entry_without_protocol_without_port",
"entry_without_protocol_with_port",
"entry_with_protocol_3.1.1",
"entry_with_protocol_3.1",
],
)
async def test_mqtt_protocol_failed_migration_to_v5(
hass: HomeAssistant,
mqtt_mock_entry: MqttMockHAClientGenerator,
hass_client: ClientSessionGenerator,
current_protocol: str,
mock_try_connection: MagicMock,
) -> None:
"""Test the MQTT protocol migration repair flow fails."""
assert await async_setup_component(hass, "repairs", {})
events = async_capture_events(hass, ir.EVENT_REPAIRS_ISSUE_REGISTRY_UPDATED)
await mqtt_mock_entry()
assert len(events) == 1
assert events[0].data["issue_id"] == "protocol_5_migration"
issue_registry = ir.async_get(hass)
assert len(issue_registry.issues) == 1
issue = issue_registry.async_get_issue(mqtt.DOMAIN, "protocol_5_migration")
assert issue is not None
assert issue.translation_key == "protocol_5_migration"
assert issue.translation_placeholders == {
"broker": "mock-broker",
"protocol": current_protocol,
}
await async_process_repairs_platforms(hass)
client = await hass_client()
data = await start_repair_fix_flow(client, mqtt.DOMAIN, "protocol_5_migration")
flow_id = data["flow_id"]
assert data["description_placeholders"] == {
"broker": "mock-broker",
"protocol": current_protocol,
}
assert data["step_id"] == "confirm"
mock_try_connection.side_effect = lambda x: False
data = await process_repair_fix_flow(client, flow_id)
assert data["type"] == "abort"
assert data["reason"] == "mqtt_broker_migration_to_v5_failed"
assert data["description_placeholders"] == {
"broker": "mock-broker",
"protocol": current_protocol,
"url_mqtt_broker_configuration": "https://www.home-assistant.io/integrations/mqtt/#broker-configuration",
}
await hass.async_block_till_done(wait_background_tasks=True)
@@ -12,6 +12,7 @@ import pytest
from requests_mock.mocker import Mocker
import voluptuous as vol
from homeassistant.components.notify import DOMAIN as NOTIFY_DOMAIN
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
@@ -25,13 +26,11 @@ from .conftest import (
SignalNotificationService,
)
BASE_COMPONENT = "notify"
async def test_signal_messenger_init(hass: HomeAssistant) -> None:
"""Test that service loads successfully."""
config = {
BASE_COMPONENT: {
NOTIFY_DOMAIN: {
"name": "test",
"platform": "signal_messenger",
"url": "http://127.0.0.1:8080",
@@ -41,10 +40,10 @@ async def test_signal_messenger_init(hass: HomeAssistant) -> None:
}
with patch("pysignalclirestapi.SignalCliRestApi.send_message", return_value=None):
assert await async_setup_component(hass, BASE_COMPONENT, config)
assert await async_setup_component(hass, NOTIFY_DOMAIN, config)
await hass.async_block_till_done()
assert hass.services.has_service(BASE_COMPONENT, "test")
assert hass.services.has_service(NOTIFY_DOMAIN, "test")
def test_send_message(
-39
View File
@@ -1,39 +0,0 @@
"""Test const module."""
import pytest
from homeassistant import const
from .common import help_test_all, import_and_test_deprecated_constant
def test_all() -> None:
"""Test module.__all__ is correctly set."""
help_test_all(const)
@pytest.mark.parametrize(
("replacement", "constant_name", "breaks_in_version"),
[
(
"p/m³",
"CONCENTRATION_PARTS_PER_CUBIC_METER",
"2027.7",
),
],
)
def test_deprecated_constant(
caplog: pytest.LogCaptureFixture,
replacement: str,
constant_name: str,
breaks_in_version: str,
) -> None:
"""Test deprecated constants, where no replacement is provided."""
import_and_test_deprecated_constant(
caplog,
const,
constant_name,
replacement,
replacement,
breaks_in_version,
)