Simplify mastodon service actions

This commit is contained in:
epenet
2025-06-12 06:50:39 +00:00
parent 8bf562b7b6
commit a67fe0e115
2 changed files with 74 additions and 70 deletions

View File

@@ -19,7 +19,7 @@ from homeassistant.util import slugify
from .const import CONF_BASE_URL, DOMAIN, LOGGER from .const import CONF_BASE_URL, DOMAIN, LOGGER
from .coordinator import MastodonConfigEntry, MastodonCoordinator, MastodonData from .coordinator import MastodonConfigEntry, MastodonCoordinator, MastodonData
from .services import setup_services from .services import async_setup_services
from .utils import construct_mastodon_username, create_mastodon_client from .utils import construct_mastodon_username, create_mastodon_client
PLATFORMS: list[Platform] = [Platform.NOTIFY, Platform.SENSOR] PLATFORMS: list[Platform] = [Platform.NOTIFY, Platform.SENSOR]
@@ -29,7 +29,7 @@ CONFIG_SCHEMA = cv.empty_config_schema(DOMAIN)
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Set up the Mastodon component.""" """Set up the Mastodon component."""
setup_services(hass) async_setup_services(hass)
return True return True

View File

@@ -9,7 +9,7 @@ from mastodon.Mastodon import MastodonAPIError, MediaAttachment
import voluptuous as vol import voluptuous as vol
from homeassistant.config_entries import ConfigEntryState from homeassistant.config_entries import ConfigEntryState
from homeassistant.core import HomeAssistant, ServiceCall, ServiceResponse from homeassistant.core import HomeAssistant, ServiceCall, ServiceResponse, callback
from homeassistant.exceptions import HomeAssistantError, ServiceValidationError from homeassistant.exceptions import HomeAssistantError, ServiceValidationError
from .const import ( from .const import (
@@ -66,12 +66,9 @@ def async_get_entry(hass: HomeAssistant, config_entry_id: str) -> MastodonConfig
return cast(MastodonConfigEntry, entry) return cast(MastodonConfigEntry, entry)
def setup_services(hass: HomeAssistant) -> None: async def _async_post(call: ServiceCall) -> ServiceResponse:
"""Set up the services for the Mastodon integration."""
async def async_post(call: ServiceCall) -> ServiceResponse:
"""Post a status.""" """Post a status."""
entry = async_get_entry(hass, call.data[ATTR_CONFIG_ENTRY_ID]) entry = async_get_entry(call.hass, call.data[ATTR_CONFIG_ENTRY_ID])
client = entry.runtime_data.client client = entry.runtime_data.client
status = call.data[ATTR_STATUS] status = call.data[ATTR_STATUS]
@@ -86,9 +83,10 @@ def setup_services(hass: HomeAssistant) -> None:
media_description: str | None = call.data.get(ATTR_MEDIA_DESCRIPTION) media_description: str | None = call.data.get(ATTR_MEDIA_DESCRIPTION)
media_warning: str | None = call.data.get(ATTR_MEDIA_WARNING) media_warning: str | None = call.data.get(ATTR_MEDIA_WARNING)
await hass.async_add_executor_job( await call.hass.async_add_executor_job(
partial( partial(
_post, _post,
hass=call.hass,
client=client, client=client,
status=status, status=status,
visibility=visibility, visibility=visibility,
@@ -101,7 +99,8 @@ def setup_services(hass: HomeAssistant) -> None:
return None return None
def _post(client: Mastodon, **kwargs: Any) -> None:
def _post(hass: HomeAssistant, client: Mastodon, **kwargs: Any) -> None:
"""Post to Mastodon.""" """Post to Mastodon."""
media_data: MediaAttachment | None = None media_data: MediaAttachment | None = None
@@ -145,6 +144,11 @@ def setup_services(hass: HomeAssistant) -> None:
translation_key="unable_to_send_message", translation_key="unable_to_send_message",
) from err ) from err
@callback
def async_setup_services(hass: HomeAssistant) -> None:
"""Set up the services for the Mastodon integration."""
hass.services.async_register( hass.services.async_register(
DOMAIN, SERVICE_POST, async_post, schema=SERVICE_POST_SCHEMA DOMAIN, SERVICE_POST, _async_post, schema=SERVICE_POST_SCHEMA
) )