Skip to content

Commit

Permalink
Add current temperature sensor
Browse files Browse the repository at this point in the history
No more messing with attribs
  • Loading branch information
andrew-codechimp committed Apr 19, 2024
1 parent 8cc53af commit d980a48
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
30 changes: 26 additions & 4 deletions custom_components/hive_local_thermostat/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
from homeassistant.core import HomeAssistant
from homeassistant.config_entries import ConfigEntry
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
from homeassistant.helpers.temperature import display_temp as show_temp
from homeassistant.components.sensor import SensorEntity, SensorEntityDescription, SensorDeviceClass
from homeassistant.const import (
Platform,
UnitOfTemperature,
PRECISION_TENTHS,
)

from .entity import HiveEntity, HiveEntityDescription
Expand All @@ -30,6 +33,7 @@ class HiveSensorEntityDescription(
"""Class describing Hive sensor entities."""

func: any | None = None
running_state: bool = False

async def async_setup_entry(
hass: HomeAssistant,
Expand All @@ -54,6 +58,20 @@ async def async_setup_entry(
topic=config_entry.options[CONF_MQTT_TOPIC],
entry_id=config_entry.entry_id,
model=config_entry.options[CONF_MODEL],
running_state = True
),
HiveSensorEntityDescription(
key="local_temperature_heat",
translation_key="local_temperature_heat",
icon="mdi:thermometer",
name=config_entry.title,
device_class=SensorDeviceClass.TEMPERATURE,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
suggested_display_precision = 1,
func=lambda js: js["local_temperature_heat"],
topic=config_entry.options[CONF_MQTT_TOPIC],
entry_id=config_entry.entry_id,
model=config_entry.options[CONF_MODEL],
),
]

Expand All @@ -72,6 +90,7 @@ async def async_setup_entry(
topic=config_entry.options[CONF_MQTT_TOPIC],
entry_id=config_entry.entry_id,
model=config_entry.options[CONF_MODEL],
running_state = True
)
)

Expand Down Expand Up @@ -107,13 +126,16 @@ def process_update(self, mqtt_data) -> None:

try:
new_value = self._func(mqtt_data)
except KeyError:
new_value = ""

if self.entity_description.running_state:
self._attr_icon = self.entity_description.icons_by_state.get(new_value, ICON_UNKNOWN)
if new_value == "":
new_value = "preheating"
except KeyError:
new_value = "preheating"

self._attr_icon = self.entity_description.icons_by_state.get(new_value, ICON_UNKNOWN)
if self.entity_description.device_class == SensorDeviceClass.TEMPERATURE:
new_value = show_temp(self.hass, new_value, self.entity_description.native_unit_of_measurement, PRECISION_TENTHS)

self._attr_native_value = new_value
if (self.hass is not None): # this is a hack to get around the fact that the entity is not yet initialized at first
Expand Down
3 changes: 3 additions & 0 deletions custom_components/hive_local_thermostat/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
},
"entity": {
"sensor": {
"local_temperature_heat": {
"name": "Current temperature"
},
"running_state_water": {
"name": "Running state water",
"state": {
Expand Down

0 comments on commit d980a48

Please sign in to comment.