Skip to content

Commit

Permalink
Merge pull request #411 from kylegordon/use-better-thermostat
Browse files Browse the repository at this point in the history
Better Thermostat
  • Loading branch information
kylegordon authored Apr 5, 2023
2 parents 2d9ee58 + 7628562 commit da87034
Show file tree
Hide file tree
Showing 36 changed files with 5,607 additions and 1 deletion.
88 changes: 88 additions & 0 deletions custom_components/better_thermostat/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
"""The better_thermostat component."""
import logging
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant, Config
from homeassistant.config_entries import ConfigEntry

from .const import (
CONF_FIX_CALIBRATION,
CONF_CALIBRATION_MODE,
CONF_HEATER,
CONF_NO_SYSTEM_MODE_OFF,
CONF_WINDOW_TIMEOUT,
)

_LOGGER = logging.getLogger(__name__)

DOMAIN = "better_thermostat"
PLATFORMS = [Platform.CLIMATE]


async def async_setup(hass: HomeAssistant, config: Config):
"""Set up this integration using YAML is not supported."""
return True


async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
entry.async_on_unload(entry.add_update_listener(config_entry_update_listener))
return True


async def config_entry_update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None:
"""Handle options update."""
await hass.config_entries.async_reload(entry.entry_id)
await async_unload_entry(hass, entry)
await async_setup_entry(hass, entry)


async def async_unload_entry(hass, entry):
"""Unload a config entry."""
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
return unload_ok


async def async_reload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> None:
await async_unload_entry(hass, config_entry)
await async_setup_entry(hass, config_entry)


async def async_migrate_entry(hass, config_entry: ConfigEntry):
"""Migrate old entry."""
_LOGGER.debug("Migrating from version %s", config_entry.version)
if config_entry.version == 1:
new = {**config_entry.data}
for trv in new[CONF_HEATER]:
trv["advanced"].update({CONF_FIX_CALIBRATION: False})
config_entry.version = 2
hass.config_entries.async_update_entry(config_entry, data=new)

if config_entry.version == 2:
new = {**config_entry.data}
new[CONF_WINDOW_TIMEOUT] = 0
config_entry.version = 3
hass.config_entries.async_update_entry(config_entry, data=new)

if config_entry.version == 3:
new = {**config_entry.data}
for trv in new[CONF_HEATER]:
if (
CONF_FIX_CALIBRATION in trv["advanced"]
and trv["advanced"][CONF_FIX_CALIBRATION]
):
trv["advanced"].update({CONF_CALIBRATION_MODE: CONF_FIX_CALIBRATION})
else:
trv["advanced"].update({CONF_CALIBRATION_MODE: "default"})
config_entry.version = 4
hass.config_entries.async_update_entry(config_entry, data=new)

if config_entry.version == 4:
new = {**config_entry.data}
for trv in new[CONF_HEATER]:
trv["advanced"].update({CONF_NO_SYSTEM_MODE_OFF: False})
config_entry.version = 5
hass.config_entries.async_update_entry(config_entry, data=new)

_LOGGER.info("Migration to version %s successful", config_entry.version)

return True
67 changes: 67 additions & 0 deletions custom_components/better_thermostat/adapters/deconz.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import logging
from .generic import (
set_temperature as generic_set_temperature,
set_hvac_mode as generic_set_hvac_mode,
)

_LOGGER = logging.getLogger(__name__)


async def get_info(self, entity_id):
"""Get info from TRV."""
_offset = self.hass.states.get(entity_id).attributes.get("offset", None)
if _offset is None:
return {"support_offset": False, "support_valve": False}
return {"support_offset": True, "support_valve": False}


async def init(self, entity_id):
return None


async def set_temperature(self, entity_id, temperature):
"""Set new target temperature."""
return await generic_set_temperature(self, entity_id, temperature)


async def set_hvac_mode(self, entity_id, hvac_mode):
"""Set new target hvac mode."""
return await generic_set_hvac_mode(self, entity_id, hvac_mode)


async def get_current_offset(self, entity_id):
"""Get current offset."""
return float(str(self.hass.states.get(entity_id).attributes.get("offset", 0)))


async def get_offset_steps(self, entity_id):
"""Get offset steps."""
return float(1.0)


async def get_min_offset(self, entity_id):
"""Get min offset."""
return -6


async def get_max_offset(self, entity_id):
"""Get max offset."""
return 6


async def set_offset(self, entity_id, offset):
"""Set new target offset."""
await self.hass.services.async_call(
"deconz",
"configure",
{"entity_id": entity_id, "offset": offset},
blocking=True,
limit=None,
context=self._context,
)
self.real_trvs[entity_id]["last_calibration"] = offset


async def set_valve(self, entity_id, valve):
"""Set new target valve."""
return None
66 changes: 66 additions & 0 deletions custom_components/better_thermostat/adapters/generic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import logging

_LOGGER = logging.getLogger(__name__)


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


async def init(self, entity_id):
return None


async def get_current_offset(self, entity_id):
"""Get current offset."""
return None


async def get_offset_steps(self, entity_id):
"""Get offset steps."""
return None


async def get_min_offset(self, entity_id):
"""Get min offset."""
return -6


async def get_max_offset(self, entity_id):
"""Get max offset."""
return 6


async def set_temperature(self, entity_id, temperature):
"""Set new target temperature."""
await self.hass.services.async_call(
"climate",
"set_temperature",
{"entity_id": entity_id, "temperature": temperature},
blocking=True,
limit=None,
context=self._context,
)


async def set_hvac_mode(self, entity_id, hvac_mode):
"""Set new target hvac mode."""
await self.hass.services.async_call(
"climate",
"set_hvac_mode",
{"entity_id": entity_id, "hvac_mode": hvac_mode},
blocking=True,
limit=None,
context=self._context,
)


async def set_offset(self, entity_id, offset):
"""Set new target offset."""
return # Not supported


async def set_valve(self, entity_id, valve):
"""Set new target valve."""
return # Not supported
Loading

0 comments on commit da87034

Please sign in to comment.