Skip to content

Commit

Permalink
Merge pull request #48 from iprak/force-plasma-off-in-custom-modes
Browse files Browse the repository at this point in the history
Force plasma off in custom modes
  • Loading branch information
iprak committed Feb 23, 2023
2 parents e49d4ef + 541adaa commit 012f699
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 6 deletions.
12 changes: 6 additions & 6 deletions custom_components/winix/device_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,20 +174,20 @@ async def async_auto(self) -> None:
self._logger.debug("%s: Setting auto mode", self._alias)
await self._driver.auto()

async def async_plasmawave_on(self) -> None:
async def async_plasmawave_on(self, force: bool = False) -> None:
"""Turn on plasma wave."""

if not self._plasma_on:
if force or not self._plasma_on:
self._plasma_on = True
self._state[ATTR_PLASMA] = ON_VALUE

self._logger.debug("%s: Turning plasmawave on", self._alias)
await self._driver.plasmawave_on()

async def async_plasmawave_off(self) -> None:
async def async_plasmawave_off(self, force: bool = False) -> None:
"""Turn off plasma wave."""

if self._plasma_on:
if force or self._plasma_on:
self._plasma_on = False
self._state[ATTR_PLASMA] = OFF_VALUE

Expand Down Expand Up @@ -261,10 +261,10 @@ async def async_set_preset_mode(self, preset_mode: str) -> None:
await self.async_plasmawave_on()
elif preset_mode == PRESET_MODE_AUTO_PLASMA_OFF:
await self.async_auto()
await self.async_plasmawave_off()
await self.async_plasmawave_off(True)
elif preset_mode == PRESET_MODE_MANUAL:
await self.async_manual()
await self.async_plasmawave_on()
elif preset_mode == PRESET_MODE_MANUAL_PLASMA_OFF:
await self.async_manual()
await self.async_plasmawave_off()
await self.async_plasmawave_off(True)
7 changes: 7 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from unittest.mock import AsyncMock, MagicMock, Mock

from homeassistant import loader
from homeassistant.components.sensor import SensorEntityDescription, SensorStateClass
import pytest

Expand Down Expand Up @@ -61,3 +62,9 @@ def mock_driver_with_payload(request) -> WinixDriver:

device_id = "device_1"
yield WinixDriver(device_id, client)


@pytest.fixture
def enable_custom_integrations(hass):
"""Enable custom integrations defined in the test dir."""
hass.data.pop(loader.DATA_CUSTOM_COMPONENTS)
85 changes: 85 additions & 0 deletions tests/test_config_flow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
"""Test config flow."""
from unittest.mock import AsyncMock, patch

from homeassistant import config_entries, data_entry_flow
from homeassistant.config_entries import SOURCE_USER
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType

from custom_components.winix.const import WINIX_AUTH_RESPONSE, WINIX_DOMAIN
from custom_components.winix.helpers import WinixException
from winix import WinixAccount, auth

TEST_USER_DATA = {
CONF_USERNAME: "user_name",
CONF_PASSWORD: "password",
}

LOGIN_AUTH_RESPONSE = {
"user_id": "test_userid",
"access_token": "AccessToken",
"refresh_token": "RefreshToken",
"id_token": "IdToken",
}


async def test_form(hass: HomeAssistant, enable_custom_integrations) -> None:
"""Test that form shows up."""

result = await hass.config_entries.flow.async_init(
WINIX_DOMAIN, context={"source": SOURCE_USER}
)
assert result["type"] == data_entry_flow.FlowResultType.FORM
assert result["errors"] == {}


async def test_invalid_user(hass: HomeAssistant, enable_custom_integrations) -> None:
"""Test user validation in form."""

with patch(
"custom_components.winix.Helpers.async_login",
side_effect=WinixException(
{"result_code": "UserNotFoundException", "message": "User not found"}
),
):
result = await hass.config_entries.flow.async_init(
WINIX_DOMAIN, context={"source": SOURCE_USER}, data=TEST_USER_DATA
)
assert result["errors"]["base"] == "invalid_user"
assert result["step_id"] == "user"
assert result["type"] == data_entry_flow.FlowResultType.FORM


async def test_invalid_authentication(
hass: HomeAssistant, enable_custom_integrations
) -> None:
"""Test user authentication in form."""

with patch(
"custom_components.winix.Helpers.async_login",
side_effect=WinixException(
{"result_code": "failure", "message": "Authentication failed"}
),
):
result = await hass.config_entries.flow.async_init(
WINIX_DOMAIN, context={"source": SOURCE_USER}, data=TEST_USER_DATA
)

assert result["errors"]["base"] == "invalid_auth"
assert result["step_id"] == "user"
assert result["type"] == data_entry_flow.FlowResultType.FORM


async def test_create_entry(hass: HomeAssistant, enable_custom_integrations) -> None:
"""Test that entry is created."""

with patch(
"custom_components.winix.Helpers.async_login",
side_effect=AsyncMock(return_value=LOGIN_AUTH_RESPONSE),
):
result = await hass.config_entries.flow.async_init(
WINIX_DOMAIN, context={"source": SOURCE_USER}, data=TEST_USER_DATA
)

assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY

0 comments on commit 012f699

Please sign in to comment.