Skip to content

Commit

Permalink
fix: list claims service call showing only first claim (#321)
Browse files Browse the repository at this point in the history
* fix: `list claims` service call showing only first claim

* update tests

* add more service tests

* formatting

* enable python 3.12 testing

* add test for `get_limit` service

* linting
  • Loading branch information
firstof9 authored Apr 23, 2024
1 parent cbe6f33 commit 0bfc2e5
Show file tree
Hide file tree
Showing 8 changed files with 212 additions and 36 deletions.
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ jobs:
python-version:
# - "3.10"
- "3.11"
- "3.12"

steps:
- name: 📥 Checkout the repository
Expand Down
37 changes: 22 additions & 15 deletions custom_components/openevse/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
callback,
)
from homeassistant.helpers import device_registry as dr
from homeassistant.helpers import config_validation as cv

from .const import (
ATTR_AUTO_RELEASE,
Expand Down Expand Up @@ -60,7 +61,7 @@ def async_register(self) -> None:
self._set_override,
schema=vol.Schema(
{
vol.Required(ATTR_DEVICE_ID): vol.Coerce(list),
vol.Required(ATTR_DEVICE_ID): cv.string,
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 @@ -85,7 +86,7 @@ def async_register(self) -> None:
self._set_limit,
schema=vol.Schema(
{
vol.Required(ATTR_DEVICE_ID): vol.Coerce(list),
vol.Required(ATTR_DEVICE_ID): cv.string,
vol.Required(ATTR_TYPE): vol.Coerce(str),
vol.Required(ATTR_VALUE): vol.Coerce(int),
vol.Optional(ATTR_AUTO_RELEASE): vol.Coerce(bool),
Expand All @@ -99,7 +100,7 @@ def async_register(self) -> None:
self._clear_override,
schema=vol.Schema(
{
vol.Required(ATTR_DEVICE_ID): vol.Coerce(list),
vol.Required(ATTR_DEVICE_ID): cv.string,
}
),
)
Expand All @@ -110,7 +111,7 @@ def async_register(self) -> None:
self._clear_limit,
schema=vol.Schema(
{
vol.Required(ATTR_DEVICE_ID): vol.Coerce(list),
vol.Required(ATTR_DEVICE_ID): cv.string,
}
),
)
Expand All @@ -121,7 +122,7 @@ def async_register(self) -> None:
self._get_limit,
schema=vol.Schema(
{
vol.Required(ATTR_DEVICE_ID): vol.Coerce(list),
vol.Required(ATTR_DEVICE_ID): cv.string,
}
),
supports_response=SupportsResponse.ONLY,
Expand All @@ -133,7 +134,7 @@ def async_register(self) -> None:
self._make_claim,
schema=vol.Schema(
{
vol.Required(ATTR_DEVICE_ID): vol.Coerce(list),
vol.Required(ATTR_DEVICE_ID): cv.string,
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 @@ -152,7 +153,7 @@ def async_register(self) -> None:
self._list_claims,
schema=vol.Schema(
{
vol.Required(ATTR_DEVICE_ID): vol.Coerce(list),
vol.Required(ATTR_DEVICE_ID): cv.string,
}
),
supports_response=SupportsResponse.ONLY,
Expand All @@ -164,7 +165,7 @@ def async_register(self) -> None:
self._release_claim,
schema=vol.Schema(
{
vol.Required(ATTR_DEVICE_ID): vol.Coerce(list),
vol.Required(ATTR_DEVICE_ID): cv.string,
}
),
)
Expand All @@ -175,7 +176,7 @@ def async_register(self) -> None:
self._list_overrides,
schema=vol.Schema(
{
vol.Required(ATTR_DEVICE_ID): vol.Coerce(list),
vol.Required(ATTR_DEVICE_ID): cv.string,
}
),
supports_response=SupportsResponse.ONLY,
Expand Down Expand Up @@ -316,7 +317,7 @@ async def _get_limit(self, service: ServiceCall) -> ServiceResponse:
"""Get 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 All @@ -333,7 +334,7 @@ async def _get_limit(self, service: ServiceCall) -> ServiceResponse:

response = await manager.get_limit()
_LOGGER.debug("Get limit response %s.", response)
return response[0]
return response

async def _make_claim(self, service: ServiceCall) -> None:
"""Make a claim."""
Expand Down Expand Up @@ -404,7 +405,7 @@ async def _list_claims(self, service: ServiceCall) -> ServiceResponse:
"""Get the claims."""
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 @@ -421,13 +422,19 @@ async def _list_claims(self, service: ServiceCall) -> ServiceResponse:

response = await manager.list_claims()
_LOGGER.debug("List claims response %s.", response)
return response[0]
claims = {}
x = 0
for claim in response:
claims[x] = claim
x += 1
_LOGGER.debug("Processed response %s.", claims)
return claims

async def _list_overrides(self, service: ServiceCall) -> ServiceResponse:
"""Get the overrides."""
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 @@ -444,4 +451,4 @@ async def _list_overrides(self, service: ServiceCall) -> ServiceResponse:

response = await manager.get_override()
_LOGGER.debug("List overrides response %s.", response)
return response[0]
return response
11 changes: 5 additions & 6 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
"""Global fixtures for openevse integration."""

from aioresponses import aioresponses
from unittest import mock
from unittest.mock import patch

import json
import os
from unittest import mock
from unittest.mock import patch

import pytest
import openevsehttp.__main__ as main
import pytest
from aioresponses import aioresponses

from tests.const import CHARGER_DATA, GETFW_DATA, FW_DATA
from tests.const import CHARGER_DATA, FW_DATA, GETFW_DATA

pytest_plugins = "pytest_homeassistant_custom_component"

Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"protocol": "-",
"espflash": 4194304,
"wifi_serial": "1234567890AB",
"version": "4.1.2",
"version": "5.1.0",
"diodet": 0,
"gfcit": 0,
"groundt": 0,
Expand Down
10 changes: 5 additions & 5 deletions tests/test_config_flow.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
"""Test config flow."""

from tests.const import CONFIG_DATA
from unittest.mock import patch
import pytest

from custom_components.openevse.const import DOMAIN

import pytest
from homeassistant import config_entries, data_entry_flow, setup
from homeassistant.const import CONF_NAME
from pytest_homeassistant_custom_component.common import MockConfigEntry
from homeassistant.data_entry_flow import FlowResult, FlowResultType
from pytest_homeassistant_custom_component.common import MockConfigEntry

from custom_components.openevse.const import DOMAIN
from tests.const import CONFIG_DATA

CHARGER_NAME = "openevse"

Expand Down
7 changes: 3 additions & 4 deletions tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
from unittest.mock import patch

import pytest

from custom_components.openevse.const import DOMAIN

from homeassistant.components.binary_sensor import DOMAIN as BINARY_SENSOR_DOMAIN
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
from homeassistant.components.select import DOMAIN as SELECT_DOMAIN
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN
from pytest_homeassistant_custom_component.common import MockConfigEntry

from custom_components.openevse.const import DOMAIN

from .const import CONFIG_DATA

pytestmark = pytest.mark.asyncio
Expand Down
9 changes: 4 additions & 5 deletions tests/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
from unittest.mock import patch

import pytest

from custom_components.openevse.const import DOMAIN

from homeassistant.components.binary_sensor import DOMAIN as BINARY_SENSOR_DOMAIN
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
from homeassistant.components.select import DOMAIN as SELECT_DOMAIN
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN
from pytest_homeassistant_custom_component.common import MockConfigEntry

from custom_components.openevse.const import DOMAIN

from .const import CONFIG_DATA

pytestmark = pytest.mark.asyncio
Expand Down Expand Up @@ -42,7 +41,7 @@ async def test_sensors(hass, test_charger, mock_ws_start):

state = hass.states.get("sensor.openevse_wifi_firmware_version")
assert state
assert state.state == "4.1.2"
assert state.state == "5.1.0"
state = hass.states.get("sensor.openevse_charge_time_elapsed")
assert state
assert state.state == "4.1"
Expand Down
Loading

0 comments on commit 0bfc2e5

Please sign in to comment.