Skip to content

Commit

Permalink
[TASK] several fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
KartoffelToby committed Jul 10, 2023
1 parent 41f97e7 commit 58d15d3
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 68 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[![Active installations](https://badge.t-haber.de/badge/better_thermostat?kill_cache=1)](https://github.com/KartoffelToby/better_thermostat/)
[![GitHub issues](https://img.shields.io/github/issues/KartoffelToby/better_thermostat?style=for-the-badge)](https://github.com/KartoffelToby/better_thermostat/issues)
[![Version - 1.2.0](https://img.shields.io/badge/Version-1.2.0-009688?style=for-the-badge)](https://github.com/KartoffelToby/better_thermostat/releases)
[![Version - 1.2.1](https://img.shields.io/badge/Version-1.2.1-009688?style=for-the-badge)](https://github.com/KartoffelToby/better_thermostat/releases)
[![Discord](https://img.shields.io/discord/925725316540923914.svg?style=for-the-badge)](https://discord.gg/9BUegWTG3K)
[![hacs_badge](https://img.shields.io/badge/HACS-Default-41BDF5.svg?style=for-the-badge)](https://github.com/hacs/integration)

Expand Down
13 changes: 12 additions & 1 deletion custom_components/better_thermostat/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

from .utils.model_quirks import load_model_quirks

from .utils.helpers import convert_to_float
from .utils.helpers import convert_to_float, find_battery_entity
from homeassistant.helpers import entity_platform
from homeassistant.core import callback, CoreState, Context, ServiceCall
import json
Expand Down Expand Up @@ -771,6 +771,17 @@ async def startup(self):
self.async_write_ha_state()
#
await asyncio.sleep(5)

# try to find battery entities for all related entities
for entity in self.all_entities:
if entity is not None:
battery_id = await find_battery_entity(self, entity)
if battery_id is not None:
self.devices_states[entity] = {
"battery_id": battery_id,
"battery": None,
}

await check_all_entities(self)
# update_hvac_action(self)
# Add listener
Expand Down
6 changes: 2 additions & 4 deletions custom_components/better_thermostat/events/trv.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ async def trigger_trv_change(self, event):
if self.context == event.context:
return

_LOGGER.debug(
f"better_thermostat {self.name}: TRV {entity_id} update received {self.context.id} {event.context.id}"
)
_LOGGER.debug(f"better_thermostat {self.name}: TRV {entity_id} update received")

_org_trv_state = self.hass.states.get(entity_id)
child_lock = self.real_trvs[entity_id]["advanced"].get("child_lock")
Expand Down Expand Up @@ -205,7 +203,7 @@ async def update_hvac_action(self):
hvac_action = None
for trv in self.all_trvs:
if trv["advanced"][CONF_HOMATICIP]:
entity_id = trv["entity_id"]
entity_id = trv["trv"]
state = self.hass.states.get(entity_id)
if state is None:
continue
Expand Down
2 changes: 1 addition & 1 deletion custom_components/better_thermostat/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@
"iot_class": "local_push",
"issue_tracker": "https://github.com/KartoffelToby/better_thermostat/issues",
"requirements": [],
"version": "1.2.0-dev"
"version": "1.2.1-dev"
}
24 changes: 15 additions & 9 deletions custom_components/better_thermostat/utils/controlling.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,13 @@ async def control_trv(self, heater_entity_id=None):
_new_hvac_mode = HVACMode.OFF

# Manage TRVs with no HVACMode.OFF
_no_off_system_mode = HVACMode.OFF not in self.real_trvs[heater_entity_id][
"hvac_modes"
] or self.real_trvs[heater_entity_id]["advanced"].get(
"no_off_system_mode", False
_no_off_system_mode = (
HVACMode.OFF not in self.real_trvs[heater_entity_id]["hvac_modes"]
) or (
self.real_trvs[heater_entity_id]["advanced"].get(
"no_off_system_mode", False
)
is True
)
if _no_off_system_mode is True and _new_hvac_mode == HVACMode.OFF:
_min_temp = self.real_trvs[heater_entity_id]["min_temp"]
Expand All @@ -126,7 +129,10 @@ async def control_trv(self, heater_entity_id=None):
if (
_new_hvac_mode is not None
and _new_hvac_mode != _trv.state
and (_no_off_system_mode is False and _new_hvac_mode != HVACMode.OFF)
and (
(_no_off_system_mode is True and _new_hvac_mode != HVACMode.OFF)
or (_no_off_system_mode is False)
)
):
_LOGGER.debug(
f"better_thermostat {self.name}: TO TRV set_hvac_mode: {heater_entity_id} from: {_trv.state} to: {_new_hvac_mode}"
Expand Down Expand Up @@ -265,10 +271,10 @@ async def check_target_temperature(self, heater_entity_id=None):
self.name,
"check_target_temperature()",
)

_LOGGER.debug(
f"better_thermostat {self.name}: {heater_entity_id} / check_target_temp / _last: {_real_trv['last_temperature']} - _current: {_current_set_temperature}"
)
if _timeout == 0:
_LOGGER.debug(
f"better_thermostat {self.name}: {heater_entity_id} / check_target_temp / _last: {_real_trv['last_temperature']} - _current: {_current_set_temperature}"
)
if (
_current_set_temperature is None
or _real_trv["last_temperature"] == _current_set_temperature
Expand Down
45 changes: 13 additions & 32 deletions custom_components/better_thermostat/utils/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,8 @@ async def find_valve_entity(self, entity_id):
"""
entity_registry = er.async_get(self.hass)
reg_entity = entity_registry.async_get(entity_id)
if reg_entity is None:
return None
entity_entries = async_entries_for_config_entry(
entity_registry, reg_entity.config_entry_id
)
Expand All @@ -479,42 +481,19 @@ async def find_valve_entity(self, entity_id):


async def find_battery_entity(self, entity_id):
"""Find the related battery entity for an entity.
entity_registry = er.async_get(self.hass)

This is a hacky way to find the local calibration entity for the TRV. It is not possible to find the entity
automatically, because the entity_id is not the same as the friendly_name. The friendly_name is the same for all
thermostats of the same brand, but the entity_id is different.
entity_info = entity_registry.entities.get(entity_id)

Parameters
----------
self :
self instance of better_thermostat
if entity_info is None:
return None

Returns
-------
str
the entity_id of the local calibration entity
None
if no local calibration entity was found
"""
entity_registry = er.async_get(self.hass)
reg_entity = entity_registry.async_get(entity_id)
entity_entries = async_entries_for_config_entry(
entity_registry, reg_entity.config_entry_id
)
for entity in entity_entries:
device_class = entity.device_class
# Make sure we use the correct device entities
if entity.device_id == reg_entity.device_id:
if device_class == "battery":
_LOGGER.debug(
f"better thermostat: Found battery level entity {entity.entity_id} for {entity_id}"
)
return entity.entity_id
device_id = entity_info.device_id

for entity in entity_registry.entities.values():
if entity.device_id == device_id and entity.device_class == "battery":
return entity.entity_id

_LOGGER.debug(
f"better thermostat: Could not find battery level entity for {entity_id}"
)
return None


Expand All @@ -539,6 +518,8 @@ async def find_local_calibration_entity(self, entity_id):
"""
entity_registry = er.async_get(self.hass)
reg_entity = entity_registry.async_get(entity_id)
if reg_entity is None:
return None
entity_entries = async_entries_for_config_entry(
entity_registry, reg_entity.config_entry_id
)
Expand Down
41 changes: 22 additions & 19 deletions custom_components/better_thermostat/utils/watcher.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from __future__ import annotations
from homeassistant.helpers import issue_registry as ir

from .helpers import find_battery_entity
from homeassistant.const import STATE_UNAVAILABLE, STATE_UNKNOWN

DOMAIN = "better_thermostat"

Expand All @@ -10,37 +9,41 @@ async def check_entity(self, entity) -> bool:
if entity is None:
return False
entity_states = self.hass.states.get(entity)
if entity_states is None:
return False
state = (
"missing"
if not entity_states
else str(entity_states.state).replace("unavailable", "unavail")
)
if entity_states is None or state in ["missing", "unknown", "unavail"]:
if state in (
STATE_UNAVAILABLE,
STATE_UNKNOWN,
None,
"missing",
"unknown",
"unavail",
):
return False
if entity in self.devices_errors:
self.devices_errors.remove(entity)
self.async_write_ha_state()
ir.async_delete_issue(self.hass, DOMAIN, f"missing_entity_{entity}")
await get_battery_status(self, entity)
self.hass.async_create_task(get_battery_status(self, entity))
return True


async def get_battery_status(self, entity):
entity_states = self.hass.states.get(entity)
if entity_states is None:
return None
battery = entity_states.attributes.get("battery")
if battery is not None:
self.devices_states[entity] = {"battery": battery}
self.async_write_ha_state()
return
else:
battery_entity = await find_battery_entity(self, entity)
if battery_entity is not None:
battery_states = self.hass.states.get(battery_entity)
if battery_states is not None:
battery = battery_states.state
self.devices_states[entity] = {"battery": battery}
if entity in self.devices_states:
battery_id = self.devices_states[entity].get("battery_id")
if battery_id is not None:
new_battery = self.hass.states.get(battery_id)
if new_battery is not None:
battery = new_battery.state
self.devices_states[entity] = {
"battery": battery,
"battery_id": battery_id,
}
self.async_write_ha_state()
return

Expand Down
2 changes: 1 addition & 1 deletion hacs.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Better Thermostat",
"render_readme": true,
"homeassistant": "2021.12.0",
"homeassistant": "2022.8.0",
"hide_default_branch": true
}

0 comments on commit 58d15d3

Please sign in to comment.