Skip to content

Commit

Permalink
Add reload and default power and energy units
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean-Marc Collin committed Mar 4, 2023
1 parent eb8cb18 commit ae32f11
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 9 deletions.
5 changes: 1 addition & 4 deletions custom_components/versatile_thermostat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,14 @@
import logging

from homeassistant.config_entries import ConfigEntry
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant

from .climate import VersatileThermostat

from .const import DOMAIN
from .const import DOMAIN, PLATFORMS

_LOGGER = logging.getLogger(__name__)

PLATFORMS: list[Platform] = [Platform.CLIMATE, Platform.BINARY_SENSOR, Platform.SENSOR]


async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up Versatile Thermostat from a config entry."""
Expand Down
12 changes: 12 additions & 0 deletions custom_components/versatile_thermostat/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.entity_component import EntityComponent
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.reload import async_setup_reload_service

from homeassistant.helpers.event import (
async_track_state_change_event,
Expand Down Expand Up @@ -93,6 +94,7 @@

from .const import (
DOMAIN,
PLATFORMS,
DEVICE_MANUFACTURER,
CONF_HEATER,
CONF_POWER_SENSOR,
Expand Down Expand Up @@ -156,6 +158,8 @@ async def async_setup_entry(
"Calling async_setup_entry entry=%s, data=%s", entry.entry_id, entry.data
)

await async_setup_reload_service(hass, DOMAIN, PLATFORMS)

unique_id = entry.entry_id
name = entry.data.get(CONF_NAME)

Expand Down Expand Up @@ -1025,6 +1029,11 @@ def total_energy(self) -> float | None:
"""Returns the total energy calculated for this thermostast"""
return self._total_energy

@property
def device_power(self) -> float | None:
"""Returns the device_power for this thermostast"""
return self._device_power

@property
def overpowering_state(self) -> bool | None:
"""Get the overpowering_state"""
Expand Down Expand Up @@ -1506,6 +1515,9 @@ async def _async_climate_changed(self, event):
"""Handle unerdlying climate state changes."""
new_state = event.data.get("new_state")
_LOGGER.debug("%s - _async_climate_changed new_state is %s", self, new_state)
if not new_state:
return

old_state = event.data.get("old_state")
old_hvac_action = (
old_state.attributes.get("hvac_action")
Expand Down
5 changes: 4 additions & 1 deletion custom_components/versatile_thermostat/const.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"""Constants for the Versatile Thermostat integration."""

from enum import Enum
from homeassistant.const import CONF_NAME
from homeassistant.const import CONF_NAME, Platform

from homeassistant.components.climate import (
# PRESET_ACTIVITY,
PRESET_BOOST,
Expand All @@ -26,6 +27,8 @@

DOMAIN = "versatile_thermostat"

PLATFORMS: list[Platform] = [Platform.CLIMATE, Platform.BINARY_SENSOR, Platform.SENSOR]

CONF_HEATER = "heater_entity_id"
CONF_TEMP_SENSOR = "temperature_sensor_entity_id"
CONF_EXTERNAL_TEMP_SENSOR = "external_temperature_sensor_entity_id"
Expand Down
22 changes: 18 additions & 4 deletions custom_components/versatile_thermostat/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from homeassistant.core import HomeAssistant, callback, Event

from homeassistant.const import UnitOfTime
from homeassistant.const import UnitOfTime, UnitOfPower, UnitOfEnergy, PERCENTAGE

from homeassistant.components.sensor import (
SensorEntity,
Expand All @@ -25,6 +25,8 @@
CONF_THERMOSTAT_TYPE,
)

THRESHOLD_WATT_KILO = 100

_LOGGER = logging.getLogger(__name__)


Expand Down Expand Up @@ -99,7 +101,13 @@ def state_class(self) -> SensorStateClass | None:

@property
def native_unit_of_measurement(self) -> str | None:
return "kWh"
if not self.my_climate:
return None

if self.my_climate.device_power > THRESHOLD_WATT_KILO:
return UnitOfEnergy.WATT_HOUR
else:
return UnitOfEnergy.KILO_WATT_HOUR

@property
def suggested_display_precision(self) -> int | None:
Expand Down Expand Up @@ -150,7 +158,13 @@ def state_class(self) -> SensorStateClass | None:

@property
def native_unit_of_measurement(self) -> str | None:
return "kW"
if not self.my_climate:
return None

if self.my_climate.device_power > THRESHOLD_WATT_KILO:
return UnitOfPower.WATT
else:
return UnitOfPower.KILO_WATT

@property
def suggested_display_precision(self) -> int | None:
Expand Down Expand Up @@ -202,7 +216,7 @@ def state_class(self) -> SensorStateClass | None:

@property
def native_unit_of_measurement(self) -> str | None:
return "%"
return PERCENTAGE

@property
def suggested_display_precision(self) -> int | None:
Expand Down
3 changes: 3 additions & 0 deletions custom_components/versatile_thermostat/services.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
reload:
description: Reload all Versatile Thermostat entities.

set_presence:
name: Set presence
description: Force the presence mode in thermostat
Expand Down

0 comments on commit ae32f11

Please sign in to comment.