Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Support for Roku binary sensors."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from dataclasses import dataclass
7 
8 from rokuecp.models import Device as RokuDevice
9 
11  BinarySensorEntity,
12  BinarySensorEntityDescription,
13 )
14 from homeassistant.config_entries import ConfigEntry
15 from homeassistant.const import EntityCategory
16 from homeassistant.core import HomeAssistant
17 from homeassistant.helpers.entity_platform import AddEntitiesCallback
18 
19 from .const import DOMAIN
20 from .entity import RokuEntity
21 
22 
23 @dataclass(frozen=True, kw_only=True)
25  """Describes a Roku binary sensor entity."""
26 
27  value_fn: Callable[[RokuDevice], bool | None]
28 
29 
30 BINARY_SENSORS: tuple[RokuBinarySensorEntityDescription, ...] = (
32  key="headphones_connected",
33  translation_key="headphones_connected",
34  value_fn=lambda device: device.info.headphones_connected,
35  ),
37  key="supports_airplay",
38  translation_key="supports_airplay",
39  entity_category=EntityCategory.DIAGNOSTIC,
40  value_fn=lambda device: device.info.supports_airplay,
41  ),
43  key="supports_ethernet",
44  translation_key="supports_ethernet",
45  entity_category=EntityCategory.DIAGNOSTIC,
46  value_fn=lambda device: device.info.ethernet_support,
47  ),
49  key="supports_find_remote",
50  translation_key="supports_find_remote",
51  entity_category=EntityCategory.DIAGNOSTIC,
52  value_fn=lambda device: device.info.supports_find_remote,
53  ),
54 )
55 
56 
58  hass: HomeAssistant,
59  entry: ConfigEntry,
60  async_add_entities: AddEntitiesCallback,
61 ) -> None:
62  """Set up a Roku binary sensors based on a config entry."""
63  coordinator = hass.data[DOMAIN][entry.entry_id]
64 
67  coordinator=coordinator,
68  description=description,
69  )
70  for description in BINARY_SENSORS
71  )
72 
73 
75  """Defines a Roku binary sensor."""
76 
77  entity_description: RokuBinarySensorEntityDescription
78 
79  @property
80  def is_on(self) -> bool | None:
81  """Return the state of the sensor."""
82  return self.entity_descriptionentity_description.value_fn(self.coordinator.data)
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)