Skip to content

Commit

Permalink
Auth fix, use new purifier control endpoints, raise exception if cont…
Browse files Browse the repository at this point in the history
…rol command fails

Auth fix, use new purifier control endpoints, raise exception if control command fails
  • Loading branch information
RobertD502 authored May 22, 2024
2 parents b33734b + 1cd7c9b commit 46f6102
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 34 deletions.
53 changes: 40 additions & 13 deletions custom_components/coway/fan.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from typing import Any
import asyncio

from cowayaio.exceptions import CowayError
from cowayaio.purifier_model import CowayPurifier

from homeassistant.components.fan import FanEntity, FanEntityFeature
Expand Down Expand Up @@ -141,7 +142,6 @@ def preset_mode(self) -> str:
if self.purifier_data.rapid_mode:
return PRESET_MODE_RAPID
else:
# if self.purifier_data.auto_eco_mode or self.purifier_data.auto_mode:
if self.purifier_data.auto_eco_mode:
return PRESET_MODE_AUTO_ECO
if self.purifier_data.auto_mode:
Expand Down Expand Up @@ -194,9 +194,12 @@ async def async_turn_on(self, percentage: int = None, preset_mode: str = None, *

if percentage is not None:
speed = HASS_FAN_SPEED_TO_IOCARE.get(percentage)
await self.coordinator.client.async_set_power(self.purifier_data.device_attr, True)
await asyncio.sleep(1.5)
await self.coordinator.client.async_set_fan_speed(self.purifier_data.device_attr, speed)
try:
await self.coordinator.client.async_set_power(self.purifier_data.device_attr, True)
await asyncio.sleep(2)
await self.coordinator.client.async_set_fan_speed(self.purifier_data.device_attr, speed)
except CowayError as ex:
raise HomeAssistantError(ex)
self.purifier_data.is_on = True
self.purifier_data.light_on = True
self.purifier_data.auto_mode = False
Expand All @@ -206,7 +209,10 @@ async def async_turn_on(self, percentage: int = None, preset_mode: str = None, *
await asyncio.sleep(1.5)
await self.coordinator.async_request_refresh()
else:
await self.coordinator.client.async_set_power(self.purifier_data.device_attr, True)
try:
await self.coordinator.client.async_set_power(self.purifier_data.device_attr, True)
except CowayError as ex:
raise HomeAssistantError(ex)
self.purifier_data.is_on = True
self.purifier_data.light_on = True
self.async_write_ha_state()
Expand All @@ -216,7 +222,10 @@ async def async_turn_on(self, percentage: int = None, preset_mode: str = None, *
async def async_turn_off(self, **kwargs) -> None:
"""Turn the air purifier off."""

await self.coordinator.client.async_set_power(self.purifier_data.device_attr, False)
try:
await self.coordinator.client.async_set_power(self.purifier_data.device_attr, False)
except CowayError as ex:
raise HomeAssistantError(ex)
self.purifier_data.is_on = False
self.purifier_data.light_on = False
self.async_write_ha_state()
Expand All @@ -233,7 +242,10 @@ async def async_set_percentage(self, percentage: int) -> None:
await self.async_turn_on(percentage)
else:
speed = HASS_FAN_SPEED_TO_IOCARE.get(percentage)
await self.coordinator.client.async_set_fan_speed(self.purifier_data.device_attr, speed)
try:
await self.coordinator.client.async_set_fan_speed(self.purifier_data.device_attr, speed)
except CowayError as ex:
raise HomeAssistantError(ex)
self.purifier_data.fan_speed = speed
self.purifier_data.auto_mode = False
self.purifier_data.night_mode = False
Expand All @@ -249,29 +261,44 @@ async def async_set_preset_mode(self, preset_mode: str) -> None:
f'Preset mode {PRESET_MODE_AUTO_ECO} cannot be manually selected. It is only used to indentify when a purifier is in this mode.'
)
if not self.is_on:
await self.coordinator.client.async_set_power(self.purifier_data.device_attr, True)
try:
await self.coordinator.client.async_set_power(self.purifier_data.device_attr, True)
except CowayError as ex:
raise HomeAssistantError(ex)
self.purifier_data.is_on = True
self.purifier_data.light_on = True
await asyncio.sleep(1.5)
await asyncio.sleep(2)
if preset_mode == PRESET_MODE_AUTO:
await self.coordinator.client.async_set_auto_mode(self.purifier_data.device_attr)
try:
await self.coordinator.client.async_set_auto_mode(self.purifier_data.device_attr)
except CowayError as ex:
raise HomeAssistantError(ex)
self.purifier_data.auto_mode = True
self.purifier_data.auto_eco_mode = False
self.purifier_data.eco_mode = False
self.purifier_data.night_mode = False
self.purifier_data.fan_speed = IOCARE_FAN_LOW
if preset_mode in [PRESET_MODE_NIGHT, PRESET_MODE_ECO]:
if self.purifier_data.device_attr['model'] == "AIRMEGA AP-1512HHS":
await self.coordinator.client.async_set_eco_mode(self.purifier_data.device_attr)
try:
await self.coordinator.client.async_set_eco_mode(self.purifier_data.device_attr)
except CowayError as ex:
raise HomeAssistantError(ex)
self.purifier_data.eco_mode = True
else:
await self.coordinator.client.async_set_night_mode(self.purifier_data.device_attr)
try:
await self.coordinator.client.async_set_night_mode(self.purifier_data.device_attr)
except CowayError as ex:
raise HomeAssistantError(ex)
self.purifier_data.auto_eco_mode = False
self.purifier_data.night_mode = True
self.purifier_data.auto_mode = False
self.purifier_data.fan_speed = IOCARE_FAN_OFF
if preset_mode == PRESET_MODE_RAPID:
await self.coordinator.client.async_set_rapid_mode(self.purifier_data.device_attr)
try:
await self.coordinator.client.async_set_rapid_mode(self.purifier_data.device_attr)
except CowayError as ex:
raise HomeAssistantError(ex)
self.purifier_data.auto_mode = False
self.purifier_data.night_mode = False
self.purifier_data.rapid_mode = True
Expand Down
8 changes: 5 additions & 3 deletions custom_components/coway/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
"integration_type": "hub",
"iot_class": "cloud_polling",
"issue_tracker": "https://github.com/RobertD502/home-assistant-iocare/issues",
"loggers": [
"cowayaio"
],
"requirements": [
"cowayaio==0.1.11"
"cowayaio==0.1.12"
],
"loggers": ["cowayaio"],
"version": "0.4.2"
"version": "0.4.3"
}
33 changes: 22 additions & 11 deletions custom_components/coway/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
import asyncio
from typing import Any

from cowayaio.exceptions import CowayError
from cowayaio.purifier_model import CowayPurifier

from homeassistant.components.select import SelectEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.entity import EntityCategory
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity
Expand All @@ -24,7 +26,6 @@
IOCARE_SMART_SENSITIVITY_TO_HASS,
IOCARE_TIMERS,
IOCARE_TIMERS_TO_HASS,
LOGGER,
)
from .coordinator import CowayDataUpdateCoordinator

