Skip to content

Commit

Permalink
Allow using input_number entities for heater or cooler.
Browse files Browse the repository at this point in the history
  • Loading branch information
ScratMan committed Jul 13, 2023
1 parent 6719299 commit f12ba98
Showing 1 changed file with 28 additions and 6 deletions.
34 changes: 28 additions & 6 deletions custom_components/smart_thermostat/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
SERVICE_SET_VALUE,
DOMAIN as NUMBER_DOMAIN
)
from homeassistant.components.input_number import DOMAIN as INPUT_NUMBER_DOMAIN
from homeassistant.core import DOMAIN as HA_DOMAIN, CoreState, callback
from homeassistant.util import slugify
import homeassistant.helpers.config_validation as cv
Expand Down Expand Up @@ -491,6 +492,9 @@ def unique_id(self):
"""Return a unique ID."""
return self._unique_id

def _get_number_entity_domain(self, entity_id):
return INPUT_NUMBER_DOMAIN if "input_number" in entity_id else NUMBER_DOMAIN

@property
def precision(self):
"""Return the precision of the system."""
Expand Down Expand Up @@ -725,13 +729,19 @@ async def async_set_hvac_mode(self, hvac_mode: HVACMode) -> None:
ATTR_VALUE: self._control_output}
_LOGGER.debug("%s: Set heater to %s from async_set_hvac_mode(%s)", self.entity_id,
self._control_output, hvac_mode)
await self.hass.services.async_call(NUMBER_DOMAIN, SERVICE_SET_VALUE, data)
await self.hass.services.async_call(
self._get_number_entity_domain(self._heater_entity_id),
SERVICE_SET_VALUE,
data)
if self._cooler_entity_id is not None:
data = {ATTR_ENTITY_ID: self._cooler_entity_id,
ATTR_VALUE: self._control_output}
_LOGGER.debug("%s: Set cooler to %s from async_set_hvac_mode(%s)", self.entity_id,
self._control_output, hvac_mode)
await self.hass.services.async_call(NUMBER_DOMAIN, SERVICE_SET_VALUE, data)
await self.hass.services.async_call(
self._get_number_entity_domain(self._cooler_entity_id),
SERVICE_SET_VALUE,
data)
# Clear the samples to avoid integrating the off period
self._previous_temp = None
self._previous_temp_time = None
Expand Down Expand Up @@ -929,11 +939,17 @@ async def _async_control_heating(self, time_func=None, calc_pid=False):
self._control_output = 0
data = {ATTR_ENTITY_ID: self._heater_entity_id,
ATTR_VALUE: self._control_output}
await self.hass.services.async_call(NUMBER_DOMAIN, SERVICE_SET_VALUE, data)
await self.hass.services.async_call(
self._get_number_entity_domain(self._heater_entity_id),
SERVICE_SET_VALUE,
data)
if self._cooler_entity_id is not None:
data = {ATTR_ENTITY_ID: self._cooler_entity_id,
ATTR_VALUE: self._control_output}
await self.hass.services.async_call(NUMBER_DOMAIN, SERVICE_SET_VALUE, data)
await self.hass.services.async_call(
self._get_number_entity_domain(self._cooler_entity_id),
SERVICE_SET_VALUE,
data)
self.async_write_ha_state()
return

Expand Down Expand Up @@ -1000,7 +1016,10 @@ async def _async_heater_turn_off(self, force=False):
self.heater_or_cooler_entity, 0)
# self.hass.states.async_set(self._heater_entity_id, self._control_output)
data = {ATTR_ENTITY_ID: self.heater_or_cooler_entity, ATTR_VALUE: 0}
await self.hass.services.async_call(NUMBER_DOMAIN, SERVICE_SET_VALUE, data)
await self.hass.services.async_call(
self._get_number_entity_domain(self.heater_or_cooler_entity),
SERVICE_SET_VALUE,
data)

async def async_set_preset_mode(self, preset_mode: str):
"""Set new preset mode.
Expand Down Expand Up @@ -1123,7 +1142,10 @@ async def set_control_value(self):
# self.hass.states.async_set(self._heater_entity_id, self._control_output)
data = {ATTR_ENTITY_ID: self.heater_or_cooler_entity,
ATTR_VALUE: abs(self._control_output)}
await self.hass.services.async_call(NUMBER_DOMAIN, SERVICE_SET_VALUE, data)
await self.hass.services.async_call(
self._get_number_entity_domain(self.heater_or_cooler_entity),
SERVICE_SET_VALUE,
data)

async def pwm_switch(self, time_on, time_off, time_passed):
"""turn off and on the heater proportionally to control_value."""
Expand Down

1 comment on commit f12ba98

@ScratMan
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should close #160 #123 #25

Please sign in to comment.