Skip to content

Commit

Permalink
feat: provides power level and power pwercent
Browse files Browse the repository at this point in the history
Fixes #196
  • Loading branch information
= authored and swingerman committed Jul 25, 2024
1 parent a9699dc commit 14b5ff9
Show file tree
Hide file tree
Showing 12 changed files with 341 additions and 24 deletions.
41 changes: 38 additions & 3 deletions custom_components/dual_smart_thermostat/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@
from custom_components.dual_smart_thermostat.managers.feature_manager import (
FeatureManager,
)
from custom_components.dual_smart_thermostat.managers.hvac_power_manager import (
HvacPowerManager,
)
from custom_components.dual_smart_thermostat.managers.opening_manager import (
OpeningHvacModeScope,
OpeningManager,
Expand All @@ -82,6 +85,8 @@
from . import DOMAIN, PLATFORMS
from .const import (
ATTR_HVAC_ACTION_REASON,
ATTR_HVAC_POWER_LEVEL,
ATTR_HVAC_POWER_PERCENT,
ATTR_PREV_HUMIDITY,
ATTR_PREV_TARGET,
ATTR_PREV_TARGET_HIGH,
Expand All @@ -106,6 +111,10 @@
CONF_HEATER,
CONF_HOT_TOLERANCE,
CONF_HUMIDITY_SENSOR,
CONF_HVAC_POWER_LEVELS,
CONF_HVAC_POWER_MAX,
CONF_HVAC_POWER_MIN,
CONF_HVAC_POWER_TOLERANCE,
CONF_INITIAL_HVAC_MODE,
CONF_KEEP_ALIVE,
CONF_MAX_FLOOR_TEMP,
Expand Down Expand Up @@ -178,7 +187,7 @@
),
}

