Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Binary Sensor for SimpleFin."""
2 
3 from collections.abc import Callable
4 from dataclasses import dataclass
5 
6 from simplefin4py import Account
7 
9  BinarySensorDeviceClass,
10  BinarySensorEntity,
11  BinarySensorEntityDescription,
12 )
13 from homeassistant.const import EntityCategory
14 from homeassistant.core import HomeAssistant
15 from homeassistant.helpers.entity_platform import AddEntitiesCallback
16 
17 from . import SimpleFinConfigEntry
18 from .entity import SimpleFinEntity
19 
20 
21 @dataclass(frozen=True, kw_only=True)
23  """Describes a sensor entity."""
24 
25  value_fn: Callable[[Account], bool]
26 
27 
28 SIMPLEFIN_BINARY_SENSORS: tuple[SimpleFinBinarySensorEntityDescription, ...] = (
30  key="possible_error",
31  translation_key="possible_error",
32  device_class=BinarySensorDeviceClass.PROBLEM,
33  entity_category=EntityCategory.DIAGNOSTIC,
34  value_fn=lambda account: account.possible_error,
35  ),
36 )
37 
38 
40  hass: HomeAssistant,
41  config_entry: SimpleFinConfigEntry,
42  async_add_entities: AddEntitiesCallback,
43 ) -> None:
44  """Set up SimpleFIN sensors for config entries."""
45 
46  sf_coordinator = config_entry.runtime_data
47  accounts = sf_coordinator.data.accounts
48 
51  sf_coordinator,
52  sensor_description,
53  account,
54  )
55  for account in accounts
56  for sensor_description in SIMPLEFIN_BINARY_SENSORS
57  )
58 
59 
61  """Extends IntellifireEntity with Binary Sensor specific logic."""
62 
63  entity_description: SimpleFinBinarySensorEntityDescription
64 
65  @property
66  def is_on(self) -> bool:
67  """Use this to get the correct value."""
68  return self.entity_descriptionentity_description.value_fn(self.account_dataaccount_data)
None async_setup_entry(HomeAssistant hass, SimpleFinConfigEntry config_entry, AddEntitiesCallback async_add_entities)