Skip to content

Commit eca4239

Browse files
committed
feat: 100% coverage
1 parent 3b5acfd commit eca4239

File tree

4 files changed

+45
-7
lines changed

4 files changed

+45
-7
lines changed

custom_components/powercalc/analytics/analytics.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,6 @@ def add(self, key: str, value: Any) -> None: # noqa: ANN401
125125

126126
def set_flag(self, key: str) -> None:
127127
"""Set a boolean flag to True"""
128-
if self._already_seen(key):
129-
return
130-
131128
self._data[key] = True # type:ignore
132129

133130

custom_components/powercalc/discovery.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ async def initialize_existing_entries(self) -> None:
133133
"""Build a list of config entries which are already setup, to prevent duplicate discovery flows"""
134134
for entry in self.hass.config_entries.async_entries(DOMAIN):
135135
if not entry.unique_id:
136-
continue
136+
continue # pragma: no cover
137137

138138
self.initialized_flows.add(entry.unique_id)
139139
entity_id = entry.data.get(CONF_ENTITY_ID)

custom_components/powercalc/select.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ async def async_setup_platform(
5252
key = _key(None)
5353
pending = hass.data[DOMAIN].setdefault(DATA_PENDING_SELECT_ENTITIES, {}).pop(key, [])
5454
if pending:
55-
_LOGGER.debug("Adding TariffSelect entities pending")
56-
async_add_entities(pending)
55+
_LOGGER.debug("Adding TariffSelect entities pending") # pragma: no cover
56+
async_add_entities(pending) # pragma: no cover
5757

5858
delayed_add_entities_handler(hass, async_add_entities)
5959

tests/test_sensor.py

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from datetime import timedelta
22
import logging
3-
from unittest.mock import patch
3+
from unittest.mock import AsyncMock, patch
44

55
from homeassistant.components import light, sensor
66
from homeassistant.components.integration.sensor import ATTR_SOURCE_ID
@@ -59,6 +59,7 @@
5959
CalculationStrategy,
6060
SensorType,
6161
)
62+
from custom_components.powercalc.errors import SensorConfigurationError
6263

6364
from .common import (
6465
create_input_boolean,
@@ -195,6 +196,46 @@ async def test_create_nested_group_sensor(hass: HomeAssistant) -> None:
195196
assert group2.state == "0.00"
196197

197198

199+
async def test_create_nested_without_group(hass: HomeAssistant) -> None:
200+
await run_powercalc_setup(
201+
hass,
202+
{
203+
CONF_ENTITIES: [
204+
get_simple_fixed_config("switch.test1", 50),
205+
{
206+
CONF_ENTITIES: [
207+
get_simple_fixed_config("switch.test2", 50),
208+
],
209+
},
210+
],
211+
},
212+
)
213+
assert hass.states.get("sensor.test1_power")
214+
assert hass.states.get("sensor.test2_power")
215+
216+
217+
async def test_create_nested_handles_exception(hass: HomeAssistant, caplog: pytest.LogCaptureFixture) -> None:
218+
caplog.set_level(logging.ERROR)
219+
220+
with patch(
221+
"custom_components.powercalc.sensor.attach_entities_to_source_device",
222+
new=AsyncMock(side_effect=SensorConfigurationError("My custom error message")),
223+
):
224+
await run_powercalc_setup(
225+
hass,
226+
{
227+
CONF_ENTITIES: [
228+
get_simple_fixed_config("switch.test1", 50),
229+
get_simple_fixed_config("switch.test2", 50),
230+
],
231+
},
232+
)
233+
assert not hass.states.get("sensor.test1_power")
234+
assert not hass.states.get("sensor.test2_power")
235+
236+
assert "My custom error message" in caplog.text
237+
238+
198239
async def test_light_lut_strategy(
199240
hass: HomeAssistant,
200241
mock_entity_with_model_information: MockEntityWithModel,

0 commit comments

Comments
 (0)