HYGROSTAT_SCHEMA = {
DEHUMIDIFYER_SCHEMA = {
vol.Optional(CONF_DRYER): cv.entity_id,
vol.Optional(CONF_HUMIDITY_SENSOR): cv.entity_id,
vol.Optional(CONF_MIN_HUMIDITY): vol.Coerce(float),
Expand All @@ -192,6 +201,13 @@
vol.Optional(CONF_HEAT_PUMP_COOLING): cv.entity_id,
}

HVAC_POWER_SCHEMA = {
vol.Optional(CONF_HVAC_POWER_LEVELS): vol.Coerce(int),
vol.Optional(CONF_HVAC_POWER_MIN): vol.Coerce(int),
vol.Optional(CONF_HVAC_POWER_MAX): vol.Coerce(int),
vol.Optional(CONF_HVAC_POWER_TOLERANCE): vol.Coerce(float),
}

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Required(CONF_HEATER): cv.entity_id,
Expand Down Expand Up @@ -241,10 +257,12 @@

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(FAN_MODE_SCHEMA)

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(HYGROSTAT_SCHEMA)
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(DEHUMIDIFYER_SCHEMA)

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(HEAT_PUMP_SCHEMA)

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(HVAC_POWER_SCHEMA)

# Add the old presets schema to avoid breaking change
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{vol.Optional(v): vol.Coerce(float) for (k, v) in CONF_PRESETS_OLD.items()}
Expand Down Expand Up @@ -281,13 +299,17 @@ async def async_setup_platform(
config,
)

hvac_power_manager = HvacPowerManager(hass, config, environment_manager)

feature_manager = FeatureManager(hass, config, environment_manager)

preset_manager = PresetManager(hass, config, environment_manager, feature_manager)

device_factory = HVACDeviceFactory(hass, config, feature_manager)

hvac_device = device_factory.create_device(environment_manager, opening_manager)
hvac_device = device_factory.create_device(
environment_manager, opening_manager, hvac_power_manager
)

async_add_entities(
[
Expand All @@ -308,6 +330,7 @@ async def async_setup_platform(
environment_manager,
opening_manager,
feature_manager,
hvac_power_manager,
)
]
)
Expand Down Expand Up @@ -362,6 +385,7 @@ def __init__(
environment_manager: EnvironmentManager,
opening_manager: OpeningManager,
feature_manager: FeatureManager,
power_manager: HvacPowerManager,
) -> None:
"""Initialize the thermostat."""
self._attr_name = name
Expand All @@ -383,6 +407,9 @@ def __init__(
# opening manager
self.openings = opening_manager

# power manager
self.power_manager = power_manager

# sensors
self.sensor_entity_id = sensor_entity_id
self.sensor_floor_entity_id = sensor_floor_entity_id
Expand Down Expand Up @@ -774,6 +801,14 @@ def extra_state_attributes(self) -> dict:
self._hvac_action_reason or HVACActionReason.NONE
)

# TODO: set these only if configured to avoid unnecessary DB writes
if self.features.is_configured_for_hvac_power_levels:
_LOGGER.debug(
"Setting HVAC Power Level: %s", self.power_manager.hvac_power_level
)
attributes[ATTR_HVAC_POWER_LEVEL] = self.power_manager.hvac_power_level
attributes[ATTR_HVAC_POWER_PERCENT] = self.power_manager.hvac_power_percent

_LOGGER.debug("Extra state attributes: %s", attributes)

return attributes
Expand Down
8 changes: 8 additions & 0 deletions custom_components/dual_smart_thermostat/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@
CONF_HEAT_COOL_MODE = "heat_cool_mode"
CONF_HEAT_PUMP_COOLING = "heat_pump_cooling"

# HVAC power levels
CONF_HVAC_POWER_LEVELS = "hvac_power_levels"
CONF_HVAC_POWER_MIN = "hvac_power_min"
CONF_HVAC_POWER_MAX = "hvac_power_max"
CONF_HVAC_POWER_TOLERANCE = "hvac_power_tolerance"
ATTR_HVAC_POWER_LEVEL = "hvac_power_level"
ATTR_HVAC_POWER_PERCENT = "hvac_power_percent"

ATTR_PREV_TARGET = "prev_target_temp"
ATTR_PREV_TARGET_LOW = "prev_target_temp_low"
ATTR_PREV_TARGET_HIGH = "prev_target_temp_high"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
from custom_components.dual_smart_thermostat.managers.feature_manager import (
FeatureManager,
)
from custom_components.dual_smart_thermostat.managers.hvac_power_manager import (
HvacPowerManager,
)
from custom_components.dual_smart_thermostat.managers.opening_manager import (
OpeningManager,
)
Expand All @@ -39,6 +42,7 @@ def __init__(
environment: EnvironmentManager,
openings: OpeningManager,
features: FeatureManager,
hvac_power: HvacPowerManager,
) -> None:
super().__init__(
hass,
Expand All @@ -48,6 +52,7 @@ def __init__(
environment,
openings,
features,
hvac_power,
hvac_goal=HvacGoal.LOWER,
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
from custom_components.dual_smart_thermostat.managers.feature_manager import (
FeatureManager,
)
from custom_components.dual_smart_thermostat.managers.hvac_power_manager import (
HvacPowerManager,
)
from custom_components.dual_smart_thermostat.managers.opening_manager import (
OpeningManager,
)
Expand All @@ -41,6 +44,7 @@ def __init__(
environment: EnvironmentManager,
openings: OpeningManager,
features: FeatureManager,
hvac_power: HvacPowerManager,
) -> None:
super().__init__(
hass,
Expand All @@ -50,6 +54,7 @@ def __init__(
environment,
openings,
features,
hvac_power,
hvac_goal=HvacGoal.LOWER,
)

Expand Down
30 changes: 23 additions & 7 deletions custom_components/dual_smart_thermostat/hvac_device/fan_device.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
from datetime import timedelta
import logging

from homeassistant.components.climate import HVACAction, HVACMode
from homeassistant.core import HomeAssistant

from custom_components.dual_smart_thermostat.hvac_device.cooler_device import (
CoolerDevice,
)
from custom_components.dual_smart_thermostat.managers.environment_manager import (
EnvironmentManager,
)
from custom_components.dual_smart_thermostat.managers.feature_manager import (
FeatureManager,
)
from custom_components.dual_smart_thermostat.managers.hvac_power_manager import (
HvacPowerManager,
)
from custom_components.dual_smart_thermostat.managers.opening_manager import (
OpeningManager,
)

_LOGGER = logging.getLogger(__name__)

Expand All @@ -16,13 +30,14 @@ class FanDevice(CoolerDevice):

def __init__(
self,
hass,
entity_id,
min_cycle_duration,
initial_hvac_mode,
environment,
openings,
features,
hass: HomeAssistant,
entity_id: str,
min_cycle_duration: timedelta,
initial_hvac_mode: HVACMode,
environment: EnvironmentManager,
openings: OpeningManager,
features: FeatureManager,
hvac_power: HvacPowerManager,
) -> None:
super().__init__(
hass,
Expand All @@ -32,6 +47,7 @@ def __init__(
environment,
openings,
features,
hvac_power,
)

if self.features.is_fan_uses_outside_air:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@
from custom_components.dual_smart_thermostat.managers.feature_manager import (
FeatureManager,
)
from custom_components.dual_smart_thermostat.managers.hvac_power_manager import (
HvacPowerManager,
)
from custom_components.dual_smart_thermostat.managers.opening_manager import (
OpeningManager,
)
Expand All @@ -67,6 +70,7 @@ def __init__(
environment: EnvironmentManager,
openings: OpeningManager,
features: FeatureManager,
hvac_power: HvacPowerManager,
hvac_goal: HvacGoal,
) -> None:
super().__init__(hass, environment, openings)
Expand All @@ -77,6 +81,7 @@ def __init__(
self.hvac_goal = hvac_goal

self.features = features
self.hvac_power = hvac_power
self.entity_id = entity_id
self.min_cycle_duration = min_cycle_duration

Expand Down Expand Up @@ -222,16 +227,15 @@ async def async_control_hvac(self, time=None, force=False):
any_opeing_open,
time,
)
# await self._async_control_when_active(time)
else:
await self.hvac_controller.async_control_device_when_off(
self.strategy,
any_opeing_open,
time,
)
# await self._async_control_when_inactive(time)

self._hvac_action_reason = self.hvac_controller.hvac_action_reason
self.hvac_power.update_hvac_power(self.strategy, self.target_env_attr)

async def async_on_startup(self):
entity_state = self.hass.states.get(self.entity_id)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from datetime import timedelta
import logging

from homeassistant.components.climate import HVACMode
from homeassistant.const import STATE_ON, STATE_UNAVAILABLE, STATE_UNKNOWN
from homeassistant.core import State, callback
from homeassistant.core import HomeAssistant, State, callback

from custom_components.dual_smart_thermostat.hvac_controller.cooler_controller import (
CoolerHvacController,
Expand All @@ -21,8 +22,18 @@
merge_hvac_modes,
)
from custom_components.dual_smart_thermostat.managers.environment_manager import (
EnvironmentManager,
TargetTemperatures,
)
from custom_components.dual_smart_thermostat.managers.feature_manager import (
FeatureManager,
)
from custom_components.dual_smart_thermostat.managers.hvac_power_manager import (
HvacPowerManager,
)
from custom_components.dual_smart_thermostat.managers.opening_manager import (
OpeningManager,
)

_LOGGER = logging.getLogger(__name__)

Expand All @@ -33,13 +44,14 @@ class HeatPumpDevice(GenericHVACDevice):

def __init__(
self,
hass,
entity_id,
min_cycle_duration,
initial_hvac_mode,
environment,
openings,
features,
hass: HomeAssistant,
entity_id: str,
min_cycle_duration: timedelta,
initial_hvac_mode: HVACMode,
environment: EnvironmentManager,
openings: OpeningManager,
features: FeatureManager,
hvac_power: HvacPowerManager,
) -> None:
super().__init__(
hass,
Expand All @@ -49,6 +61,7 @@ def __init__(
environment,
openings,
features,
hvac_power,
hvac_goal=HvacGoal.RAISE, # will not take effect as we will define new controllers
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
from custom_components.dual_smart_thermostat.managers.feature_manager import (
FeatureManager,
)
from custom_components.dual_smart_thermostat.managers.hvac_power_manager import (
HvacPowerManager,
)
from custom_components.dual_smart_thermostat.managers.opening_manager import (
OpeningManager,
)
Expand All @@ -39,6 +42,7 @@ def __init__(
environment: EnvironmentManager,
openings: OpeningManager,
features: FeatureManager,
hvac_power: HvacPowerManager,
) -> None:
super().__init__(
hass,
Expand All @@ -48,6 +52,7 @@ def __init__(
environment,
openings,
features,
hvac_power,
hvac_goal=HvacGoal.RAISE,
)

Expand Down
Loading

0 comments on commit 14b5ff9

Please sign in to comment.