Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Sensor config - monarch money."""
2 
3 from collections.abc import Callable
4 from dataclasses import dataclass
5 from datetime import datetime
6 
7 from typedmonarchmoney.models import MonarchAccount, MonarchCashflowSummary
8 
10  SensorDeviceClass,
11  SensorEntity,
12  SensorEntityDescription,
13  SensorStateClass,
14 )
15 from homeassistant.const import CURRENCY_DOLLAR, PERCENTAGE, EntityCategory
16 from homeassistant.core import HomeAssistant
17 from homeassistant.helpers.entity_platform import AddEntitiesCallback
18 from homeassistant.helpers.typing import StateType
19 
20 from . import MonarchMoneyConfigEntry
21 from .entity import MonarchMoneyAccountEntity, MonarchMoneyCashFlowEntity
22 
23 
24 @dataclass(frozen=True, kw_only=True)
26  """Describe an account sensor entity."""
27 
28  value_fn: Callable[[MonarchAccount], StateType | datetime]
29  picture_fn: Callable[[MonarchAccount], str | None] | None = None
30 
31 
32 @dataclass(frozen=True, kw_only=True)
34  """Describe a cashflow sensor entity."""
35 
36  summary_fn: Callable[[MonarchCashflowSummary], StateType]
37 
38 
39 # These sensors include assets like a boat that might have value
40 MONARCH_MONEY_VALUE_SENSORS: tuple[MonarchMoneyAccountSensorEntityDescription, ...] = (
42  key="value",
43  translation_key="value",
44  state_class=SensorStateClass.TOTAL,
45  device_class=SensorDeviceClass.MONETARY,
46  value_fn=lambda account: account.balance,
47  picture_fn=lambda account: account.logo_url,
48  native_unit_of_measurement=CURRENCY_DOLLAR,
49  ),
50 )
51 
52 # Most accounts are balance sensors
53 MONARCH_MONEY_SENSORS: tuple[MonarchMoneyAccountSensorEntityDescription, ...] = (
55  key="currentBalance",
56  translation_key="balance",
57  state_class=SensorStateClass.TOTAL,
58  device_class=SensorDeviceClass.MONETARY,
59  value_fn=lambda account: account.balance,
60  picture_fn=lambda account: account.logo_url,
61  native_unit_of_measurement=CURRENCY_DOLLAR,
62  ),
63 )
64 
65 MONARCH_MONEY_AGE_SENSORS: tuple[MonarchMoneyAccountSensorEntityDescription, ...] = (
67  key="age",
68  translation_key="age",
69  device_class=SensorDeviceClass.TIMESTAMP,
70  entity_category=EntityCategory.DIAGNOSTIC,
71  value_fn=lambda account: account.last_update,
72  ),
73 )
74 
75 MONARCH_CASHFLOW_SENSORS: tuple[MonarchMoneyCashflowSensorEntityDescription, ...] = (
77  key="sum_income",
78  translation_key="sum_income",
79  summary_fn=lambda summary: summary.income,
80  state_class=SensorStateClass.TOTAL,
81  device_class=SensorDeviceClass.MONETARY,
82  native_unit_of_measurement=CURRENCY_DOLLAR,
83  ),
85  key="sum_expense",
86  translation_key="sum_expense",
87  summary_fn=lambda summary: summary.expenses,
88  state_class=SensorStateClass.TOTAL,
89  device_class=SensorDeviceClass.MONETARY,
90  native_unit_of_measurement=CURRENCY_DOLLAR,
91  ),
93  key="savings",
94  translation_key="savings",
95  summary_fn=lambda summary: summary.savings,
96  state_class=SensorStateClass.TOTAL,
97  device_class=SensorDeviceClass.MONETARY,
98  native_unit_of_measurement=CURRENCY_DOLLAR,
99  ),
101  key="savings_rate",
102  translation_key="savings_rate",
103  summary_fn=lambda summary: summary.savings_rate * 100,
104  suggested_display_precision=1,
105  native_unit_of_measurement=PERCENTAGE,
106  ),
107 )
108 
109 
111  hass: HomeAssistant,
112  config_entry: MonarchMoneyConfigEntry,
113  async_add_entities: AddEntitiesCallback,
114 ) -> None:
115  """Set up Monarch Money sensors for config entries."""
116  mm_coordinator = config_entry.runtime_data
117 
118  entity_list: list[MonarchMoneySensor | MonarchMoneyCashFlowSensor] = [
120  mm_coordinator,
121  sensor_description,
122  )
123  for sensor_description in MONARCH_CASHFLOW_SENSORS
124  ]
125  entity_list.extend(
127  mm_coordinator,
128  sensor_description,
129  account,
130  )
131  for account in mm_coordinator.balance_accounts
132  for sensor_description in MONARCH_MONEY_SENSORS
133  )
134  entity_list.extend(
136  mm_coordinator,
137  sensor_description,
138  account,
139  )
140  for account in mm_coordinator.accounts
141  for sensor_description in MONARCH_MONEY_AGE_SENSORS
142  )
143  entity_list.extend(
145  mm_coordinator,
146  sensor_description,
147  account,
148  )
149  for account in mm_coordinator.value_accounts
150  for sensor_description in MONARCH_MONEY_VALUE_SENSORS
151  )
152 
153  async_add_entities(entity_list)
154 
155 
157  """Cashflow summary sensor."""
158 
159  entity_description: MonarchMoneyCashflowSensorEntityDescription
160 
161  @property
162  def native_value(self) -> StateType:
163  """Return the state."""
164  return self.entity_descriptionentity_description.summary_fn(self.summary_datasummary_data)
165 
166 
168  """Define a monarch money sensor."""
169 
170  entity_description: MonarchMoneyAccountSensorEntityDescription
171 
172  @property
173  def native_value(self) -> StateType | datetime:
174  """Return the state."""
175  return self.entity_descriptionentity_description.value_fn(self.account_dataaccount_data)
176 
177  @property
178  def entity_picture(self) -> str | None:
179  """Return the picture of the account as provided by monarch money if it exists."""
180  if self.entity_descriptionentity_description.picture_fn is not None:
181  return self.entity_descriptionentity_description.picture_fn(self.account_dataaccount_data)
182  return None
None async_setup_entry(HomeAssistant hass, MonarchMoneyConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:114