Skip to content

Commit

Permalink
fix: service calls throwing invalid format
Browse files Browse the repository at this point in the history
  • Loading branch information
firstof9 committed Apr 25, 2024
1 parent fe4039b commit b57c079
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 15 deletions.
30 changes: 15 additions & 15 deletions custom_components/openevse/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def async_register(self) -> None:
self._set_override,
schema=vol.Schema(
{
vol.Required(ATTR_DEVICE_ID): cv.string,
vol.Required(ATTR_DEVICE_ID): vol.Coerce(str),
vol.Optional(ATTR_STATE): vol.Coerce(str),
vol.Optional(ATTR_CHARGE_CURRENT): vol.All(
vol.Coerce(int), vol.Range(min=1, max=48)
Expand All @@ -86,7 +86,7 @@ def async_register(self) -> None:
self._set_limit,
schema=vol.Schema(
{
vol.Required(ATTR_DEVICE_ID): cv.string,
vol.Required(ATTR_DEVICE_ID): vol.Coerce(str),
vol.Required(ATTR_TYPE): vol.Coerce(str),
vol.Required(ATTR_VALUE): vol.Coerce(int),
vol.Optional(ATTR_AUTO_RELEASE): vol.Coerce(bool),
Expand All @@ -100,7 +100,7 @@ def async_register(self) -> None:
self._clear_override,
schema=vol.Schema(
{
vol.Required(ATTR_DEVICE_ID): cv.string,
vol.Required(ATTR_DEVICE_ID): vol.Coerce(str),
}
),
)
Expand All @@ -111,7 +111,7 @@ def async_register(self) -> None:
self._clear_limit,
schema=vol.Schema(
{
vol.Required(ATTR_DEVICE_ID): cv.string,
vol.Required(ATTR_DEVICE_ID): vol.Coerce(str),
}
),
)
Expand All @@ -122,7 +122,7 @@ def async_register(self) -> None:
self._get_limit,
schema=vol.Schema(
{
vol.Required(ATTR_DEVICE_ID): cv.string,
vol.Required(ATTR_DEVICE_ID): vol.Coerce(str),
}
),
supports_response=SupportsResponse.ONLY,
Expand All @@ -134,7 +134,7 @@ def async_register(self) -> None:
self._make_claim,
schema=vol.Schema(
{
vol.Required(ATTR_DEVICE_ID): cv.string,
vol.Required(ATTR_DEVICE_ID): vol.Coerce(str),
vol.Optional(ATTR_STATE): vol.Coerce(str),
vol.Optional(ATTR_CHARGE_CURRENT): vol.All(
vol.Coerce(int), vol.Range(min=1, max=48)
Expand All @@ -153,7 +153,7 @@ def async_register(self) -> None:
self._list_claims,
schema=vol.Schema(
{
vol.Required(ATTR_DEVICE_ID): cv.string,
vol.Required(ATTR_DEVICE_ID): vol.Coerce(str),
}
),
supports_response=SupportsResponse.ONLY,
Expand All @@ -165,7 +165,7 @@ def async_register(self) -> None:
self._release_claim,
schema=vol.Schema(
{
vol.Required(ATTR_DEVICE_ID): cv.string,
vol.Required(ATTR_DEVICE_ID): vol.Coerce(str),
}
),
)
Expand All @@ -176,7 +176,7 @@ def async_register(self) -> None:
self._list_overrides,
schema=vol.Schema(
{
vol.Required(ATTR_DEVICE_ID): cv.string,
vol.Required(ATTR_DEVICE_ID): vol.Coerce(str),
}
),
supports_response=SupportsResponse.ONLY,
Expand All @@ -186,7 +186,7 @@ def async_register(self) -> None:
async def _set_override(self, service: ServiceCall) -> None:
"""Set the override."""
data = service.data
for device in data[ATTR_DEVICE_ID]:
for device in data.values():
device_id = device
_LOGGER.debug("Device ID: %s", device_id)

Expand Down Expand Up @@ -240,7 +240,7 @@ async def _clear_override(self, service: ServiceCall) -> None:
"""Clear the manual override."""
data = service.data
_LOGGER.debug("Data: %s", data)
for device in data[ATTR_DEVICE_ID]:
for device in data.values():
device_id = device
_LOGGER.debug("Device ID: %s", device_id)

Expand All @@ -261,7 +261,7 @@ async def _clear_override(self, service: ServiceCall) -> None:
async def _set_limit(self, service: ServiceCall) -> None:
"""Set the limit."""
data = service.data
for device in data[ATTR_DEVICE_ID]:
for device in data.values():
device_id = device
_LOGGER.debug("Device ID: %s", device_id)

Expand Down Expand Up @@ -295,7 +295,7 @@ async def _clear_limit(self, service: ServiceCall) -> None:
"""Clear the limit."""
data = service.data
_LOGGER.debug("Data: %s", data)
for device in data[ATTR_DEVICE_ID]:
for device in data.values():
device_id = device
_LOGGER.debug("Device ID: %s", device_id)

Expand Down Expand Up @@ -339,7 +339,7 @@ async def _get_limit(self, service: ServiceCall) -> ServiceResponse:
async def _make_claim(self, service: ServiceCall) -> None:
"""Make a claim."""
data = service.data
for device in data[ATTR_DEVICE_ID]:
for device in data.values():
device_id = device
_LOGGER.debug("Device ID: %s", device_id)

Expand Down Expand Up @@ -383,7 +383,7 @@ async def _release_claim(self, service: ServiceCall) -> None:
"""Release a claim."""
data = service.data
_LOGGER.debug("Data: %s", data)
for device in data[ATTR_DEVICE_ID]:
for device in data.values():
device_id = device
_LOGGER.debug("Device ID: %s", device_id)

Expand Down
77 changes: 77 additions & 0 deletions tests/test_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@

from custom_components.openevse.const import (
DOMAIN,
SERVICE_CLEAR_OVERRIDE,
SERVICE_GET_LIMIT,
SERVICE_LIST_CLAIMS,
SERVICE_LIST_OVERRIDES,
SERVICE_RELEASE_CLAIM,
)

from .const import CONFIG_DATA
Expand Down Expand Up @@ -169,3 +171,78 @@ async def test_get_limit(
return_response=True,
)
assert response == {"type": "energy", "value": 10}

async def test_release_claim(
hass,
test_charger,
mock_aioclient,
mock_ws_start,
entity_registry: er.EntityRegistry,
caplog,
):
"""Test release claim service call."""
entry = MockConfigEntry(
domain=DOMAIN,
title=CHARGER_NAME,
data=CONFIG_DATA,
)
mock_aioclient.delete(
f"{TEST_URL_CLAIMS}/4",
status=200,
body='[{"msg":"done"}]',
repeat=True,
)
entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()

entry = entity_registry.async_get("sensor.openevse_station_status")
assert entry
assert entry.device_id

# setup service call
with caplog.at_level(logging.DEBUG):
await hass.services.async_call(
DOMAIN,
SERVICE_RELEASE_CLAIM,
{CONF_DEVICE_ID: entry.device_id},
blocking=True,
)
assert "Release claim command sent." in caplog.text

async def test_clear_override(
hass,
test_charger,
mock_aioclient,
mock_ws_start,
entity_registry: er.EntityRegistry,
caplog,
):
"""Test release claim service call."""
entry = MockConfigEntry(
domain=DOMAIN,
title=CHARGER_NAME,
data=CONFIG_DATA,
)
mock_aioclient.delete(
TEST_URL_OVERRIDE,
status=200,
body='{"msg": "OK"}',
)
entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()

entry = entity_registry.async_get("sensor.openevse_station_status")
assert entry
assert entry.device_id

# setup service call
with caplog.at_level(logging.DEBUG):
await hass.services.async_call(
DOMAIN,
SERVICE_CLEAR_OVERRIDE,
{CONF_DEVICE_ID: entry.device_id},
blocking=True,
)
assert "Override clear command sent." in caplog.text

0 comments on commit b57c079

Please sign in to comment.