Expand Down Expand Up @@ -134,13 +135,14 @@ async def async_select_option(self, option: str) -> None:

if self.purifier_data.is_on:
time = HASS_TIMER_TO_IOCARE.get(option)
await self.coordinator.client.async_set_timer(self.purifier_data.device_attr, time)
try:
await self.coordinator.client.async_set_timer(self.purifier_data.device_attr, time)
except CowayError as ex:
raise HomeAssistantError(ex)
self.purifier_data.timer = time
self.purifier_data.timer_remaining = time
else:
LOGGER.error(f'Setting a timer for {self.purifier_data.device_attr["name"]} can only be done when the purifier is On.')
self.purifier_data.timer = '0'
self.purifier_data.timer_remaining = '0'
raise HomeAssistantError(f'Setting a timer for {self.purifier_data.device_attr["name"]} can only be done when the purifier is On.')

self.async_write_ha_state()
await asyncio.sleep(1.5)
Expand Down Expand Up @@ -227,10 +229,13 @@ async def async_select_option(self, option: str) -> None:

if self.purifier_data.is_on:
wash_frequency = HASS_PRE_FILTER_TO_IOCARE.get(option)
await self.coordinator.client.async_change_prefilter_setting(self.purifier_data.device_attr, str(wash_frequency))
try:
await self.coordinator.client.async_change_prefilter_setting(self.purifier_data.device_attr, wash_frequency)
except CowayError as ex:
raise HomeAssistantError(ex)
self.purifier_data.pre_filter_change_frequency = wash_frequency
else:
LOGGER.error(f'Setting a pre-filter wash frequency for {self.purifier_data.device_attr["name"]} can only be done when the purifier is On.')
raise HomeAssistantError(f'Setting a pre-filter wash frequency for {self.purifier_data.device_attr["name"]} can only be done when the purifier is On.')

