diff --git a/custom_components/powercalc/strategy/lut.py b/custom_components/powercalc/strategy/lut.py index 134e32eeb..8714b4cf5 100644 --- a/custom_components/powercalc/strategy/lut.py +++ b/custom_components/powercalc/strategy/lut.py @@ -93,6 +93,10 @@ def get_lut_file(power_profile: PowerProfile, color_mode: ColorMode) -> TextIO: _LOGGER.debug("Loading LUT data file: %s", gzip_path) return gzip.open(gzip_path, "rt") + if os.path.exists(path): + _LOGGER.debug("Loading LUT data file: %s", path) + return open(path) + raise LutFileNotFoundError("Data file not found: %s") async def get_supported_color_modes(self, power_profile: PowerProfile) -> set[ColorMode]: @@ -102,8 +106,9 @@ async def get_supported_color_modes(self, power_profile: PowerProfile) -> set[Co if supported_color_modes is None: supported_color_modes = set() for file in await self._hass.async_add_executor_job(os.listdir, power_profile.get_model_directory()): - if file.endswith(".csv.gz"): - color_mode = ColorMode(file.removesuffix(".csv.gz")) + if file.endswith((".csv.gz", ".csv")): + base_name = file.split(".", 1)[0] + color_mode = ColorMode(base_name) if color_mode in LUT_COLOR_MODES: supported_color_modes.add(color_mode) self._supported_color_modes[cache_key] = supported_color_modes diff --git a/tests/strategy/test_lut.py b/tests/strategy/test_lut.py index bb559806b..83a9c2c92 100644 --- a/tests/strategy/test_lut.py +++ b/tests/strategy/test_lut.py @@ -21,7 +21,7 @@ from custom_components.powercalc.strategy.strategy_interface import ( PowerCalculationStrategyInterface, ) -from tests.common import run_powercalc_setup +from tests.common import get_test_config_dir, run_powercalc_setup from tests.strategy.common import create_source_entity @@ -276,11 +276,30 @@ async def test_warning_is_logged_when_color_mode_is_none(hass: HomeAssistant, ca assert "color mode unknown" in caplog.text +async def test_fallback_to_non_gzipped_file(hass: HomeAssistant) -> None: + """ + Test that a fallback is done when a gzipped file is not available. + See: https://github.com/bramstroker/homeassistant-powercalc/issues/2798 + """ + strategy = await _create_lut_strategy( + hass, + "test", + "test", + custom_profile_dir=get_test_config_dir("powercalc_profiles/lut-non-gzipped"), + ) + await _calculate_and_assert_power( + strategy, + state=_create_light_color_temp_state(1, 153), + expected_power=0.96, + ) + + async def _create_lut_strategy( hass: HomeAssistant, manufacturer: str, model: str, source_entity: SourceEntity | None = None, + custom_profile_dir: str | None = None, ) -> PowerCalculationStrategyInterface: if not source_entity: source_entity = create_source_entity(LIGHT_DOMAIN) @@ -288,6 +307,7 @@ async def _create_lut_strategy( library = await ProfileLibrary.factory(hass) power_profile = await library.get_profile( ModelInfo(manufacturer, model), + custom_directory=custom_profile_dir, ) return await strategy_factory.create( config={}, diff --git a/tests/testing_config/powercalc_profiles/lut-non-gzipped/color_temp.csv b/tests/testing_config/powercalc_profiles/lut-non-gzipped/color_temp.csv new file mode 100644 index 000000000..915518c77 --- /dev/null +++ b/tests/testing_config/powercalc_profiles/lut-non-gzipped/color_temp.csv @@ -0,0 +1,17 @@ +bri,mired,watt +1,153,0.96 +1,163,0.89 +255,373,6.95 +255,383,6.73 +255,393,6.55 +255,403,6.34 +255,413,6.16 +255,423,5.83 +255,433,5.69 +255,443,5.44 +255,453,5.31 +255,463,5.1 +255,473,5.0 +255,483,5.07 +255,493,4.81 +255,500,4.78 diff --git a/tests/testing_config/powercalc_profiles/lut-non-gzipped/model.json b/tests/testing_config/powercalc_profiles/lut-non-gzipped/model.json new file mode 100644 index 000000000..7e0a11786 --- /dev/null +++ b/tests/testing_config/powercalc_profiles/lut-non-gzipped/model.json @@ -0,0 +1,15 @@ +{ + "measure_description": "Measured with utils/measure script", + "measure_device": "xx", + "measure_method": "script", + "measure_settings": { + "SAMPLE_COUNT": 2, + "SLEEP_TIME": 3, + "VERSION": "v1.9.8:docker" + }, + "name": "Test", + "standby_power": 0.3, + "calculation_strategy": "lut", + "created_at": "2023-12-06T18:41:16", + "author": "test" +}