2020-04-20 02:05:58 +02:00
|
|
|
import pytest
|
|
|
|
from mock import Mock
|
|
|
|
|
|
|
|
from esphome import cpp_helpers as ch
|
|
|
|
from esphome import const
|
|
|
|
from esphome.cpp_generator import MockObj
|
|
|
|
|
|
|
|
|
2021-05-17 07:14:15 +02:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_gpio_pin_expression__conf_is_none(monkeypatch):
|
|
|
|
actual = await ch.gpio_pin_expression(None)
|
2020-04-20 02:05:58 +02:00
|
|
|
|
|
|
|
assert actual is None
|
|
|
|
|
|
|
|
|
2021-05-17 07:14:15 +02:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_gpio_pin_expression__new_pin(monkeypatch):
|
|
|
|
actual = await ch.gpio_pin_expression(
|
2021-03-07 20:03:16 +01:00
|
|
|
{const.CONF_NUMBER: 42, const.CONF_MODE: "input", const.CONF_INVERTED: False}
|
|
|
|
)
|
2020-04-20 02:05:58 +02:00
|
|
|
|
|
|
|
assert isinstance(actual, MockObj)
|
|
|
|
|
|
|
|
|
2021-05-17 07:14:15 +02:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_register_component(monkeypatch):
|
2020-04-20 02:05:58 +02:00
|
|
|
var = Mock(base="foo.bar")
|
|
|
|
|
|
|
|
app_mock = Mock(register_component=Mock(return_value=var))
|
|
|
|
monkeypatch.setattr(ch, "App", app_mock)
|
|
|
|
|
|
|
|
core_mock = Mock(component_ids=["foo.bar"])
|
|
|
|
monkeypatch.setattr(ch, "CORE", core_mock)
|
|
|
|
|
|
|
|
add_mock = Mock()
|
|
|
|
monkeypatch.setattr(ch, "add", add_mock)
|
|
|
|
|
2021-05-17 07:14:15 +02:00
|
|
|
actual = await ch.register_component(var, {})
|
2020-04-20 02:05:58 +02:00
|
|
|
|
|
|
|
assert actual is var
|
2021-08-23 20:49:19 +02:00
|
|
|
assert add_mock.call_count == 2
|
2020-04-20 02:05:58 +02:00
|
|
|
app_mock.register_component.assert_called_with(var)
|
|
|
|
assert core_mock.component_ids == []
|
|
|
|
|
|
|
|
|
2021-05-17 07:14:15 +02:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_register_component__no_component_id(monkeypatch):
|
2020-04-20 02:05:58 +02:00
|
|
|
var = Mock(base="foo.eek")
|
|
|
|
|
|
|
|
core_mock = Mock(component_ids=["foo.bar"])
|
|
|
|
monkeypatch.setattr(ch, "CORE", core_mock)
|
|
|
|
|
|
|
|
with pytest.raises(ValueError, match="Component ID foo.eek was not declared to"):
|
2021-05-17 07:14:15 +02:00
|
|
|
await ch.register_component(var, {})
|
2020-04-20 02:05:58 +02:00
|
|
|
|
|
|
|
|
2021-05-17 07:14:15 +02:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_register_component__with_setup_priority(monkeypatch):
|
2020-04-20 02:05:58 +02:00
|
|
|
var = Mock(base="foo.bar")
|
|
|
|
|
|
|
|
app_mock = Mock(register_component=Mock(return_value=var))
|
|
|
|
monkeypatch.setattr(ch, "App", app_mock)
|
|
|
|
|
|
|
|
core_mock = Mock(component_ids=["foo.bar"])
|
|
|
|
monkeypatch.setattr(ch, "CORE", core_mock)
|
|
|
|
|
|
|
|
add_mock = Mock()
|
|
|
|
monkeypatch.setattr(ch, "add", add_mock)
|
|
|
|
|
2021-05-17 07:14:15 +02:00
|
|
|
actual = await ch.register_component(
|
2021-03-07 20:03:16 +01:00
|
|
|
var,
|
|
|
|
{
|
|
|
|
const.CONF_SETUP_PRIORITY: "123",
|
|
|
|
const.CONF_UPDATE_INTERVAL: "456",
|
|
|
|
},
|
|
|
|
)
|
2020-04-20 02:05:58 +02:00
|
|
|
|
|
|
|
assert actual is var
|
|
|
|
add_mock.assert_called()
|
2021-08-23 20:49:19 +02:00
|
|
|
assert add_mock.call_count == 4
|
2020-04-20 02:05:58 +02:00
|
|
|
app_mock.register_component.assert_called_with(var)
|
|
|
|
assert core_mock.component_ids == []
|