From e0736f07a58cf0f690fd7bb0edb69864699dc705 Mon Sep 17 00:00:00 2001 From: "firstof9@gmail.com" Date: Sun, 8 Dec 2024 19:26:03 -0700 Subject: [PATCH 1/2] feat: add fixed charges sensor --- custom_components/openei/const.py | 6 ++++++ custom_components/openei/manifest.json | 2 +- requirements_tests.txt | 2 +- tests/test_init.py | 6 +++--- tests/test_sensors.py | 30 +++++++++++++++----------- 5 files changed, 29 insertions(+), 17 deletions(-) diff --git a/custom_components/openei/const.py b/custom_components/openei/const.py index 558cf33..5fdb446 100644 --- a/custom_components/openei/const.py +++ b/custom_components/openei/const.py @@ -89,6 +89,12 @@ icon="mdi:cash-multiple", entity_category=EntityCategory.DIAGNOSTIC, ), + "fixedchargefirstmeter": SensorEntityDescription( + key="fixedchargefirstmeter", + name="Fixed Charge (first meter)", + icon="mdi:cash-multiple", + entity_category=EntityCategory.DIAGNOSTIC, + ), } BINARY_SENSORS: Final[dict[str, BinarySensorEntityDescription]] = { diff --git a/custom_components/openei/manifest.json b/custom_components/openei/manifest.json index edb7669..605a4b5 100644 --- a/custom_components/openei/manifest.json +++ b/custom_components/openei/manifest.json @@ -7,6 +7,6 @@ "documentation": "https://github.com/firstof9/ha-openei", "iot_class": "cloud_polling", "issue_tracker": "https://github.com/firstof9/ha-openei/issues", - "requirements": ["python_openei==0.2.3", "aiofiles"], + "requirements": ["python_openei==0.2.4", "aiofiles"], "version": "0.1.6" } diff --git a/requirements_tests.txt b/requirements_tests.txt index 6f5ab03..39ad072 100644 --- a/requirements_tests.txt +++ b/requirements_tests.txt @@ -1,5 +1,5 @@ -r requirements_dev.txt -python_openei==0.2.3 +python_openei pytest pytest-cov pytest-homeassistant-custom-component diff --git a/tests/test_init.py b/tests/test_init.py index 857812d..202b79f 100644 --- a/tests/test_init.py +++ b/tests/test_init.py @@ -38,7 +38,7 @@ async def test_setup_entry(hass, mock_aioclient, caplog): await hass.config_entries.async_setup(entry.entry_id) await hass.async_block_till_done() - assert len(hass.states.async_entity_ids(SENSOR_DOMAIN)) == 7 + assert len(hass.states.async_entity_ids(SENSOR_DOMAIN)) == 8 assert len(hass.states.async_entity_ids(BINARY_SENSOR_DOMAIN)) == 1 entries = hass.config_entries.async_entries(DOMAIN) assert len(entries) == 1 @@ -56,14 +56,14 @@ async def test_unload_entry(hass, mock_api): assert await hass.config_entries.async_setup(entry.entry_id) await hass.async_block_till_done() - assert len(hass.states.async_entity_ids(SENSOR_DOMAIN)) == 7 + assert len(hass.states.async_entity_ids(SENSOR_DOMAIN)) == 8 assert len(hass.states.async_entity_ids(BINARY_SENSOR_DOMAIN)) == 1 entries = hass.config_entries.async_entries(DOMAIN) assert len(entries) == 1 assert await hass.config_entries.async_unload(entries[0].entry_id) await hass.async_block_till_done() - assert len(hass.states.async_entity_ids(SENSOR_DOMAIN)) == 7 + assert len(hass.states.async_entity_ids(SENSOR_DOMAIN)) == 8 assert len(hass.states.async_entity_ids(BINARY_SENSOR_DOMAIN)) == 1 assert len(hass.states.async_entity_ids(DOMAIN)) == 0 diff --git a/tests/test_sensors.py b/tests/test_sensors.py index 85f1330..66c8efe 100644 --- a/tests/test_sensors.py +++ b/tests/test_sensors.py @@ -1,5 +1,6 @@ """Tests for sensors.""" +import logging import pytest from pytest_homeassistant_custom_component.common import MockConfigEntry @@ -7,6 +8,7 @@ from tests.const import CONFIG_DATA FAKE_MINCHARGE_SENSOR = "sensor.fake_utility_co_minimum_charge" +FAKE_FIXEDCHARGE_SENSOR = "sensor.fake_utility_co_fixed_charge_first_meter" FAKE_CURRENT_RATE_SENSOR = "sensor.fake_utility_co_current_energy_rate" FAKE_CURRENT_RATE_STRUCTURE_SENSOR = ( "sensor.fake_utility_co_current_energy_rate_structure" @@ -15,25 +17,29 @@ pytestmark = pytest.mark.asyncio -async def test_sensors(hass, mock_api): +async def test_sensors(hass, mock_api, caplog): """Test settting up entities.""" entry = MockConfigEntry( domain=DOMAIN, title="Fake Utility Co", data=CONFIG_DATA, ) + with caplog.at_level(logging.DEBUG): + entry.add_to_hass(hass) + assert await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() - entry.add_to_hass(hass) - assert await hass.config_entries.async_setup(entry.entry_id) - await hass.async_block_till_done() + state = hass.states.get(FAKE_MINCHARGE_SENSOR) + assert state is not None + assert state.state == "unknown" - state = hass.states.get(FAKE_MINCHARGE_SENSOR) - assert state is not None - assert state.state == "unknown" + state = hass.states.get(FAKE_CURRENT_RATE_SENSOR) + assert state is not None + assert state.attributes["all_rates"] == [0.24477, 0.06118, 0.19847, 0.06116] - state = hass.states.get(FAKE_CURRENT_RATE_SENSOR) - assert state is not None - assert state.attributes["all_rates"] == [0.24477, 0.06118, 0.19847, 0.06116] + state = hass.states.get(FAKE_CURRENT_RATE_STRUCTURE_SENSOR) + assert state is not None - state = hass.states.get(FAKE_CURRENT_RATE_STRUCTURE_SENSOR) - assert state is not None + state = hass.states.get(FAKE_FIXEDCHARGE_SENSOR) + assert state is not None + assert state.state == "16.91" \ No newline at end of file From 9062f9661ba6ab8bef8c60378d760fc259001c00 Mon Sep 17 00:00:00 2001 From: "firstof9@gmail.com" Date: Sun, 8 Dec 2024 19:40:08 -0700 Subject: [PATCH 2/2] formatting --- custom_components/openei/const.py | 2 +- tests/test_sensors.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/custom_components/openei/const.py b/custom_components/openei/const.py index 5fdb446..abe2b1b 100644 --- a/custom_components/openei/const.py +++ b/custom_components/openei/const.py @@ -94,7 +94,7 @@ name="Fixed Charge (first meter)", icon="mdi:cash-multiple", entity_category=EntityCategory.DIAGNOSTIC, - ), + ), } BINARY_SENSORS: Final[dict[str, BinarySensorEntityDescription]] = { diff --git a/tests/test_sensors.py b/tests/test_sensors.py index 66c8efe..79def72 100644 --- a/tests/test_sensors.py +++ b/tests/test_sensors.py @@ -42,4 +42,4 @@ async def test_sensors(hass, mock_api, caplog): state = hass.states.get(FAKE_FIXEDCHARGE_SENSOR) assert state is not None - assert state.state == "16.91" \ No newline at end of file + assert state.state == "16.91"