From e9eaeedf2b6db4d2e52b58021e0d0b19e7b553b1 Mon Sep 17 00:00:00 2001 From: Manu <4445816+tr4nt0r@users.noreply.github.com> Date: Mon, 18 Nov 2024 10:52:23 +0100 Subject: [PATCH] Add entity picture for gems to Habitica integration (#130827) --- homeassistant/components/habitica/sensor.py | 11 ++++++++++- tests/components/habitica/snapshots/test_sensor.ambr | 1 + 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/habitica/sensor.py b/homeassistant/components/habitica/sensor.py index 3b2395ecc52507..d6943fcae56ce6 100644 --- a/homeassistant/components/habitica/sensor.py +++ b/homeassistant/components/habitica/sensor.py @@ -24,7 +24,7 @@ ) from homeassistant.helpers.typing import StateType -from .const import DOMAIN, UNIT_TASKS +from .const import ASSETS_URL, DOMAIN, UNIT_TASKS from .entity import HabiticaBase from .types import HabiticaConfigEntry from .util import entity_used_in, get_attribute_points, get_attributes_total @@ -40,6 +40,7 @@ class HabitipySensorEntityDescription(SensorEntityDescription): attributes_fn: ( Callable[[dict[str, Any], dict[str, Any]], dict[str, Any] | None] | None ) = None + entity_picture: str | None = None @dataclass(kw_only=True, frozen=True) @@ -144,6 +145,7 @@ class HabitipySensorEntity(StrEnum): value_fn=lambda user, _: user.get("balance", 0) * 4, suggested_display_precision=0, native_unit_of_measurement="gems", + entity_picture="shop_gem.png", ), HabitipySensorEntityDescription( key=HabitipySensorEntity.TRINKETS, @@ -293,6 +295,13 @@ def extra_state_attributes(self) -> dict[str, float | None] | None: return func(self.coordinator.data.user, self.coordinator.content) return None + @property + def entity_picture(self) -> str | None: + """Return the entity picture to use in the frontend, if any.""" + if entity_picture := self.entity_description.entity_picture: + return f"{ASSETS_URL}{entity_picture}" + return None + class HabitipyTaskSensor(HabiticaBase, SensorEntity): """A Habitica task sensor.""" diff --git a/tests/components/habitica/snapshots/test_sensor.ambr b/tests/components/habitica/snapshots/test_sensor.ambr index 3a43069bfc461c..07eddf496b270b 100644 --- a/tests/components/habitica/snapshots/test_sensor.ambr +++ b/tests/components/habitica/snapshots/test_sensor.ambr @@ -405,6 +405,7 @@ # name: test_sensors[sensor.test_user_gems-state] StateSnapshot({ 'attributes': ReadOnlyDict({ + 'entity_picture': 'https://habitica-assets.s3.amazonaws.com/mobileApp/images/shop_gem.png', 'friendly_name': 'test-user Gems', 'unit_of_measurement': 'gems', }),