Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Sensor platform for Kaleidescape integration."""
2 
3 from __future__ import annotations
4 
5 from dataclasses import dataclass
6 from typing import TYPE_CHECKING
7 
8 from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
9 from homeassistant.const import PERCENTAGE, EntityCategory
10 
11 from .const import DOMAIN as KALEIDESCAPE_DOMAIN
12 from .entity import KaleidescapeEntity
13 
14 if TYPE_CHECKING:
15  from collections.abc import Callable
16 
17  from kaleidescape import Device as KaleidescapeDevice
18 
19  from homeassistant.config_entries import ConfigEntry
20  from homeassistant.core import HomeAssistant
21  from homeassistant.helpers.entity_platform import AddEntitiesCallback
22  from homeassistant.helpers.typing import StateType
23 
24 
25 @dataclass(frozen=True, kw_only=True)
27  """Describes Kaleidescape sensor entity."""
28 
29  value_fn: Callable[[KaleidescapeDevice], StateType]
30 
31 
32 SENSOR_TYPES: tuple[KaleidescapeSensorEntityDescription, ...] = (
34  key="media_location",
35  translation_key="media_location",
36  value_fn=lambda device: device.automation.movie_location,
37  ),
39  key="play_status",
40  translation_key="play_status",
41  value_fn=lambda device: device.movie.play_status,
42  ),
44  key="play_speed",
45  translation_key="play_speed",
46  value_fn=lambda device: device.movie.play_speed,
47  ),
49  key="video_mode",
50  translation_key="video_mode",
51  entity_category=EntityCategory.DIAGNOSTIC,
52  value_fn=lambda device: device.automation.video_mode,
53  ),
55  key="video_color_eotf",
56  translation_key="video_color_eotf",
57  entity_category=EntityCategory.DIAGNOSTIC,
58  value_fn=lambda device: device.automation.video_color_eotf,
59  ),
61  key="video_color_space",
62  translation_key="video_color_space",
63  entity_category=EntityCategory.DIAGNOSTIC,
64  value_fn=lambda device: device.automation.video_color_space,
65  ),
67  key="video_color_depth",
68  translation_key="video_color_depth",
69  entity_category=EntityCategory.DIAGNOSTIC,
70  value_fn=lambda device: device.automation.video_color_depth,
71  ),
73  key="video_color_sampling",
74  translation_key="video_color_sampling",
75  entity_category=EntityCategory.DIAGNOSTIC,
76  value_fn=lambda device: device.automation.video_color_sampling,
77  ),
79  key="screen_mask_ratio",
80  translation_key="screen_mask_ratio",
81  entity_category=EntityCategory.DIAGNOSTIC,
82  value_fn=lambda device: device.automation.screen_mask_ratio,
83  ),
85  key="screen_mask_top_trim_rel",
86  translation_key="screen_mask_top_trim_rel",
87  entity_category=EntityCategory.DIAGNOSTIC,
88  native_unit_of_measurement=PERCENTAGE,
89  value_fn=lambda device: device.automation.screen_mask_top_trim_rel / 10.0,
90  ),
92  key="screen_mask_bottom_trim_rel",
93  translation_key="screen_mask_bottom_trim_rel",
94  entity_category=EntityCategory.DIAGNOSTIC,
95  native_unit_of_measurement=PERCENTAGE,
96  value_fn=lambda device: device.automation.screen_mask_bottom_trim_rel / 10.0,
97  ),
99  key="screen_mask_conservative_ratio",
100  translation_key="screen_mask_conservative_ratio",
101  entity_category=EntityCategory.DIAGNOSTIC,
102  value_fn=lambda device: device.automation.screen_mask_conservative_ratio,
103  ),
105  key="screen_mask_top_mask_abs",
106  translation_key="screen_mask_top_mask_abs",
107  entity_category=EntityCategory.DIAGNOSTIC,
108  native_unit_of_measurement=PERCENTAGE,
109  value_fn=lambda device: device.automation.screen_mask_top_mask_abs / 10.0,
110  ),
112  key="screen_mask_bottom_mask_abs",
113  translation_key="screen_mask_bottom_mask_abs",
114  entity_category=EntityCategory.DIAGNOSTIC,
115  native_unit_of_measurement=PERCENTAGE,
116  value_fn=lambda device: device.automation.screen_mask_bottom_mask_abs / 10.0,
117  ),
119  key="cinemascape_mask",
120  translation_key="cinemascape_mask",
121  entity_category=EntityCategory.DIAGNOSTIC,
122  value_fn=lambda device: device.automation.cinemascape_mask,
123  ),
125  key="cinemascape_mode",
126  translation_key="cinemascape_mode",
127  entity_category=EntityCategory.DIAGNOSTIC,
128  value_fn=lambda device: device.automation.cinemascape_mode,
129  ),
130 )
131 
132 
134  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
135 ) -> None:
136  """Set up the platform from a config entry."""
137  device: KaleidescapeDevice = hass.data[KALEIDESCAPE_DOMAIN][entry.entry_id]
139  KaleidescapeSensor(device, description) for description in SENSOR_TYPES
140  )
141 
142 
144  """Representation of a Kaleidescape sensor."""
145 
146  entity_description: KaleidescapeSensorEntityDescription
147 
148  def __init__(
149  self,
150  device: KaleidescapeDevice,
151  entity_description: KaleidescapeSensorEntityDescription,
152  ) -> None:
153  """Initialize sensor."""
154  super().__init__(device)
155  self.entity_descriptionentity_description = entity_description
156  self._attr_unique_id_attr_unique_id_attr_unique_id = f"{self._attr_unique_id}-{entity_description.key}"
157 
158  @property
159  def native_value(self) -> StateType:
160  """Return value of sensor."""
161  return self.entity_descriptionentity_description.value_fn(self._device_device)
None __init__(self, KaleidescapeDevice device, KaleidescapeSensorEntityDescription entity_description)
Definition: sensor.py:152
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:135