Skip to content

Commit

Permalink
Commit WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
bramstroker committed Aug 12, 2022
1 parent a87847b commit e3c308c
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 3 deletions.
23 changes: 22 additions & 1 deletion custom_components/powercalc/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
CONF_FIXED,
CONF_GAMMA_CURVE,
CONF_GROUP_ENERGY_ENTITIES,
CONF_GROUP_MEMBER_SENSORS,
CONF_GROUP_POWER_ENTITIES,
CONF_HIDE_MEMBERS,
CONF_LINEAR,
Expand Down Expand Up @@ -599,7 +600,27 @@ def _create_group_schema(hass: HomeAssistant, base_schema: vol.Schema) -> vol.Sc
options=sub_groups, multiple=True, mode=selector.SelectSelectorMode.DROPDOWN
)
)
return base_schema.extend({vol.Optional(CONF_SUB_GROUPS): sub_group_selector})

member_sensors = [
selector.SelectOptionDict(
value=config_entry.entry_id, label=config_entry.data.get(CONF_NAME)
)
for config_entry in hass.config_entries.async_entries(DOMAIN)
if config_entry.data.get(CONF_SENSOR_TYPE) == SensorType.VIRTUAL_POWER
and config_entry.unique_id is not None
]
member_sensor_selector = selector.SelectSelector(
selector.SelectSelectorConfig(
options=member_sensors, multiple=True, mode=selector.SelectSelectorMode.DROPDOWN
)
)

return base_schema.extend(
{
vol.Optional(CONF_GROUP_MEMBER_SENSORS): member_sensor_selector,
vol.Optional(CONF_SUB_GROUPS): sub_group_selector
}
)


def _validate_group_input(user_input: dict[str, str] = None) -> dict:
Expand Down
2 changes: 2 additions & 0 deletions custom_components/powercalc/sensors/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ def resolve_entity_ids_recursively(
# Include the power/energy sensors for an existing Virtual Power config entry
entity_reg = er.async_get(hass)
member_entry_ids = entry.data.get(CONF_GROUP_MEMBER_SENSORS)
# Unfortunately device_class is not correctly set at this time in the entity_registry
# So we need to match on state_class.
state_class = SensorStateClass.MEASUREMENT if device_class == SensorDeviceClass.POWER else SensorStateClass.TOTAL
entities = [
entity_entry.entity_id for entity_entry
Expand Down
33 changes: 32 additions & 1 deletion tests/common.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
from homeassistant import config_entries
from homeassistant.components import input_boolean, input_number, light, sensor
from homeassistant.components.light import ColorMode
from homeassistant.const import CONF_ENTITY_ID, CONF_PLATFORM, STATE_ON
from homeassistant.const import (
CONF_ENTITY_ID,
CONF_NAME,
CONF_PLATFORM,
CONF_UNIQUE_ID,
STATE_ON,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr
from homeassistant.helpers import entity_registry as er
Expand All @@ -16,8 +23,11 @@
CONF_FIXED,
CONF_MODE,
CONF_POWER,
CONF_SENSOR_TYPE,
DOMAIN,
DUMMY_ENTITY_ID,
CalculationStrategy,
SensorType,
)


Expand Down Expand Up @@ -122,6 +132,27 @@ def get_simple_fixed_config(entity_id: str, power: float = 50) -> ConfigType:
CONF_FIXED: {CONF_POWER: power},
}

async def create_mocked_virtual_power_sensor_entry(hass: HomeAssistant, name: str, unique_id: str | None) -> config_entries.ConfigEntry:
config_entry = MockConfigEntry(
domain=DOMAIN,
unique_id=unique_id,
data={
CONF_SENSOR_TYPE: SensorType.VIRTUAL_POWER,
CONF_UNIQUE_ID: unique_id,
CONF_ENTITY_ID: DUMMY_ENTITY_ID,
CONF_NAME: name,
CONF_MODE: CalculationStrategy.FIXED,
CONF_FIXED: {
CONF_POWER: 50
}
},
)

config_entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
return config_entry


def assert_entity_state(hass: HomeAssistant, entity_id: str, expected_state: StateType):
state = hass.states.get(entity_id)
Expand Down
19 changes: 18 additions & 1 deletion tests/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
CONF_CREATE_UTILITY_METERS,
CONF_DAILY_FIXED_ENERGY,
CONF_FIXED,
CONF_GROUP_MEMBER_SENSORS,
CONF_GROUP_POWER_ENTITIES,
CONF_HIDE_MEMBERS,
CONF_LINEAR,
Expand All @@ -54,7 +55,7 @@
from custom_components.powercalc.errors import StrategyConfigurationError
from custom_components.test.light import MockLight

from .common import MockConfigEntry, create_mock_light_entity
from .common import MockConfigEntry, create_mock_light_entity, create_mocked_virtual_power_sensor_entry

DEFAULT_ENTITY_ID = "light.test"
DEFAULT_UNIQUE_ID = "7c009ef6829f"
Expand Down Expand Up @@ -379,6 +380,22 @@ async def test_create_group_entry(hass: HomeAssistant):
await hass.async_block_till_done()
assert hass.states.get("sensor.my_group_sensor_power")

async def test_can_select_existing_powercalc_entry_as_group_member(hass: HomeAssistant):
"""
Test if we can select previously created virtual power config entries as the group member.
Only entries with a unique ID must be selectable
"""

config_entry_1 = await create_mocked_virtual_power_sensor_entry(hass, "VirtualPower1", "abcdef")
config_entry_2 = await create_mocked_virtual_power_sensor_entry(hass, "VirtualPower2", None)

result = await _select_sensor_type(hass, SensorType.GROUP)
assert result["type"] == data_entry_flow.FlowResultType.FORM
data_schema: vol.Schema = result["data_schema"]
select: SelectSelector = data_schema.schema[CONF_GROUP_MEMBER_SENSORS]
options = select.config["options"]
assert {"value": config_entry_1.entry_id, "label": "VirtualPower1"} in options
assert {"value": config_entry_2.entry_id, "label": "VirtualPower2"} not in options

async def test_group_error_mandatory(hass: HomeAssistant):
result = await _select_sensor_type(hass, SensorType.GROUP)
Expand Down

0 comments on commit e3c308c

Please sign in to comment.