diff --git a/homeassistant/components/abode/services.py b/homeassistant/components/abode/services.py index c4f8b7fe1f6432..5b2a05f52287b2 100644 --- a/homeassistant/components/abode/services.py +++ b/homeassistant/components/abode/services.py @@ -12,10 +12,6 @@ from .const import DOMAIN, DOMAIN_DATA, LOGGER -SERVICE_SETTINGS = "change_setting" -SERVICE_CAPTURE_IMAGE = "capture_image" -SERVICE_TRIGGER_AUTOMATION = "trigger_automation" - ATTR_SETTING = "setting" ATTR_VALUE = "value" @@ -75,16 +71,13 @@ def async_setup_services(hass: HomeAssistant) -> None: """Home Assistant services.""" hass.services.async_register( - DOMAIN, SERVICE_SETTINGS, _change_setting, schema=CHANGE_SETTING_SCHEMA + DOMAIN, "change_setting", _change_setting, schema=CHANGE_SETTING_SCHEMA ) hass.services.async_register( - DOMAIN, SERVICE_CAPTURE_IMAGE, _capture_image, schema=CAPTURE_IMAGE_SCHEMA + DOMAIN, "capture_image", _capture_image, schema=CAPTURE_IMAGE_SCHEMA ) hass.services.async_register( - DOMAIN, - SERVICE_TRIGGER_AUTOMATION, - _trigger_automation, - schema=AUTOMATION_SCHEMA, + DOMAIN, "trigger_automation", _trigger_automation, schema=AUTOMATION_SCHEMA ) diff --git a/homeassistant/components/advantage_air/services.py b/homeassistant/components/advantage_air/services.py index a7347234c07eb8..a64d1c9e225e62 100644 --- a/homeassistant/components/advantage_air/services.py +++ b/homeassistant/components/advantage_air/services.py @@ -10,8 +10,6 @@ from .const import DOMAIN -ADVANTAGE_AIR_SERVICE_SET_TIME_TO = "set_time_to" - @callback def async_setup_services(hass: HomeAssistant) -> None: @@ -20,7 +18,7 @@ def async_setup_services(hass: HomeAssistant) -> None: service.async_register_platform_entity_service( hass, DOMAIN, - ADVANTAGE_AIR_SERVICE_SET_TIME_TO, + "set_time_to", entity_domain=SENSOR_DOMAIN, schema={vol.Required("minutes"): cv.positive_int}, func="set_time_to", diff --git a/homeassistant/components/agent_dvr/services.py b/homeassistant/components/agent_dvr/services.py index d80d94427fbd28..b9c5c0f7ec653f 100644 --- a/homeassistant/components/agent_dvr/services.py +++ b/homeassistant/components/agent_dvr/services.py @@ -8,18 +8,12 @@ from .const import DOMAIN -_DEV_EN_ALT = "enable_alerts" -_DEV_DS_ALT = "disable_alerts" -_DEV_EN_REC = "start_recording" -_DEV_DS_REC = "stop_recording" -_DEV_SNAP = "snapshot" - CAMERA_SERVICES = { - _DEV_EN_ALT: "async_enable_alerts", - _DEV_DS_ALT: "async_disable_alerts", - _DEV_EN_REC: "async_start_recording", - _DEV_DS_REC: "async_stop_recording", - _DEV_SNAP: "async_snapshot", + "enable_alerts": "async_enable_alerts", + "disable_alerts": "async_disable_alerts", + "start_recording": "async_start_recording", + "stop_recording": "async_stop_recording", + "snapshot": "async_snapshot", } diff --git a/homeassistant/components/alarmdecoder/services.py b/homeassistant/components/alarmdecoder/services.py index 98a58239265aa8..d9d5002ca947be 100644 --- a/homeassistant/components/alarmdecoder/services.py +++ b/homeassistant/components/alarmdecoder/services.py @@ -13,9 +13,6 @@ from .const import DOMAIN -SERVICE_ALARM_TOGGLE_CHIME = "alarm_toggle_chime" - -SERVICE_ALARM_KEYPRESS = "alarm_keypress" ATTR_KEYPRESS = "keypress" @@ -26,7 +23,7 @@ def async_setup_services(hass: HomeAssistant) -> None: service.async_register_platform_entity_service( hass, DOMAIN, - SERVICE_ALARM_TOGGLE_CHIME, + "alarm_toggle_chime", entity_domain=ALARM_CONTROL_PANEL_DOMAIN, schema={ vol.Required(ATTR_CODE): cv.string, @@ -37,7 +34,7 @@ def async_setup_services(hass: HomeAssistant) -> None: service.async_register_platform_entity_service( hass, DOMAIN, - SERVICE_ALARM_KEYPRESS, + "alarm_keypress", entity_domain=ALARM_CONTROL_PANEL_DOMAIN, schema={ vol.Required(ATTR_KEYPRESS): cv.string, diff --git a/homeassistant/components/amberelectric/const.py b/homeassistant/components/amberelectric/const.py index 3a1dbc9023a996..cfe840dcde82e8 100644 --- a/homeassistant/components/amberelectric/const.py +++ b/homeassistant/components/amberelectric/const.py @@ -16,8 +16,6 @@ LOGGER = logging.getLogger(__package__) PLATFORMS = [Platform.BINARY_SENSOR, Platform.SENSOR] -SERVICE_GET_FORECASTS = "get_forecasts" - GENERAL_CHANNEL = "general" CONTROLLED_LOAD_CHANNEL = "controlled_load" FEED_IN_CHANNEL = "feed_in" diff --git a/homeassistant/components/amberelectric/services.py b/homeassistant/components/amberelectric/services.py index c4549498b91324..f936d4a3d3c2b7 100644 --- a/homeassistant/components/amberelectric/services.py +++ b/homeassistant/components/amberelectric/services.py @@ -22,7 +22,6 @@ DOMAIN, FEED_IN_CHANNEL, GENERAL_CHANNEL, - SERVICE_GET_FORECASTS, ) from .coordinator import AmberConfigEntry from .helpers import format_cents_to_dollars, normalize_descriptor @@ -101,7 +100,7 @@ async def handle_get_forecasts(call: ServiceCall) -> ServiceResponse: hass.services.async_register( DOMAIN, - SERVICE_GET_FORECASTS, + "get_forecasts", handle_get_forecasts, GET_FORECASTS_SCHEMA, supports_response=SupportsResponse.ONLY, diff --git a/homeassistant/components/androidtv/media_player.py b/homeassistant/components/androidtv/media_player.py index 9621282208e1e6..57a45798364e83 100644 --- a/homeassistant/components/androidtv/media_player.py +++ b/homeassistant/components/androidtv/media_player.py @@ -36,7 +36,7 @@ SIGNAL_CONFIG_ENTITY, ) from .entity import AndroidTVEntity, adb_decorator -from .services import ATTR_ADB_RESPONSE, ATTR_HDMI_INPUT, SERVICE_LEARN_SENDEVENT +from .services import ATTR_ADB_RESPONSE, ATTR_HDMI_INPUT _LOGGER = logging.getLogger(__name__) @@ -271,7 +271,7 @@ async def learn_sendevent(self) -> None: self.async_write_ha_state() msg = ( - f"Output from service '{SERVICE_LEARN_SENDEVENT}' from" + f"Output from service 'learn_sendevent' from" f" {self.entity_id}: '{output}'" ) persistent_notification.async_create( diff --git a/homeassistant/components/androidtv/services.py b/homeassistant/components/androidtv/services.py index 8a44399b727468..895f9d334ce73d 100644 --- a/homeassistant/components/androidtv/services.py +++ b/homeassistant/components/androidtv/services.py @@ -16,11 +16,6 @@ ATTR_HDMI_INPUT = "hdmi_input" ATTR_LOCAL_PATH = "local_path" -SERVICE_ADB_COMMAND = "adb_command" -SERVICE_DOWNLOAD = "download" -SERVICE_LEARN_SENDEVENT = "learn_sendevent" -SERVICE_UPLOAD = "upload" - @callback def async_setup_services(hass: HomeAssistant) -> None: @@ -29,7 +24,7 @@ def async_setup_services(hass: HomeAssistant) -> None: service.async_register_platform_entity_service( hass, DOMAIN, - SERVICE_ADB_COMMAND, + "adb_command", entity_domain=MEDIA_PLAYER_DOMAIN, schema={vol.Required(ATTR_COMMAND): cv.string}, func="adb_command", @@ -37,7 +32,7 @@ def async_setup_services(hass: HomeAssistant) -> None: service.async_register_platform_entity_service( hass, DOMAIN, - SERVICE_LEARN_SENDEVENT, + "learn_sendevent", entity_domain=MEDIA_PLAYER_DOMAIN, schema=None, func="learn_sendevent", @@ -45,7 +40,7 @@ def async_setup_services(hass: HomeAssistant) -> None: service.async_register_platform_entity_service( hass, DOMAIN, - SERVICE_DOWNLOAD, + "download", entity_domain=MEDIA_PLAYER_DOMAIN, schema={ vol.Required(ATTR_DEVICE_PATH): cv.string, @@ -56,7 +51,7 @@ def async_setup_services(hass: HomeAssistant) -> None: service.async_register_platform_entity_service( hass, DOMAIN, - SERVICE_UPLOAD, + "upload", entity_domain=MEDIA_PLAYER_DOMAIN, schema={ vol.Required(ATTR_DEVICE_PATH): cv.string, diff --git a/homeassistant/components/anthropic/entity.py b/homeassistant/components/anthropic/entity.py index 62f39bd4a02ce1..658267219e3506 100644 --- a/homeassistant/components/anthropic/entity.py +++ b/homeassistant/components/anthropic/entity.py @@ -858,6 +858,11 @@ async def _async_handle_chat_log( ] ) messages.extend(new_messages) + except anthropic.AuthenticationError as err: + self.entry.async_start_reauth(self.hass) + raise HomeAssistantError( + "Authentication error with Anthropic API, reauthentication required" + ) from err except anthropic.AnthropicError as err: raise HomeAssistantError( f"Sorry, I had a problem talking to Anthropic: {err}" diff --git a/homeassistant/components/aws_s3/quality_scale.yaml b/homeassistant/components/aws_s3/quality_scale.yaml index 230a13678c0d8d..0410a22c698911 100644 --- a/homeassistant/components/aws_s3/quality_scale.yaml +++ b/homeassistant/components/aws_s3/quality_scale.yaml @@ -38,7 +38,7 @@ rules: docs-installation-parameters: done entity-unavailable: done integration-owner: done - log-when-unavailable: todo + log-when-unavailable: done parallel-updates: done reauthentication-flow: todo test-coverage: done diff --git a/homeassistant/components/compit/strings.json b/homeassistant/components/compit/strings.json index cd46543142e324..56624669e0d8fe 100644 --- a/homeassistant/components/compit/strings.json +++ b/homeassistant/components/compit/strings.json @@ -329,8 +329,8 @@ "nano_nr_3": "Nano 3", "nano_nr_4": "Nano 4", "nano_nr_5": "Nano 5", - "off": "Off", - "on": "On", + "off": "[%key:common::state::off%]", + "on": "[%key:common::state::on%]", "summer": "Summer", "winter": "Winter" } @@ -368,8 +368,8 @@ "pump_status": { "name": "Pump status", "state": { - "off": "Off", - "on": "On" + "off": "[%key:common::state::off%]", + "on": "[%key:common::state::on%]" } }, "return_circuit_temperature": { diff --git a/homeassistant/components/frontend/manifest.json b/homeassistant/components/frontend/manifest.json index 0cc4d09685cb42..28e9253d805656 100644 --- a/homeassistant/components/frontend/manifest.json +++ b/homeassistant/components/frontend/manifest.json @@ -21,5 +21,5 @@ "integration_type": "system", "preview_features": { "winter_mode": {} }, "quality_scale": "internal", - "requirements": ["home-assistant-frontend==20260225.0"] + "requirements": ["home-assistant-frontend==20260226.0"] } diff --git a/homeassistant/components/openai_conversation/config_flow.py b/homeassistant/components/openai_conversation/config_flow.py index 07f26771dcbbd9..d86137c0982eef 100644 --- a/homeassistant/components/openai_conversation/config_flow.py +++ b/homeassistant/components/openai_conversation/config_flow.py @@ -512,6 +512,11 @@ async def async_step_model( options.pop(CONF_WEB_SEARCH_REGION, None) options.pop(CONF_WEB_SEARCH_COUNTRY, None) options.pop(CONF_WEB_SEARCH_TIMEZONE, None) + if ( + user_input.get(CONF_CODE_INTERPRETER) + and user_input.get(CONF_REASONING_EFFORT) == "minimal" + ): + errors[CONF_CODE_INTERPRETER] = "code_interpreter_minimal_reasoning" options.update(user_input) if not errors: @@ -539,15 +544,15 @@ def _get_reasoning_options(self, model: str) -> list[str]: if not model.startswith(("o", "gpt-5")) or model.startswith("gpt-5-pro"): return [] - MODELS_REASONING_MAP = { + models_reasoning_map: dict[str | tuple[str, ...], list[str]] = { "gpt-5.2-pro": ["medium", "high", "xhigh"], - "gpt-5.2": ["none", "low", "medium", "high", "xhigh"], + ("gpt-5.2", "gpt-5.3"): ["none", "low", "medium", "high", "xhigh"], "gpt-5.1": ["none", "low", "medium", "high"], "gpt-5": ["minimal", "low", "medium", "high"], "": ["low", "medium", "high"], # The default case } - for prefix, options in MODELS_REASONING_MAP.items(): + for prefix, options in models_reasoning_map.items(): if model.startswith(prefix): return options return [] # pragma: no cover diff --git a/homeassistant/components/openai_conversation/strings.json b/homeassistant/components/openai_conversation/strings.json index 5af703097211c1..e29596b8b90c46 100644 --- a/homeassistant/components/openai_conversation/strings.json +++ b/homeassistant/components/openai_conversation/strings.json @@ -38,6 +38,7 @@ }, "entry_type": "AI task", "error": { + "code_interpreter_minimal_reasoning": "[%key:component::openai_conversation::config_subentries::conversation::error::code_interpreter_minimal_reasoning%]", "model_not_supported": "[%key:component::openai_conversation::config_subentries::conversation::error::model_not_supported%]", "web_search_minimal_reasoning": "[%key:component::openai_conversation::config_subentries::conversation::error::web_search_minimal_reasoning%]" }, @@ -93,6 +94,7 @@ }, "entry_type": "Conversation agent", "error": { + "code_interpreter_minimal_reasoning": "Code interpreter is not supported with minimal reasoning effort", "model_not_supported": "This model is not supported, please select a different model", "web_search_minimal_reasoning": "Web search is currently not supported with minimal reasoning effort" }, diff --git a/homeassistant/components/orvibo/__init__.py b/homeassistant/components/orvibo/__init__.py index 81cddecb672192..71c6e0609c5944 100644 --- a/homeassistant/components/orvibo/__init__.py +++ b/homeassistant/components/orvibo/__init__.py @@ -1 +1,51 @@ -"""The orvibo component.""" +"""The orvibo integration.""" + +import logging + +from orvibo.s20 import S20, S20Exception + +from homeassistant import core +from homeassistant.const import CONF_HOST, CONF_MAC, Platform +from homeassistant.core import HomeAssistant +from homeassistant.exceptions import ConfigEntryNotReady + +from .const import DOMAIN +from .models import S20ConfigEntry + +PLATFORMS = [Platform.SWITCH] + +_LOGGER = logging.getLogger(__name__) + + +async def async_setup_entry(hass: core.HomeAssistant, entry: S20ConfigEntry) -> bool: + """Set up platform from a ConfigEntry.""" + + try: + s20 = await hass.async_add_executor_job( + S20, + entry.data[CONF_HOST], + entry.data[CONF_MAC], + ) + _LOGGER.debug("Initialized S20 at %s", entry.data[CONF_HOST]) + except S20Exception as err: + _LOGGER.debug("S20 at %s couldn't be initialized", entry.data[CONF_HOST]) + + raise ConfigEntryNotReady( + translation_domain=DOMAIN, + translation_key="init_error", + translation_placeholders={ + "host": entry.data[CONF_HOST], + }, + ) from err + + entry.runtime_data = s20 + + await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) + + return True + + +async def async_unload_entry(hass: HomeAssistant, entry: S20ConfigEntry) -> bool: + """Unload a config entry.""" + + return await hass.config_entries.async_unload_platforms(entry, PLATFORMS) diff --git a/homeassistant/components/orvibo/config_flow.py b/homeassistant/components/orvibo/config_flow.py new file mode 100644 index 00000000000000..13f914e094ec7d --- /dev/null +++ b/homeassistant/components/orvibo/config_flow.py @@ -0,0 +1,205 @@ +"""Config flow for the orvibo integration.""" + +import asyncio +import logging +from typing import Any + +from orvibo.s20 import S20, S20Exception, discover +import voluptuous as vol + +from homeassistant.config_entries import ConfigFlow, ConfigFlowResult +from homeassistant.const import CONF_HOST, CONF_MAC, CONF_NAME +from homeassistant.helpers import config_validation as cv +from homeassistant.helpers.device_registry import format_mac + +from .const import CONF_SWITCH_LIST, DEFAULT_NAME, DOMAIN + +_LOGGER = logging.getLogger(__name__) + + +FULL_EDIT_SCHEMA = vol.Schema( + { + vol.Required(CONF_HOST): cv.string, + vol.Optional(CONF_MAC): cv.string, + } +) + + +class S20ConfigFlow(ConfigFlow, domain=DOMAIN): + """Handle the config flow for Orvibo S20 switches.""" + + VERSION = 1 + MINOR_VERSION = 1 + + def __init__(self) -> None: + """Initialize an instance of the S20 config flow.""" + self.discovery_task: asyncio.Task | None = None + self._discovered_switches: dict[str, dict[str, Any]] = {} + self.chosen_switch: dict[str, Any] = {} + + async def _async_discover(self) -> None: + def _filter_discovered_switches( + switches: dict[str, dict[str, Any]], + ) -> dict[str, dict[str, Any]]: + # Get existing unique_ids from config entries + existing_ids = {entry.unique_id for entry in self._async_current_entries()} + _LOGGER.debug("Existing unique IDs: %s", existing_ids) + # Build a new filtered dict + filtered = {} + for ip, info in switches.items(): + mac_bytes = info.get("mac") + if not mac_bytes: + continue # skip if no MAC + + unique_id = format_mac(mac_bytes.hex()).lower() + if unique_id not in existing_ids: + filtered[ip] = info + _LOGGER.debug("New switches: %s", filtered) + return filtered + + # Discover S20 devices. + _LOGGER.debug("Discovering S20 switches") + + _unfiltered_switches = await self.hass.async_add_executor_job(discover) + _LOGGER.debug("All discovered switches: %s", _unfiltered_switches) + + self._discovered_switches = _filter_discovered_switches(_unfiltered_switches) + + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: + """Handle a flow initialized by the user.""" + + return self.async_show_menu( + step_id="user", menu_options=["start_discovery", "edit"] + ) + + async def _validate_input(self, user_input: dict[str, Any]) -> str | None: + """Validate user input and discover MAC if missing.""" + + if user_input.get(CONF_MAC): + user_input[CONF_MAC] = format_mac(user_input[CONF_MAC]).lower() + if len(user_input[CONF_MAC]) != 17 or user_input[CONF_MAC].count(":") != 5: + return "invalid_mac" + + try: + device = await self.hass.async_add_executor_job( + S20, + user_input[CONF_HOST], + user_input.get(CONF_MAC), + ) + + if not user_input.get(CONF_MAC): + # Using private attribute access here since S20 class doesn't have a public method to get the MAC without repeating discovery + if not device._mac: # noqa: SLF001 + return "cannot_discover" + user_input[CONF_MAC] = format_mac(device._mac.hex()).lower() # noqa: SLF001 + + except S20Exception: + return "cannot_connect" + + return None + + async def async_step_edit( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: + """Edit a discovered or manually configured server.""" + + errors = {} + if user_input: + error = await self._validate_input(user_input) + if not error: + await self.async_set_unique_id(user_input[CONF_MAC]) + self._abort_if_unique_id_configured() + return self.async_create_entry( + title=f"{DEFAULT_NAME} ({user_input[CONF_HOST]})", data=user_input + ) + errors["base"] = error + + return self.async_show_form( + step_id="edit", + data_schema=FULL_EDIT_SCHEMA, + errors=errors, + ) + + async def async_step_start_discovery( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: + """Handle a flow initialized by the user.""" + if not self.discovery_task: + self.discovery_task = self.hass.async_create_task(self._async_discover()) + return self.async_show_progress( + step_id="start_discovery", + progress_action="start_discovery", + progress_task=self.discovery_task, + ) + if self.discovery_task.done(): + try: + self.discovery_task.result() + except (S20Exception, OSError) as err: + _LOGGER.debug("Discovery task failed: %s", err) + self.discovery_task = None + return self.async_show_progress_done( + next_step_id=( + "choose_switch" if self._discovered_switches else "discovery_failed" + ) + ) + return self.async_show_progress( + step_id="start_discovery", + progress_action="start_discovery", + progress_task=self.discovery_task, + ) + + async def async_step_choose_switch( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: + """Choose manual or discover flow.""" + _chosen_host: str + + if user_input: + _chosen_host = user_input[CONF_SWITCH_LIST] + for host, data in self._discovered_switches.items(): + if _chosen_host == host: + self.chosen_switch[CONF_HOST] = host + self.chosen_switch[CONF_MAC] = format_mac( + data[CONF_MAC].hex() + ).lower() + await self.async_set_unique_id(self.chosen_switch[CONF_MAC]) + self._abort_if_unique_id_configured() + return self.async_create_entry( + title=f"{DEFAULT_NAME} ({host})", data=self.chosen_switch + ) + _LOGGER.debug("discovered switches: %s", self._discovered_switches) + + _options = { + host: f"{host} ({format_mac(data[CONF_MAC].hex()).lower()})" + for host, data in self._discovered_switches.items() + } + return self.async_show_form( + step_id="choose_switch", + data_schema=vol.Schema({vol.Required(CONF_SWITCH_LIST): vol.In(_options)}), + ) + + async def async_step_discovery_failed( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: + """Handle a failed discovery.""" + + return self.async_show_menu( + step_id="discovery_failed", menu_options=["start_discovery", "edit"] + ) + + async def async_step_import(self, user_input: dict[str, Any]) -> ConfigFlowResult: + """Handle import from configuration.yaml.""" + _LOGGER.debug("Importing config: %s", user_input) + + error = await self._validate_input(user_input) + if error: + return self.async_abort(reason=error) + + await self.async_set_unique_id(user_input[CONF_MAC]) + self._abort_if_unique_id_configured() + + return self.async_create_entry( + title=user_input.get(CONF_NAME, user_input[CONF_HOST]), data=user_input + ) diff --git a/homeassistant/components/orvibo/const.py b/homeassistant/components/orvibo/const.py new file mode 100644 index 00000000000000..0286588ddbe218 --- /dev/null +++ b/homeassistant/components/orvibo/const.py @@ -0,0 +1,5 @@ +"""Constants for the orvibo integration.""" + +DOMAIN = "orvibo" +DEFAULT_NAME = "S20" +CONF_SWITCH_LIST = "switches" diff --git a/homeassistant/components/orvibo/manifest.json b/homeassistant/components/orvibo/manifest.json index e3a6676b2f2f8d..8ec76f83513a8a 100644 --- a/homeassistant/components/orvibo/manifest.json +++ b/homeassistant/components/orvibo/manifest.json @@ -2,6 +2,7 @@ "domain": "orvibo", "name": "Orvibo", "codeowners": [], + "config_flow": true, "documentation": "https://www.home-assistant.io/integrations/orvibo", "iot_class": "local_push", "loggers": ["orvibo"], diff --git a/homeassistant/components/orvibo/models.py b/homeassistant/components/orvibo/models.py new file mode 100644 index 00000000000000..d702ecef61ac07 --- /dev/null +++ b/homeassistant/components/orvibo/models.py @@ -0,0 +1,7 @@ +"""Data models for the Orvibo integration.""" + +from orvibo.s20 import S20 + +from homeassistant.config_entries import ConfigEntry + +type S20ConfigEntry = ConfigEntry[S20] diff --git a/homeassistant/components/orvibo/strings.json b/homeassistant/components/orvibo/strings.json new file mode 100644 index 00000000000000..93ab02b755e0b2 --- /dev/null +++ b/homeassistant/components/orvibo/strings.json @@ -0,0 +1,71 @@ +{ + "config": { + "abort": { + "already_configured": "[%key:common::config_flow::abort::already_configured_device%]", + "already_in_progress": "[%key:common::config_flow::abort::already_in_progress%]", + "cannot_connect": "Unable to connect to the S20 switch", + "cannot_discover": "Unable to discover MAC address of S20 switch. Please enter the MAC address.", + "invalid_mac": "Invalid MAC address format" + }, + "error": { + "cannot_connect": "[%key:component::orvibo::config::abort::cannot_connect%]", + "cannot_discover": "[%key:component::orvibo::config::abort::cannot_discover%]", + "invalid_mac": "Invalid MAC address format" + }, + "progress": { + "start_discovery": "Attempting to discover new S20 switches\n\nThis will take about 3 seconds\n\nDiscovery may fail if the switch is asleep. If your switch does not appear, please power toggle your switch before re-running discovery.", + "title": "Orvibo S20" + }, + "step": { + "choose_switch": { + "data": { + "switches": "Choose discovered switch to configure" + }, + "title": "Discovered switches" + }, + "discovery_failed": { + "description": "No S20 switches were discovered on the network. Discovery may have failed if the switch is asleep. Please power toggle your switch before re-running discovery.", + "menu_options": { + "edit": "Enter configuration manually", + "start_discovery": "Try discovering again" + }, + "title": "Discovery failed" + }, + "edit": { + "data": { + "host": "[%key:common::config_flow::data::host%]", + "mac": "MAC address" + }, + "title": "Configure Orvibo S20 switch" + }, + "user": { + "menu_options": { + "edit": "Enter configuration manually", + "start_discovery": "Discover new S20 switches" + }, + "title": "Orvibo S20 Configuration" + } + } + }, + "exceptions": { + "init_error": { + "message": "Error while initializing S20 {host}." + }, + "turn_off_error": { + "message": "Error while turning off S20 {name}." + }, + "turn_on_error": { + "message": "Error while turning on S20 {name}." + } + }, + "issues": { + "yaml_deprecation": { + "description": "The device (MAC: {mac}, Host: {host}) is configured in `configuration.yaml`. The Orvibo integration now supports UI-based configuration and this device has been migrated to the new UI. Please remove the YAML block from `configuration.yaml` to avoid future issues.", + "title": "Legacy YAML configuration detected {host}" + }, + "yaml_deprecation_import_issue": { + "description": "Attempting to import this device (MAC: {mac}, Host: {host}) from YAML has failed for reason {reason}. 1) Remove the YAML block from `configuration.yaml`, 2) Restart Home Assistant, 3) Add the device using the UI configuration flow.", + "title": "Legacy YAML configuration import issue for {host}" + } + } +} diff --git a/homeassistant/components/orvibo/switch.py b/homeassistant/components/orvibo/switch.py index 3853c10e01c8de..a7a829d7b66b7a 100644 --- a/homeassistant/components/orvibo/switch.py +++ b/homeassistant/components/orvibo/switch.py @@ -1,13 +1,14 @@ -"""Support for Orvibo S20 Wifi Smart Switches.""" +"""Switch platform for the Orvibo integration.""" from __future__ import annotations import logging from typing import Any -from orvibo.s20 import S20, S20Exception, discover +from orvibo.s20 import S20, S20Exception import voluptuous as vol +from homeassistant import config_entries from homeassistant.components.switch import ( PLATFORM_SCHEMA as SWITCH_PLATFORM_SCHEMA, SwitchEntity, @@ -20,14 +21,25 @@ CONF_SWITCHES, ) from homeassistant.core import HomeAssistant -from homeassistant.helpers import config_validation as cv -from homeassistant.helpers.entity_platform import AddEntitiesCallback +from homeassistant.data_entry_flow import FlowResultType +from homeassistant.exceptions import HomeAssistantError +from homeassistant.helpers import config_validation as cv, issue_registry as ir +from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC, DeviceInfo +from homeassistant.helpers.entity_platform import ( + AddConfigEntryEntitiesCallback, + AddEntitiesCallback, +) from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType +from .const import DEFAULT_NAME, DOMAIN +from .models import S20ConfigEntry + _LOGGER = logging.getLogger(__name__) -DEFAULT_NAME = "Orvibo S20 Switch" -DEFAULT_DISCOVERY = True +DEFAULT_DISCOVERY = False + +# Library is not thread safe and uses global variables, so we limit to 1 update at a time +PARALLEL_UPDATES = 1 PLATFORM_SCHEMA = SWITCH_PLATFORM_SCHEMA.extend( { @@ -46,65 +58,138 @@ ) -def setup_platform( +async def async_setup_platform( hass: HomeAssistant, config: ConfigType, add_entities_callback: AddEntitiesCallback, discovery_info: DiscoveryInfoType | None = None, ) -> None: - """Set up S20 switches.""" - - switch_data = {} - switches = [] - switch_conf = config.get(CONF_SWITCHES, [config]) - - if config.get(CONF_DISCOVERY): - _LOGGER.debug("Discovering S20 switches") - switch_data.update(discover()) - - for switch in switch_conf: - switch_data[switch.get(CONF_HOST)] = switch - - for host, data in switch_data.items(): - try: - switches.append( - S20Switch(data.get(CONF_NAME), S20(host, mac=data.get(CONF_MAC))) + """Set up the integration from configuration.yaml.""" + for switch in config.get(CONF_SWITCHES, []): + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": config_entries.SOURCE_IMPORT}, + data=switch, + ) + + if ( + result.get("type") is FlowResultType.ABORT + and result.get("reason") != "already_configured" + ): + ir.async_create_issue( + hass, + DOMAIN, + f"yaml_deprecation_import_issue_{switch.get('host')}_{(switch.get('mac') or 'unknown_mac').replace(':', '').lower()}", + breaks_in_ha_version="2026.9.0", + is_fixable=False, + is_persistent=False, + issue_domain=DOMAIN, + severity=ir.IssueSeverity.WARNING, + translation_key="yaml_deprecation_import_issue", + translation_placeholders={ + "reason": str(result.get("reason")), + "host": switch.get("host"), + "mac": switch.get("mac", ""), + }, ) - _LOGGER.debug("Initialized S20 at %s", host) - except S20Exception: - _LOGGER.error("S20 at %s couldn't be initialized", host) - - add_entities_callback(switches) + continue + + ir.async_create_issue( + hass, + DOMAIN, + f"yaml_deprecation_{switch.get('host')}_{(switch.get('mac') or 'unknown_mac').replace(':', '').lower()}", + breaks_in_ha_version="2026.9.0", + is_fixable=False, + is_persistent=False, + severity=ir.IssueSeverity.WARNING, + translation_key="yaml_deprecation", + translation_placeholders={ + "host": switch.get("host"), + "mac": switch.get("mac") or "Unknown MAC", + }, + ) + + +async def async_setup_entry( + hass: HomeAssistant, + entry: S20ConfigEntry, + async_add_entities: AddConfigEntryEntitiesCallback, +) -> None: + """Set up orvibo from a config entry.""" + async_add_entities( + [ + S20Switch( + entry.title, + entry.data[CONF_HOST], + entry.data[CONF_MAC], + entry.runtime_data, + ) + ] + ) class S20Switch(SwitchEntity): """Representation of an S20 switch.""" - def __init__(self, name, s20): + _attr_has_entity_name = True + + def __init__(self, name: str, host: str, mac: str, s20: S20) -> None: """Initialize the S20 device.""" - self._attr_name = name - self._s20 = s20 self._attr_is_on = False - self._exc = S20Exception - - def update(self) -> None: - """Update device state.""" - try: - self._attr_is_on = self._s20.on - except self._exc: - _LOGGER.exception("Error while fetching S20 state") + self._host = host + self._mac = mac + self._s20 = s20 + self._attr_unique_id = self._mac + self._name = name + self._attr_name = None + self._attr_device_info = DeviceInfo( + identifiers={ + # MAC addresses are used as unique identifiers within this domain + (DOMAIN, self._attr_unique_id) + }, + name=name, + manufacturer="Orvibo", + model="S20", + connections={(CONNECTION_NETWORK_MAC, self._mac)}, + ) def turn_on(self, **kwargs: Any) -> None: """Turn the device on.""" try: self._s20.on = True - except self._exc: - _LOGGER.exception("Error while turning on S20") + except S20Exception as err: + raise HomeAssistantError( + translation_domain=DOMAIN, + translation_key="turn_on_error", + translation_placeholders={"name": self._name}, + ) from err def turn_off(self, **kwargs: Any) -> None: """Turn the device off.""" try: self._s20.on = False - except self._exc: - _LOGGER.exception("Error while turning off S20") + except S20Exception as err: + raise HomeAssistantError( + translation_domain=DOMAIN, + translation_key="turn_off_error", + translation_placeholders={"name": self._name}, + ) from err + + def update(self) -> None: + """Update device state.""" + try: + self._attr_is_on = self._s20.on + + # If the device was previously offline, let the user know it's back! + if not self._attr_available: + _LOGGER.info("Orvibo switch %s reconnected", self._name) + self._attr_available = True + + except S20Exception as err: + # Only log the error if this is the FIRST time it failed + if self._attr_available: + _LOGGER.info( + "Error communicating with Orvibo switch %s: %s", self._name, err + ) + self._attr_available = False diff --git a/homeassistant/components/philips_js/light.py b/homeassistant/components/philips_js/light.py index 579c43b58cdc04..112ee0cd2caa83 100644 --- a/homeassistant/components/philips_js/light.py +++ b/homeassistant/components/philips_js/light.py @@ -137,11 +137,10 @@ class PhilipsTVLightEntity(PhilipsJsEntity, LightEntity): _attr_effect: str _attr_translation_key = "ambilight" + _attr_supported_color_modes = {ColorMode.HS} + _attr_supported_features = LightEntityFeature.EFFECT - def __init__( - self, - coordinator: PhilipsTVDataUpdateCoordinator, - ) -> None: + def __init__(self, coordinator: PhilipsTVDataUpdateCoordinator) -> None: """Initialize light.""" self._tv = coordinator.api self._hs = None @@ -150,8 +149,6 @@ def __init__( self._last_selected_effect: AmbilightEffect | None = None super().__init__(coordinator) - self._attr_supported_color_modes = {ColorMode.HS, ColorMode.ONOFF} - self._attr_supported_features = LightEntityFeature.EFFECT self._attr_unique_id = coordinator.unique_id self._update_from_coordinator() diff --git a/homeassistant/components/portainer/binary_sensor.py b/homeassistant/components/portainer/binary_sensor.py index 188e99f647ab9c..0e190a3e77666f 100644 --- a/homeassistant/components/portainer/binary_sensor.py +++ b/homeassistant/components/portainer/binary_sensor.py @@ -16,7 +16,7 @@ from . import PortainerConfigEntry from .const import CONTAINER_STATE_RUNNING, STACK_STATUS_ACTIVE -from .coordinator import PortainerContainerData, PortainerCoordinator +from .coordinator import PortainerContainerData from .entity import ( PortainerContainerEntity, PortainerCoordinatorData, @@ -165,18 +165,6 @@ class PortainerEndpointSensor(PortainerEndpointEntity, BinarySensorEntity): entity_description: PortainerEndpointBinarySensorEntityDescription - def __init__( - self, - coordinator: PortainerCoordinator, - entity_description: PortainerEndpointBinarySensorEntityDescription, - device_info: PortainerCoordinatorData, - ) -> None: - """Initialize Portainer endpoint binary sensor entity.""" - self.entity_description = entity_description - super().__init__(device_info, coordinator) - - self._attr_unique_id = f"{coordinator.config_entry.entry_id}_{device_info.id}_{entity_description.key}" - @property def is_on(self) -> bool | None: """Return true if the binary sensor is on.""" @@ -188,19 +176,6 @@ class PortainerContainerSensor(PortainerContainerEntity, BinarySensorEntity): entity_description: PortainerContainerBinarySensorEntityDescription - def __init__( - self, - coordinator: PortainerCoordinator, - entity_description: PortainerContainerBinarySensorEntityDescription, - device_info: PortainerContainerData, - via_device: PortainerCoordinatorData, - ) -> None: - """Initialize the Portainer container sensor.""" - self.entity_description = entity_description - super().__init__(device_info, coordinator, via_device) - - self._attr_unique_id = f"{coordinator.config_entry.entry_id}_{self.device_name}_{entity_description.key}" - @property def is_on(self) -> bool | None: """Return true if the binary sensor is on.""" @@ -212,19 +187,6 @@ class PortainerStackSensor(PortainerStackEntity, BinarySensorEntity): entity_description: PortainerStackBinarySensorEntityDescription - def __init__( - self, - coordinator: PortainerCoordinator, - entity_description: PortainerStackBinarySensorEntityDescription, - device_info: PortainerStackData, - via_device: PortainerCoordinatorData, - ) -> None: - """Initialize the Portainer stack sensor.""" - self.entity_description = entity_description - super().__init__(device_info, coordinator, via_device) - - self._attr_unique_id = f"{coordinator.config_entry.entry_id}_{device_info.stack.id}_{entity_description.key}" - @property def is_on(self) -> bool | None: """Return true if the binary sensor is on.""" diff --git a/homeassistant/components/portainer/button.py b/homeassistant/components/portainer/button.py index 9b9e59e311de71..b6963c26d09051 100644 --- a/homeassistant/components/portainer/button.py +++ b/homeassistant/components/portainer/button.py @@ -167,18 +167,6 @@ class PortainerEndpointButton(PortainerEndpointEntity, PortainerBaseButton): entity_description: PortainerButtonDescription - def __init__( - self, - coordinator: PortainerCoordinator, - entity_description: PortainerButtonDescription, - device_info: PortainerCoordinatorData, - ) -> None: - """Initialize the Portainer endpoint button entity.""" - self.entity_description = entity_description - super().__init__(device_info, coordinator) - - self._attr_unique_id = f"{coordinator.config_entry.entry_id}_{device_info.id}_{entity_description.key}" - async def _async_press_call(self) -> None: """Call the endpoint button press action.""" await self.entity_description.press_action( @@ -191,19 +179,6 @@ class PortainerContainerButton(PortainerContainerEntity, PortainerBaseButton): entity_description: PortainerButtonDescription - def __init__( - self, - coordinator: PortainerCoordinator, - entity_description: PortainerButtonDescription, - device_info: PortainerContainerData, - via_device: PortainerCoordinatorData, - ) -> None: - """Initialize the Portainer button entity.""" - self.entity_description = entity_description - super().__init__(device_info, coordinator, via_device) - - self._attr_unique_id = f"{coordinator.config_entry.entry_id}_{self.device_name}_{entity_description.key}" - async def _async_press_call(self) -> None: """Call the container button press action.""" await self.entity_description.press_action( diff --git a/homeassistant/components/portainer/coordinator.py b/homeassistant/components/portainer/coordinator.py index a63a86855dc9f6..6586614a1a659e 100644 --- a/homeassistant/components/portainer/coordinator.py +++ b/homeassistant/components/portainer/coordinator.py @@ -170,11 +170,11 @@ async def _async_update_data(self) -> dict[int, PortainerCoordinatorData]: docker_system_df, stacks, ) = await asyncio.gather( - self.portainer.get_containers(endpoint_id=endpoint.id), - self.portainer.docker_version(endpoint_id=endpoint.id), - self.portainer.docker_info(endpoint_id=endpoint.id), + self.portainer.get_containers(endpoint.id), + self.portainer.docker_version(endpoint.id), + self.portainer.docker_info(endpoint.id), self.portainer.docker_system_df(endpoint.id), - self.portainer.get_stacks(endpoint_id=endpoint.id), + self.portainer.get_stacks(endpoint.id), ) prev_endpoint = self.data.get(endpoint.id) if self.data else None diff --git a/homeassistant/components/portainer/entity.py b/homeassistant/components/portainer/entity.py index e0bc7ea12ea803..9fb87248e633dc 100644 --- a/homeassistant/components/portainer/entity.py +++ b/homeassistant/components/portainer/entity.py @@ -4,6 +4,7 @@ from homeassistant.const import CONF_URL from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo +from homeassistant.helpers.entity import EntityDescription from homeassistant.helpers.update_coordinator import CoordinatorEntity from .const import DEFAULT_NAME, DOMAIN @@ -26,11 +27,13 @@ class PortainerEndpointEntity(PortainerCoordinatorEntity): def __init__( self, - device_info: PortainerCoordinatorData, coordinator: PortainerCoordinator, + entity_description: EntityDescription, + device_info: PortainerCoordinatorData, ) -> None: """Initialize a Portainer endpoint.""" super().__init__(coordinator) + self.entity_description = entity_description self._device_info = device_info self.device_id = device_info.endpoint.id self._attr_device_info = DeviceInfo( @@ -45,6 +48,7 @@ def __init__( name=device_info.endpoint.name, entry_type=DeviceEntryType.SERVICE, ) + self._attr_unique_id = f"{coordinator.config_entry.entry_id}_{device_info.id}_{entity_description.key}" @property def available(self) -> bool: @@ -57,12 +61,14 @@ class PortainerContainerEntity(PortainerCoordinatorEntity): def __init__( self, - device_info: PortainerContainerData, coordinator: PortainerCoordinator, + entity_description: EntityDescription, + device_info: PortainerContainerData, via_device: PortainerCoordinatorData, ) -> None: """Initialize a Portainer container.""" super().__init__(coordinator) + self.entity_description = entity_description self._device_info = device_info self.device_id = self._device_info.container.id self.endpoint_id = via_device.endpoint.id @@ -98,6 +104,7 @@ def __init__( translation_key=None if self.device_name else "unknown_container", entry_type=DeviceEntryType.SERVICE, ) + self._attr_unique_id = f"{coordinator.config_entry.entry_id}_{self.device_name}_{entity_description.key}" @property def available(self) -> bool: @@ -119,12 +126,14 @@ class PortainerStackEntity(PortainerCoordinatorEntity): def __init__( self, - device_info: PortainerStackData, coordinator: PortainerCoordinator, + entity_description: EntityDescription, + device_info: PortainerStackData, via_device: PortainerCoordinatorData, ) -> None: """Initialize a Portainer stack.""" super().__init__(coordinator) + self.entity_description = entity_description self._device_info = device_info self.stack_id = device_info.stack.id self.device_name = device_info.stack.name @@ -149,6 +158,7 @@ def __init__( f"{coordinator.config_entry.entry_id}_{self.endpoint_id}", ), ) + self._attr_unique_id = f"{coordinator.config_entry.entry_id}_{self.stack_id}_{entity_description.key}" @property def available(self) -> bool: diff --git a/homeassistant/components/portainer/sensor.py b/homeassistant/components/portainer/sensor.py index 81f80b5b7b70b1..be23d58a4f301b 100644 --- a/homeassistant/components/portainer/sensor.py +++ b/homeassistant/components/portainer/sensor.py @@ -21,7 +21,6 @@ from .coordinator import ( PortainerConfigEntry, PortainerContainerData, - PortainerCoordinator, PortainerStackData, ) from .entity import ( @@ -398,19 +397,6 @@ class PortainerContainerSensor(PortainerContainerEntity, SensorEntity): entity_description: PortainerContainerSensorEntityDescription - def __init__( - self, - coordinator: PortainerCoordinator, - entity_description: PortainerContainerSensorEntityDescription, - device_info: PortainerContainerData, - via_device: PortainerCoordinatorData, - ) -> None: - """Initialize the Portainer container sensor.""" - self.entity_description = entity_description - super().__init__(device_info, coordinator, via_device) - - self._attr_unique_id = f"{coordinator.config_entry.entry_id}_{self.device_name}_{entity_description.key}" - @property def native_value(self) -> StateType: """Return the state of the sensor.""" @@ -422,18 +408,6 @@ class PortainerEndpointSensor(PortainerEndpointEntity, SensorEntity): entity_description: PortainerEndpointSensorEntityDescription - def __init__( - self, - coordinator: PortainerCoordinator, - entity_description: PortainerEndpointSensorEntityDescription, - device_info: PortainerCoordinatorData, - ) -> None: - """Initialize the Portainer endpoint sensor.""" - self.entity_description = entity_description - super().__init__(device_info, coordinator) - - self._attr_unique_id = f"{coordinator.config_entry.entry_id}_{device_info.id}_{entity_description.key}" - @property def native_value(self) -> StateType: """Return the state of the sensor.""" @@ -446,19 +420,6 @@ class PortainerStackSensor(PortainerStackEntity, SensorEntity): entity_description: PortainerStackSensorEntityDescription - def __init__( - self, - coordinator: PortainerCoordinator, - entity_description: PortainerStackSensorEntityDescription, - device_info: PortainerStackData, - via_device: PortainerCoordinatorData, - ) -> None: - """Initialize the Portainer stack sensor.""" - self.entity_description = entity_description - super().__init__(device_info, coordinator, via_device) - - self._attr_unique_id = f"{coordinator.config_entry.entry_id}_{device_info.stack.id}_{entity_description.key}" - @property def native_value(self) -> StateType: """Return the state of the sensor.""" diff --git a/homeassistant/components/portainer/switch.py b/homeassistant/components/portainer/switch.py index 429b4fee469fba..32b705083027d1 100644 --- a/homeassistant/components/portainer/switch.py +++ b/homeassistant/components/portainer/switch.py @@ -167,19 +167,6 @@ class PortainerContainerSwitch(PortainerContainerEntity, SwitchEntity): entity_description: PortainerSwitchEntityDescription - def __init__( - self, - coordinator: PortainerCoordinator, - entity_description: PortainerSwitchEntityDescription, - device_info: PortainerContainerData, - via_device: PortainerCoordinatorData, - ) -> None: - """Initialize the Portainer container switch.""" - self.entity_description = entity_description - super().__init__(device_info, coordinator, via_device) - - self._attr_unique_id = f"{coordinator.config_entry.entry_id}_{self.device_name}_{entity_description.key}" - @property def is_on(self) -> bool | None: """Return the state of the device.""" @@ -209,19 +196,6 @@ class PortainerStackSwitch(PortainerStackEntity, SwitchEntity): entity_description: PortainerStackSwitchEntityDescription - def __init__( - self, - coordinator: PortainerCoordinator, - entity_description: PortainerStackSwitchEntityDescription, - device_info: PortainerStackData, - via_device: PortainerCoordinatorData, - ) -> None: - """Initialize the Portainer stack switch.""" - self.entity_description = entity_description - super().__init__(device_info, coordinator, via_device) - - self._attr_unique_id = f"{coordinator.config_entry.entry_id}_{device_info.stack.id}_{entity_description.key}" - @property def is_on(self) -> bool | None: """Return the state of the device.""" diff --git a/homeassistant/components/proxmoxve/binary_sensor.py b/homeassistant/components/proxmoxve/binary_sensor.py index 1d607a741bd7cf..d688e41b624460 100644 --- a/homeassistant/components/proxmoxve/binary_sensor.py +++ b/homeassistant/components/proxmoxve/binary_sensor.py @@ -17,7 +17,7 @@ from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback from .const import NODE_ONLINE, VM_CONTAINER_RUNNING -from .coordinator import ProxmoxConfigEntry, ProxmoxCoordinator, ProxmoxNodeData +from .coordinator import ProxmoxConfigEntry, ProxmoxNodeData from .entity import ProxmoxContainerEntity, ProxmoxNodeEntity, ProxmoxVMEntity _LOGGER = logging.getLogger(__name__) @@ -147,18 +147,6 @@ class ProxmoxNodeBinarySensor(ProxmoxNodeEntity, BinarySensorEntity): entity_description: ProxmoxNodeBinarySensorEntityDescription - def __init__( - self, - coordinator: ProxmoxCoordinator, - entity_description: ProxmoxNodeBinarySensorEntityDescription, - node_data: ProxmoxNodeData, - ) -> None: - """Initialize Proxmox node binary sensor entity.""" - self.entity_description = entity_description - super().__init__(coordinator, node_data) - - self._attr_unique_id = f"{coordinator.config_entry.entry_id}_{node_data.node['id']}_{entity_description.key}" - @property def is_on(self) -> bool | None: """Return true if the binary sensor is on.""" @@ -170,19 +158,6 @@ class ProxmoxVMBinarySensor(ProxmoxVMEntity, BinarySensorEntity): entity_description: ProxmoxVMBinarySensorEntityDescription - def __init__( - self, - coordinator: ProxmoxCoordinator, - entity_description: ProxmoxVMBinarySensorEntityDescription, - vm_data: dict[str, Any], - node_data: ProxmoxNodeData, - ) -> None: - """Initialize the Proxmox VM binary sensor.""" - self.entity_description = entity_description - super().__init__(coordinator, vm_data, node_data) - - self._attr_unique_id = f"{coordinator.config_entry.entry_id}_{self.device_id}_{entity_description.key}" - @property def is_on(self) -> bool | None: """Return true if the binary sensor is on.""" @@ -194,19 +169,6 @@ class ProxmoxContainerBinarySensor(ProxmoxContainerEntity, BinarySensorEntity): entity_description: ProxmoxContainerBinarySensorEntityDescription - def __init__( - self, - coordinator: ProxmoxCoordinator, - entity_description: ProxmoxContainerBinarySensorEntityDescription, - container_data: dict[str, Any], - node_data: ProxmoxNodeData, - ) -> None: - """Initialize the Proxmox Container binary sensor.""" - self.entity_description = entity_description - super().__init__(coordinator, container_data, node_data) - - self._attr_unique_id = f"{coordinator.config_entry.entry_id}_{self.device_id}_{entity_description.key}" - @property def is_on(self) -> bool | None: """Return true if the binary sensor is on.""" diff --git a/homeassistant/components/proxmoxve/button.py b/homeassistant/components/proxmoxve/button.py index 8f8e3ddeb723dd..da23ecbc84201e 100644 --- a/homeassistant/components/proxmoxve/button.py +++ b/homeassistant/components/proxmoxve/button.py @@ -262,18 +262,6 @@ class ProxmoxNodeButtonEntity(ProxmoxNodeEntity, ProxmoxBaseButton): entity_description: ProxmoxNodeButtonNodeEntityDescription - def __init__( - self, - coordinator: ProxmoxCoordinator, - entity_description: ProxmoxNodeButtonNodeEntityDescription, - node_data: ProxmoxNodeData, - ) -> None: - """Initialize the Proxmox Node button entity.""" - self.entity_description = entity_description - super().__init__(coordinator, node_data) - - self._attr_unique_id = f"{coordinator.config_entry.entry_id}_{node_data.node['id']}_{entity_description.key}" - async def _async_press_call(self) -> None: """Execute the node button action via executor.""" await self.hass.async_add_executor_job( @@ -288,19 +276,6 @@ class ProxmoxVMButtonEntity(ProxmoxVMEntity, ProxmoxBaseButton): entity_description: ProxmoxVMButtonEntityDescription - def __init__( - self, - coordinator: ProxmoxCoordinator, - entity_description: ProxmoxVMButtonEntityDescription, - vm_data: dict[str, Any], - node_data: ProxmoxNodeData, - ) -> None: - """Initialize the Proxmox VM button entity.""" - self.entity_description = entity_description - super().__init__(coordinator, vm_data, node_data) - - self._attr_unique_id = f"{coordinator.config_entry.entry_id}_{self.device_id}_{entity_description.key}" - async def _async_press_call(self) -> None: """Execute the VM button action via executor.""" await self.hass.async_add_executor_job( @@ -316,19 +291,6 @@ class ProxmoxContainerButtonEntity(ProxmoxContainerEntity, ProxmoxBaseButton): entity_description: ProxmoxContainerButtonEntityDescription - def __init__( - self, - coordinator: ProxmoxCoordinator, - entity_description: ProxmoxContainerButtonEntityDescription, - container_data: dict[str, Any], - node_data: ProxmoxNodeData, - ) -> None: - """Initialize the Proxmox Container button entity.""" - self.entity_description = entity_description - super().__init__(coordinator, container_data, node_data) - - self._attr_unique_id = f"{coordinator.config_entry.entry_id}_{self.device_id}_{entity_description.key}" - async def _async_press_call(self) -> None: """Execute the container button action via executor.""" await self.hass.async_add_executor_job( diff --git a/homeassistant/components/proxmoxve/entity.py b/homeassistant/components/proxmoxve/entity.py index 2bae10f7ed37f8..5684845391a6d3 100644 --- a/homeassistant/components/proxmoxve/entity.py +++ b/homeassistant/components/proxmoxve/entity.py @@ -8,6 +8,7 @@ from homeassistant.const import CONF_HOST, CONF_PORT from homeassistant.helpers.device_registry import DeviceInfo +from homeassistant.helpers.entity import EntityDescription from homeassistant.helpers.update_coordinator import CoordinatorEntity from .const import DOMAIN @@ -36,6 +37,7 @@ class ProxmoxNodeEntity(ProxmoxCoordinatorEntity): def __init__( self, coordinator: ProxmoxCoordinator, + entity_description: EntityDescription, node_data: ProxmoxNodeData, ) -> None: """Initialize the Proxmox node entity.""" @@ -43,6 +45,7 @@ def __init__( self._node_data = node_data self.device_id = node_data.node["id"] self.device_name = node_data.node["node"] + self.entity_description = entity_description self._attr_device_info = DeviceInfo( identifiers={ (DOMAIN, f"{coordinator.config_entry.entry_id}_node_{self.device_id}") @@ -54,6 +57,8 @@ def __init__( ), ) + self._attr_unique_id = f"{coordinator.config_entry.entry_id}_{node_data.node['id']}_{entity_description.key}" + @property def available(self) -> bool: """Return if the device is available.""" @@ -66,11 +71,13 @@ class ProxmoxVMEntity(ProxmoxCoordinatorEntity): def __init__( self, coordinator: ProxmoxCoordinator, + entity_description: EntityDescription, vm_data: dict[str, Any], node_data: ProxmoxNodeData, ) -> None: """Initialize the Proxmox VM entity.""" super().__init__(coordinator) + self.entity_description = entity_description self._vm_data = vm_data self._node_name = node_data.node["node"] self.device_id = vm_data["vmid"] @@ -91,6 +98,8 @@ def __init__( ), ) + self._attr_unique_id = f"{coordinator.config_entry.entry_id}_{self.device_id}_{entity_description.key}" + @property def available(self) -> bool: """Return if the device is available.""" @@ -112,11 +121,13 @@ class ProxmoxContainerEntity(ProxmoxCoordinatorEntity): def __init__( self, coordinator: ProxmoxCoordinator, + entity_description: EntityDescription, container_data: dict[str, Any], node_data: ProxmoxNodeData, ) -> None: """Initialize the Proxmox Container entity.""" super().__init__(coordinator) + self.entity_description = entity_description self._container_data = container_data self._node_name = node_data.node["node"] self.device_id = container_data["vmid"] @@ -140,6 +151,8 @@ def __init__( ), ) + self._attr_unique_id = f"{coordinator.config_entry.entry_id}_{self.device_id}_{entity_description.key}" + @property def available(self) -> bool: """Return if the device is available.""" diff --git a/homeassistant/components/proxmoxve/sensor.py b/homeassistant/components/proxmoxve/sensor.py index 1a680b1a4a3912..f8137b6e757efd 100644 --- a/homeassistant/components/proxmoxve/sensor.py +++ b/homeassistant/components/proxmoxve/sensor.py @@ -18,7 +18,7 @@ from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback -from .coordinator import ProxmoxConfigEntry, ProxmoxCoordinator, ProxmoxNodeData +from .coordinator import ProxmoxConfigEntry, ProxmoxNodeData from .entity import ProxmoxContainerEntity, ProxmoxNodeEntity, ProxmoxVMEntity @@ -320,18 +320,6 @@ class ProxmoxNodeSensor(ProxmoxNodeEntity, SensorEntity): entity_description: ProxmoxNodeSensorEntityDescription - def __init__( - self, - coordinator: ProxmoxCoordinator, - entity_description: ProxmoxNodeSensorEntityDescription, - node_data: ProxmoxNodeData, - ) -> None: - """Initialize the sensor.""" - super().__init__(coordinator, node_data) - self.entity_description = entity_description - - self._attr_unique_id = f"{coordinator.config_entry.entry_id}_{node_data.node['id']}_{entity_description.key}" - @property def native_value(self) -> StateType: """Return the native value of the sensor.""" @@ -343,19 +331,6 @@ class ProxmoxVMSensor(ProxmoxVMEntity, SensorEntity): entity_description: ProxmoxVMSensorEntityDescription - def __init__( - self, - coordinator: ProxmoxCoordinator, - entity_description: ProxmoxVMSensorEntityDescription, - vm_data: dict[str, Any], - node_data: ProxmoxNodeData, - ) -> None: - """Initialize the Proxmox VM sensor.""" - self.entity_description = entity_description - super().__init__(coordinator, vm_data, node_data) - - self._attr_unique_id = f"{coordinator.config_entry.entry_id}_{self.device_id}_{entity_description.key}" - @property def native_value(self) -> StateType: """Return the native value of the sensor.""" @@ -367,19 +342,6 @@ class ProxmoxContainerSensor(ProxmoxContainerEntity, SensorEntity): entity_description: ProxmoxContainerSensorEntityDescription - def __init__( - self, - coordinator: ProxmoxCoordinator, - entity_description: ProxmoxContainerSensorEntityDescription, - container_data: dict[str, Any], - node_data: ProxmoxNodeData, - ) -> None: - """Initialize the Proxmox container sensor.""" - self.entity_description = entity_description - super().__init__(coordinator, container_data, node_data) - - self._attr_unique_id = f"{coordinator.config_entry.entry_id}_{self.device_id}_{entity_description.key}" - @property def native_value(self) -> StateType: """Return the native value of the sensor.""" diff --git a/homeassistant/components/schlage/__init__.py b/homeassistant/components/schlage/__init__.py index 509a335aafe8fb..ed995d4aa3d4a7 100644 --- a/homeassistant/components/schlage/__init__.py +++ b/homeassistant/components/schlage/__init__.py @@ -4,11 +4,16 @@ from pycognito.exceptions import WarrantException import pyschlage +import voluptuous as vol +from homeassistant.components.lock import DOMAIN as LOCK_DOMAIN from homeassistant.const import CONF_PASSWORD, CONF_USERNAME, Platform -from homeassistant.core import HomeAssistant +from homeassistant.core import HomeAssistant, SupportsResponse from homeassistant.exceptions import ConfigEntryAuthFailed +from homeassistant.helpers import config_validation as cv, service +from homeassistant.helpers.typing import ConfigType +from .const import DOMAIN, SERVICE_ADD_CODE, SERVICE_DELETE_CODE, SERVICE_GET_CODES from .coordinator import SchlageConfigEntry, SchlageDataUpdateCoordinator PLATFORMS: list[Platform] = [ @@ -19,6 +24,46 @@ Platform.SWITCH, ] +CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN) + + +async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: + """Set up the Schlage component.""" + service.async_register_platform_entity_service( + hass, + DOMAIN, + SERVICE_ADD_CODE, + entity_domain=LOCK_DOMAIN, + schema={ + vol.Required("name"): cv.string, + vol.Required("code"): cv.matches_regex(r"^\d{4,8}$"), + }, + func=SERVICE_ADD_CODE, + ) + + service.async_register_platform_entity_service( + hass, + DOMAIN, + SERVICE_DELETE_CODE, + entity_domain=LOCK_DOMAIN, + schema={ + vol.Required("name"): cv.string, + }, + func=SERVICE_DELETE_CODE, + ) + + service.async_register_platform_entity_service( + hass, + DOMAIN, + SERVICE_GET_CODES, + entity_domain=LOCK_DOMAIN, + schema=None, + func=SERVICE_GET_CODES, + supports_response=SupportsResponse.ONLY, + ) + + return True + async def async_setup_entry(hass: HomeAssistant, entry: SchlageConfigEntry) -> bool: """Set up Schlage from a config entry.""" diff --git a/homeassistant/components/schlage/const.py b/homeassistant/components/schlage/const.py index 1effd4bb33429f..75033520d3f07e 100644 --- a/homeassistant/components/schlage/const.py +++ b/homeassistant/components/schlage/const.py @@ -7,3 +7,7 @@ LOGGER = logging.getLogger(__package__) MANUFACTURER = "Schlage" UPDATE_INTERVAL = timedelta(seconds=30) + +SERVICE_ADD_CODE = "add_code" +SERVICE_DELETE_CODE = "delete_code" +SERVICE_GET_CODES = "get_codes" diff --git a/homeassistant/components/schlage/icons.json b/homeassistant/components/schlage/icons.json new file mode 100644 index 00000000000000..c231233be5167f --- /dev/null +++ b/homeassistant/components/schlage/icons.json @@ -0,0 +1,13 @@ +{ + "services": { + "add_code": { + "service": "mdi:key-plus" + }, + "delete_code": { + "service": "mdi:key-minus" + }, + "get_codes": { + "service": "mdi:table-key" + } + } +} diff --git a/homeassistant/components/schlage/lock.py b/homeassistant/components/schlage/lock.py index 83abf9214e38e8..739e5a0b1d70c1 100644 --- a/homeassistant/components/schlage/lock.py +++ b/homeassistant/components/schlage/lock.py @@ -4,10 +4,15 @@ from typing import Any +from pyschlage.code import AccessCode +from pyschlage.exceptions import Error as SchlageError + from homeassistant.components.lock import LockEntity -from homeassistant.core import HomeAssistant, callback +from homeassistant.core import HomeAssistant, ServiceResponse, callback +from homeassistant.exceptions import HomeAssistantError, ServiceValidationError from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback +from .const import DOMAIN from .coordinator import LockData, SchlageConfigEntry, SchlageDataUpdateCoordinator from .entity import SchlageEntity @@ -64,3 +69,108 @@ async def async_unlock(self, **kwargs: Any) -> None: """Unlock the device.""" await self.hass.async_add_executor_job(self._lock.unlock) await self.coordinator.async_request_refresh() + + @staticmethod + def _normalize_code_name(name: str) -> str: + """Normalize a code name for comparison.""" + return name.lower().strip() + + def _validate_code_name( + self, codes: dict[str, AccessCode] | None, name: str + ) -> None: + """Validate that the code name doesn't already exist.""" + normalized = self._normalize_code_name(name) + if codes and any( + self._normalize_code_name(code.name) == normalized + for code in codes.values() + ): + raise ServiceValidationError( + translation_domain=DOMAIN, + translation_key="schlage_name_exists", + translation_placeholders={"name": name}, + ) + + def _validate_code_value( + self, codes: dict[str, AccessCode] | None, code: str + ) -> None: + """Validate that the code value doesn't already exist.""" + if codes and any( + existing_code.code == code for existing_code in codes.values() + ): + raise ServiceValidationError( + translation_domain=DOMAIN, + translation_key="schlage_code_exists", + ) + + async def _async_fetch_access_codes(self) -> dict[str, AccessCode] | None: + """Fetch access codes from the lock on demand.""" + try: + await self.hass.async_add_executor_job(self._lock.refresh_access_codes) + except SchlageError as ex: + raise HomeAssistantError( + translation_domain=DOMAIN, + translation_key="schlage_refresh_failed", + ) from ex + return self._lock.access_codes + + async def add_code(self, name: str, code: str) -> None: + """Add a lock code.""" + + codes = await self._async_fetch_access_codes() + self._validate_code_name(codes, name) + self._validate_code_value(codes, code) + + access_code = AccessCode(name=name, code=code) + try: + await self.hass.async_add_executor_job( + self._lock.add_access_code, access_code + ) + except SchlageError as ex: + raise HomeAssistantError( + translation_domain=DOMAIN, + translation_key="schlage_add_code_failed", + ) from ex + await self.coordinator.async_request_refresh() + + async def delete_code(self, name: str) -> None: + """Delete a lock code.""" + codes = await self._async_fetch_access_codes() + if not codes: + return + + normalized = self._normalize_code_name(name) + code_id_to_delete = next( + ( + code_id + for code_id, code_data in codes.items() + if self._normalize_code_name(code_data.name) == normalized + ), + None, + ) + + if not code_id_to_delete: + # Code not found in defined codes, operation successful + return + + try: + await self.hass.async_add_executor_job(codes[code_id_to_delete].delete) + except SchlageError as ex: + raise HomeAssistantError( + translation_domain=DOMAIN, + translation_key="schlage_delete_code_failed", + ) from ex + await self.coordinator.async_request_refresh() + + async def get_codes(self) -> ServiceResponse: + """Get lock codes.""" + await self._async_fetch_access_codes() + + if self._lock.access_codes: + return { + code: { + "name": self._lock.access_codes[code].name, + "code": self._lock.access_codes[code].code, + } + for code in self._lock.access_codes + } + return {} diff --git a/homeassistant/components/schlage/services.yaml b/homeassistant/components/schlage/services.yaml new file mode 100644 index 00000000000000..97412251ea43c4 --- /dev/null +++ b/homeassistant/components/schlage/services.yaml @@ -0,0 +1,38 @@ +get_codes: + target: + entity: + domain: lock + integration: schlage + +add_code: + target: + entity: + domain: lock + integration: schlage + fields: + name: + required: true + example: "Example Person" + selector: + text: + multiline: false + code: + required: true + example: "1111" + selector: + text: + multiline: false + type: password + +delete_code: + target: + entity: + domain: lock + integration: schlage + fields: + name: + required: true + example: "Example Person" + selector: + text: + multiline: false diff --git a/homeassistant/components/schlage/strings.json b/homeassistant/components/schlage/strings.json index 838dc049808629..3710fd7e3f781d 100644 --- a/homeassistant/components/schlage/strings.json +++ b/homeassistant/components/schlage/strings.json @@ -56,8 +56,50 @@ } }, "exceptions": { + "schlage_add_code_failed": { + "message": "Failed to add PIN code to the lock." + }, + "schlage_code_exists": { + "message": "A PIN code with this value already exists on the lock." + }, + "schlage_delete_code_failed": { + "message": "Failed to delete PIN code from the lock." + }, + "schlage_name_exists": { + "message": "A PIN code with the name \"{name}\" already exists on the lock." + }, "schlage_refresh_failed": { - "message": "Failed to refresh Schlage data" + "message": "Failed to refresh Schlage data." + } + }, + "services": { + "add_code": { + "description": "Add a PIN code to a lock.", + "fields": { + "code": { + "description": "The PIN code to add. Must be unique to lock and be between 4 and 8 digits long.", + "name": "PIN code" + }, + "name": { + "description": "Name for PIN code. Must be case insensitively unique to lock.", + "name": "PIN name" + } + }, + "name": "Add PIN code" + }, + "delete_code": { + "description": "Delete a PIN code from a lock.", + "fields": { + "name": { + "description": "Name of PIN code to delete.", + "name": "PIN name" + } + }, + "name": "Delete PIN code" + }, + "get_codes": { + "description": "Retrieve all PIN codes from the lock.", + "name": "Get PIN codes" } } } diff --git a/homeassistant/components/unifiprotect/manifest.json b/homeassistant/components/unifiprotect/manifest.json index e226adce0bd7c9..d921b4127d2a6a 100644 --- a/homeassistant/components/unifiprotect/manifest.json +++ b/homeassistant/components/unifiprotect/manifest.json @@ -41,7 +41,7 @@ "iot_class": "local_push", "loggers": ["uiprotect", "unifi_discovery"], "quality_scale": "platinum", - "requirements": ["uiprotect==10.2.1", "unifi-discovery==1.2.0"], + "requirements": ["uiprotect==10.2.2", "unifi-discovery==1.2.0"], "ssdp": [ { "manufacturer": "Ubiquiti Networks", diff --git a/homeassistant/generated/config_flows.py b/homeassistant/generated/config_flows.py index 1086fad04be771..cbb5542d493cf6 100644 --- a/homeassistant/generated/config_flows.py +++ b/homeassistant/generated/config_flows.py @@ -515,6 +515,7 @@ "openweathermap", "opower", "oralb", + "orvibo", "osoenergy", "otbr", "otp", diff --git a/homeassistant/generated/integrations.json b/homeassistant/generated/integrations.json index e3890c5187747b..0bdb5625a1febc 100644 --- a/homeassistant/generated/integrations.json +++ b/homeassistant/generated/integrations.json @@ -5002,7 +5002,7 @@ "orvibo": { "name": "Orvibo", "integration_type": "hub", - "config_flow": false, + "config_flow": true, "iot_class": "local_push" }, "osoenergy": { diff --git a/homeassistant/package_constraints.txt b/homeassistant/package_constraints.txt index e06b0866c6225b..c67210a9d9bb9f 100644 --- a/homeassistant/package_constraints.txt +++ b/homeassistant/package_constraints.txt @@ -40,7 +40,7 @@ habluetooth==5.8.0 hass-nabucasa==1.15.0 hassil==3.5.0 home-assistant-bluetooth==1.13.1 -home-assistant-frontend==20260225.0 +home-assistant-frontend==20260226.0 home-assistant-intents==2026.2.13 httpx==0.28.1 ifaddr==0.2.0 diff --git a/requirements_all.txt b/requirements_all.txt index d2cad36d058cb5..408a2f1b01a32e 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -1226,7 +1226,7 @@ hole==0.9.0 holidays==0.84 # homeassistant.components.frontend -home-assistant-frontend==20260225.0 +home-assistant-frontend==20260226.0 # homeassistant.components.conversation home-assistant-intents==2026.2.13 @@ -3148,7 +3148,7 @@ uasiren==0.0.1 uhooapi==1.2.6 # homeassistant.components.unifiprotect -uiprotect==10.2.1 +uiprotect==10.2.2 # homeassistant.components.landisgyr_heat_meter ultraheat-api==0.5.7 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 4ffd87f4305641..1945f4579acf84 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -1087,7 +1087,7 @@ hole==0.9.0 holidays==0.84 # homeassistant.components.frontend -home-assistant-frontend==20260225.0 +home-assistant-frontend==20260226.0 # homeassistant.components.conversation home-assistant-intents==2026.2.13 @@ -1499,6 +1499,9 @@ opower==0.17.0 # homeassistant.components.oralb oralb-ble==1.0.2 +# homeassistant.components.orvibo +orvibo==1.1.2 + # homeassistant.components.ourgroceries ourgroceries==1.5.4 @@ -2648,7 +2651,7 @@ uasiren==0.0.1 uhooapi==1.2.6 # homeassistant.components.unifiprotect -uiprotect==10.2.1 +uiprotect==10.2.2 # homeassistant.components.landisgyr_heat_meter ultraheat-api==0.5.7 diff --git a/tests/components/abode/test_init.py b/tests/components/abode/test_init.py index f767c2a9a3d73a..0ef1f2c92fc933 100644 --- a/tests/components/abode/test_init.py +++ b/tests/components/abode/test_init.py @@ -9,7 +9,6 @@ ) from homeassistant.components.abode.const import DOMAIN -from homeassistant.components.abode.services import SERVICE_SETTINGS from homeassistant.components.alarm_control_panel import DOMAIN as ALARM_DOMAIN from homeassistant.config_entries import ConfigEntryState from homeassistant.const import CONF_USERNAME @@ -25,7 +24,7 @@ async def test_change_settings(hass: HomeAssistant) -> None: with patch("jaraco.abode.client.Client.set_setting") as mock_set_setting: await hass.services.async_call( DOMAIN, - SERVICE_SETTINGS, + "change_setting", {"setting": "confirm_snd", "value": "loud"}, blocking=True, ) diff --git a/tests/components/abode/test_switch.py b/tests/components/abode/test_switch.py index 7e67c0d74146af..5e661402bb4b8a 100644 --- a/tests/components/abode/test_switch.py +++ b/tests/components/abode/test_switch.py @@ -3,7 +3,6 @@ from unittest.mock import patch from homeassistant.components.abode.const import DOMAIN -from homeassistant.components.abode.services import SERVICE_TRIGGER_AUTOMATION from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN from homeassistant.const import ( ATTR_ENTITY_ID, @@ -118,7 +117,7 @@ async def test_trigger_automation(hass: HomeAssistant) -> None: with patch("jaraco.abode.automation.Automation.trigger") as mock: await hass.services.async_call( DOMAIN, - SERVICE_TRIGGER_AUTOMATION, + "trigger_automation", {ATTR_ENTITY_ID: AUTOMATION_ID}, blocking=True, ) diff --git a/tests/components/advantage_air/test_sensor.py b/tests/components/advantage_air/test_sensor.py index 3e2120d20e3f0e..7ab0224f14bf25 100644 --- a/tests/components/advantage_air/test_sensor.py +++ b/tests/components/advantage_air/test_sensor.py @@ -7,9 +7,6 @@ from homeassistant.components.advantage_air.sensor import ( ADVANTAGE_AIR_SET_COUNTDOWN_VALUE, ) -from homeassistant.components.advantage_air.services import ( - ADVANTAGE_AIR_SERVICE_SET_TIME_TO, -) from homeassistant.const import ATTR_ENTITY_ID from homeassistant.core import HomeAssistant from homeassistant.helpers import entity_registry as er @@ -44,7 +41,7 @@ async def test_sensor_platform( await hass.services.async_call( DOMAIN, - ADVANTAGE_AIR_SERVICE_SET_TIME_TO, + "set_time_to", {ATTR_ENTITY_ID: [entity_id], ADVANTAGE_AIR_SET_COUNTDOWN_VALUE: value}, blocking=True, ) @@ -64,7 +61,7 @@ async def test_sensor_platform( value = 0 await hass.services.async_call( DOMAIN, - ADVANTAGE_AIR_SERVICE_SET_TIME_TO, + "set_time_to", {ATTR_ENTITY_ID: [entity_id], ADVANTAGE_AIR_SET_COUNTDOWN_VALUE: value}, blocking=True, ) diff --git a/tests/components/amberelectric/test_services.py b/tests/components/amberelectric/test_services.py index cec59fc8f75ba1..c5737ebf523301 100644 --- a/tests/components/amberelectric/test_services.py +++ b/tests/components/amberelectric/test_services.py @@ -5,7 +5,7 @@ import pytest import voluptuous as vol -from homeassistant.components.amberelectric.const import DOMAIN, SERVICE_GET_FORECASTS +from homeassistant.components.amberelectric.const import DOMAIN from homeassistant.components.amberelectric.services import ATTR_CHANNEL_TYPE from homeassistant.const import ATTR_CONFIG_ENTRY_ID from homeassistant.core import HomeAssistant @@ -30,7 +30,7 @@ async def test_get_general_forecasts( await setup_integration(hass, general_channel_config_entry) result = await hass.services.async_call( DOMAIN, - SERVICE_GET_FORECASTS, + "get_forecasts", {ATTR_CONFIG_ENTRY_ID: GENERAL_ONLY_SITE_ID, ATTR_CHANNEL_TYPE: "general"}, blocking=True, return_response=True, @@ -59,7 +59,7 @@ async def test_get_controlled_load_forecasts( await setup_integration(hass, general_channel_and_controlled_load_config_entry) result = await hass.services.async_call( DOMAIN, - SERVICE_GET_FORECASTS, + "get_forecasts", { ATTR_CONFIG_ENTRY_ID: GENERAL_AND_CONTROLLED_SITE_ID, ATTR_CHANNEL_TYPE: "controlled_load", @@ -91,7 +91,7 @@ async def test_get_feed_in_forecasts( await setup_integration(hass, general_channel_and_feed_in_config_entry) result = await hass.services.async_call( DOMAIN, - SERVICE_GET_FORECASTS, + "get_forecasts", { ATTR_CONFIG_ENTRY_ID: GENERAL_AND_FEED_IN_SITE_ID, ATTR_CHANNEL_TYPE: "feed_in", @@ -130,7 +130,7 @@ async def test_incorrect_channel_type( ): await hass.services.async_call( DOMAIN, - SERVICE_GET_FORECASTS, + "get_forecasts", { ATTR_CONFIG_ENTRY_ID: GENERAL_ONLY_SITE_ID, ATTR_CHANNEL_TYPE: "incorrect", @@ -153,7 +153,7 @@ async def test_unavailable_channel_type( ): await hass.services.async_call( DOMAIN, - SERVICE_GET_FORECASTS, + "get_forecasts", { ATTR_CONFIG_ENTRY_ID: GENERAL_ONLY_SITE_ID, ATTR_CHANNEL_TYPE: "controlled_load", @@ -178,7 +178,7 @@ async def test_service_entry_availability( with pytest.raises(ServiceValidationError) as err: await hass.services.async_call( DOMAIN, - SERVICE_GET_FORECASTS, + "get_forecasts", { ATTR_CONFIG_ENTRY_ID: mock_config_entry2.entry_id, ATTR_CHANNEL_TYPE: "general", @@ -192,7 +192,7 @@ async def test_service_entry_availability( with pytest.raises(ServiceValidationError) as err: await hass.services.async_call( DOMAIN, - SERVICE_GET_FORECASTS, + "get_forecasts", {ATTR_CONFIG_ENTRY_ID: "bad-config_id", ATTR_CHANNEL_TYPE: "general"}, blocking=True, return_response=True, diff --git a/tests/components/androidtv/test_media_player.py b/tests/components/androidtv/test_media_player.py index 2588f61177f5f3..bc20a25ff9cf91 100644 --- a/tests/components/androidtv/test_media_player.py +++ b/tests/components/androidtv/test_media_player.py @@ -24,10 +24,6 @@ from homeassistant.components.androidtv.services import ( ATTR_DEVICE_PATH, ATTR_LOCAL_PATH, - SERVICE_ADB_COMMAND, - SERVICE_DOWNLOAD, - SERVICE_LEARN_SENDEVENT, - SERVICE_UPLOAD, ) from homeassistant.components.media_player import ( ATTR_INPUT_SOURCE, @@ -503,7 +499,7 @@ async def test_adb_command(hass: HomeAssistant) -> None: ) as patch_shell: await hass.services.async_call( DOMAIN, - SERVICE_ADB_COMMAND, + "adb_command", {ATTR_ENTITY_ID: entity_id, ATTR_COMMAND: command}, blocking=True, ) @@ -534,7 +530,7 @@ async def test_adb_command_unicode_decode_error(hass: HomeAssistant) -> None: ): await hass.services.async_call( DOMAIN, - SERVICE_ADB_COMMAND, + "adb_command", {ATTR_ENTITY_ID: entity_id, ATTR_COMMAND: command}, blocking=True, ) @@ -563,7 +559,7 @@ async def test_adb_command_key(hass: HomeAssistant) -> None: ) as patch_shell: await hass.services.async_call( DOMAIN, - SERVICE_ADB_COMMAND, + "adb_command", {ATTR_ENTITY_ID: entity_id, ATTR_COMMAND: command}, blocking=True, ) @@ -594,7 +590,7 @@ async def test_adb_command_get_properties(hass: HomeAssistant) -> None: ) as patch_get_props: await hass.services.async_call( DOMAIN, - SERVICE_ADB_COMMAND, + "adb_command", {ATTR_ENTITY_ID: entity_id, ATTR_COMMAND: command}, blocking=True, ) @@ -624,7 +620,7 @@ async def test_learn_sendevent(hass: HomeAssistant) -> None: ) as patch_learn_sendevent: await hass.services.async_call( DOMAIN, - SERVICE_LEARN_SENDEVENT, + "learn_sendevent", {ATTR_ENTITY_ID: entity_id}, blocking=True, ) @@ -693,7 +689,7 @@ async def test_download(hass: HomeAssistant) -> None: with patch("androidtv.basetv.basetv_async.BaseTVAsync.adb_pull") as patch_pull: await hass.services.async_call( DOMAIN, - SERVICE_DOWNLOAD, + "download", { ATTR_ENTITY_ID: entity_id, ATTR_DEVICE_PATH: device_path, @@ -710,7 +706,7 @@ async def test_download(hass: HomeAssistant) -> None: ): await hass.services.async_call( DOMAIN, - SERVICE_DOWNLOAD, + "download", { ATTR_ENTITY_ID: entity_id, ATTR_DEVICE_PATH: device_path, @@ -739,7 +735,7 @@ async def test_upload(hass: HomeAssistant) -> None: with patch("androidtv.basetv.basetv_async.BaseTVAsync.adb_push") as patch_push: await hass.services.async_call( DOMAIN, - SERVICE_UPLOAD, + "upload", { ATTR_ENTITY_ID: entity_id, ATTR_DEVICE_PATH: device_path, @@ -756,7 +752,7 @@ async def test_upload(hass: HomeAssistant) -> None: ): await hass.services.async_call( DOMAIN, - SERVICE_UPLOAD, + "upload", { ATTR_ENTITY_ID: entity_id, ATTR_DEVICE_PATH: device_path, diff --git a/tests/components/anthropic/test_conversation.py b/tests/components/anthropic/test_conversation.py index b3aa18265817b7..8c3327b6cc9642 100644 --- a/tests/components/anthropic/test_conversation.py +++ b/tests/components/anthropic/test_conversation.py @@ -4,7 +4,7 @@ from typing import Any from unittest.mock import AsyncMock, Mock, patch -from anthropic import RateLimitError +from anthropic import AuthenticationError, RateLimitError from anthropic.types import ( CitationsWebSearchResultLocation, CitationWebSearchResultLocationParam, @@ -36,8 +36,10 @@ CONF_WEB_SEARCH_REGION, CONF_WEB_SEARCH_TIMEZONE, CONF_WEB_SEARCH_USER_LOCATION, + DOMAIN, ) from homeassistant.components.anthropic.entity import CitationDetails, ContentDetails +from homeassistant.config_entries import SOURCE_REAUTH from homeassistant.const import CONF_LLM_HASS_API from homeassistant.core import Context, HomeAssistant from homeassistant.exceptions import HomeAssistantError @@ -107,7 +109,7 @@ async def test_error_handling( mock_init_component, mock_create_stream: AsyncMock, ) -> None: - """Test that the default prompt works.""" + """Test error handling.""" mock_create_stream.side_effect = RateLimitError( message=None, response=Response(status_code=429, request=Request(method="POST", url=URL())), @@ -122,6 +124,38 @@ async def test_error_handling( assert result.response.error_code == "unknown", result +async def test_auth_error_handling( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + mock_init_component, + mock_create_stream: AsyncMock, +) -> None: + """Test reauth after authentication error during conversation.""" + mock_create_stream.side_effect = AuthenticationError( + message="Invalid API key", + response=Response(status_code=403, request=Request(method="POST", url=URL())), + body=None, + ) + + result = await conversation.async_converse( + hass, "hello", None, Context(), agent_id="conversation.claude_conversation" + ) + + assert result.response.response_type == intent.IntentResponseType.ERROR + assert result.response.error_code == "unknown", result + + await hass.async_block_till_done() + flows = hass.config_entries.flow.async_progress() + assert len(flows) == 1 + + flow = flows[0] + assert flow["step_id"] == "reauth_confirm" + assert flow["handler"] == DOMAIN + assert "context" in flow + assert flow["context"]["source"] == SOURCE_REAUTH + assert flow["context"]["entry_id"] == mock_config_entry.entry_id + + async def test_template_error( hass: HomeAssistant, mock_config_entry: MockConfigEntry, diff --git a/tests/components/device_automation/test_init.py b/tests/components/device_automation/test_init.py index a176199ff91aed..86891e4d28360f 100644 --- a/tests/components/device_automation/test_init.py +++ b/tests/components/device_automation/test_init.py @@ -37,34 +37,34 @@ class MockDeviceEntry(dr.DeviceEntry): @pytest.fixture def fake_integration(hass: HomeAssistant) -> None: """Set up a mock integration with device automation support.""" - DOMAIN = "fake_integration" + FAKE_DOMAIN = "fake_integration" - hass.config.components.add(DOMAIN) + hass.config.components.add(FAKE_DOMAIN) async def _async_get_actions( hass: HomeAssistant, device_id: str ) -> list[dict[str, str]]: """List device actions.""" - return await toggle_entity.async_get_actions(hass, device_id, DOMAIN) + return await toggle_entity.async_get_actions(hass, device_id, FAKE_DOMAIN) async def _async_get_conditions( hass: HomeAssistant, device_id: str ) -> list[dict[str, str]]: """List device conditions.""" - return await toggle_entity.async_get_conditions(hass, device_id, DOMAIN) + return await toggle_entity.async_get_conditions(hass, device_id, FAKE_DOMAIN) async def _async_get_triggers( hass: HomeAssistant, device_id: str ) -> list[dict[str, str]]: """List device triggers.""" - return await toggle_entity.async_get_triggers(hass, device_id, DOMAIN) + return await toggle_entity.async_get_triggers(hass, device_id, FAKE_DOMAIN) mock_platform( hass, - f"{DOMAIN}.device_action", + f"{FAKE_DOMAIN}.device_action", Mock( ACTION_SCHEMA=toggle_entity.ACTION_SCHEMA.extend( - {vol.Required("domain"): DOMAIN} + {vol.Required("domain"): FAKE_DOMAIN} ), async_get_actions=_async_get_actions, spec=["ACTION_SCHEMA", "async_get_actions"], @@ -73,10 +73,10 @@ async def _async_get_triggers( mock_platform( hass, - f"{DOMAIN}.device_condition", + f"{FAKE_DOMAIN}.device_condition", Mock( CONDITION_SCHEMA=toggle_entity.CONDITION_SCHEMA.extend( - {vol.Required("domain"): DOMAIN} + {vol.Required("domain"): FAKE_DOMAIN} ), async_get_conditions=_async_get_conditions, spec=["CONDITION_SCHEMA", "async_get_conditions"], @@ -85,11 +85,13 @@ async def _async_get_triggers( mock_platform( hass, - f"{DOMAIN}.device_trigger", + f"{FAKE_DOMAIN}.device_trigger", Mock( TRIGGER_SCHEMA=vol.All( toggle_entity.TRIGGER_SCHEMA, - vol.Schema({vol.Required("domain"): DOMAIN}, extra=vol.ALLOW_EXTRA), + vol.Schema( + {vol.Required("domain"): FAKE_DOMAIN}, extra=vol.ALLOW_EXTRA + ), ), async_get_triggers=_async_get_triggers, spec=["TRIGGER_SCHEMA", "async_get_triggers"], @@ -1398,7 +1400,7 @@ async def test_automation_with_sub_condition( entity_registry: er.EntityRegistry, ) -> None: """Test automation with device condition under and/or conditions.""" - DOMAIN = "light" + LIGHT_DOMAIN = "light" config_entry = MockConfigEntry(domain="test", data={}) config_entry.add_to_hass(hass) @@ -1429,14 +1431,14 @@ async def test_automation_with_sub_condition( "conditions": [ { "condition": "device", - "domain": DOMAIN, + "domain": LIGHT_DOMAIN, "device_id": device_entry.id, "entity_id": entity_entry1.id, "type": "is_on", }, { "condition": "device", - "domain": DOMAIN, + "domain": LIGHT_DOMAIN, "device_id": device_entry.id, "entity_id": entity_entry2.id, "type": "is_on", @@ -1462,14 +1464,14 @@ async def test_automation_with_sub_condition( "conditions": [ { "condition": "device", - "domain": DOMAIN, + "domain": LIGHT_DOMAIN, "device_id": device_entry.id, "entity_id": entity_entry1.id, "type": "is_on", }, { "condition": "device", - "domain": DOMAIN, + "domain": LIGHT_DOMAIN, "device_id": device_entry.id, "entity_id": entity_entry2.id, "type": "is_on", diff --git a/tests/components/openai_conversation/test_config_flow.py b/tests/components/openai_conversation/test_config_flow.py index a2bdaed9f15972..15c3d32753b2b3 100644 --- a/tests/components/openai_conversation/test_config_flow.py +++ b/tests/components/openai_conversation/test_config_flow.py @@ -267,6 +267,7 @@ async def test_subentry_unsupported_model( ("gpt-5.1", ["none", "low", "medium", "high"]), ("gpt-5.2", ["none", "low", "medium", "high", "xhigh"]), ("gpt-5.2-pro", ["medium", "high", "xhigh"]), + ("gpt-5.3-codex", ["none", "low", "medium", "high", "xhigh"]), ], ) async def test_subentry_reasoning_effort_list( @@ -311,8 +312,15 @@ async def test_subentry_reasoning_effort_list( ) -async def test_subentry_websearch_unsupported_reasoning_effort( - hass: HomeAssistant, mock_config_entry, mock_init_component +@pytest.mark.parametrize( + ("parameter", "error"), + [ + (CONF_WEB_SEARCH, "web_search_minimal_reasoning"), + (CONF_CODE_INTERPRETER, "code_interpreter_minimal_reasoning"), + ], +) +async def test_subentry_unsupported_reasoning_effort( + hass: HomeAssistant, mock_config_entry, mock_init_component, parameter, error ) -> None: """Test the subentry form giving error about unsupported minimal reasoning effort.""" subentry = next(iter(mock_config_entry.subentries.values())) @@ -349,18 +357,18 @@ async def test_subentry_websearch_unsupported_reasoning_effort( subentry_flow["flow_id"], { CONF_REASONING_EFFORT: "minimal", - CONF_WEB_SEARCH: True, + parameter: True, }, ) assert subentry_flow["type"] is FlowResultType.FORM - assert subentry_flow["errors"] == {"web_search": "web_search_minimal_reasoning"} + assert subentry_flow["errors"] == {parameter: error} # Reconfigure model step subentry_flow = await hass.config_entries.subentries.async_configure( subentry_flow["flow_id"], { CONF_REASONING_EFFORT: "low", - CONF_WEB_SEARCH: True, + parameter: True, }, ) assert subentry_flow["type"] is FlowResultType.ABORT diff --git a/tests/components/orvibo/__init__.py b/tests/components/orvibo/__init__.py new file mode 100644 index 00000000000000..d069874c9098f2 --- /dev/null +++ b/tests/components/orvibo/__init__.py @@ -0,0 +1 @@ +"""Tests for the Orvibo integration.""" diff --git a/tests/components/orvibo/conftest.py b/tests/components/orvibo/conftest.py new file mode 100644 index 00000000000000..af20da8030a4c2 --- /dev/null +++ b/tests/components/orvibo/conftest.py @@ -0,0 +1,54 @@ +"""Fixtures for testing the Orvibo integration (core version).""" + +from unittest.mock import patch + +# The orvibo library executes a global UDP socket bind on import. +# We force the import here inside a patch context manager to prevent parallel +# CI test workers from crashing with 'OSError: [Errno 98] Address already in use'. +with patch("socket.socket.bind"): + import orvibo.s20 # noqa: F401 + +import pytest + +from homeassistant.components.orvibo.const import DOMAIN +from homeassistant.const import CONF_HOST, CONF_MAC + +from tests.common import MockConfigEntry + + +@pytest.fixture +def mock_s20(): + """Mock the Orvibo S20 class.""" + with patch("homeassistant.components.orvibo.config_flow.S20") as mock_class: + yield mock_class + + +@pytest.fixture +def mock_discover(): + """Mock Orvibo S20 discovery returning multiple devices.""" + with patch("homeassistant.components.orvibo.config_flow.discover") as mock_func: + mock_func.return_value = { + "192.168.1.100": {"mac": b"\xac\xcf\x23\x12\x34\x56"}, + "192.168.1.101": {"mac": b"\xac\xcf\x23\x78\x9a\xbc"}, + } + yield mock_func + + +@pytest.fixture +def mock_config_entry() -> MockConfigEntry: + """Return a mock config entry for an Orvibo S20 switch.""" + return MockConfigEntry( + domain=DOMAIN, + title="Orvibo (192.168.1.10)", + data={CONF_HOST: "192.168.1.10", CONF_MAC: "aa:bb:cc:dd:ee:ff"}, + unique_id="aa:bb:cc:dd:ee:ff", + ) + + +@pytest.fixture +def mock_setup_entry(): + """Override async_setup_entry so config flow tests don't try to setup the integration.""" + with patch( + "homeassistant.components.orvibo.async_setup_entry", return_value=True + ) as mock_setup: + yield mock_setup diff --git a/tests/components/orvibo/test_config_flow.py b/tests/components/orvibo/test_config_flow.py new file mode 100644 index 00000000000000..efc42528c4eeb3 --- /dev/null +++ b/tests/components/orvibo/test_config_flow.py @@ -0,0 +1,352 @@ +"""Tests for the Orvibo config flow in Home Assistant core.""" + +import asyncio +from typing import Any +from unittest.mock import patch + +from orvibo.s20 import S20Exception +import pytest +import voluptuous as vol + +from homeassistant import config_entries +from homeassistant.components.orvibo.const import CONF_SWITCH_LIST, DEFAULT_NAME, DOMAIN +from homeassistant.const import CONF_HOST, CONF_MAC +from homeassistant.core import HomeAssistant +from homeassistant.data_entry_flow import FlowResultType + +from tests.common import MockConfigEntry + + +async def test_user_menu_display(hass: HomeAssistant) -> None: + """Initial step displays the user menu correctly.""" + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": config_entries.SOURCE_USER} + ) + + assert result["type"] == FlowResultType.MENU + assert result["step_id"] == "user" + assert set(result["menu_options"]) == {"start_discovery", "edit"} + + +@pytest.mark.parametrize( + ("user_input", "expected_mac", "mock_mac_bytes"), + [ + ( + {CONF_HOST: "192.168.1.2", CONF_MAC: "ac:cf:23:12:34:56"}, + "ac:cf:23:12:34:56", + None, + ), + ({CONF_HOST: "192.168.1.2"}, "aa:bb:cc:dd:ee:ff", b"\xaa\xbb\xcc\xdd\xee\xff"), + ], +) +async def test_edit_flow_success( + hass: HomeAssistant, + mock_discover, + mock_setup_entry, + mock_s20, + user_input: dict[str, Any], + expected_mac: str, + mock_mac_bytes: bytes | None, +) -> None: + """Test manual flow succeeds with provided MAC or discovered MAC.""" + mock_s20.return_value._mac = mock_mac_bytes + mock_discover.return_value = {"192.168.1.2": {"mac": b"\xaa\xbb\xcc\xdd\xee\xff"}} + + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": config_entries.SOURCE_USER} + ) + result = await hass.config_entries.flow.async_configure( + result["flow_id"], {"next_step_id": "edit"} + ) + result = await hass.config_entries.flow.async_configure( + result["flow_id"], user_input + ) + + assert result["type"] == FlowResultType.CREATE_ENTRY + assert result["title"] == f"{DEFAULT_NAME} (192.168.1.2)" + assert result["data"][CONF_HOST] == "192.168.1.2" + assert result["data"][CONF_MAC] == expected_mac + assert result["result"].unique_id == expected_mac + + +@pytest.mark.parametrize( + ("user_input", "expected_error", "mock_exception", "mock_mac_bytes"), + [ + ( + {CONF_HOST: "192.168.1.2", CONF_MAC: "not_a_mac"}, + "invalid_mac", + None, + b"dummy", + ), + ({CONF_HOST: "192.168.1.99"}, "cannot_discover", None, None), + ( + {CONF_HOST: "192.168.1.3", CONF_MAC: "ac:cf:23:12:34:56"}, + "cannot_connect", + S20Exception("Connection failed"), + b"dummy", + ), + ], +) +async def test_edit_flow_errors( + hass: HomeAssistant, + mock_s20, + mock_discover, + mock_setup_entry, + user_input: dict[str, Any], + expected_error: str, + mock_exception: Exception | None, + mock_mac_bytes: bytes | None, +) -> None: + """Test various errors in the manual (edit) step and recover.""" + mock_discover.return_value = {} + mock_s20.side_effect = mock_exception + mock_s20.return_value._mac = mock_mac_bytes + + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": config_entries.SOURCE_USER} + ) + result = await hass.config_entries.flow.async_configure( + result["flow_id"], {"next_step_id": "edit"} + ) + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], user_input + ) + + assert result["type"] == FlowResultType.FORM + assert result["errors"]["base"] == expected_error + + mock_s20.side_effect = None + mock_s20.return_value._mac = b"\xac\xcf\x23\x12\x34\x56" + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + {CONF_HOST: "192.168.1.2", CONF_MAC: "ac:cf:23:12:34:56"}, + ) + + assert result["type"] == FlowResultType.CREATE_ENTRY + assert result["title"] == f"{DEFAULT_NAME} (192.168.1.2)" + assert result["data"][CONF_HOST] == "192.168.1.2" + assert result["data"][CONF_MAC] == "ac:cf:23:12:34:56" + + +async def test_discovery_success( + hass: HomeAssistant, mock_discover, mock_setup_entry +) -> None: + """Verify discovery finds devices and completes config entry creation.""" + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": config_entries.SOURCE_USER} + ) + assert result["type"] == FlowResultType.MENU + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], {"next_step_id": "start_discovery"} + ) + assert result["type"] == FlowResultType.SHOW_PROGRESS + assert result["step_id"] == "start_discovery" + assert result["progress_action"] == "start_discovery" + + await hass.async_block_till_done() + + result = await hass.config_entries.flow.async_configure(result["flow_id"]) + + assert result["type"] == FlowResultType.FORM + assert result["step_id"] == "choose_switch" + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], {CONF_SWITCH_LIST: "192.168.1.100"} + ) + + assert result["type"] == FlowResultType.CREATE_ENTRY + assert result["title"] == f"{DEFAULT_NAME} (192.168.1.100)" + assert result["data"][CONF_HOST] == "192.168.1.100" + assert result["data"][CONF_MAC] == "ac:cf:23:12:34:56" + assert result["result"].unique_id == "ac:cf:23:12:34:56" + + +async def test_discovery_no_devices( + hass: HomeAssistant, mock_discover, mock_s20, mock_setup_entry +) -> None: + """Discovery with no found devices should go to discovery_failed and recover via edit.""" + mock_discover.return_value = {} + + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": config_entries.SOURCE_USER} + ) + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], {"next_step_id": "start_discovery"} + ) + await hass.async_block_till_done() + + result = await hass.config_entries.flow.async_configure(result["flow_id"]) + + assert result["type"] == FlowResultType.MENU + assert result["step_id"] == "discovery_failed" + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], {"next_step_id": "edit"} + ) + + assert result["type"] == FlowResultType.FORM + assert result["step_id"] == "edit" + + mock_s20.return_value._mac = b"\xaa\xbb\xcc\xdd\xee\xff" + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + {CONF_HOST: "192.168.1.10", CONF_MAC: "aa:bb:cc:dd:ee:ff"}, + ) + + assert result["type"] == FlowResultType.CREATE_ENTRY + assert result["title"] == f"{DEFAULT_NAME} (192.168.1.10)" + assert result["data"][CONF_HOST] == "192.168.1.10" + assert result["data"][CONF_MAC] == "aa:bb:cc:dd:ee:ff" + + +@pytest.mark.parametrize( + ("import_data", "expected_mac", "mock_mac_bytes"), + [ + ( + {CONF_HOST: "192.168.1.5", CONF_MAC: "ac:cf:23:12:34:56"}, + "ac:cf:23:12:34:56", + None, + ), + ({CONF_HOST: "192.168.1.5"}, "11:22:33:44:55:66", b"\x11\x22\x33\x44\x55\x66"), + ], +) +async def test_import_flow_success( + hass: HomeAssistant, + mock_discover, + mock_setup_entry, + mock_s20, + import_data: dict[str, Any], + expected_mac: str, + mock_mac_bytes: bytes | None, +) -> None: + """Test importing configuration.yaml entry succeeds with provided or discovered MAC.""" + mock_s20.return_value._mac = mock_mac_bytes + mock_discover.return_value = {"192.168.1.5": {"mac": b"\x11\x22\x33\x44\x55\x66"}} + + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": config_entries.SOURCE_IMPORT}, data=import_data + ) + + assert result["type"] == FlowResultType.CREATE_ENTRY + assert result["title"] == "192.168.1.5" + assert result["data"][CONF_MAC] == expected_mac + + +@pytest.mark.parametrize( + ("import_data", "expected_reason", "mock_exception", "mock_mac_bytes"), + [ + ({CONF_HOST: "192.168.1.5"}, "cannot_discover", None, None), + ( + {CONF_HOST: "192.168.1.5", CONF_MAC: "ac:cf:23:12:34:56"}, + "cannot_connect", + S20Exception("Connection failed"), + b"dummy", + ), + ], +) +async def test_import_flow_errors( + hass: HomeAssistant, + mock_s20, + mock_discover, + import_data: dict[str, Any], + expected_reason: str, + mock_exception: Exception | None, + mock_mac_bytes: bytes | None, +) -> None: + """Test various abort errors in the import flow.""" + mock_discover.return_value = {} + mock_s20.side_effect = mock_exception + mock_s20.return_value._mac = mock_mac_bytes + + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": config_entries.SOURCE_IMPORT}, data=import_data + ) + + assert result["type"] == FlowResultType.ABORT + assert result["reason"] == expected_reason + + +async def test_discover_skips_existing_and_invalid_mac( + hass: HomeAssistant, mock_config_entry: MockConfigEntry, mock_discover +) -> None: + """Test discovery ignores devices already configured and devices without MACs.""" + mock_config_entry.add_to_hass(hass) + + mock_discover.return_value = { + "192.168.1.10": {"mac": b"\xaa\xbb\xcc\xdd\xee\xff"}, + "192.168.1.11": {}, + "192.168.1.12": {"mac": b"\x11\x22\x33\x44\x55\x66"}, + } + + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": config_entries.SOURCE_USER} + ) + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], {"next_step_id": "start_discovery"} + ) + + await hass.async_block_till_done() + + result = await hass.config_entries.flow.async_configure(result["flow_id"]) + + assert result["type"] == FlowResultType.FORM + assert result["step_id"] == "choose_switch" + + schema = result["data_schema"].schema + dropdown_options = schema[vol.Required(CONF_SWITCH_LIST)].container + + assert "192.168.1.12" in dropdown_options + assert "192.168.1.10" not in dropdown_options + assert "192.168.1.11" not in dropdown_options + + +async def test_start_discovery_shows_progress(hass: HomeAssistant) -> None: + """Test polling the flow while discovery is still in progress.""" + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": config_entries.SOURCE_USER} + ) + + async def delayed_executor_job(*args, **kwargs) -> dict[str, Any]: + await asyncio.sleep(0.1) + return {} + + with patch.object(hass, "async_add_executor_job", side_effect=delayed_executor_job): + result = await hass.config_entries.flow.async_configure( + result["flow_id"], {"next_step_id": "start_discovery"} + ) + assert result["type"] == FlowResultType.SHOW_PROGRESS + + result = await hass.config_entries.flow.async_configure(result["flow_id"]) + + assert result["type"] == FlowResultType.SHOW_PROGRESS + assert result["progress_action"] == "start_discovery" + + await hass.async_block_till_done() + + +async def test_discovery_flow_task_exception( + hass: HomeAssistant, mock_discover +) -> None: + """Test the discovery process when the background task raises an error.""" + mock_discover.side_effect = S20Exception("Network timeout") + + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": config_entries.SOURCE_USER} + ) + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], {"next_step_id": "start_discovery"} + ) + + await hass.async_block_till_done() + + result = await hass.config_entries.flow.async_configure(result["flow_id"]) + + assert result["type"] == FlowResultType.MENU + assert result["step_id"] == "discovery_failed" diff --git a/tests/components/schlage/test_lock.py b/tests/components/schlage/test_lock.py index 6a3bb799213d5d..1d801154f0a16f 100644 --- a/tests/components/schlage/test_lock.py +++ b/tests/components/schlage/test_lock.py @@ -4,10 +4,21 @@ from unittest.mock import Mock from freezegun.api import FrozenDateTimeFactory +from pyschlage.code import AccessCode +from pyschlage.exceptions import Error as SchlageError +import pytest +import voluptuous as vol from homeassistant.components.lock import DOMAIN as LOCK_DOMAIN, LockState +from homeassistant.components.schlage.const import ( + DOMAIN, + SERVICE_ADD_CODE, + SERVICE_DELETE_CODE, + SERVICE_GET_CODES, +) from homeassistant.const import ATTR_ENTITY_ID, SERVICE_LOCK, SERVICE_UNLOCK from homeassistant.core import HomeAssistant +from homeassistant.exceptions import HomeAssistantError, ServiceValidationError from . import MockSchlageConfigEntry @@ -84,3 +95,418 @@ async def test_changed_by( lock_device = hass.states.get("lock.vault_door") assert lock_device is not None assert lock_device.attributes.get("changed_by") == "access code - foo" + + +async def test_add_code_service( + hass: HomeAssistant, + mock_lock: Mock, + mock_added_config_entry: MockSchlageConfigEntry, +) -> None: + """Test add_code service.""" + # Mock access_codes as empty initially + mock_lock.access_codes = {} + mock_lock.add_access_code = Mock() + + await hass.services.async_call( + DOMAIN, + SERVICE_ADD_CODE, + service_data={ + "entity_id": "lock.vault_door", + "name": "test_user", + "code": "1234", + }, + blocking=True, + ) + await hass.async_block_till_done() + + # Verify add_access_code was called with correct AccessCode + mock_lock.refresh_access_codes.assert_called_once() + mock_lock.add_access_code.assert_called_once() + call_args = mock_lock.add_access_code.call_args[0][0] + assert isinstance(call_args, AccessCode) + assert call_args.name == "test_user" + assert call_args.code == "1234" + + +@pytest.mark.parametrize( + "code", + [ + "abc", + "123", + "123456789", + "12ab", + ], + ids=["non_digits", "too_short", "too_long", "mixed"], +) +async def test_add_code_service_invalid_code( + hass: HomeAssistant, + mock_lock: Mock, + mock_added_config_entry: MockSchlageConfigEntry, + code: str, +) -> None: + """Test add_code service rejects invalid PIN codes.""" + mock_lock.access_codes = {} + + with pytest.raises(vol.Invalid): + await hass.services.async_call( + DOMAIN, + SERVICE_ADD_CODE, + service_data={ + "entity_id": "lock.vault_door", + "name": "test_user", + "code": code, + }, + blocking=True, + ) + + +async def test_add_code_service_duplicate_name( + hass: HomeAssistant, + mock_lock: Mock, + mock_added_config_entry: MockSchlageConfigEntry, +) -> None: + """Test add_code service with duplicate name.""" + + # Mock existing access code + existing_code = Mock() + existing_code.name = "test_user" + existing_code.code = "5678" + mock_lock.access_codes = {"1": existing_code} + + with pytest.raises( + ServiceValidationError, + match='A PIN code with the name "test_user" already exists on the lock.', + ) as exc_info: + await hass.services.async_call( + DOMAIN, + SERVICE_ADD_CODE, + service_data={ + "entity_id": "lock.vault_door", + "name": "test_user", + "code": "1234", + }, + blocking=True, + ) + assert exc_info.value.translation_key == "schlage_name_exists" + assert exc_info.value.translation_placeholders == {"name": "test_user"} + + +async def test_add_code_service_duplicate_code( + hass: HomeAssistant, + mock_lock: Mock, + mock_added_config_entry: MockSchlageConfigEntry, +) -> None: + """Test add_code service with duplicate code.""" + # Mock existing access code + + existing_code = Mock() + existing_code.name = "existing_user" + existing_code.code = "1234" + mock_lock.access_codes = {"1": existing_code} + + with pytest.raises( + ServiceValidationError, + match="A PIN code with this value already exists on the lock.", + ) as exc_info: + await hass.services.async_call( + DOMAIN, + SERVICE_ADD_CODE, + service_data={ + "entity_id": "lock.vault_door", + "name": "test_user", + "code": "1234", + }, + blocking=True, + ) + assert exc_info.value.translation_key == "schlage_code_exists" + + +async def test_delete_code_service( + hass: HomeAssistant, + mock_lock: Mock, + mock_added_config_entry: MockSchlageConfigEntry, +) -> None: + """Test delete_code service.""" + # Mock existing access code + existing_code = Mock() + existing_code.name = "test_user" + existing_code.delete = Mock() + mock_lock.access_codes = {"1": existing_code} + + await hass.services.async_call( + DOMAIN, + SERVICE_DELETE_CODE, + service_data={ + "entity_id": "lock.vault_door", + "name": "test_user", + }, + blocking=True, + ) + await hass.async_block_till_done() + + existing_code.delete.assert_called_once() + mock_lock.refresh_access_codes.assert_called_once() + + +async def test_delete_code_service_case_insensitive( + hass: HomeAssistant, + mock_lock: Mock, + mock_added_config_entry: MockSchlageConfigEntry, +) -> None: + """Test delete_code service is case insensitive.""" + # Mock existing access code + existing_code = Mock() + existing_code.name = "Test_User" + existing_code.delete = Mock() + mock_lock.access_codes = {"1": existing_code} + + await hass.services.async_call( + DOMAIN, + SERVICE_DELETE_CODE, + service_data={ + "entity_id": "lock.vault_door", + "name": "test_user", + }, + blocking=True, + ) + await hass.async_block_till_done() + + existing_code.delete.assert_called_once() + + +async def test_delete_code_service_nonexistent_code( + hass: HomeAssistant, + mock_lock: Mock, + mock_added_config_entry: MockSchlageConfigEntry, +) -> None: + """Test delete_code service with nonexistent code.""" + mock_lock.access_codes = {} + + # Should not raise an error, just return silently + await hass.services.async_call( + DOMAIN, + SERVICE_DELETE_CODE, + service_data={ + "entity_id": "lock.vault_door", + "name": "nonexistent", + }, + blocking=True, + ) + await hass.async_block_till_done() + + +async def test_delete_code_service_no_access_codes( + hass: HomeAssistant, + mock_lock: Mock, + mock_added_config_entry: MockSchlageConfigEntry, +) -> None: + """Test delete_code service when access_codes is None.""" + mock_lock.access_codes = None + + # Should not raise an error, just return silently + await hass.services.async_call( + DOMAIN, + SERVICE_DELETE_CODE, + service_data={ + "entity_id": "lock.vault_door", + "name": "test_user", + }, + blocking=True, + ) + await hass.async_block_till_done() + + +async def test_get_codes_service( + hass: HomeAssistant, + mock_lock: Mock, + mock_added_config_entry: MockSchlageConfigEntry, +) -> None: + """Test get_codes service.""" + # Mock existing access codes + code1 = Mock() + code1.name = "user1" + code1.code = "1234" + code2 = Mock() + code2.name = "user2" + code2.code = "5678" + mock_lock.access_codes = {"1": code1, "2": code2} + + response = await hass.services.async_call( + DOMAIN, + SERVICE_GET_CODES, + service_data={ + "entity_id": "lock.vault_door", + }, + blocking=True, + return_response=True, + ) + await hass.async_block_till_done() + + assert response == { + "lock.vault_door": { + "1": {"name": "user1", "code": "1234"}, + "2": {"name": "user2", "code": "5678"}, + } + } + + +async def test_get_codes_service_no_codes( + hass: HomeAssistant, + mock_lock: Mock, + mock_added_config_entry: MockSchlageConfigEntry, +) -> None: + """Test get_codes service with no codes.""" + mock_lock.access_codes = None + + response = await hass.services.async_call( + DOMAIN, + SERVICE_GET_CODES, + service_data={ + "entity_id": "lock.vault_door", + }, + blocking=True, + return_response=True, + ) + await hass.async_block_till_done() + + assert response == {"lock.vault_door": {}} + + +async def test_get_codes_service_empty_codes( + hass: HomeAssistant, + mock_lock: Mock, + mock_added_config_entry: MockSchlageConfigEntry, +) -> None: + """Test get_codes service with empty codes dict.""" + mock_lock.access_codes = {} + + response = await hass.services.async_call( + DOMAIN, + SERVICE_GET_CODES, + service_data={ + "entity_id": "lock.vault_door", + }, + blocking=True, + return_response=True, + ) + await hass.async_block_till_done() + + assert response == {"lock.vault_door": {}} + + +async def test_delete_code_service_nonexistent_code_with_existing_codes( + hass: HomeAssistant, + mock_lock: Mock, + mock_added_config_entry: MockSchlageConfigEntry, +) -> None: + """Test delete_code service with nonexistent code when other codes exist.""" + # Mock existing access code with a different name + existing_code = Mock() + existing_code.name = "existing_user" + existing_code.delete = Mock() + mock_lock.access_codes = {"1": existing_code} + + # Try to delete a code that doesn't exist + await hass.services.async_call( + DOMAIN, + SERVICE_DELETE_CODE, + service_data={ + "entity_id": "lock.vault_door", + "name": "nonexistent_user", + }, + blocking=True, + ) + await hass.async_block_till_done() + + # Verify that delete was not called on the existing code + existing_code.delete.assert_not_called() + + +async def test_add_code_service_refresh_error( + hass: HomeAssistant, + mock_lock: Mock, + mock_added_config_entry: MockSchlageConfigEntry, +) -> None: + """Test add_code service raises HomeAssistantError on refresh failure.""" + mock_lock.refresh_access_codes.side_effect = SchlageError("API error") + + with pytest.raises(HomeAssistantError) as exc_info: + await hass.services.async_call( + DOMAIN, + SERVICE_ADD_CODE, + service_data={ + "entity_id": "lock.vault_door", + "name": "test_user", + "code": "1234", + }, + blocking=True, + ) + assert exc_info.value.translation_key == "schlage_refresh_failed" + + +async def test_add_code_service_api_error( + hass: HomeAssistant, + mock_lock: Mock, + mock_added_config_entry: MockSchlageConfigEntry, +) -> None: + """Test add_code service raises HomeAssistantError on add failure.""" + mock_lock.access_codes = {} + mock_lock.add_access_code.side_effect = SchlageError("API error") + + with pytest.raises(HomeAssistantError) as exc_info: + await hass.services.async_call( + DOMAIN, + SERVICE_ADD_CODE, + service_data={ + "entity_id": "lock.vault_door", + "name": "test_user", + "code": "1234", + }, + blocking=True, + ) + assert exc_info.value.translation_key == "schlage_add_code_failed" + + +async def test_delete_code_service_api_error( + hass: HomeAssistant, + mock_lock: Mock, + mock_added_config_entry: MockSchlageConfigEntry, +) -> None: + """Test delete_code service raises HomeAssistantError on delete failure.""" + existing_code = Mock() + existing_code.name = "test_user" + existing_code.delete.side_effect = SchlageError("API error") + mock_lock.access_codes = {"1": existing_code} + + with pytest.raises(HomeAssistantError) as exc_info: + await hass.services.async_call( + DOMAIN, + SERVICE_DELETE_CODE, + service_data={ + "entity_id": "lock.vault_door", + "name": "test_user", + }, + blocking=True, + ) + assert exc_info.value.translation_key == "schlage_delete_code_failed" + + +async def test_get_codes_service_refresh_error( + hass: HomeAssistant, + mock_lock: Mock, + mock_added_config_entry: MockSchlageConfigEntry, +) -> None: + """Test get_codes service raises HomeAssistantError on refresh failure.""" + mock_lock.refresh_access_codes.side_effect = SchlageError("API error") + + with pytest.raises(HomeAssistantError) as exc_info: + await hass.services.async_call( + DOMAIN, + SERVICE_GET_CODES, + service_data={ + "entity_id": "lock.vault_door", + }, + blocking=True, + return_response=True, + ) + assert exc_info.value.translation_key == "schlage_refresh_failed"