Home Assistant Unofficial Reference 2024.12.1
helper.py
Go to the documentation of this file.
1 """Camera helper functions."""
2 
3 from __future__ import annotations
4 
5 from typing import TYPE_CHECKING
6 
7 from homeassistant.core import HomeAssistant
8 from homeassistant.exceptions import HomeAssistantError
9 
10 from .const import DATA_COMPONENT
11 
12 if TYPE_CHECKING:
13  from . import Camera
14 
15 
16 def get_camera_from_entity_id(hass: HomeAssistant, entity_id: str) -> Camera:
17  """Get camera component from entity_id."""
18  component = hass.data.get(DATA_COMPONENT)
19  if component is None:
20  raise HomeAssistantError("Camera integration not set up")
21 
22  if (camera := component.get_entity(entity_id)) is None:
23  raise HomeAssistantError("Camera not found")
24 
25  if not camera.is_on:
26  raise HomeAssistantError("Camera is off")
27 
28  return camera
Camera get_camera_from_entity_id(HomeAssistant hass, str entity_id)
Definition: helper.py:16