Skip to content

Commit

Permalink
HACS update to PowerCalc #16
Browse files Browse the repository at this point in the history
  • Loading branch information
BeardedTinker committed May 18, 2024
1 parent 226d481 commit 7690ef1
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 32 deletions.
23 changes: 16 additions & 7 deletions custom_components/powercalc/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,21 +431,22 @@ async def async_step_daily_energy(
) -> ConfigFlowResult:
errors = _validate_daily_energy_input(user_input)

schema = _create_daily_energy_schema(self.hass)
if user_input is not None and not errors:
self.selected_sensor_type = SensorType.DAILY_ENERGY
self.name = user_input.get(CONF_NAME)
unique_id = user_input.get(CONF_UNIQUE_ID) or user_input.get(CONF_NAME)
await self.async_set_unique_id(unique_id)
self._abort_if_unique_id_configured()

self.sensor_config.update(_build_daily_energy_config(user_input))
self.sensor_config.update(_build_daily_energy_config(user_input, schema))
if self.sensor_config.get(CONF_CREATE_UTILITY_METERS):
return await self.async_step_utility_meter_options()
return self.create_config_entry()

return self.async_show_form(
step_id="daily_energy",
data_schema=SCHEMA_DAILY_ENERGY,
data_schema=schema,
errors=errors,
)

Expand Down Expand Up @@ -856,7 +857,7 @@ async def save_options(
self._process_user_input(user_input, schema)

if self.sensor_type == SensorType.DAILY_ENERGY:
self.current_config.update(_build_daily_energy_config(user_input))
self.current_config.update(_build_daily_energy_config(user_input, _create_daily_energy_schema(self.hass)))

if self.sensor_type == SensorType.VIRTUAL_POWER:
generic_option_schema = SCHEMA_POWER_OPTIONS.extend(
Expand Down Expand Up @@ -1028,6 +1029,14 @@ def _create_virtual_power_schema(
return schema.extend(power_options.schema) # type: ignore


def _create_daily_energy_schema(hass: HomeAssistant) -> vol.Schema:
return SCHEMA_DAILY_ENERGY.extend( # type: ignore
{
vol.Optional(CONF_GROUP): _create_group_selector(hass),
},
)


def _create_schema_advanced(sensor_config: ConfigType) -> vol.Schema:
schema = SCHEMA_POWER_ADVANCED

Expand Down Expand Up @@ -1136,6 +1145,7 @@ def _create_group_selector(
options=options,
multiple=multiple,
mode=selector.SelectSelectorMode.DROPDOWN,
custom_value=True,
),
)

Expand Down Expand Up @@ -1252,18 +1262,17 @@ def _build_strategy_config(
return strategy_options


def _build_daily_energy_config(user_input: dict[str, Any]) -> dict[str, Any]:
def _build_daily_energy_config(user_input: dict[str, Any], schema: vol.Schema) -> dict[str, Any]:
"""Build the config under daily_energy: key."""
schema = SCHEMA_DAILY_ENERGY_OPTIONS
config: dict[str, Any] = {
CONF_DAILY_FIXED_ENERGY: {},
}
for key in schema.schema:
val = user_input.get(key)
if val is None:
continue
if key == CONF_CREATE_UTILITY_METERS:
config[CONF_CREATE_UTILITY_METERS] = val
if key in [CONF_CREATE_UTILITY_METERS, CONF_GROUP, CONF_NAME, CONF_UNIQUE_ID]:
config[key] = val
continue

config[CONF_DAILY_FIXED_ENERGY][str(key)] = val
Expand Down
2 changes: 1 addition & 1 deletion custom_components/powercalc/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@
"requirements": [
"numpy>=1.21.1"
],
"version": "v1.12.2"
"version": "v1.12.3"
}
21 changes: 19 additions & 2 deletions custom_components/powercalc/sensors/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
SensorEntity,
SensorStateClass,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
from homeassistant.const import (
ATTR_ENTITY_ID,
ATTR_UNIT_OF_MEASUREMENT,
Expand Down Expand Up @@ -52,6 +52,7 @@
PowerConverter,
)

from custom_components.powercalc.config_flow import ConfigFlow
from custom_components.powercalc.const import (
ATTR_ENTITIES,
ATTR_IS_GROUP,
Expand Down Expand Up @@ -279,7 +280,7 @@ async def add_to_associated_group(
we need to add this config entry to the group members sensors and update the group.
"""
sensor_type = config_entry.data.get(CONF_SENSOR_TYPE)
if sensor_type != SensorType.VIRTUAL_POWER:
if sensor_type not in [SensorType.VIRTUAL_POWER, SensorType.DAILY_ENERGY]:
return None

if CONF_GROUP not in config_entry.data:
Expand All @@ -288,6 +289,22 @@ async def add_to_associated_group(
group_entry_id = str(config_entry.data.get(CONF_GROUP))
group_entry = hass.config_entries.async_get_entry(group_entry_id)

# When we are not dealing with a uuid, the user has set a group name manually
# Create a new group entry for this group
if not group_entry and len(group_entry_id) != 32:
group_entry = ConfigEntry(
version=ConfigFlow.VERSION,
minor_version=ConfigFlow.MINOR_VERSION,
domain=DOMAIN,
source=SOURCE_IMPORT,
title=group_entry_id,
data={
CONF_SENSOR_TYPE: SensorType.GROUP,
CONF_NAME: group_entry_id,
},
)
await hass.config_entries.async_add(group_entry)

if not group_entry:
_LOGGER.warning(
"ConfigEntry %s: Cannot add/remove to group %s. It does not exist.",
Expand Down
24 changes: 2 additions & 22 deletions custom_components/powercalc/strategy/lut.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,12 @@
ATTR_HS_COLOR,
COLOR_MODES_COLOR,
ColorMode,
filter_supported_color_modes,
)
from homeassistant.core import State

from custom_components.powercalc.common import SourceEntity
from custom_components.powercalc.errors import (
LutFileNotFoundError,
ModelNotSupportedError,
StrategyConfigurationError,
)
from custom_components.powercalc.power_profile.power_profile import PowerProfile
Expand Down Expand Up @@ -134,8 +132,8 @@ async def calculate(self, entity_state: State) -> Decimal | None:
color_mode,
)
except LutFileNotFoundError:
_LOGGER.warning(
"%s: Lookup table not found (model: %s, color_mode: %s)",
_LOGGER.error(
"%s: Lookup table not found for color mode (model: %s, color_mode: %s)",
entity_state.entity_id,
self._profile.model,
color_mode,
Expand Down Expand Up @@ -268,24 +266,6 @@ async def validate_config(self) -> None:
"lut_unsupported_color_mode",
)

color_modes = self._source_entity.supported_color_modes
if not color_modes:
return
for color_mode in filter_supported_color_modes(color_modes):
if color_mode in COLOR_MODES_COLOR:
color_mode = ColorMode.HS
if color_mode in LUT_COLOR_MODES:
try:
await self._lut_registry.get_lookup_dictionary(
self._profile,
color_mode,
)
except LutFileNotFoundError:
raise ModelNotSupportedError( # noqa: B904
f"No lookup file found for mode: {color_mode}",
"lut_unsupported_color_mode",
)


@dataclass
class LightSetting:
Expand Down

0 comments on commit 7690ef1

Please sign in to comment.