Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bramstroker committed Dec 3, 2022
1 parent 2346f94 commit e8f956a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
9 changes: 6 additions & 3 deletions custom_components/powercalc/power_profile/power_profile.py
Expand Up @@ -14,7 +14,7 @@
from homeassistant.helpers.typing import ConfigType

from ..common import SourceEntity
from ..const import CalculationStrategy
from ..const import CalculationStrategy, CONF_POWER
from ..errors import ModelNotSupported, PowercalcSetupError, UnsupportedStrategy

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -127,13 +127,16 @@ def linear_mode_config(self) -> ConfigType | None:
return self._json_data.get("linear_config")

@property
def fixed_mode_config(self) -> ConfigType | None:
def fixed_mode_config(self) -> ConfigType:
"""Get configuration to setup fixed strategy"""
if not self.is_strategy_supported(CalculationStrategy.FIXED):
raise UnsupportedStrategy(
f"Strategy fixed is not supported by model: {self._model}"
)
return self._json_data.get("fixed_config")
fixed_config = self._json_data.get("fixed_config")
if fixed_config is None and self.standby_power_on:
fixed_config = {CONF_POWER: 0}
return fixed_config

@property
def sensor_config(self) -> ConfigType:
Expand Down
15 changes: 10 additions & 5 deletions tests/power_profile/device_types/test_smart_switch.py
@@ -1,5 +1,3 @@
import os

from homeassistant.const import CONF_ENTITY_ID, STATE_OFF, STATE_ON
from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceEntry
Expand All @@ -11,8 +9,10 @@

from custom_components.powercalc.const import (
CONF_CUSTOM_MODEL_DIRECTORY,
CONF_FIXED,
CONF_MANUFACTURER,
CONF_MODEL,
CONF_POWER,
)
from tests.common import get_test_profile_dir, run_powercalc_setup_yaml_config

Expand Down Expand Up @@ -72,9 +72,11 @@ async def test_smart_switch(hass: HomeAssistant):
assert hass.states.get(power_sensor_id).state == "0.52"


async def test_smart_switch_power_input(hass: HomeAssistant):
async def test_smart_switch_power_input_yaml(hass: HomeAssistant):
"""
Test that smart plug can be setup from profile library
Test a smart switch can be setup with YAML and a fixed power value for the appliance configured by the user
The values for standby power on and off should be taken from the power profile library.
The fixed power value from the user should be added to the total power consumption. standby_power_on + power
"""
switch_id = "switch.heater"
manufacturer = "IKEA"
Expand Down Expand Up @@ -109,6 +111,9 @@ async def test_smart_switch_power_input(hass: HomeAssistant):
CONF_MANUFACTURER: manufacturer,
CONF_MODEL: model,
CONF_CUSTOM_MODEL_DIRECTORY: get_test_profile_dir("smart_switch"),
CONF_FIXED: {
CONF_POWER: 50
}
},
)

Expand All @@ -119,7 +124,7 @@ async def test_smart_switch_power_input(hass: HomeAssistant):
hass.states.async_set(switch_id, STATE_ON)
await hass.async_block_till_done()

assert hass.states.get(power_sensor_id).state == "0.82"
assert hass.states.get(power_sensor_id).state == "50.82"

hass.states.async_set(switch_id, STATE_OFF)
await hass.async_block_till_done()
Expand Down

0 comments on commit e8f956a

Please sign in to comment.