self.async_write_ha_state()
await asyncio.sleep(1.5)
Expand Down Expand Up @@ -317,10 +322,13 @@ async def async_select_option(self, option: str) -> None:

if self.purifier_data.is_on:
sensitivity = HASS_SMART_SENSITIVITY_TO_IOCARE.get(option)
await self.coordinator.client.async_set_smart_mode_sensitivity(self.purifier_data.device_attr, str(sensitivity))
try:
await self.coordinator.client.async_set_smart_mode_sensitivity(self.purifier_data.device_attr, str(sensitivity))
except CowayError as ex:
raise HomeAssistantError(ex)
self.purifier_data.smart_mode_sensitivity = sensitivity
else:
LOGGER.error(f'Setting smart mode sensitivity for {self.purifier_data.device_attr["name"]} can only be done when the purifier is On.')
raise HomeAssistantError(f'Setting smart mode sensitivity for {self.purifier_data.device_attr["name"]} can only be done when the purifier is On.')

self.async_write_ha_state()
await asyncio.sleep(1.5)
Expand Down Expand Up @@ -403,10 +411,13 @@ async def async_select_option(self, option: str) -> None:

if self.purifier_data.is_on:
mode = HASS_LIGHT_MODE_TO_IOCARE.get(option)
await self.coordinator.client.async_set_light_mode(self.purifier_data.device_attr, mode)
try:
await self.coordinator.client.async_set_light_mode(self.purifier_data.device_attr, mode)
except CowayError as ex:
raise HomeAssistantError(ex)
self.purifier_data.light_mode = mode
else:
LOGGER.error(f'Setting light mode for {self.purifier_data.device_attr["name"]} can only be done when the purifier is On.')
raise HomeAssistantError(f'Setting light mode for {self.purifier_data.device_attr["name"]} can only be done when the purifier is On.')

self.async_write_ha_state()
await asyncio.sleep(1.5)
Expand Down
20 changes: 13 additions & 7 deletions custom_components/coway/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@
import asyncio
from typing import Any

from cowayaio.exceptions import CowayError
from cowayaio.purifier_model import CowayPurifier

from homeassistant.components.switch import SwitchEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity

from .const import COWAY_COORDINATOR, DOMAIN, LOGGER
from .const import COWAY_COORDINATOR, DOMAIN
from .coordinator import CowayDataUpdateCoordinator


Expand Down Expand Up @@ -96,11 +98,13 @@ async def async_turn_on(self, **kwargs) -> None:
"""Turn the switch on."""

if self.purifier_data.is_on:
await self.coordinator.client.async_set_light(self.purifier_data.device_attr, True)
try:
await self.coordinator.client.async_set_light(self.purifier_data.device_attr, True)
except CowayError as ex:
raise HomeAssistantError(ex)
self.purifier_data.light_on = True
else:
LOGGER.error(f'{self.purifier_data.device_attr["name"]} light can only be controlled when the purifier is On.')
self.purifier_data.light_on = False
raise HomeAssistantError(f'{self.purifier_data.device_attr["name"]} light can only be controlled when the purifier is On.')

self.async_write_ha_state()
await asyncio.sleep(1.5)
Expand All @@ -110,11 +114,13 @@ async def async_turn_off(self, **kwargs) -> None:
"""Turn the switch off."""

if self.purifier_data.is_on:
await self.coordinator.client.async_set_light(self.purifier_data.device_attr, False)
try:
await self.coordinator.client.async_set_light(self.purifier_data.device_attr, False)
except CowayError as ex:
raise HomeAssistantError(ex)
self.purifier_data.light_on = False
else:
LOGGER.error(f'{self.purifier_data.device_attr["name"]} light can only be controlled when the purifier is On.')
self.purifier_data.light_on = False
raise HomeAssistantError(f'{self.purifier_data.device_attr["name"]} light can only be controlled when the purifier is On.')

self.async_write_ha_state()
await asyncio.sleep(1.5)
Expand Down

0 comments on commit 46f6102

Please sign in to comment.