Skip to content

Commit

Permalink
Merge pull request #2800 from bramstroker/feat/non-gzip
Browse files Browse the repository at this point in the history
Support non gzipped files for LUT custom profiles
  • Loading branch information
bramstroker authored Dec 14, 2024
2 parents daf5246 + 2f36bd3 commit facaab1
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 3 deletions.
9 changes: 7 additions & 2 deletions custom_components/powercalc/strategy/lut.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand All @@ -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
Expand Down
22 changes: 21 additions & 1 deletion tests/strategy/test_lut.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -276,18 +276,38 @@ 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)
strategy_factory = PowerCalculatorStrategyFactory(hass)
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={},
Expand Down
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions tests/testing_config/powercalc_profiles/lut-non-gzipped/model.json
Original file line number Diff line number Diff line change
@@ -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"
}

0 comments on commit facaab1

Please sign in to comment.