Skip to content

Commit

Permalink
Merge pull request #32 from radical-squared/additional-fixes-v3-0-2
Browse files Browse the repository at this point in the history
Additional fixes v3.0.2
  • Loading branch information
elad-bar authored May 30, 2023
2 parents 2437be7 + eb9fcd9 commit db428aa
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 15 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
- Add `suggested_unit_of_measurement=UnitOfElectricPotential.VOLT` to sensors of `SensorDeviceClass.VOLTAGE`
- Fix token clean-up
- Code clean up for entities
- HVAC Mode Mapping in v3.0.1 is mixed up [#29](https://github.com/radical-squared/aquatemp/issues/29)
- Block API operations if initial login failed

## v3.0.1

Expand Down
10 changes: 7 additions & 3 deletions custom_components/aqua_temp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from homeassistant.core import HomeAssistant

from .common.consts import ALL_ENTITIES, DOMAIN
from .common.exceptions import LoginError
from .managers.aqua_temp_api import AquaTempAPI
from .managers.aqua_temp_config_manager import AquaTempConfigManager
from .managers.aqua_temp_coordinator import AquaTempCoordinator
Expand All @@ -25,9 +26,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
await config_manager.initialize()

api = AquaTempAPI(hass, config_manager)
await api.initialize()

await api.update()
await api.validate()

coordinator = AquaTempCoordinator(hass, api, config_manager)

Expand All @@ -53,6 +52,11 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:

initialized = True

except LoginError:
_LOGGER.error(
"Failed to login Aqua Temp API, Correct credentials and try again"
)

except Exception as ex:
exc_type, exc_obj, tb = sys.exc_info()
line_number = tb.tb_lineno
Expand Down
6 changes: 6 additions & 0 deletions custom_components/aqua_temp/common/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@
HVACMode.AUTO: MODE_TEMPERATURE_AUTO,
}

HVAC_MODE_ACTION = {
HVACMode.COOL: "0",
HVACMode.HEAT: "1",
HVACMode.AUTO: "2",
}

MANUAL_MUTE_AUTO = "0"
MANUAL_MUTE_LOW = "1"

Expand Down
33 changes: 21 additions & 12 deletions custom_components/aqua_temp/managers/aqua_temp_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
GETFAULT_PATH,
HEADERS,
HTTP_HEADER_X_TOKEN,
HVAC_MODE_ACTION,
HVAC_MODE_MAPPING,
LOGIN_PATH,
POWER_MODE_OFF,
Expand Down Expand Up @@ -103,12 +104,14 @@ async def update(self):
if not self.is_connected:
await self.initialize()

for device_code in self.data:
_LOGGER.debug(f"Starting to update device: {device_code}")
if self.is_connected:
for device_code in self.data:
_LOGGER.debug(f"Starting to update device: {device_code}")

await self._fetch_data(device_code)
await self._fetch_data(device_code)

await self._fetch_errors(device_code)

await self._fetch_errors(device_code)
except Exception as ex:
self.set_token()

Expand Down Expand Up @@ -160,7 +163,7 @@ async def _set_hvac_mode(self, device_code: str, hvac_mode):
if hvac_mode == HVAC_MODE_OFF:
return

value = HVAC_MODE_MAPPING.get(device_code, hvac_mode)
value = HVAC_MODE_ACTION.get(device_code, hvac_mode)

request_data = {"mode": value}

Expand Down Expand Up @@ -233,17 +236,23 @@ async def _login(self):

data = {"user_name": username, "password": password, "type": "2"}

login_response = await self._post_request(LOGIN_PATH, data)
object_result = login_response.get("object_result", {})
try:
login_response = await self._post_request(LOGIN_PATH, data)
object_result = login_response.get("object_result", {})

for key in object_result:
self.data[key] = object_result[key]

for key in object_result:
self.data[key] = object_result[key]
token = object_result.get(HTTP_HEADER_X_TOKEN)

token = object_result.get(HTTP_HEADER_X_TOKEN)
self.set_token(token)

self.set_token(token)
await self._update_device_code()

except Exception as ex:
self.set_token()

await self._update_device_code()
_LOGGER.error(f"Failed to login, Error: {ex}")

async def _update_device_code(self):
data = {}
Expand Down
1 change: 1 addition & 0 deletions custom_components/aqua_temp/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ def __init__(
self._attr_device_info = device_info
self._attr_name = entity_name
self._attr_unique_id = unique_id
self._attr_current_option = coordinator.get_temperature_unit(self._device_code)

async def async_select_option(self, option: str) -> None:
"""Change the selected option."""
Expand Down

0 comments on commit db428aa

Please sign in to comment.