Skip to content

Commit

Permalink
[TASK] fixing
Browse files Browse the repository at this point in the history
  • Loading branch information
KartoffelToby committed Oct 7, 2022
1 parent f64ce30 commit cd4c312
Show file tree
Hide file tree
Showing 11 changed files with 128 additions and 167 deletions.
16 changes: 5 additions & 11 deletions blueprints/night_mode.yaml
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
blueprint:
name: BT Night mode
name: Better Thermostat Night mode
description: Set BT Thermostats to night mode if Schedule event is active.
domain: automation
source_url: https://github.com/KartoffelToby/better_thermostat/blob/master/blueprints/night_mode.yaml
input:
night_times_schedule:
name: Schedule helper
selector:
target:
entity:
domain: schedule
device:
integration: schedule
entity:
domain: schedule

thermostat_target:
name: Thermostats
Expand All @@ -33,19 +30,16 @@ blueprint:
max: 35
unit_of_measurement: °C


mode: queued
max_exceeded: silent

trigger:
- platform: state
entity_id:
- !input night_times_schedule
entity_id: !input night_times_schedule
from: "on"
to: "off"
- platform: state
entity_id:
- !input night_times_schedule
entity_id: !input night_times_schedule
from: "off"
to: "on"
condition: []
Expand Down
30 changes: 14 additions & 16 deletions custom_components/better_thermostat/adapters/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ def get_info():
return {"support_offset": False, "support_valve": False}


async def init(self):
return None


async def set_temperature(self, temperature):
"""Set new target temperature."""
_LOGGER.info(
Expand All @@ -20,37 +24,31 @@ async def set_temperature(self, temperature):
"set_temperature",
{"entity_id": self.heater_entity_id, "temperature": temperature},
blocking=True,
limit=None,
context=self._context,
)
await asyncio.sleep(1)


async def set_hvac_mode(self, hvac_mode):
"""Set new target hvac mode."""
_LOGGER.info(f"better_thermostat {self.name}: TO TRV set_hvac_mode: {hvac_mode}")
await self.hass.services.async_call(
"climate",
"set_hvac_mode",
{"entity_id": self.heater_entity_id, "hvac_mode": hvac_mode},
blocking=True,
context=self._context,
)
if hvac_mode == HVAC_MODE_OFF:
await self.hass.services.async_call(
"climate",
"turn_off",
{"entity_id": self.heater_entity_id},
blocking=True,
limit=None,
context=self._context,
)
else:
await self.hass.services.async_call(
"climate",
"turn_on",
{"entity_id": self.heater_entity_id},
blocking=True,
context=self._context,
)
await self.hass.services.async_call(
"climate",
"set_hvac_mode",
{"entity_id": self.heater_entity_id, "hvac_mode": hvac_mode},
blocking=True,
limit=None,
context=self._context,
)
await asyncio.sleep(2)


Expand Down
25 changes: 15 additions & 10 deletions custom_components/better_thermostat/adapters/mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ def get_info():
return {"support_offset": True, "support_valve": True}


async def init(self):
if self.local_temperature_calibration_entity is None and self.calibration_type == 0:
self.local_temperature_calibration_entity = await find_local_calibration_entity(
self
)
_LOGGER.info(
"better_thermostat %s: uses local calibration entity %s",
self.name,
self.local_temperature_calibration_entity,
)


async def set_temperature(self, temperature):
"""Set new target temperature."""
return await generic_set_temperature(self, temperature)
Expand All @@ -27,23 +39,14 @@ async def set_hvac_mode(self, hvac_mode):

