2727
2828from .const import (
2929 ATTR_CHARGE_POINTS ,
30+ ATTR_TRANSACTIONS ,
3031 ATTR_WALLET ,
3132 ATTRIBUTION ,
3233 DOMAIN ,
@@ -178,6 +179,16 @@ def _parse_date(chargedate: str):
178179 ),
179180)
180181
182+ TRANSACTION_ENTITY_DESCRIPTIONS : tuple [MontaSensorEntityDescription , ...] = (
183+ MontaSensorEntityDescription ( # pylint: disable=unexpected-keyword-arg
184+ key = "monta-latest-wallet-transactions" ,
185+ name = "Monta - Latest Wallet Transactions" ,
186+ icon = "mdi:wallet-outline" ,
187+ value_fn = lambda data : data ,
188+ extra_state_attributes_fn = None ,
189+ ),
190+ )
191+
181192
182193async def async_setup_entry (
183194 hass : HomeAssistant , entry : ConfigEntry , async_add_entities : AddEntitiesCallback
@@ -197,16 +208,19 @@ async def async_setup_entry(
197208 for description in CHARGE_POINT_ENTITY_DESCRIPTIONS
198209 ]
199210 )
211+
200212 async_add_entities (
201213 [
202- MontaWalletSensor (
203- coordinator ,
204- entry ,
205- description ,
206- )
214+ MontaWalletSensor (coordinator , entry , description )
207215 for description in WALLET_ENTITY_DESCRIPTIONS
208216 ]
209217 )
218+ async_add_entities (
219+ [
220+ MontaTransactionsSensor (coordinator , entry , description )
221+ for description in TRANSACTION_ENTITY_DESCRIPTIONS
222+ ]
223+ )
210224
211225
212226class MontaChargePointSensor (MontaEntity , SensorEntity ):
@@ -276,10 +290,7 @@ def __init__(
276290 @property
277291 def native_unit_of_measurement (self ) -> str | None :
278292 """Return the unit of measurement of the sensor."""
279- if (
280- self .entity_description .key == "monta-wallet-amount"
281- and self .coordinator .data
282- ):
293+ if self .coordinator .data :
283294 wallet_data = self .coordinator .data .get (ATTR_WALLET , {})
284295 if wallet_currency := wallet_data .get ("currency" ):
285296 return wallet_currency .get ("identifier" , "" ).upper ()
@@ -302,3 +313,54 @@ def extra_state_attributes(self) -> dict[str, str] | None:
302313 self .coordinator .data [ATTR_WALLET ]
303314 )
304315 return None
316+
317+
318+ class MontaTransactionsSensor (
319+ CoordinatorEntity [MontaDataUpdateCoordinator ], SensorEntity
320+ ):
321+ """monta Sensor class."""
322+
323+ _attr_attribution = ATTRIBUTION
324+ _attr_has_entity_name = True
325+
326+ def __init__ (
327+ self ,
328+ coordinator : MontaDataUpdateCoordinator ,
329+ _ : ConfigEntry ,
330+ entity_description : SensorEntityDescription ,
331+ ) -> None :
332+ """Initialize the sensor class."""
333+ super ().__init__ (coordinator )
334+
335+ self .entity_description = entity_description
336+ self ._attr_unique_id = generate_entity_id (
337+ ENTITY_ID_FORMAT ,
338+ f"monta_{ snake_case (entity_description .key )} " ,
339+ "monta_latest_transactions" ,
340+ )
341+
342+ @property
343+ def native_value (self ) -> StateType :
344+ """Return the state."""
345+ return self .entity_description .value_fn (
346+ self .coordinator .data [ATTR_TRANSACTIONS ][0 ]["state" ]
347+ )
348+
349+ @property
350+ def extra_attributes (self ) -> str :
351+ """Return extra attributes for the sensor."""
352+ return None
353+
354+ @property
355+ def extra_state_attributes (self ) -> dict [str , str ] | None :
356+ """Converts the dates to correct home assitant format."""
357+ attributes = {}
358+
359+ if data := self .coordinator .data .get (ATTR_TRANSACTIONS , []):
360+ for transaction in data :
361+ for key in WALLET_DATE_KEYS :
362+ if key in transaction :
363+ transaction [key ] = _parse_date (transaction [key ])
364+ attributes ["transactions" ] = data
365+
366+ return attributes
0 commit comments