hass-core/tests/components/demo/test_siren.py
Raman Gupta 0f076610fd
Add siren platform (#48309)
* Add siren platform

* add more supported flags and an ability to set siren duration

* tone can be int or string

* fix typing

* fix typehinting

* fix typehints

* implement a proposed approach based on discussion

* Address comments

* fix tests

* Small fix

* Update homeassistant/components/demo/siren.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Update homeassistant/components/demo/siren.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Update homeassistant/components/demo/siren.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Update homeassistant/components/demo/siren.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Update homeassistant/components/demo/siren.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* typing

* use class attributes

* fix naming

* remove device from service description

* Filter out params from turn on service

* fix tests

* fix bugs and tests

* add test

* Combine is_on test with turn on/off/toggle service tests

* Update homeassistant/components/siren/__init__.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* fix filtering of turn_on attributes

* none check

* remove services and attributes for volume level, default duration, and default tone

* Update homeassistant/components/siren/__init__.py

Co-authored-by: Franck Nijhof <frenck@frenck.nl>

* Update homeassistant/components/siren/__init__.py

Co-authored-by: Franck Nijhof <frenck@frenck.nl>

* Update homeassistant/components/siren/__init__.py

Co-authored-by: Franck Nijhof <frenck@frenck.nl>

* import final

* Update homeassistant/components/siren/__init__.py

Co-authored-by: Franck Nijhof <frenck@frenck.nl>

* Fix typing and used TypedDict for service parameters

* remove is_on function

* remove class name redundancy

* remove extra service descriptions

* switch to positive_int

* fix schema for tone

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
Co-authored-by: Franck Nijhof <frenck@frenck.nl>
2021-07-11 16:51:11 -04:00

108 lines
3.2 KiB
Python

"""The tests for the demo siren component."""
from unittest.mock import call, patch
import pytest
from homeassistant.components.siren.const import (
ATTR_AVAILABLE_TONES,
ATTR_VOLUME_LEVEL,
DOMAIN,
)
from homeassistant.const import (
ATTR_ENTITY_ID,
SERVICE_TOGGLE,
SERVICE_TURN_OFF,
SERVICE_TURN_ON,
STATE_OFF,
STATE_ON,
)
from homeassistant.setup import async_setup_component
ENTITY_SIREN = "siren.siren"
ENTITY_SIREN_WITH_ALL_FEATURES = "siren.siren_with_all_features"
@pytest.fixture(autouse=True)
async def setup_demo_siren(hass):
"""Initialize setup demo siren."""
assert await async_setup_component(hass, DOMAIN, {"siren": {"platform": "demo"}})
await hass.async_block_till_done()
def test_setup_params(hass):
"""Test the initial parameters."""
state = hass.states.get(ENTITY_SIREN)
assert state.state == STATE_ON
assert ATTR_AVAILABLE_TONES not in state.attributes
def test_all_setup_params(hass):
"""Test the setup with all parameters."""
state = hass.states.get(ENTITY_SIREN_WITH_ALL_FEATURES)
assert state.attributes.get(ATTR_AVAILABLE_TONES) == ["fire", "alarm"]
async def test_turn_on(hass):
"""Test turn on device."""
await hass.services.async_call(
DOMAIN, SERVICE_TURN_OFF, {ATTR_ENTITY_ID: ENTITY_SIREN}, blocking=True
)
state = hass.states.get(ENTITY_SIREN)
assert state.state == STATE_OFF
await hass.services.async_call(
DOMAIN, SERVICE_TURN_ON, {ATTR_ENTITY_ID: ENTITY_SIREN}, blocking=True
)
state = hass.states.get(ENTITY_SIREN)
assert state.state == STATE_ON
async def test_turn_off(hass):
"""Test turn off device."""
await hass.services.async_call(
DOMAIN, SERVICE_TURN_ON, {ATTR_ENTITY_ID: ENTITY_SIREN}, blocking=True
)
state = hass.states.get(ENTITY_SIREN)
assert state.state == STATE_ON
await hass.services.async_call(
DOMAIN, SERVICE_TURN_OFF, {ATTR_ENTITY_ID: ENTITY_SIREN}, blocking=True
)
state = hass.states.get(ENTITY_SIREN)
assert state.state == STATE_OFF
async def test_toggle(hass):
"""Test toggle device."""
await hass.services.async_call(
DOMAIN, SERVICE_TURN_ON, {ATTR_ENTITY_ID: ENTITY_SIREN}, blocking=True
)
state = hass.states.get(ENTITY_SIREN)
assert state.state == STATE_ON
await hass.services.async_call(
DOMAIN, SERVICE_TOGGLE, {ATTR_ENTITY_ID: ENTITY_SIREN}, blocking=True
)
state = hass.states.get(ENTITY_SIREN)
assert state.state == STATE_OFF
await hass.services.async_call(
DOMAIN, SERVICE_TOGGLE, {ATTR_ENTITY_ID: ENTITY_SIREN}, blocking=True
)
state = hass.states.get(ENTITY_SIREN)
assert state.state == STATE_ON
async def test_turn_on_strip_attributes(hass):
"""Test attributes are stripped from turn_on service call when not supported."""
with patch(
"homeassistant.components.demo.siren.DemoSiren.async_turn_on"
) as svc_call:
await hass.services.async_call(
DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: ENTITY_SIREN, ATTR_VOLUME_LEVEL: 1},
blocking=True,
)
assert svc_call.called
assert svc_call.call_args_list[0] == call(**{ATTR_ENTITY_ID: [ENTITY_SIREN]})