Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Provides a binary sensor which is a collection of ffmpeg tools."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from haffmpeg.core import HAFFmpeg
8 import haffmpeg.sensor as ffmpeg_sensor
9 import voluptuous as vol
10 
12  PLATFORM_SCHEMA as BINARY_SENSOR_PLATFORM_SCHEMA,
13  BinarySensorDeviceClass,
14  BinarySensorEntity,
15 )
17  CONF_EXTRA_ARGUMENTS,
18  CONF_INITIAL_STATE,
19  CONF_INPUT,
20  FFmpegBase,
21  FFmpegManager,
22  get_ffmpeg_manager,
23 )
24 from homeassistant.const import CONF_NAME, CONF_REPEAT
25 from homeassistant.core import HomeAssistant, callback
27 from homeassistant.helpers.entity_platform import AddEntitiesCallback
28 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
29 
30 CONF_RESET = "reset"
31 CONF_CHANGES = "changes"
32 CONF_REPEAT_TIME = "repeat_time"
33 
34 DEFAULT_NAME = "FFmpeg Motion"
35 DEFAULT_INIT_STATE = True
36 
37 PLATFORM_SCHEMA = BINARY_SENSOR_PLATFORM_SCHEMA.extend(
38  {
39  vol.Required(CONF_INPUT): cv.string,
40  vol.Optional(CONF_INITIAL_STATE, default=DEFAULT_INIT_STATE): cv.boolean,
41  vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
42  vol.Optional(CONF_EXTRA_ARGUMENTS): cv.string,
43  vol.Optional(CONF_RESET, default=10): vol.All(
44  vol.Coerce(int), vol.Range(min=1)
45  ),
46  vol.Optional(CONF_CHANGES, default=10): vol.All(
47  vol.Coerce(float), vol.Range(min=0, max=99)
48  ),
49  vol.Inclusive(CONF_REPEAT, "repeat"): vol.All(
50  vol.Coerce(int), vol.Range(min=1)
51  ),
52  vol.Inclusive(CONF_REPEAT_TIME, "repeat"): vol.All(
53  vol.Coerce(int), vol.Range(min=1)
54  ),
55  }
56 )
57 
58 
60  hass: HomeAssistant,
61  config: ConfigType,
62  async_add_entities: AddEntitiesCallback,
63  discovery_info: DiscoveryInfoType | None = None,
64 ) -> None:
65  """Set up the FFmpeg binary motion sensor."""
66  manager = get_ffmpeg_manager(hass)
67  entity = FFmpegMotion(hass, manager, config)
68  async_add_entities([entity])
69 
70 
71 class FFmpegBinarySensor[_HAFFmpegT: HAFFmpeg](
72  FFmpegBase[_HAFFmpegT], BinarySensorEntity
73 ):
74  """A binary sensor which use FFmpeg for noise detection."""
75 
76  def __init__(self, ffmpeg: _HAFFmpegT, config: dict[str, Any]) -> None:
77  """Init for the binary sensor noise detection."""
78  super().__init__(ffmpeg, config[CONF_INITIAL_STATE])
79 
80  self._state: bool | None = False
81  self._config = config
82  self._name: str = config[CONF_NAME]
83 
84  @callback
85  def _async_callback(self, state: bool | None) -> None:
86  """HA-FFmpeg callback for noise detection."""
87  self._state = state
88  self.async_write_ha_state()
89 
90  @property
91  def is_on(self) -> bool | None:
92  """Return true if the binary sensor is on."""
93  return self._state
94 
95  @property
96  def name(self) -> str:
97  """Return the name of the entity."""
98  return self._name
99 
100 
101 class FFmpegMotion(FFmpegBinarySensor[ffmpeg_sensor.SensorMotion]):
102  """A binary sensor which use FFmpeg for noise detection."""
103 
104  def __init__(
105  self, hass: HomeAssistant, manager: FFmpegManager, config: dict[str, Any]
106  ) -> None:
107  """Initialize FFmpeg motion binary sensor."""
108  ffmpeg = ffmpeg_sensor.SensorMotion(manager.binary, self._async_callback)
109  super().__init__(ffmpeg, config)
110 
111  async def _async_start_ffmpeg(self, entity_ids: list[str] | None) -> None:
112  """Start a FFmpeg instance.
113 
114  This method is a coroutine.
115  """
116  if entity_ids is not None and self.entity_id not in entity_ids:
117  return
118 
119  # init config
120  self.ffmpeg.set_options(
121  time_reset=self._config[CONF_RESET],
122  time_repeat=self._config.get(CONF_REPEAT_TIME, 0),
123  repeat=self._config.get(CONF_REPEAT, 0),
124  changes=self._config[CONF_CHANGES],
125  )
126 
127  # run
128  await self.ffmpeg.open_sensor(
129  input_source=self._config[CONF_INPUT],
130  extra_cmd=self._config.get(CONF_EXTRA_ARGUMENTS),
131  )
132 
133  @property
134  def device_class(self) -> BinarySensorDeviceClass:
135  """Return the class of this sensor, from DEVICE_CLASSES."""
136  return BinarySensorDeviceClass.MOTION
None __init__(self, HomeAssistant hass, FFmpegManager manager, dict[str, Any] config)
web.Response get(self, web.Request request, str config_key)
Definition: view.py:88
None async_setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback async_add_entities, DiscoveryInfoType|None discovery_info=None)
None __init__(self, _HAFFmpegT ffmpeg, dict[str, Any] config)
FFmpegManager get_ffmpeg_manager(HomeAssistant hass)
Definition: __init__.py:106