Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add fixed charges sensor #88

Merged
merged 2 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions custom_components/openei/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]] = {
Expand Down
2 changes: 1 addition & 1 deletion custom_components/openei/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
2 changes: 1 addition & 1 deletion requirements_tests.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-r requirements_dev.txt
python_openei==0.2.3
python_openei
pytest
pytest-cov
pytest-homeassistant-custom-component
Expand Down
6 changes: 3 additions & 3 deletions tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
30 changes: 18 additions & 12 deletions tests/test_sensors.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
"""Tests for sensors."""

import logging
import pytest
from pytest_homeassistant_custom_component.common import MockConfigEntry

from custom_components.openei.const import DOMAIN
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"
Expand All @@ -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"
Loading