mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +00:00
Use HassKey in image (#126322)
This commit is contained in:
parent
f7004188d2
commit
32f02aa3c6
@ -30,7 +30,7 @@ from homeassistant.helpers.event import (
|
|||||||
from homeassistant.helpers.httpx_client import get_async_client
|
from homeassistant.helpers.httpx_client import get_async_client
|
||||||
from homeassistant.helpers.typing import UNDEFINED, ConfigType, UndefinedType
|
from homeassistant.helpers.typing import UNDEFINED, ConfigType, UndefinedType
|
||||||
|
|
||||||
from .const import DOMAIN, IMAGE_TIMEOUT
|
from .const import DOMAIN, DOMAIN_DATA, IMAGE_TIMEOUT
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -88,7 +88,7 @@ async def _async_get_image(image_entity: ImageEntity, timeout: int) -> Image:
|
|||||||
|
|
||||||
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
||||||
"""Set up the image component."""
|
"""Set up the image component."""
|
||||||
component = hass.data[DOMAIN] = EntityComponent[ImageEntity](
|
component = hass.data[DOMAIN_DATA] = EntityComponent[ImageEntity](
|
||||||
_LOGGER, DOMAIN, hass, SCAN_INTERVAL
|
_LOGGER, DOMAIN, hass, SCAN_INTERVAL
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -120,14 +120,12 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
|||||||
|
|
||||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
"""Set up a config entry."""
|
"""Set up a config entry."""
|
||||||
component: EntityComponent[ImageEntity] = hass.data[DOMAIN]
|
return await hass.data[DOMAIN_DATA].async_setup_entry(entry)
|
||||||
return await component.async_setup_entry(entry)
|
|
||||||
|
|
||||||
|
|
||||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
"""Unload a config entry."""
|
"""Unload a config entry."""
|
||||||
component: EntityComponent[ImageEntity] = hass.data[DOMAIN]
|
return await hass.data[DOMAIN_DATA].async_unload_entry(entry)
|
||||||
return await component.async_unload_entry(entry)
|
|
||||||
|
|
||||||
|
|
||||||
CACHED_PROPERTIES_WITH_ATTR_ = {
|
CACHED_PROPERTIES_WITH_ATTR_ = {
|
||||||
|
@ -1,7 +1,18 @@
|
|||||||
"""Constants for the image integration."""
|
"""Constants for the image integration."""
|
||||||
|
|
||||||
from typing import Final
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from typing import TYPE_CHECKING, Final
|
||||||
|
|
||||||
|
from homeassistant.util.hass_dict import HassKey
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from homeassistant.helpers.entity_component import EntityComponent
|
||||||
|
|
||||||
|
from . import ImageEntity
|
||||||
|
|
||||||
|
|
||||||
DOMAIN: Final = "image"
|
DOMAIN: Final = "image"
|
||||||
|
DOMAIN_DATA: HassKey[EntityComponent[ImageEntity]] = HassKey(DOMAIN)
|
||||||
|
|
||||||
IMAGE_TIMEOUT: Final = 10
|
IMAGE_TIMEOUT: Final = 10
|
||||||
|
@ -14,10 +14,8 @@ from homeassistant.components.media_source import (
|
|||||||
)
|
)
|
||||||
from homeassistant.const import ATTR_FRIENDLY_NAME
|
from homeassistant.const import ATTR_FRIENDLY_NAME
|
||||||
from homeassistant.core import HomeAssistant, State
|
from homeassistant.core import HomeAssistant, State
|
||||||
from homeassistant.helpers.entity_component import EntityComponent
|
|
||||||
|
|
||||||
from . import ImageEntity
|
from .const import DOMAIN, DOMAIN_DATA
|
||||||
from .const import DOMAIN
|
|
||||||
|
|
||||||
|
|
||||||
async def async_get_media_source(hass: HomeAssistant) -> ImageMediaSource:
|
async def async_get_media_source(hass: HomeAssistant) -> ImageMediaSource:
|
||||||
@ -37,8 +35,7 @@ class ImageMediaSource(MediaSource):
|
|||||||
|
|
||||||
async def async_resolve_media(self, item: MediaSourceItem) -> PlayMedia:
|
async def async_resolve_media(self, item: MediaSourceItem) -> PlayMedia:
|
||||||
"""Resolve media to a url."""
|
"""Resolve media to a url."""
|
||||||
component: EntityComponent[ImageEntity] = self.hass.data[DOMAIN]
|
image = self.hass.data[DOMAIN_DATA].get_entity(item.identifier)
|
||||||
image = component.get_entity(item.identifier)
|
|
||||||
|
|
||||||
if not image:
|
if not image:
|
||||||
raise Unresolvable(f"Could not resolve media item: {item.identifier}")
|
raise Unresolvable(f"Could not resolve media item: {item.identifier}")
|
||||||
@ -55,7 +52,6 @@ class ImageMediaSource(MediaSource):
|
|||||||
if item.identifier:
|
if item.identifier:
|
||||||
raise BrowseError("Unknown item")
|
raise BrowseError("Unknown item")
|
||||||
|
|
||||||
component: EntityComponent[ImageEntity] = self.hass.data[DOMAIN]
|
|
||||||
children = [
|
children = [
|
||||||
BrowseMediaSource(
|
BrowseMediaSource(
|
||||||
domain=DOMAIN,
|
domain=DOMAIN,
|
||||||
@ -69,7 +65,7 @@ class ImageMediaSource(MediaSource):
|
|||||||
can_play=True,
|
can_play=True,
|
||||||
can_expand=False,
|
can_expand=False,
|
||||||
)
|
)
|
||||||
for image in component.entities
|
for image in self.hass.data[DOMAIN_DATA].entities
|
||||||
]
|
]
|
||||||
|
|
||||||
return BrowseMediaSource(
|
return BrowseMediaSource(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user