mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +00:00
Add user profile info to Habitica sensor and device URL (#137152)
Add user profile attributes to Habitica sensor and device URL
This commit is contained in:
parent
d46e29d7a9
commit
e71f8c444b
@ -4,6 +4,8 @@ from __future__ import annotations
|
|||||||
|
|
||||||
from typing import TYPE_CHECKING
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
|
from yarl import URL
|
||||||
|
|
||||||
from homeassistant.const import CONF_NAME, CONF_URL
|
from homeassistant.const import CONF_NAME, CONF_URL
|
||||||
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
|
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
|
||||||
from homeassistant.helpers.entity import EntityDescription
|
from homeassistant.helpers.entity import EntityDescription
|
||||||
@ -36,6 +38,10 @@ class HabiticaBase(CoordinatorEntity[HabiticaDataUpdateCoordinator]):
|
|||||||
manufacturer=MANUFACTURER,
|
manufacturer=MANUFACTURER,
|
||||||
model=NAME,
|
model=NAME,
|
||||||
name=coordinator.config_entry.data[CONF_NAME],
|
name=coordinator.config_entry.data[CONF_NAME],
|
||||||
configuration_url=coordinator.config_entry.data[CONF_URL],
|
configuration_url=(
|
||||||
|
URL(coordinator.config_entry.data[CONF_URL])
|
||||||
|
/ "profile"
|
||||||
|
/ coordinator.config_entry.unique_id
|
||||||
|
),
|
||||||
identifiers={(DOMAIN, coordinator.config_entry.unique_id)},
|
identifiers={(DOMAIN, coordinator.config_entry.unique_id)},
|
||||||
)
|
)
|
||||||
|
@ -35,6 +35,7 @@ from homeassistant.helpers.issue_registry import (
|
|||||||
async_delete_issue,
|
async_delete_issue,
|
||||||
)
|
)
|
||||||
from homeassistant.helpers.typing import StateType
|
from homeassistant.helpers.typing import StateType
|
||||||
|
from homeassistant.util import dt as dt_util
|
||||||
|
|
||||||
from .const import ASSETS_URL, DOMAIN
|
from .const import ASSETS_URL, DOMAIN
|
||||||
from .coordinator import HabiticaConfigEntry, HabiticaDataUpdateCoordinator
|
from .coordinator import HabiticaConfigEntry, HabiticaDataUpdateCoordinator
|
||||||
@ -105,6 +106,20 @@ SENSOR_DESCRIPTIONS: tuple[HabiticaSensorEntityDescription, ...] = (
|
|||||||
key=HabiticaSensorEntity.DISPLAY_NAME,
|
key=HabiticaSensorEntity.DISPLAY_NAME,
|
||||||
translation_key=HabiticaSensorEntity.DISPLAY_NAME,
|
translation_key=HabiticaSensorEntity.DISPLAY_NAME,
|
||||||
value_fn=lambda user, _: user.profile.name,
|
value_fn=lambda user, _: user.profile.name,
|
||||||
|
attributes_fn=lambda user, _: {
|
||||||
|
"blurb": user.profile.blurb,
|
||||||
|
"joined": (
|
||||||
|
dt_util.as_local(joined).date()
|
||||||
|
if (joined := user.auth.timestamps.created)
|
||||||
|
else None
|
||||||
|
),
|
||||||
|
"last_login": (
|
||||||
|
dt_util.as_local(last).date()
|
||||||
|
if (last := user.auth.timestamps.loggedin)
|
||||||
|
else None
|
||||||
|
),
|
||||||
|
"total_logins": user.loginIncentives,
|
||||||
|
},
|
||||||
),
|
),
|
||||||
HabiticaSensorEntityDescription(
|
HabiticaSensorEntityDescription(
|
||||||
key=HabiticaSensorEntity.HEALTH,
|
key=HabiticaSensorEntity.HEALTH,
|
||||||
@ -393,6 +408,11 @@ class HabiticaSensor(HabiticaBase, SensorEntity):
|
|||||||
):
|
):
|
||||||
return SVG_CLASS[_class]
|
return SVG_CLASS[_class]
|
||||||
|
|
||||||
|
if self.entity_description.key is HabiticaSensorEntity.DISPLAY_NAME and (
|
||||||
|
img_url := self.coordinator.data.user.profile.imageUrl
|
||||||
|
):
|
||||||
|
return img_url
|
||||||
|
|
||||||
if entity_picture := self.entity_description.entity_picture:
|
if entity_picture := self.entity_description.entity_picture:
|
||||||
return (
|
return (
|
||||||
entity_picture
|
entity_picture
|
||||||
|
@ -199,7 +199,21 @@
|
|||||||
},
|
},
|
||||||
"sensor": {
|
"sensor": {
|
||||||
"display_name": {
|
"display_name": {
|
||||||
"name": "Display name"
|
"name": "Display name",
|
||||||
|
"state_attributes": {
|
||||||
|
"blurb": {
|
||||||
|
"name": "About"
|
||||||
|
},
|
||||||
|
"joined": {
|
||||||
|
"name": "Joined"
|
||||||
|
},
|
||||||
|
"last_login": {
|
||||||
|
"name": "Last login"
|
||||||
|
},
|
||||||
|
"total_logins": {
|
||||||
|
"name": "Total logins"
|
||||||
|
}
|
||||||
|
}
|
||||||
},
|
},
|
||||||
"health": {
|
"health": {
|
||||||
"name": "Health",
|
"name": "Health",
|
||||||
|
@ -2,8 +2,18 @@
|
|||||||
"success": true,
|
"success": true,
|
||||||
"data": {
|
"data": {
|
||||||
"api_user": "test-api-user",
|
"api_user": "test-api-user",
|
||||||
"profile": { "name": "test-user" },
|
"profile": {
|
||||||
"auth": { "local": { "username": "test-username" } },
|
"name": "test-user",
|
||||||
|
"blurb": "My mind is a swirling miasma of scintillating thoughts and turgid ideas.",
|
||||||
|
"imageUrl": "https://pbs.twimg.com/profile_images/378800000771780608/a32e71fe6a64eba6773c20d289eddc8e.png"
|
||||||
|
},
|
||||||
|
"auth": {
|
||||||
|
"local": { "username": "test-username" },
|
||||||
|
"timestamps": {
|
||||||
|
"created": "2013-12-02T22:23:29.249Z",
|
||||||
|
"loggedin": "2025-02-02T03:14:33.864Z"
|
||||||
|
}
|
||||||
|
},
|
||||||
"stats": {
|
"stats": {
|
||||||
"buffs": {
|
"buffs": {
|
||||||
"str": 26,
|
"str": 26,
|
||||||
@ -162,6 +172,7 @@
|
|||||||
"createdAt": "2025-02-08T22:06:08.894Z",
|
"createdAt": "2025-02-08T22:06:08.894Z",
|
||||||
"updatedAt": "2025-02-08T22:06:17.195Z"
|
"updatedAt": "2025-02-08T22:06:17.195Z"
|
||||||
}
|
}
|
||||||
]
|
],
|
||||||
|
"loginIncentives": 241
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -154,7 +154,12 @@
|
|||||||
# name: test_sensors[sensor.test_user_display_name-state]
|
# name: test_sensors[sensor.test_user_display_name-state]
|
||||||
StateSnapshot({
|
StateSnapshot({
|
||||||
'attributes': ReadOnlyDict({
|
'attributes': ReadOnlyDict({
|
||||||
|
'blurb': 'My mind is a swirling miasma of scintillating thoughts and turgid ideas.',
|
||||||
|
'entity_picture': 'https://pbs.twimg.com/profile_images/378800000771780608/a32e71fe6a64eba6773c20d289eddc8e.png',
|
||||||
'friendly_name': 'test-user Display name',
|
'friendly_name': 'test-user Display name',
|
||||||
|
'joined': datetime.date(2013, 12, 2),
|
||||||
|
'last_login': datetime.date(2025, 2, 1),
|
||||||
|
'total_logins': 241,
|
||||||
}),
|
}),
|
||||||
'context': <ANY>,
|
'context': <ANY>,
|
||||||
'entity_id': 'sensor.test_user_display_name',
|
'entity_id': 'sensor.test_user_display_name',
|
||||||
|
Loading…
x
Reference in New Issue
Block a user