Skip to content

Commit

Permalink
Add time attribute to local_time sensor
Browse files Browse the repository at this point in the history
  • Loading branch information
pnbruckner committed Jan 4, 2024
1 parent a8ee8ff commit dddcb14
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
7 changes: 5 additions & 2 deletions custom_components/entity_tz/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_TIME_ZONE
from homeassistant.const import ATTR_TIME, CONF_TIME_ZONE
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
import homeassistant.util.dt as dt_util
Expand Down Expand Up @@ -80,13 +80,16 @@ def __init__(self, entry: ConfigEntry) -> None:

async def async_update(self) -> None:
"""Update sensor."""
self._attr_extra_state_attributes = {ATTR_TIME: None}
if not self._sources_valid:
return

value = dt_util.now(self._entity_tz).time().isoformat("minutes")
dt_now = dt_util.now(self._entity_tz)
value = dt_now.time().isoformat("minutes")
if value[0] == "0":
value = value[1:]
self._attr_native_value = value
self._attr_extra_state_attributes[ATTR_TIME] = dt_now


class EntityTimeZoneSensor(ETZEntity, SensorEntity):
Expand Down
5 changes: 4 additions & 1 deletion custom_components/entity_tz/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@
}
},
"local_time": {
"name": "local time"
"name": "local time",
"state_attributes": {
"time": {"name": "Time"}
}
},
"time_zone": {
"name": "time zone",
Expand Down
5 changes: 4 additions & 1 deletion custom_components/entity_tz/translations/nl.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@
}
},
"local_time": {
"name": "Lokale tijd"
"name": "Lokale tijd",
"state_attributes": {
"time": {"name": "Tijd"}
}
},
"time_zone": {
"name": "tijdzone",
Expand Down

0 comments on commit dddcb14

Please sign in to comment.