1
0
mirror of https://github.com/home-assistant/core.git synced 2026-05-08 17:49:37 +01:00

Use runtime_data in Slack (#167864)

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
epenet
2026-04-10 11:50:19 +02:00
committed by GitHub
parent 1597b740da
commit a54ea071f8
3 changed files with 38 additions and 24 deletions
+26 -8
View File
@@ -2,6 +2,7 @@
from __future__ import annotations
from dataclasses import dataclass
import logging
from aiohttp.client_exceptions import ClientError
@@ -30,6 +31,17 @@ PLATFORMS = [Platform.NOTIFY, Platform.SENSOR]
CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN)
type SlackConfigEntry = ConfigEntry[SlackData]
@dataclass
class SlackData:
"""Runtime data for the Slack integration."""
client: AsyncWebClient
url: str
user_id: str
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Set up the Slack component."""
@@ -37,7 +49,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
return True
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def async_setup_entry(hass: HomeAssistant, entry: SlackConfigEntry) -> bool:
"""Set up Slack from a config entry."""
session = aiohttp_client.async_get_clientsession(hass)
slack = AsyncWebClient(
@@ -52,19 +64,25 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
return False
raise ConfigEntryNotReady("Error while setting up integration") from ex
data = {
DATA_CLIENT: slack,
ATTR_URL: res[ATTR_URL],
ATTR_USER_ID: res[ATTR_USER_ID],
}
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = entry.data | {SLACK_DATA: data}
entry.runtime_data = SlackData(
client=slack,
url=res[ATTR_URL],
user_id=res[ATTR_USER_ID],
)
hass.async_create_task(
discovery.async_load_platform(
hass,
Platform.NOTIFY,
DOMAIN,
hass.data[DOMAIN][entry.entry_id],
entry.data
| {
SLACK_DATA: {
DATA_CLIENT: slack,
ATTR_URL: res[ATTR_URL],
ATTR_USER_ID: res[ATTR_USER_ID],
}
},
hass.data[DATA_HASS_CONFIG],
)
)
+7 -11
View File
@@ -1,14 +1,10 @@
"""The slack integration."""
from __future__ import annotations
from slack_sdk.web.async_client import AsyncWebClient
from homeassistant.config_entries import ConfigEntry
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
from homeassistant.helpers.entity import Entity, EntityDescription
from .const import ATTR_URL, ATTR_USER_ID, DATA_CLIENT, DEFAULT_NAME, DOMAIN
from . import SlackConfigEntry, SlackData
from .const import DEFAULT_NAME, DOMAIN
class SlackEntity(Entity):
@@ -16,16 +12,16 @@ class SlackEntity(Entity):
def __init__(
self,
data: dict[str, AsyncWebClient],
data: SlackData,
description: EntityDescription,
entry: ConfigEntry,
entry: SlackConfigEntry,
) -> None:
"""Initialize a Slack entity."""
self._client: AsyncWebClient = data[DATA_CLIENT]
self._client = data.client
self.entity_description = description
self._attr_unique_id = f"{data[ATTR_USER_ID]}_{description.key}"
self._attr_unique_id = f"{data.user_id}_{description.key}"
self._attr_device_info = DeviceInfo(
configuration_url=str(data[ATTR_URL]),
configuration_url=data.url,
entry_type=DeviceEntryType.SERVICE,
identifiers={(DOMAIN, entry.entry_id)},
manufacturer=DEFAULT_NAME,
+5 -5
View File
@@ -9,25 +9,25 @@ from homeassistant.components.sensor import (
SensorEntity,
SensorEntityDescription,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from homeassistant.util import dt as dt_util
from .const import ATTR_SNOOZE, DOMAIN, SLACK_DATA
from . import SlackConfigEntry
from .const import ATTR_SNOOZE
from .entity import SlackEntity
async def async_setup_entry(
hass: HomeAssistant,
entry: ConfigEntry,
entry: SlackConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
"""Set up the Slack select."""
"""Set up the Slack sensor."""
async_add_entities(
[
SlackSensorEntity(
hass.data[DOMAIN][entry.entry_id][SLACK_DATA],
entry.runtime_data,
SensorEntityDescription(
key="do_not_disturb_until",
translation_key="do_not_disturb_until",