hass-core/homeassistant/components/monzo/sensor.py
Jake Martin 6e024d54f1
Add Monzo integration (#101731)
* Initial monzo implementation

* Tests and fixes

* Extracted api to pypi package

* Add app confirmation step

* Corrected data path for accounts

* Removed useless check

* Improved tests

* Exclude partially tested files from coverage check

* Use has_entity_name naming

* Bumped monzopy to 1.0.10

* Remove commented out code

* Remove reauth from initial PR

* Remove useless code

* Correct comment

* Remove reauth tests

* Remove device triggers from intial PR

* Set attr outside constructor

* Remove f-strings where no longer needed in entity.py

* Rename field to make clearer it's a Callable

* Correct native_unit_of_measurement

* Remove pot transfer service from intial PR

* Remove reauth string

* Remove empty fields in manifest.json

* Freeze SensorEntityDescription and remove Mixin

Also use list comprehensions for producing sensor lists

* Use consts in application_credentials.py

* Revert "Remove useless code"

Apparently this wasn't useless

This reverts commit c6b7109e47202f866c766ea4c16ce3eb0588795b.

* Ruff and pylint style fixes

* Bumped monzopy to 1.1.0

Adds support for joint/business/etc account pots

* Update test snapshot

* Rename AsyncConfigEntryAuth

* Use dataclasses instead of dictionaries

* Move OAuth constants to application_credentials.py

* Remove remaining constants and dependencies for services from this PR

* Remove empty manifest entry

* Fix comment

* Set device entry_type to service

* ACC_SENSORS -> ACCOUNT_SENSORS

* Make value_fn of sensors return StateType

* Rename OAuthMonzoAPI again

* Fix tests

* Patch API instead of integration for unavailable test

* Move pot constant to sensor.py

* Improve type safety in async_get_monzo_api_data()

* Update async_oauth_create_entry() docstring

---------

Co-authored-by: Erik Montnemery <erik@montnemery.com>
2024-05-07 20:38:58 +02:00

123 lines
3.6 KiB
Python

"""Platform for sensor integration."""
from __future__ import annotations
from collections.abc import Callable
from dataclasses import dataclass
from typing import Any
from homeassistant.components.sensor import (
SensorDeviceClass,
SensorEntity,
SensorEntityDescription,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import StateType
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
from .const import DOMAIN
from .data import MonzoSensorData
from .entity import MonzoBaseEntity
@dataclass(frozen=True, kw_only=True)
class MonzoSensorEntityDescription(SensorEntityDescription):
"""Describes Monzo sensor entity."""
value_fn: Callable[[dict[str, Any]], StateType]
ACCOUNT_SENSORS = (
MonzoSensorEntityDescription(
key="balance",
translation_key="balance",
value_fn=lambda data: data["balance"]["balance"] / 100,
device_class=SensorDeviceClass.MONETARY,
native_unit_of_measurement="GBP",
suggested_display_precision=2,
),
MonzoSensorEntityDescription(
key="total_balance",
translation_key="total_balance",
value_fn=lambda data: data["balance"]["total_balance"] / 100,
device_class=SensorDeviceClass.MONETARY,
native_unit_of_measurement="GBP",
suggested_display_precision=2,
),
)
POT_SENSORS = (
MonzoSensorEntityDescription(
key="pot_balance",
translation_key="pot_balance",
value_fn=lambda data: data["balance"] / 100,
device_class=SensorDeviceClass.MONETARY,
native_unit_of_measurement="GBP",
suggested_display_precision=2,
),
)
MODEL_POT = "Pot"
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Defer sensor setup to the shared sensor module."""
coordinator = hass.data[DOMAIN][config_entry.entry_id].coordinator
accounts = [
MonzoSensor(
coordinator,
entity_description,
index,
account["name"],
lambda x: x.accounts,
)
for entity_description in ACCOUNT_SENSORS
for index, account in enumerate(
hass.data[DOMAIN][config_entry.entry_id].accounts
)
]
pots = [
MonzoSensor(coordinator, entity_description, index, MODEL_POT, lambda x: x.pots)
for entity_description in POT_SENSORS
for index, _pot in enumerate(hass.data[DOMAIN][config_entry.entry_id].pots)
]
async_add_entities(accounts + pots)
class MonzoSensor(MonzoBaseEntity, SensorEntity):
"""Represents a Monzo sensor."""
entity_description: MonzoSensorEntityDescription
def __init__(
self,
coordinator: DataUpdateCoordinator[MonzoSensorData],
entity_description: MonzoSensorEntityDescription,
index: int,
device_model: str,
data_accessor: Callable[[MonzoSensorData], list[dict[str, Any]]],
) -> None:
"""Initialize the sensor."""
super().__init__(coordinator, index, device_model, data_accessor)
self.entity_description = entity_description
self._attr_unique_id = f"{self.data['id']}_{entity_description.key}"
@property
def native_value(self) -> StateType:
"""Return the state."""
try:
state = self.entity_description.value_fn(self.data)
except (KeyError, ValueError):
return None
return state