Skip to content

Commit

Permalink
Merge pull request #1055 from wtom/feature/generic_temperature_calibr…
Browse files Browse the repository at this point in the history
…ation

Feature/generic temperature calibration
  • Loading branch information
KartoffelToby committed Sep 18, 2023
2 parents 2f323a9 + 50d9924 commit a117a9f
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 8 deletions.
119 changes: 113 additions & 6 deletions custom_components/better_thermostat/adapters/generic.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,109 @@
import asyncio
import logging

from homeassistant.components.number.const import SERVICE_SET_VALUE
from ..utils.helpers import find_local_calibration_entity
from homeassistant.const import STATE_UNAVAILABLE, STATE_UNKNOWN

_LOGGER = logging.getLogger(__name__)


async def get_info(self, entity_id):
"""Get info from TRV."""
return {"support_offset": False, "support_valve": False}
support_offset = False

offset = await find_local_calibration_entity(self, entity_id)
if offset is not None:
support_offset = True
return {"support_offset": support_offset, "support_valve": False}


async def init(self, entity_id):
if (
self.real_trvs[entity_id]["local_temperature_calibration_entity"] is None
and self.real_trvs[entity_id]["calibration"] == 0
):
self.real_trvs[entity_id][
"local_temperature_calibration_entity"
] = await find_local_calibration_entity(self, entity_id)
_LOGGER.debug(
"better_thermostat %s: uses local calibration entity %s",
self.name,
self.real_trvs[entity_id]["local_temperature_calibration_entity"],
)
# Wait for the entity to be available
_ready = True
while _ready:
if self.hass.states.get(
self.real_trvs[entity_id]["local_temperature_calibration_entity"]
).state in (STATE_UNAVAILABLE, STATE_UNKNOWN, None):
_LOGGER.info(
"better_thermostat %s: waiting for TRV/climate entity with id '%s' to become fully available...",
self.name,
self.real_trvs[entity_id]["local_temperature_calibration_entity"],
)
await asyncio.sleep(5)
continue
_ready = False
return

return None


async def get_current_offset(self, entity_id):
"""Get current offset."""
return None
if self.real_trvs[entity_id]["local_temperature_calibration_entity"] is not None:
return float(
str(
self.hass.states.get(
self.real_trvs[entity_id]["local_temperature_calibration_entity"]
).state
)
)
else:
return None


async def get_offset_steps(self, entity_id):
"""Get offset steps."""
return None
if self.real_trvs[entity_id]["local_temperature_calibration_entity"] is not None:
return float(
str(
self.hass.states.get(
self.real_trvs[entity_id]["local_temperature_calibration_entity"]
).attributes.get("step", 1)
)
)
else:
return None


async def get_min_offset(self, entity_id):
"""Get min offset."""
return -6
if self.real_trvs[entity_id]["local_temperature_calibration_entity"] is not None:
return float(
str(
self.hass.states.get(
self.real_trvs[entity_id]["local_temperature_calibration_entity"]
).attributes.get("min", -10)
)
)
else:
return -6


async def get_max_offset(self, entity_id):
"""Get max offset."""
return 6
if self.real_trvs[entity_id]["local_temperature_calibration_entity"] is not None:
return float(
str(
self.hass.states.get(
self.real_trvs[entity_id]["local_temperature_calibration_entity"]
).attributes.get("max", 10)
)
)
else:
return 6


async def set_temperature(self, entity_id, temperature):
Expand All @@ -56,7 +130,40 @@ async def set_hvac_mode(self, entity_id, hvac_mode):

async def set_offset(self, entity_id, offset):
"""Set new target offset."""
return # Not supported
if self.real_trvs[entity_id]["local_temperature_calibration_entity"] is not None:
max_calibration = await get_max_offset(self, entity_id)
min_calibration = await get_min_offset(self, entity_id)

if offset >= max_calibration:
offset = max_calibration
if offset <= min_calibration:
offset = min_calibration

await self.hass.services.async_call(
"number",
SERVICE_SET_VALUE,
{
"entity_id": self.real_trvs[entity_id][
"local_temperature_calibration_entity"
],
"value": offset,
},
blocking=True,
context=self.context,
)
self.real_trvs[entity_id]["last_calibration"] = offset
if (
self.real_trvs[entity_id]["last_hvac_mode"] is not None
and self.real_trvs[entity_id]["last_hvac_mode"] != "off"
):
await asyncio.sleep(3)
await set_hvac_mode(
self, entity_id, self.real_trvs[entity_id]["last_hvac_mode"]
)

return offset
else:
return # Not supported


async def set_valve(self, entity_id, valve):
Expand Down
4 changes: 2 additions & 2 deletions custom_components/better_thermostat/utils/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -551,10 +551,10 @@ async def find_local_calibration_entity(self, entity_id):
entity_registry, reg_entity.config_entry_id
)
for entity in entity_entries:
uid = entity.unique_id
uid = entity.unique_id + " " + entity.entity_id
# Make sure we use the correct device entities
if entity.device_id == reg_entity.device_id:
if "local_temperature_calibration" in uid:
if "temperature_calibration" in uid or "temperature_offset" in uid:
_LOGGER.debug(
f"better thermostat: Found local calibration entity {entity.entity_id} for {entity_id}"
)
Expand Down

0 comments on commit a117a9f

Please sign in to comment.