|
| 1 | +"""Test pyscripts test module.""" |
| 2 | +from custom_components.pyscript.state import State |
| 3 | +from pytest_homeassistant_custom_component.async_mock import patch |
| 4 | + |
| 5 | +from homeassistant.core import Context |
| 6 | +from homeassistant.helpers.state import State as HassState |
| 7 | + |
| 8 | + |
| 9 | +async def test_service_call(hass): |
| 10 | + """Test calling a service using the entity_id as a property.""" |
| 11 | + with patch( |
| 12 | + "custom_components.pyscript.state.async_get_all_descriptions", |
| 13 | + return_value={ |
| 14 | + "test": { |
| 15 | + "test": {"description": None, "fields": {"entity_id": "blah", "other_service_data": "blah"}} |
| 16 | + } |
| 17 | + }, |
| 18 | + ), patch.object(hass.states, "get", return_value=HassState("test.entity", "True")), patch.object( |
| 19 | + hass.services, "async_call" |
| 20 | + ) as call: |
| 21 | + State.init(hass) |
| 22 | + await State.get_service_params() |
| 23 | + |
| 24 | + func = await State.get("test.entity.test") |
| 25 | + await func(context=Context(id="test"), blocking=True, limit=1, other_service_data="test") |
| 26 | + assert call.called |
| 27 | + assert call.call_args[0] == ( |
| 28 | + "test", |
| 29 | + "test", |
| 30 | + {"other_service_data": "test", "entity_id": "test.entity"}, |
| 31 | + ) |
| 32 | + assert call.call_args[1] == {"context": Context(id="test"), "blocking": True, "limit": 1} |
| 33 | + call.reset_mock() |
| 34 | + |
| 35 | + func = await State.get("test.entity.test") |
| 36 | + await func(context=Context(id="test"), blocking=False, other_service_data="test") |
| 37 | + assert call.called |
| 38 | + assert call.call_args[0] == ( |
| 39 | + "test", |
| 40 | + "test", |
| 41 | + {"other_service_data": "test", "entity_id": "test.entity"}, |
| 42 | + ) |
| 43 | + assert call.call_args[1] == {"context": Context(id="test"), "blocking": False} |
0 commit comments