-
-
Notifications
You must be signed in to change notification settings - Fork 38.7k
Add minutes and energy sensors to Nuheat #160751
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| """Sensor platform for NuHeat integration.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from collections.abc import Callable | ||
| from dataclasses import dataclass | ||
| from typing import Any | ||
|
|
||
| from homeassistant.components.sensor import ( | ||
| SensorDeviceClass, | ||
| SensorEntity, | ||
| SensorEntityDescription, | ||
| SensorStateClass, | ||
| ) | ||
| from homeassistant.config_entries import ConfigEntry | ||
| from homeassistant.const import UnitOfEnergy, UnitOfTime | ||
| from homeassistant.core import HomeAssistant | ||
| from homeassistant.helpers.device_registry import DeviceInfo | ||
| from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback | ||
| from homeassistant.helpers.typing import StateType | ||
| from homeassistant.helpers.update_coordinator import ( | ||
| CoordinatorEntity, | ||
| DataUpdateCoordinator, | ||
| ) | ||
|
|
||
| from . import NuHeatEnergyData | ||
| from .const import DOMAIN, MANUFACTURER | ||
|
|
||
| PARALLEL_UPDATES = 0 | ||
|
|
||
|
|
||
| @dataclass(frozen=True, kw_only=True) | ||
| class NuHeatSensorEntityDescription(SensorEntityDescription): | ||
| """Describes NuHeat sensor entity.""" | ||
|
|
||
| value_fn: Callable[[NuHeatEnergyData], StateType] | ||
|
|
||
|
|
||
| SENSOR_DESCRIPTIONS: tuple[NuHeatSensorEntityDescription, ...] = ( | ||
| NuHeatSensorEntityDescription( | ||
| key="energy", | ||
| translation_key="energy", | ||
| device_class=SensorDeviceClass.ENERGY, | ||
| state_class=SensorStateClass.TOTAL_INCREASING, | ||
| native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, | ||
| suggested_display_precision=2, | ||
| value_fn=lambda data: data.energy_kwh, | ||
| ), | ||
| NuHeatSensorEntityDescription( | ||
| key="heating_time", | ||
| translation_key="heating_time", | ||
| device_class=SensorDeviceClass.DURATION, | ||
| state_class=SensorStateClass.TOTAL_INCREASING, | ||
| native_unit_of_measurement=UnitOfTime.MINUTES, | ||
| value_fn=lambda data: data.heating_minutes, | ||
| ), | ||
| ) | ||
|
|
||
|
|
||
| async def async_setup_entry( | ||
| hass: HomeAssistant, | ||
| entry: ConfigEntry, | ||
| async_add_entities: AddConfigEntryEntitiesCallback, | ||
| ) -> None: | ||
| """Set up NuHeat sensor entities.""" | ||
| thermostat, _coordinator, energy_coordinator = hass.data[DOMAIN][entry.entry_id] | ||
|
|
||
| entities: list[NuHeatSensor] = [] | ||
|
|
||
| for description in SENSOR_DESCRIPTIONS: | ||
| # Only add energy sensor if we have valid kWh data | ||
| if description.key == "energy" and energy_coordinator.data.energy_kwh is None: | ||
| continue | ||
|
|
||
| entities.append( | ||
| NuHeatSensor( | ||
| energy_coordinator, | ||
| thermostat, | ||
| description, | ||
| ) | ||
| ) | ||
|
|
||
| async_add_entities(entities) | ||
|
|
||
|
|
||
| class NuHeatSensor( | ||
| CoordinatorEntity[DataUpdateCoordinator[NuHeatEnergyData]], SensorEntity | ||
| ): | ||
| """NuHeat sensor entity.""" | ||
|
|
||
| _attr_has_entity_name = True | ||
| entity_description: NuHeatSensorEntityDescription | ||
|
|
||
| def __init__( | ||
| self, | ||
| coordinator: DataUpdateCoordinator[NuHeatEnergyData], | ||
| thermostat: Any, | ||
| description: NuHeatSensorEntityDescription, | ||
| ) -> None: | ||
| """Initialize the sensor.""" | ||
| super().__init__(coordinator) | ||
| self._thermostat = thermostat | ||
| self.entity_description = description | ||
| self._attr_unique_id = f"{thermostat.serial_number}_{description.key}" | ||
|
|
||
| @property | ||
| def native_value(self) -> StateType: | ||
| """Return the sensor value.""" | ||
| return self.entity_description.value_fn(self.coordinator.data) | ||
|
|
||
| @property | ||
| def available(self) -> bool: | ||
| """Return if entity is available.""" | ||
| if not self.coordinator.last_update_success: | ||
| return False | ||
| if self.entity_description.key == "energy": | ||
| return self.coordinator.data.energy_kwh is not None | ||
| return True | ||
|
|
||
| @property | ||
| def device_info(self) -> DeviceInfo: | ||
| """Return the device_info of the device.""" | ||
| return DeviceInfo( | ||
| identifiers={(DOMAIN, self._thermostat.serial_number)}, | ||
| serial_number=self._thermostat.serial_number, | ||
| name=self._thermostat.room, | ||
| model="nVent Signature", | ||
| manufacturer=MANUFACTURER, | ||
| suggested_area=self._thermostat.room, | ||
| ) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The conditional logic prioritizes user-configured floor area over API-provided kWh data. This may be unexpected behavior. Consider documenting this precedence decision or validating that this is the intended priority.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is intentional.