Skip to content

Commit

Permalink
Simplify access to hass in service calls (home-assistant#133062)
Browse files Browse the repository at this point in the history
  • Loading branch information
epenet authored Dec 13, 2024
1 parent f9f37b9 commit 899fb09
Show file tree
Hide file tree
Showing 7 changed files with 204 additions and 94 deletions.
6 changes: 4 additions & 2 deletions homeassistant/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2432,17 +2432,19 @@ def __init__(
class ServiceCall:
"""Representation of a call to a service."""

__slots__ = ("domain", "service", "data", "context", "return_response")
__slots__ = ("hass", "domain", "service", "data", "context", "return_response")

def __init__(
self,
hass: HomeAssistant,
domain: str,
service: str,
data: dict[str, Any] | None = None,
context: Context | None = None,
return_response: bool = False,
) -> None:
"""Initialize a service call."""
self.hass = hass
self.domain = domain
self.service = service
self.data = ReadOnlyDict(data or {})
Expand Down Expand Up @@ -2768,7 +2770,7 @@ async def async_call(
processed_data = service_data

service_call = ServiceCall(
domain, service, processed_data, context, return_response
self._hass, domain, service, processed_data, context, return_response
)

self._hass.bus.async_fire_internal(
Expand Down
1 change: 1 addition & 0 deletions tests/components/homeassistant/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ async def test_turn_on_skips_domains_without_service(
# because by mocking out the call service method, we mock out all
# So we mimic how the service registry calls services
service_call = ha.ServiceCall(
hass,
"homeassistant",
"turn_on",
{"entity_id": ["light.test", "sensor.bla", "binary_sensor.blub", "light.bla"]},
Expand Down
9 changes: 5 additions & 4 deletions tests/components/text/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,21 +64,22 @@ async def test_text_set_value(hass: HomeAssistant) -> None:

with pytest.raises(ValueError):
await _async_set_value(
text, ServiceCall(DOMAIN, SERVICE_SET_VALUE, {ATTR_VALUE: ""})
text, ServiceCall(hass, DOMAIN, SERVICE_SET_VALUE, {ATTR_VALUE: ""})
)

with pytest.raises(ValueError):
await _async_set_value(
text, ServiceCall(DOMAIN, SERVICE_SET_VALUE, {ATTR_VALUE: "hello world!"})
text,
ServiceCall(hass, DOMAIN, SERVICE_SET_VALUE, {ATTR_VALUE: "hello world!"}),
)

with pytest.raises(ValueError):
await _async_set_value(
text, ServiceCall(DOMAIN, SERVICE_SET_VALUE, {ATTR_VALUE: "HELLO"})
text, ServiceCall(hass, DOMAIN, SERVICE_SET_VALUE, {ATTR_VALUE: "HELLO"})
)

await _async_set_value(
text, ServiceCall(DOMAIN, SERVICE_SET_VALUE, {ATTR_VALUE: "test2"})
text, ServiceCall(hass, DOMAIN, SERVICE_SET_VALUE, {ATTR_VALUE: "test2"})
)

assert text.state == "test2"
Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1899,7 +1899,7 @@ async def _async_call(
return_response: bool = False,
) -> ServiceResponse:
calls.append(
ServiceCall(domain, service, service_data, context, return_response)
ServiceCall(hass, domain, service, service_data, context, return_response)
)
try:
return await _original_async_call(
Expand Down
17 changes: 10 additions & 7 deletions tests/helpers/test_entity_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,13 +189,14 @@ async def test_extract_from_service_available_device(hass: HomeAssistant) -> Non
]
)

call_1 = ServiceCall("test", "service", data={"entity_id": ENTITY_MATCH_ALL})
call_1 = ServiceCall(hass, "test", "service", data={"entity_id": ENTITY_MATCH_ALL})

assert sorted(
ent.entity_id for ent in (await component.async_extract_from_service(call_1))
) == ["test_domain.test_1", "test_domain.test_3"]

call_2 = ServiceCall(
hass,
"test",
"service",
data={"entity_id": ["test_domain.test_3", "test_domain.test_4"]},
Expand Down Expand Up @@ -256,17 +257,18 @@ async def test_extract_from_service_fails_if_no_entity_id(hass: HomeAssistant) -
)

assert (
await component.async_extract_from_service(ServiceCall("test", "service")) == []
await component.async_extract_from_service(ServiceCall(hass, "test", "service"))
== []
)
assert (
await component.async_extract_from_service(
ServiceCall("test", "service", {"entity_id": ENTITY_MATCH_NONE})
ServiceCall(hass, "test", "service", {"entity_id": ENTITY_MATCH_NONE})
)
== []
)
assert (
await component.async_extract_from_service(
ServiceCall("test", "service", {"area_id": ENTITY_MATCH_NONE})
ServiceCall(hass, "test", "service", {"area_id": ENTITY_MATCH_NONE})
)
== []
)
Expand All @@ -283,6 +285,7 @@ async def test_extract_from_service_filter_out_non_existing_entities(
)

call = ServiceCall(
hass,
"test",
"service",
{"entity_id": ["test_domain.test_2", "test_domain.non_exist"]},
Expand All @@ -299,7 +302,7 @@ async def test_extract_from_service_no_group_expand(hass: HomeAssistant) -> None
await component.async_setup({})
await component.async_add_entities([MockEntity(entity_id="group.test_group")])

call = ServiceCall("test", "service", {"entity_id": ["group.test_group"]})
call = ServiceCall(hass, "test", "service", {"entity_id": ["group.test_group"]})

extracted = await component.async_extract_from_service(call, expand_group=False)
assert len(extracted) == 1
Expand Down Expand Up @@ -465,7 +468,7 @@ async def test_extract_all_omit_entity_id(
[MockEntity(name="test_1"), MockEntity(name="test_2")]
)

call = ServiceCall("test", "service")
call = ServiceCall(hass, "test", "service")

assert (
sorted(
Expand All @@ -485,7 +488,7 @@ async def test_extract_all_use_match_all(
[MockEntity(name="test_1"), MockEntity(name="test_2")]
)

call = ServiceCall("test", "service", {"entity_id": "all"})
call = ServiceCall(hass, "test", "service", {"entity_id": "all"})

assert sorted(
ent.entity_id for ent in await component.async_extract_from_service(call)
Expand Down
Loading

0 comments on commit 899fb09

Please sign in to comment.