From 15b1fee42da42e30f4e7df0aa8581e3e1b14b741 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Wed, 11 Feb 2026 17:24:05 +0100 Subject: [PATCH] Use service helper to extract mastodon config entry (#162798) --- homeassistant/components/mastodon/services.py | 25 ++++--------------- .../components/mastodon/strings.json | 6 ----- tests/components/mastodon/test_services.py | 6 ++--- 3 files changed, 7 insertions(+), 30 deletions(-) diff --git a/homeassistant/components/mastodon/services.py b/homeassistant/components/mastodon/services.py index 2bbf10ce642..2cca69fdfb1 100644 --- a/homeassistant/components/mastodon/services.py +++ b/homeassistant/components/mastodon/services.py @@ -2,16 +2,16 @@ from enum import StrEnum from functools import partial -from typing import Any, cast +from typing import Any from mastodon import Mastodon from mastodon.Mastodon import MastodonAPIError, MediaAttachment import voluptuous as vol -from homeassistant.config_entries import ConfigEntryState from homeassistant.const import ATTR_CONFIG_ENTRY_ID from homeassistant.core import HomeAssistant, ServiceCall, ServiceResponse, callback from homeassistant.exceptions import HomeAssistantError, ServiceValidationError +from homeassistant.helpers import service from .const import ( ATTR_CONTENT_WARNING, @@ -53,30 +53,15 @@ SERVICE_POST_SCHEMA = vol.Schema( ) -def async_get_entry(hass: HomeAssistant, config_entry_id: str) -> MastodonConfigEntry: - """Get the Mastodon config entry.""" - if not (entry := hass.config_entries.async_get_entry(config_entry_id)): - raise ServiceValidationError( - translation_domain=DOMAIN, - translation_key="integration_not_found", - translation_placeholders={"target": DOMAIN}, - ) - if entry.state is not ConfigEntryState.LOADED: - raise ServiceValidationError( - translation_domain=DOMAIN, - translation_key="not_loaded", - translation_placeholders={"target": entry.title}, - ) - return cast(MastodonConfigEntry, entry) - - @callback def async_setup_services(hass: HomeAssistant) -> None: """Set up the services for the Mastodon integration.""" async def async_post(call: ServiceCall) -> ServiceResponse: """Post a status.""" - entry = async_get_entry(hass, call.data[ATTR_CONFIG_ENTRY_ID]) + entry: MastodonConfigEntry = service.async_get_config_entry( + hass, DOMAIN, call.data[ATTR_CONFIG_ENTRY_ID] + ) client = entry.runtime_data.client status: str = call.data[ATTR_STATUS] diff --git a/homeassistant/components/mastodon/strings.json b/homeassistant/components/mastodon/strings.json index 0b149919289..5c50c348b7c 100644 --- a/homeassistant/components/mastodon/strings.json +++ b/homeassistant/components/mastodon/strings.json @@ -72,12 +72,6 @@ "idempotency_key_too_short": { "message": "Idempotency key must be at least 4 characters long." }, - "integration_not_found": { - "message": "Integration \"{target}\" not found in registry." - }, - "not_loaded": { - "message": "{target} is not loaded." - }, "not_whitelisted_directory": { "message": "{media} is not a whitelisted directory." }, diff --git a/tests/components/mastodon/test_services.py b/tests/components/mastodon/test_services.py index 01f3bf8c96e..02cc6fdc5cb 100644 --- a/tests/components/mastodon/test_services.py +++ b/tests/components/mastodon/test_services.py @@ -311,7 +311,7 @@ async def test_service_entry_availability( payload = {"status": "test toot"} - with pytest.raises(ServiceValidationError, match="Mock Title is not loaded"): + with pytest.raises(ServiceValidationError, match="service_config_entry_not_loaded"): await hass.services.async_call( DOMAIN, SERVICE_POST, @@ -320,9 +320,7 @@ async def test_service_entry_availability( return_response=False, ) - with pytest.raises( - ServiceValidationError, match='Integration "mastodon" not found in registry' - ): + with pytest.raises(ServiceValidationError, match="service_config_entry_not_found"): await hass.services.async_call( DOMAIN, SERVICE_POST,