Skip to content

Commit

Permalink
Merge branch 'master' of github.com:bramstroker/homeassistant-powercalc
Browse files Browse the repository at this point in the history
  • Loading branch information
bramstroker committed Jul 31, 2022
2 parents 27cd30f + 3ccebb9 commit 3f73346
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 32 deletions.
21 changes: 15 additions & 6 deletions custom_components/powercalc/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,9 +344,11 @@ async def async_step_lut(self, user_input: dict[str, str] = None) -> FlowResult:

return await self.async_step_lut_manufacturer()

model_info = await autodiscover_model(
self.hass, self.source_entity.entity_entry
)
model_info = None
if self.source_entity.entity_entry:
model_info = await autodiscover_model(
self.hass, self.source_entity.entity_entry
)
if model_info:
return self.async_show_form(
step_id="lut",
Expand Down Expand Up @@ -380,16 +382,19 @@ async def async_step_lut_manufacturer(
async def async_step_lut_model(
self, user_input: dict[str, str] = None
) -> FlowResult:
errors = {}
if user_input is not None:
self.sensor_config.update({CONF_MODEL: user_input.get(CONF_MODEL)})
return self.create_config_entry()
errors = await self.validate_strategy_config()
if not errors:
return self.create_config_entry()

return self.async_show_form(
step_id="lut_model",
data_schema=_create_lut_schema_model(
self.hass, self.sensor_config.get(CONF_MANUFACTURER)
),
errors={},
errors=errors,
)

async def validate_strategy_config(self) -> dict:
Expand All @@ -400,7 +405,11 @@ async def validate_strategy_config(self) -> dict:
try:
await strategy.validate_config()
except StrategyConfigurationError as error:
return {"base": error.get_config_flow_translate_key()}
translation = error.get_config_flow_translate_key()
if translation is None:
translation = "unknown"
_LOGGER.error(str(error))
return {"base": translation}
return {}

@callback
Expand Down
2 changes: 1 addition & 1 deletion custom_components/powercalc/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def __init__(self, message: str, config_flow_trans_key: str = None):
super().__init__(message)
self._config_flow_trans_key = config_flow_trans_key

def get_config_flow_translate_key(self) -> str:
def get_config_flow_translate_key(self) -> str | None:
return self._config_flow_trans_key


Expand Down
19 changes: 6 additions & 13 deletions custom_components/powercalc/strategy/lut.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,19 +230,9 @@ def get_nearest_higher_brightness(self, dict: dict, search_key: int) -> int:

async def validate_config(self):
if self._source_entity.domain != light.DOMAIN:
raise StrategyConfigurationError("Only light entities can use the LUT mode")

if self._model.manufacturer is None:
_LOGGER.error(
"Manufacturer not supplied for entity: %s",
self._source_entity.entity_id,
)

if self._model.model is None:
_LOGGER.error(
"Model not supplied for entity: %s", self._source_entity.entity_id
raise StrategyConfigurationError(
"Only light entities can use the LUT mode", "lut_unsupported_color_mode"
)
return

for color_mode in self._source_entity.supported_color_modes:
if color_mode in LUT_COLOR_MODES:
Expand All @@ -251,7 +241,10 @@ async def validate_config(self):
self._model, color_mode
)
except LutFileNotFound:
raise ModelNotSupported("No lookup file found for mode", color_mode)
raise ModelNotSupported(
f"No lookup file found for mode: {color_mode}",
"lut_unsupported_color_mode",
)


@dataclass
Expand Down
8 changes: 6 additions & 2 deletions custom_components/powercalc/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,10 @@
"daily_energy_mandatory": "You must supply at least one of Value or Value template",
"fixed_mandatory": "You must supply at least one of Power, Power template or States power",
"fixed_states_power_only": "This entity can only work with 'states_power' not 'power'",
"group_mandatory": "You must define at least subgroups or power and energy-entities"
"group_mandatory": "You must define at least subgroups or power and energy-entities",
"lut_unsupported_color_mode": "The LUT profile does not support one of the color modes of your light. See the logs for more info",
"lut_wrong_domain": "Only light entities can use the LUT mode",
"unknown": "Unknown error occured, please see the logs for additional information"
}
},
"options": {
Expand Down Expand Up @@ -172,7 +175,8 @@
"linear_min_higher_as_max": "[%key:component::powercalc::config::error::linear_min_higher_as_max%]",
"fixed_mandatory": "[%key:component::powercalc::config::error::fixed_mandatory%]",
"fixed_states_power_only": "[%key:component::powercalc::config::error::fixed_states_power_only%]",
"group_mandatory": "[%key:component::powercalc::config::error::group_mandatory%]"
"group_mandatory": "[%key:component::powercalc::config::error::group_mandatory%]",
"unknown": "[%key:component::powercalc::config::error::unknown%]"
}
}
}
8 changes: 6 additions & 2 deletions custom_components/powercalc/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
"fixed_states_power_only": "This entity can only work with 'states_power' not 'power'",
"group_mandatory": "You must define at least subgroups or power and energy-entities",
"linear_mandatory": "You must supply at least one of max_power or calibrate",
"linear_min_higher_as_max": "Max power cannot be lower than min power"
"linear_min_higher_as_max": "Max power cannot be lower than min power",
"lut_unsupported_color_mode": "The LUT profile does not support one of the color modes of your light. See the logs for more info",
"lut_wrong_domain": "Only light entities can use the LUT mode",
"unknown": "Unknown error occured, please see the logs for additional information"
},
"step": {
"daily_energy": {
Expand Down Expand Up @@ -138,7 +141,8 @@
"fixed_states_power_only": "This entity can only work with 'states_power' not 'power'",
"group_mandatory": "You must define at least subgroups or power and energy-entities",
"linear_mandatory": "You must supply at least one of max_power or calibrate",
"linear_min_higher_as_max": "Max power cannot be lower than min power"
"linear_min_higher_as_max": "Max power cannot be lower than min power",
"unknown": "Unknown error occured, please see the logs for additional information"
},
"step": {
"init": {
Expand Down
15 changes: 15 additions & 0 deletions tests/strategy/test_lut.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,21 @@ async def test_validation_fails_for_non_light_entities(hass: HomeAssistant):
await strategy.validate_config()


async def test_validation_fails_unsupported_color_mode(hass: HomeAssistant):
with pytest.raises(StrategyConfigurationError):
source_entity = create_source_entity("light", [ColorMode.COLOR_TEMP])
strategy_factory = PowerCalculatorStrategyFactory(hass)
strategy = strategy_factory.create(
config={},
strategy=CalculationStrategy.LUT,
light_model=LightModel(
hass, "signify", "LWA017", None
), # This model only supports brightness
source_entity=source_entity,
)
await strategy.validate_config()


def _create_lut_strategy(
hass: HomeAssistant,
manufacturer: str,
Expand Down
10 changes: 10 additions & 0 deletions tests/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,16 @@ async def test_lut_autodiscover_flow(hass: HomeAssistant):
assert hass.states.get("sensor.test_energy")


async def test_lut_not_autodiscovered(hass: HomeAssistant):
light_entity = MockLight("test", STATE_ON)
light_entity._attr_unique_id = None
await create_mock_light_entity(hass, light_entity)

result = await _goto_virtual_power_strategy_step(hass, CalculationStrategy.LUT)
assert result["type"] == data_entry_flow.FlowResultType.FORM
assert result["step_id"] == "lut_manufacturer"


async def test_lut_autodiscover_flow_not_confirmed(hass: HomeAssistant):
"""
When manufacturer and model are auto detected and user chooses to not accept it,
Expand Down
20 changes: 12 additions & 8 deletions tests/test_init.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
from homeassistant.components import input_boolean, light
from homeassistant.config_entries import ConfigEntryState
from homeassistant.const import (
CONF_ENTITY_ID,
CONF_NAME,
CONF_UNIQUE_ID,
STATE_UNAVAILABLE,
)
from homeassistant.core import HomeAssistant
from homeassistant.const import CONF_ENTITY_ID, CONF_NAME, CONF_UNIQUE_ID, STATE_UNAVAILABLE
from homeassistant.setup import async_setup_component
from homeassistant.helpers.entity_registry import EntityRegistry
from homeassistant.config_entries import ConfigEntryState
from homeassistant.setup import async_setup_component
from pytest_homeassistant_custom_component.common import MockConfigEntry

from custom_components.powercalc import create_domain_groups
from custom_components.powercalc.const import (
Expand All @@ -27,7 +33,6 @@
run_powercalc_setup_yaml_config,
)

from pytest_homeassistant_custom_component.common import MockConfigEntry

async def test_autodiscovery(hass: HomeAssistant):
"""Test that models are automatically discovered and power sensors created"""
Expand Down Expand Up @@ -100,6 +105,7 @@ async def test_domain_groups(hass: HomeAssistant):

assert not hass.states.get("sensor.all_light_power")


async def test_unload_entry(hass: HomeAssistant, entity_reg: EntityRegistry):
unique_id = "98493943242"
entry = MockConfigEntry(
Expand All @@ -109,9 +115,7 @@ async def test_unload_entry(hass: HomeAssistant, entity_reg: EntityRegistry):
CONF_UNIQUE_ID: unique_id,
CONF_NAME: "testentry",
CONF_ENTITY_ID: DUMMY_ENTITY_ID,
CONF_FIXED: {
CONF_POWER: 50
}
CONF_FIXED: {CONF_POWER: 50},
},
unique_id=unique_id,
)
Expand All @@ -128,4 +132,4 @@ async def test_unload_entry(hass: HomeAssistant, entity_reg: EntityRegistry):
assert entry.state is ConfigEntryState.NOT_LOADED

assert not hass.states.get("sensor.testentry_power")
assert not entity_reg.async_get("sensor.testentry_power")
assert not entity_reg.async_get("sensor.testentry_power")

0 comments on commit 3f73346

Please sign in to comment.