hass-core/homeassistant/components/simplefin/sensor.py
Jeef 0213f1c5c0
Add SimpleFIN integration (#108336)
* reset to latest dev branch

* Update homeassistant/components/simplefin/sensor.py

Co-authored-by: Erik Montnemery <erik@montnemery.com>

* formatting tweak

* Removed info errors

* fix bad billing error message

* addressing PR

* addressing PR

* reauth abort and already_confiugred added to strings.json

* adding the reauth message

* ruff

* update reqs

* reset to latest dev branch

* Update homeassistant/components/simplefin/sensor.py

Co-authored-by: Erik Montnemery <erik@montnemery.com>

* formatting tweak

* Removed info errors

* fix bad billing error message

* addressing PR

* addressing PR

* reauth abort and already_confiugred added to strings.json

* adding the reauth message

* ruff

* update reqs

* Update homeassistant/components/simplefin/__init__.py

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>

* Addressing a few PR comments - removing nix - and adding runtime data

* updated comments

* rename config flow

* pulling reauth :( - inline stuff

* inline more

* fixed a tab issue

* reverting changes

* various PR updates and code removal

* generator async add

* Update homeassistant/components/simplefin/__init__.py

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>

* Update homeassistant/components/simplefin/config_flow.py

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>

* Update homeassistant/components/simplefin/config_flow.py

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>

* Update homeassistant/components/simplefin/sensor.py

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>

* Update homeassistant/components/simplefin/sensor.py

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>

* always callable

* Update homeassistant/components/simplefin/coordinator.py

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>

* no-verify

* type

* fixing missing domain

* it looks like this file is gone now

* typing

* sorta pass

* fix license

* Update homeassistant/components/simplefin/config_flow.py

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>

* Update homeassistant/components/simplefin/entity.py

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>

* addressing PR

* Update homeassistant/components/simplefin/strings.json

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>

* move property to entity.py

* moved stuff out to else block

* Initial Snappshot Testing ... still have unadressed changes to make

* Addressing PR Comments

* pushing back to joost

* removing non-needed file

* added more asserts

* reducing mocks - need to fix patch paths still

* Changed patch to be more localized to config_flow

* Removed unneeded fixture

* Moved coordinator around

* Cleaning up the code

* Removed a NOQA"

* Upping the number of asserts

* cleanup

* fixed abort call

* incremental update - for Josot... changed a function signature and removed an annotatoin

* no-verify

* Added an abort test

* ruff

* increased coverage but it might not pass muster for JOOST

* increased coverage but it might not pass muster for JOOST

* Much nicer test now

* tried to simplify

* Fix nits

---------

Co-authored-by: Erik Montnemery <erik@montnemery.com>
Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
2024-07-10 12:44:04 +02:00

91 lines
2.6 KiB
Python

"""Platform for sensor integration."""
from __future__ import annotations
from collections.abc import Callable
from dataclasses import dataclass
from simplefin4py import Account
from homeassistant.components.sensor import (
SensorDeviceClass,
SensorEntity,
SensorEntityDescription,
SensorStateClass,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import StateType
from . import SimpleFinConfigEntry
from .entity import SimpleFinEntity
@dataclass(frozen=True, kw_only=True)
class SimpleFinSensorEntityDescription(SensorEntityDescription):
"""Describes a sensor entity."""
value_fn: Callable[[Account], StateType]
icon_fn: Callable[[Account], str] | None = None
unit_fn: Callable[[Account], str] | None = None
SIMPLEFIN_SENSORS: tuple[SimpleFinSensorEntityDescription, ...] = (
SimpleFinSensorEntityDescription(
key="balance",
translation_key="balance",
state_class=SensorStateClass.TOTAL,
device_class=SensorDeviceClass.MONETARY,
value_fn=lambda account: account.balance,
unit_fn=lambda account: account.currency,
icon_fn=lambda account: account.inferred_account_type,
),
)
async def async_setup_entry(
hass: HomeAssistant,
config_entry: SimpleFinConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up SimpleFIN sensors for config entries."""
sf_coordinator = config_entry.runtime_data
accounts = sf_coordinator.data.accounts
async_add_entities(
SimpleFinSensor(
sf_coordinator,
sensor_description,
account,
)
for account in accounts
for sensor_description in SIMPLEFIN_SENSORS
)
class SimpleFinSensor(SimpleFinEntity, SensorEntity):
"""Defines a SimpleFIN sensor."""
entity_description: SimpleFinSensorEntityDescription
@property
def native_value(self) -> StateType:
"""Return the state."""
return self.entity_description.value_fn(self.account_data)
@property
def icon(self) -> str | None:
"""Return the icon of this account."""
if self.entity_description.icon_fn is not None:
return self.entity_description.icon_fn(self.account_data)
return None
@property
def native_unit_of_measurement(self) -> str | None:
"""Return the currency of this account."""
if self.entity_description.unit_fn:
return self.entity_description.unit_fn(self.account_data)
return None