async def set_offset(self, offset):
"""Set new target offset."""
_LOGGER.info(f"better_thermostat {self.name}: TO TRV set_offset: {offset}")
if self.local_temperature_entity is None:
self.local_temperature_calibration_entity = await find_local_calibration_entity(
self
)
_LOGGER.info(
"better_thermostat %s: uses local calibration entity %s",
self.name,
self.local_temperature_calibration_entity,
)
current_calibration = self.hass.states.get(
self.local_temperature_calibration_entity
).state
if current_calibration != offset and (
(self._last_calibration + timedelta(minutes=5)).timestamp()
< datetime.now().timestamp()
):
_LOGGER.info(f"better_thermostat {self.name}: TO TRV set_offset: {offset}")
max_calibration = self.hass.states.get(
self.local_temperature_calibration_entity
).attributes.get("max", 127)
Expand All @@ -59,6 +62,7 @@ async def set_offset(self, offset):
SERVICE_SET_VALUE,
{"entity_id": self.local_temperature_calibration_entity, "value": offset},
blocking=True,
limit=None,
context=self._context,
)
self._last_calibration = datetime.now()
Expand All @@ -76,5 +80,6 @@ async def set_valve(self, valve):
SERVICE_SET_VALUE,
{"entity_id": self.valve_position_entity, "value": valve},
blocking=True,
limit=None,
context=self._context,
)
36 changes: 4 additions & 32 deletions custom_components/better_thermostat/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from random import randint

from .utils.weather import check_ambient_air_temperature
from .utils.bridge import load_adapter
from .utils.bridge import init, load_adapter
from .utils.helpers import convert_to_float, get_trv_intigration, mode_remap
from homeassistant.helpers import entity_platform

