Skip to content

Commit

Permalink
Merge d71d616 into 62a993c
Browse files Browse the repository at this point in the history
  • Loading branch information
bramstroker committed Jul 31, 2022
2 parents 62a993c + d71d616 commit 737a1fd
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 25 deletions.
21 changes: 15 additions & 6 deletions custom_components/powercalc/config_flow.py
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
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
16 changes: 2 additions & 14 deletions custom_components/powercalc/strategy/lut.py
Expand Up @@ -230,19 +230,7 @@ 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
)
return
raise StrategyConfigurationError("Only light entities can use the LUT mode", "lut_unsupported_color_mode")

for color_mode in self._source_entity.supported_color_modes:
if color_mode in LUT_COLOR_MODES:
Expand All @@ -251,7 +239,7 @@ 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
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
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
11 changes: 11 additions & 0 deletions tests/strategy/test_lut.py
Expand Up @@ -115,6 +115,17 @@ 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,
Expand Down
8 changes: 8 additions & 0 deletions tests/test_config_flow.py
Expand Up @@ -248,6 +248,14 @@ async def test_lut_autodiscover_flow(hass: HomeAssistant):
assert hass.states.get("sensor.test_power")
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):
"""
Expand Down

0 comments on commit 737a1fd

Please sign in to comment.