Update requirement version and add switcher_kis services (#23477)
* Update aioswitcher requirement to 2019.4.26. * Removed unnecessary legacy function call. * Fixed log message capital first letter. * Replaced None argument with empty dict. * Replaced guard. * Added ServiceCallType. * Added set_auto_off and update_device_name services to the component. * Added test cases for service calls. * Conditioned the component services registry with the platform discovery. * Update homeassistant/components/switcher_kis/__init__.py Co-Authored-By: TomerFi <tomer.figenblat@gmail.com> * Update homeassistant/components/switcher_kis/__init__.py Co-Authored-By: TomerFi <tomer.figenblat@gmail.com> * Resolved change requests. * Added ContextType. * Addes permission verification for service calls. * Added test cases for permision verification and more. * Replaced POLICY_CONTROL with the more suited POLICY_EDIT. * More appropriate function name. * Added domain and entity_id validation for calling services. * Removed service for setting the vendor's device name.
This commit is contained in:
1 parent
f9b3ba2887
commit
fe8a330a45
10 files changed
+202
-16
No files matched your search
@@ -98,7 +98,9 @@ def mock_bridge_fixture() -> Generator[None, Any, None]:
|
||||
patchers = [
|
||||
patch('aioswitcher.bridge.SwitcherV2Bridge.start', new=mock_bridge),
|
||||
patch('aioswitcher.bridge.SwitcherV2Bridge.stop', new=mock_bridge),
|
||||
patch('aioswitcher.bridge.SwitcherV2Bridge.queue', get=mock_queue)
|
||||
patch('aioswitcher.bridge.SwitcherV2Bridge.queue', get=mock_queue),
|
||||
patch('aioswitcher.bridge.SwitcherV2Bridge.running',
|
||||
return_value=True)
|
||||
]
|
||||
|
||||
for patcher in patchers:
|
||||
@@ -130,3 +132,22 @@ def mock_failed_bridge_fixture() -> Generator[None, Any, None]:
|
||||
|
||||
for patcher in patchers:
|
||||
patcher.stop()
|
||||
|
||||
|
||||
@fixture(name='mock_api')
|
||||
def mock_api_fixture() -> Generator[CoroutineMock, Any, None]:
|
||||
"""Fixture for mocking aioswitcher.api.SwitcherV2Api."""
|
||||
mock_api = CoroutineMock()
|
||||
|
||||
patchers = [
|
||||
patch('aioswitcher.api.SwitcherV2Api.connect', new=mock_api),
|
||||
patch('aioswitcher.api.SwitcherV2Api.disconnect', new=mock_api)
|
||||
]
|
||||
|
||||
for patcher in patchers:
|
||||
patcher.start()
|
||||
|
||||
yield
|
||||
|
||||
for patcher in patchers:
|
||||
patcher.stop()
|
||||
@@ -17,6 +17,9 @@ DUMMY_PHONE_ID = '1234'
|
||||
DUMMY_POWER_CONSUMPTION = 2780
|
||||
DUMMY_REMAINING_TIME = '01:29:32'
|
||||
|
||||
# Adjust if any modification were made to DUMMY_DEVICE_NAME
|
||||
SWITCH_ENTITY_ID = "switch.switcher_kis_device_name"
|
||||
|
||||
MANDATORY_CONFIGURATION = {
|
||||
DOMAIN: {
|
||||
CONF_PHONE_ID: DUMMY_PHONE_ID,
|
||||
|
||||
@@ -1,16 +1,32 @@
|
||||
"""Test cases for the switcher_kis component."""
|
||||
|
||||
from typing import Any, Generator
|
||||
from datetime import timedelta
|
||||
from typing import Any, Generator, TYPE_CHECKING
|
||||
|
||||
from homeassistant.components.switcher_kis import (DOMAIN, DATA_DEVICE)
|
||||
from pytest import raises
|
||||
|
||||
from homeassistant.const import CONF_ENTITY_ID
|
||||
from homeassistant.components.switcher_kis import (
|
||||
CONF_AUTO_OFF, DOMAIN, DATA_DEVICE, SERVICE_SET_AUTO_OFF_NAME,
|
||||
SERVICE_SET_AUTO_OFF_SCHEMA, SIGNAL_SWITCHER_DEVICE_UPDATE)
|
||||
from homeassistant.core import callback, Context
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
from homeassistant.helpers.typing import HomeAssistantType
|
||||
from homeassistant.exceptions import Unauthorized, UnknownUser
|
||||
from homeassistant.setup import async_setup_component
|
||||
from homeassistant.util import dt
|
||||
|
||||
from tests.common import async_mock_service, async_fire_time_changed
|
||||
|
||||
from .consts import (
|
||||
DUMMY_AUTO_OFF_SET, DUMMY_DEVICE_ID, DUMMY_DEVICE_NAME,
|
||||
DUMMY_DEVICE_STATE, DUMMY_ELECTRIC_CURRENT, DUMMY_IP_ADDRESS,
|
||||
DUMMY_MAC_ADDRESS, DUMMY_PHONE_ID, DUMMY_POWER_CONSUMPTION,
|
||||
DUMMY_REMAINING_TIME, MANDATORY_CONFIGURATION)
|
||||
DUMMY_REMAINING_TIME, MANDATORY_CONFIGURATION, SWITCH_ENTITY_ID)
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from tests.common import MockUser
|
||||
from aioswitcher.devices import SwitcherV2Device
|
||||
|
||||
|
||||
async def test_failed_config(
|
||||
@@ -49,3 +65,81 @@ async def test_discovery_data_bucket(
|
||||
assert device.power_consumption == DUMMY_POWER_CONSUMPTION
|
||||
assert device.electric_current == DUMMY_ELECTRIC_CURRENT
|
||||
assert device.phone_id == DUMMY_PHONE_ID
|
||||
|
||||
|
||||
async def test_set_auto_off_service(
|
||||
hass: HomeAssistantType, mock_bridge: Generator[None, Any, None],
|
||||
mock_api: Generator[None, Any, None], hass_owner_user: 'MockUser',
|
||||
hass_read_only_user: 'MockUser') -> None:
|
||||
"""Test the set_auto_off service."""
|
||||
assert await async_setup_component(hass, DOMAIN, MANDATORY_CONFIGURATION)
|
||||
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert hass.services.has_service(DOMAIN, SERVICE_SET_AUTO_OFF_NAME)
|
||||
|
||||
await hass.services.async_call(
|
||||
DOMAIN, SERVICE_SET_AUTO_OFF_NAME,
|
||||
{CONF_ENTITY_ID: SWITCH_ENTITY_ID,
|
||||
CONF_AUTO_OFF: DUMMY_AUTO_OFF_SET},
|
||||
blocking=True, context=Context(user_id=hass_owner_user.id))
|
||||
|
||||
with raises(Unauthorized) as unauthorized_read_only_exc:
|
||||
await hass.services.async_call(
|
||||
DOMAIN, SERVICE_SET_AUTO_OFF_NAME,
|
||||
{CONF_ENTITY_ID: SWITCH_ENTITY_ID,
|
||||
CONF_AUTO_OFF: DUMMY_AUTO_OFF_SET},
|
||||
blocking=True, context=Context(user_id=hass_read_only_user.id))
|
||||
|
||||
assert unauthorized_read_only_exc.type is Unauthorized
|
||||
|
||||
with raises(Unauthorized) as unauthorized_wrong_entity_exc:
|
||||
await hass.services.async_call(
|
||||
DOMAIN, SERVICE_SET_AUTO_OFF_NAME,
|
||||
{CONF_ENTITY_ID: "light.not_related_entity",
|
||||
CONF_AUTO_OFF: DUMMY_AUTO_OFF_SET},
|
||||
blocking=True, context=Context(user_id=hass_owner_user.id))
|
||||
|
||||
assert unauthorized_wrong_entity_exc.type is Unauthorized
|
||||
|
||||
with raises(UnknownUser) as unknown_user_exc:
|
||||
await hass.services.async_call(
|
||||
DOMAIN, SERVICE_SET_AUTO_OFF_NAME,
|
||||
{CONF_ENTITY_ID: SWITCH_ENTITY_ID,
|
||||
CONF_AUTO_OFF: DUMMY_AUTO_OFF_SET},
|
||||
blocking=True, context=Context(user_id="not_real_user"))
|
||||
|
||||
assert unknown_user_exc.type is UnknownUser
|
||||
|
||||
service_calls = async_mock_service(
|
||||
hass, DOMAIN, SERVICE_SET_AUTO_OFF_NAME, SERVICE_SET_AUTO_OFF_SCHEMA)
|
||||
|
||||
await hass.services.async_call(
|
||||
DOMAIN, SERVICE_SET_AUTO_OFF_NAME,
|
||||
{CONF_ENTITY_ID: SWITCH_ENTITY_ID,
|
||||
CONF_AUTO_OFF: DUMMY_AUTO_OFF_SET})
|
||||
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(service_calls) == 1
|
||||
assert str(service_calls[0].data[CONF_AUTO_OFF]) \
|
||||
== DUMMY_AUTO_OFF_SET.lstrip('0')
|
||||
|
||||
|
||||
async def test_signal_dispatcher(
|
||||
hass: HomeAssistantType,
|
||||
mock_bridge: Generator[None, Any, None]) -> None:
|
||||
"""Test signal dispatcher dispatching device updates every 4 seconds."""
|
||||
assert await async_setup_component(hass, DOMAIN, MANDATORY_CONFIGURATION)
|
||||
|
||||
await hass.async_block_till_done()
|
||||
|
||||
@callback
|
||||
def verify_update_data(device: 'SwitcherV2Device') -> None:
|
||||
"""Use as callback for signal dispatcher."""
|
||||
pass
|
||||
|
||||
async_dispatcher_connect(
|
||||
hass, SIGNAL_SWITCHER_DEVICE_UPDATE, verify_update_data)
|
||||
|
||||
async_fire_time_changed(hass, dt.utcnow() + timedelta(seconds=5))
|
||||
Reference in new issue
Block a user