Expand All @@ -26,7 +26,7 @@
STATE_UNAVAILABLE,
STATE_UNKNOWN,
)
from homeassistant.core import callback, CoreState, Event
from homeassistant.core import callback, CoreState
from homeassistant.helpers.event import (
async_track_state_change_event,
async_track_time_change,
Expand Down Expand Up @@ -267,18 +267,6 @@ async def async_added_to_hass(self):
-------
None
"""

@callback
def async_state_changed_listener(event: Event) -> None:
"""Handle child updates."""
self.async_set_context(event.context)
self.async_write_ha_state()

self.async_on_remove(
async_track_state_change_event(
self.hass, self.heater_entity_id, async_state_changed_listener
)
)
await super().async_added_to_hass()

_LOGGER.info(
Expand Down Expand Up @@ -444,9 +432,6 @@ async def startup(self):
window = self.hass.states.get(self.window_id)

check = window.state
_LOGGER.debug(
"better_thermostat %s: window sensor state is %s", self.name, check
)
if check in ("on", "open", "true"):
self.window_open = True
else:
Expand All @@ -466,8 +451,6 @@ async def startup(self):
self._config["has_system_mode"] = has_system_mode
# Check If we have an old state
old_state = await self.async_get_last_state()
_LOGGER.debug(old_state)

if old_state is not None:
# If we have no initial temperature, restore
# If we have a previously saved temperature
Expand Down Expand Up @@ -552,11 +535,13 @@ async def startup(self):
self._available = True
self.startup_running = False
self.async_write_ha_state()
await init(self)
await self._trigger_time(None)
await self.control_queue_task.put(self)
# Add listener
if self.outdoor_sensor is not None:
async_track_time_change(self.hass, self._trigger_time, 5, 0, 0)

async_track_state_change_event(
self.hass, [self.sensor_entity_id], self._trigger_temperature_change
)
Expand Down Expand Up @@ -694,27 +679,14 @@ def hvac_mode(self):
@property
def hvac_action(self):
"""Return the current HVAC action"""

if self._bt_hvac_mode == HVAC_MODE_OFF:
_LOGGER.debug(
f"better_thermostat {self.name}: HA asked for our HVAC action, we will respond with: {CURRENT_HVAC_OFF}"
)
return CURRENT_HVAC_OFF
if self._bt_hvac_mode == HVAC_MODE_HEAT:
if self.window_open:
_LOGGER.debug(
f"better_thermostat {self.name}: HA asked for our HVAC action, we will respond with '{CURRENT_HVAC_OFF}' because a window is open"
)
return CURRENT_HVAC_OFF

if self.call_for_heat is False:
_LOGGER.debug(
f"better_thermostat {self.name}: HA asked for our HVAC action, we will respond with '{CURRENT_HVAC_IDLE}' since call for heat is false"
)
return CURRENT_HVAC_IDLE
_LOGGER.debug(
f"better_thermostat {self.name}: HA asked for our HVAC action, we will respond with: {CURRENT_HVAC_HEAT}"
)
return CURRENT_HVAC_HEAT

@property
Expand Down
5 changes: 0 additions & 5 deletions custom_components/better_thermostat/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ def async_get_options_flow(
async def async_step_confirm(self, user_input=None, confirm_type=None):
"""Handle user-confirmation of discovered node."""
errors = {}
_LOGGER.debug(user_input)
if user_input is not None:
_LOGGER.debug(self.data)
if self.data is not None:
await self.async_set_unique_id(self.data["name"])
return self.async_create_entry(title=self.data["name"], data=self.data)
Expand Down Expand Up @@ -267,9 +265,6 @@ async def async_step_user(self, user_input=None):
self.updated_config[CONF_CALIBRATION] = user_input.get(CONF_CALIBRATION)
self.updated_config[CONF_CHILD_LOCK] = user_input.get(CONF_CHILD_LOCK)
self.updated_config[CONF_HOMATICIP] = user_input.get(CONF_HOMATICIP)

_LOGGER.debug("Updated config: %s", self.updated_config)

self.hass.config_entries.async_update_entry(
self.config_entry, data=self.updated_config
)
Expand Down
5 changes: 0 additions & 5 deletions custom_components/better_thermostat/events/trv.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ async def trigger_trv_change(self, event):
_updated_needed = False
child_lock = False
if self.startup_running:
_LOGGER.debug(
f"better_thermostat {self.name}: skipping trigger_trv_change because startup is running"
)
return

old_state = event.data.get("old_state")
Expand Down Expand Up @@ -145,14 +142,12 @@ async def trigger_trv_change(self, event):
_LOGGER.info(f"better_thermostat {self.name}: TRV update received")
self.async_write_ha_state()
# make sure we only update the latest user interaction
"""
try:
if self.control_queue_task.qsize() > 0:
while self.control_queue_task.qsize() > 1:
self.control_queue_task.task_done()
except AttributeError:
pass
"""
await self.control_queue_task.put(self)


Expand Down
3 changes: 2 additions & 1 deletion custom_components/better_thermostat/events/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging

from homeassistant.core import callback
from homeassistant.const import STATE_OFF

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -70,7 +71,7 @@ async def window_queue(self):
await asyncio.sleep(self.window_delay)
# remap off on to true false
current_window_state = True
if self.hass.states.get(self.window_id).state == "off":
if self.hass.states.get(self.window_id).state == STATE_OFF:
current_window_state = False
# make sure the current state is the suggested change state to prevent a false positive:
if current_window_state == window_event_to_process:
Expand Down
18 changes: 16 additions & 2 deletions custom_components/better_thermostat/utils/bridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

def load_adapter(self):
"""Load adapter."""
if self.integration == "generic_thermostat":
self.integration = "generic"

try:
self.adapter = import_module(
"custom_components.better_thermostat.adapters." + self.integration,
Expand All @@ -14,12 +17,23 @@ def load_adapter(self):
_LOGGER.info(
"better_thermostat %s: uses adapter %s", self.name, self.integration
)
except ImportError:
except Exception:
self.adapter = import_module(
"custom_components.better_thermostat.adapters.generic",
package="better_thermostat",
)
_LOGGER.info("better_thermostat %s: uses adapter %s", self.name, "generic")
_LOGGER.error(
"better_thermostat %s: intigration: %s isn't native supported, feel free to open an issue, fallback adapter %s",
self.name,
self.integration,
"generic",
)
pass


async def init(self):
"""Init adapter."""
return await self.adapter.init(self)


async def set_temperature(self, temperature):
Expand Down
Loading

0 comments on commit cd4c312

Please sign in to comment.