Skip to content

Commit

Permalink
fix: make all_rates an attribute of current_rate (#69)
Browse files Browse the repository at this point in the history
* fix: make all_rates an attribute of current_rate

* adjust tests

* fix test sensor name

* allow coordinator to get all_rates sensor

* update mock_api
  • Loading branch information
firstof9 authored Jan 28, 2023
1 parent 04b2dd7 commit 85e94d4
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 4 deletions.
1 change: 0 additions & 1 deletion custom_components/openei/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@
key="all_rates",
name="All Listed Rates",
icon="mdi:format-list-bulleted",
entity_category=EntityCategory.DIAGNOSTIC,
),
"monthly_tier_rate": SensorEntityDescription(
key="monthly_tier_rate",
Expand Down
4 changes: 4 additions & 0 deletions custom_components/openei/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ async def async_setup_entry(hass, entry, async_add_devices):

sensors = []
for sensor in SENSOR_TYPES:
if sensor == "all_rates":
continue
sensors.append(OpenEISensor(hass, SENSOR_TYPES[sensor], entry, coordinator))

async_add_devices(sensors, False)
Expand Down Expand Up @@ -72,6 +74,8 @@ def extra_state_attributes(self) -> Optional[dict]:
"""Return sesnsor attributes."""
attrs = {}
attrs[ATTR_ATTRIBUTION] = ATTRIBUTION
if self._key == "current_rate":
attrs["all_rates"] = self.coordinator.data.get("all_rates")
return attrs

@property
Expand Down
2 changes: 2 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def mock_api():
mock_api.return_value.lookup_plans = (
'"Fake Utility Co": [{"name": "Fake Plan Name", "label": "randomstring"}]'
)
mock_api.return_value.all_rates = [ 0.24477, 0.007 ]

yield mock_api

Expand Down Expand Up @@ -53,6 +54,7 @@ def mock_get_sensors():
"rate_name": "Fake Test Rate",
"mincharge": 10,
"mincharge_uom": "$/month",
"all_rates": [ 0.24477, 0.007 ],
}
yield mock_sensors

Expand Down
6 changes: 3 additions & 3 deletions tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async def test_setup_entry(hass, mock_sensors, 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)) == 6
assert len(hass.states.async_entity_ids(BINARY_SENSOR_DOMAIN)) == 1
entries = hass.config_entries.async_entries(DOMAIN)
assert len(entries) == 1
Expand All @@ -42,14 +42,14 @@ async def test_unload_entry(hass, mock_sensors, 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)) == 6
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)) == 6
assert len(hass.states.async_entity_ids(BINARY_SENSOR_DOMAIN)) == 1
assert len(hass.states.async_entity_ids(DOMAIN)) == 0

Expand Down
6 changes: 6 additions & 0 deletions tests/test_sensors.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from tests.const import CONFIG_DATA

FAKE_MINCHARGE_SENSOR = "sensor.fake_utility_co_minimum_charge"
FAKE_CURRENT_RATE_SENSOR = "sensor.fake_utility_co_current_energy_rate"


async def test_sensors(hass, mock_sensors, mock_api):
Expand All @@ -24,3 +25,8 @@ async def test_sensors(hass, mock_sensors, mock_api):
assert state is not None
assert state.state == "10"
assert state.attributes["unit_of_measurement"] == "$/month"

state = hass.states.get(FAKE_CURRENT_RATE_SENSOR)
assert state is not None
assert state.state == "0.24477"
assert state.attributes["all_rates"] == [ 0.24477, 0.007 ]

0 comments on commit 85e94d4

Please sign in to comment.