Skip to content

Commit

Permalink
fix: multi switch setup using GUI (#2623)
Browse files Browse the repository at this point in the history
  • Loading branch information
bramstroker authored Oct 26, 2024
1 parent 323ad1b commit 075152c
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 8 deletions.
5 changes: 5 additions & 0 deletions custom_components/powercalc/const.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""The Powercalc constants."""

from __future__ import annotations

from datetime import timedelta
from enum import StrEnum
from typing import Literal
Expand Down Expand Up @@ -207,6 +209,9 @@ class CalculationStrategy(StrEnum):
WLED = "wled"


CALCULATION_STRATEGY_CONF_KEYS: list[str] = [strategy.value for strategy in CalculationStrategy]


class SensorType(StrEnum):
"""Possible modes for a number selector."""

Expand Down
10 changes: 3 additions & 7 deletions custom_components/powercalc/sensors/power.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,27 +50,23 @@
ATTR_INTEGRATION,
ATTR_SOURCE_DOMAIN,
ATTR_SOURCE_ENTITY,
CALCULATION_STRATEGY_CONF_KEYS,
CONF_CALCULATION_ENABLED_CONDITION,
CONF_COMPOSITE,
CONF_DELAY,
CONF_DISABLE_EXTENDED_ATTRIBUTES,
CONF_DISABLE_STANDBY_POWER,
CONF_FIXED,
CONF_FORCE_UPDATE_FREQUENCY,
CONF_IGNORE_UNAVAILABLE_STATE,
CONF_LINEAR,
CONF_MODEL,
CONF_MULTIPLY_FACTOR,
CONF_MULTIPLY_FACTOR_STANDBY,
CONF_PLAYBOOK,
CONF_POWER,
CONF_POWER_SENSOR_CATEGORY,
CONF_POWER_SENSOR_ID,
CONF_POWER_SENSOR_PRECISION,
CONF_SLEEP_POWER,
CONF_STANDBY_POWER,
CONF_UNAVAILABLE_POWER,
CONF_WLED,
DATA_CALCULATOR_FACTORY,
DATA_DISCOVERY_MANAGER,
DATA_STANDBY_POWER_SENSORS,
Expand Down Expand Up @@ -299,11 +295,11 @@ def is_manually_configured(sensor_config: ConfigType) -> bool:
"""
if CONF_MODEL in sensor_config:
return False
return any(key in sensor_config for key in [CONF_LINEAR, CONF_FIXED, CONF_PLAYBOOK, CONF_COMPOSITE])
return any(key in sensor_config for key in CALCULATION_STRATEGY_CONF_KEYS)


def is_fully_configured(config: ConfigType) -> bool:
return any(key in config for key in [CONF_LINEAR, CONF_WLED, CONF_FIXED, CONF_PLAYBOOK])
return any(key in config for key in CALCULATION_STRATEGY_CONF_KEYS)


class PowerSensor(BaseEntity):
Expand Down
37 changes: 36 additions & 1 deletion tests/config_flow/test_virtual_power_multi_switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
CalculationStrategy,
SensorType,
)
from tests.common import get_test_config_dir
from tests.common import get_test_config_dir, run_powercalc_setup
from tests.config_flow.common import (
DEFAULT_UNIQUE_ID,
create_mock_entry,
Expand Down Expand Up @@ -170,3 +170,38 @@ async def test_options_flow(hass: HomeAssistant) -> None:

assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
assert entry.data[CONF_MULTI_SWITCH] == {CONF_POWER: 5, CONF_POWER_OFF: 20, CONF_ENTITIES: ["switch.a", "switch.c"]}


async def test_regression_2612(hass: HomeAssistant, mock_entity_with_model_information: MockEntityWithModel) -> None:
"""
See #2612
When the source entity had manufacturer and model information the multi switch setup would fail
And raise error "Model not found in library" in the logs
"""

mock_entity_with_model_information(
"switch.test",
"_TZ3000_u3oupgdy",
"TS0004",
unique_id=DEFAULT_UNIQUE_ID,
)

create_mock_entry(
hass,
{
CONF_ENTITY_ID: "switch.test",
CONF_SENSOR_TYPE: SensorType.VIRTUAL_POWER,
CONF_MODE: CalculationStrategy.MULTI_SWITCH,
CONF_MULTI_SWITCH: {
CONF_POWER: 10,
CONF_POWER_OFF: 40,
CONF_ENTITIES: ["switch.a", "switch.b"],
},
CONF_NAME: "Foo bar",
},
)

await run_powercalc_setup(hass, {})

assert hass.states.get("sensor.foo_bar_power")
assert hass.states.get("sensor.foo_bar_energy")

0 comments on commit 075152c

Please sign in to comment.