Skip to content

Commit

Permalink
move extra_state_attributes and add entryTimestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
jcgoette committed Dec 15, 2021
1 parent eb05070 commit fb8ac62
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
12 changes: 0 additions & 12 deletions custom_components/weight_gurus/entity.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
"""weight_gurus entity."""
from __future__ import annotations

from typing import Any

from homeassistant.const import CONF_EMAIL
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.update_coordinator import CoordinatorEntity
Expand Down Expand Up @@ -33,16 +31,6 @@ def device_info(self) -> DeviceInfo:
manufacturer=NAME,
)

@property
def extra_state_attributes(self) -> dict[str, Any]:
"""Return the extra state attributes."""
return {
ATTR_FIRST_NAME: str(self.coordinator.data.get(ATTR_FIRST_NAME)),
ATTR_LAST_NAME: str(self.coordinator.data.get(ATTR_LAST_NAME)),
ATTR_HEIGHT: str(self.coordinator.data.get(ATTR_HEIGHT) / 10),
ATTR_ACTIVITY_LEVEL: str(self.coordinator.data.get(ATTR_ACTIVITY_LEVEL)),
}

@property
def state_class(self) -> str:
"""Return the state class."""
Expand Down
21 changes: 21 additions & 0 deletions custom_components/weight_gurus/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import StateType
from homeassistant.util.dt import as_local, parse_datetime
from voluptuous.validators import Datetime

from .const import (
ATTR_ACTIVITY_LEVEL,
Expand Down Expand Up @@ -109,6 +111,25 @@ def __init__(
def available(self) -> bool:
"""Return True if entity is available."""
return self.coordinator.data[self._description.key]

@property
def extra_state_attributes(self) -> dict[str, str | Datetime]:
"""Return the extra state attributes."""
extra_state_attributes = {
ATTR_FIRST_NAME: str(self.coordinator.data.get(ATTR_FIRST_NAME)),
ATTR_LAST_NAME: str(self.coordinator.data.get(ATTR_LAST_NAME)),
ATTR_HEIGHT: str(self.coordinator.data.get(ATTR_HEIGHT) / 10),
ATTR_ACTIVITY_LEVEL: str(self.coordinator.data.get(ATTR_ACTIVITY_LEVEL)),
}
if self.coordinator.data.get(f"{self._description.key}_{ATTR_ENTRY_TIMESTAMP}"):
timestamp_str = self.coordinator.data[
f"{self._description.key}_{ATTR_ENTRY_TIMESTAMP}"
]
timestamp_dt = parse_datetime(timestamp_str)
timestamp_local = as_local(timestamp_dt)
extra_state_attributes[ATTR_ENTRY_TIMESTAMP] = timestamp_local
return extra_state_attributes

@property
def icon(self) -> str | None:
"""Return the icon."""
Expand Down

0 comments on commit fb8ac62

Please sign in to comment.