Skip to content

Commit

Permalink
Refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
cyr-ius committed Apr 2, 2023
1 parent 72c6740 commit 7d87e11
Showing 1 changed file with 11 additions and 22 deletions.
33 changes: 11 additions & 22 deletions custom_components/heatzy/climate.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
"""Climate sensors for Heatzy."""
import asyncio
import logging
import time

from heatzypy.exception import HeatzyException

#BRAGITMAN: fixed imports
from homeassistant.components.climate import (
ClimateEntity,
ClimateEntityFeature,
HVACMode,
HVACAction,
HVACMode,
)
from homeassistant.components.climate.const import (
ATTR_TARGET_TEMP_HIGH,
Expand Down Expand Up @@ -65,7 +64,7 @@ async def async_setup_entry(
) -> None:
"""Load all Heatzy devices."""
coordinator = hass.data[DOMAIN][entry.entry_id]
entities = []
entities: list[HeatzyThermostat] = []
for unique_id, device in coordinator.data.items():
product_key = device.get(CONF_PRODUCT_KEY)
if product_key in PILOTE_V1:
Expand All @@ -91,16 +90,12 @@ def __init__(self, coordinator: HeatzyDataUpdateCoordinator, unique_id):
super().__init__(coordinator)
self._attr_unique_id = unique_id
self._attr_name = coordinator.data[unique_id][CONF_ALIAS]

@property
def device_info(self):
"""Return the device info."""
return DeviceInfo(
identifiers={(DOMAIN, self.unique_id)},
name=self.name,
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, unique_id)},
name=coordinator.data[unique_id][CONF_ALIAS],
manufacturer=DOMAIN,
sw_version=self.coordinator.data[self.unique_id].get(CONF_VERSION),
model=self.coordinator.data[self.unique_id].get(CONF_MODEL),
sw_version=coordinator.data[unique_id].get(CONF_VERSION),
model=coordinator.data[unique_id].get(CONF_MODEL),
)

@property
Expand Down Expand Up @@ -229,7 +224,6 @@ async def async_turn_on(self) -> str:
"""Turn device on."""
try:
_LOGGER.debug("Turn on %s", self.HA_TO_HEATZY_STATE[PRESET_COMFORT])
#BRAGITMAN: Have to turn off timer and vacation mode first in a separate call to updating Preset Mode. And need a small delay between calls so hopefully Heatzy processes in correct order
await self.coordinator.api.async_control_device(
self.unique_id,
{
Expand All @@ -240,7 +234,7 @@ async def async_turn_on(self) -> str:
}
},
)
time.sleep(2)
await asyncio.sleep(2)
await self.coordinator.api.async_control_device(
self.unique_id,
{
Expand All @@ -256,7 +250,6 @@ async def async_turn_on(self) -> str:
async def async_turn_off(self) -> str:
"""Turn device on."""
try:
#BRAGITMAN: Have to turn off timer and vacation mode first in a separate call to updating Preset Mode. And need a small delay between calls so hopefully Heatzy processes in correct order
await self.coordinator.api.async_control_device(
self.unique_id,
{
Expand All @@ -267,7 +260,7 @@ async def async_turn_off(self) -> str:
}
},
)
time.sleep(2)
await asyncio.sleep(2)
await self.coordinator.api.async_control_device(
self.unique_id,
{
Expand Down Expand Up @@ -302,17 +295,14 @@ async def async_turn_auto(self) -> str:
async def async_set_preset_mode(self, preset_mode: str) -> None:
"""Set new preset mode."""
_attr = self.coordinator.data[self.unique_id].get(CONF_ATTR, {})
#BRAGITMAN: Removed CONF_DEROG_MODE and CONF_DEROG_TIME attributes, these are only included if in Vacation mode
config = {
CONF_ATTRS: {
CONF_MODE: self.HA_TO_HEATZY_STATE.get(preset_mode),
}
}
# If in VACATION mode then as well as setting preset mode we also stop the VACATION mode
if _attr.get(CONF_DEROG_MODE) == 1:
config[CONF_ATTRS].update({CONF_DEROG_MODE: 0,
CONF_DEROG_TIME: 0
})
config[CONF_ATTRS].update({CONF_DEROG_MODE: 0, CONF_DEROG_TIME: 0})
try:
await self.coordinator.api.async_control_device(self.unique_id, config)
await self.coordinator.async_request_refresh()
Expand Down Expand Up @@ -423,7 +413,6 @@ async def async_turn_on(self) -> str:
# When turning ON ensure PROGRAM and VACATION mode are OFF
try:
await self.coordinator.api.async_control_device(
#BRAGITMAN: Removed preset mode from update. When turning on it will now remain the same preset mode. e.g. If HA shows "Off - Eco", turning on will now change it to "On - Eco"
self.unique_id,
{
CONF_ATTRS: {
Expand Down

0 comments on commit 7d87e11

Please sign in to comment.