Skip to content

Commit

Permalink
Use generic lut autodiscover flow
Browse files Browse the repository at this point in the history
  • Loading branch information
bramstroker committed Sep 23, 2022
1 parent 0918112 commit 7a7f9fc
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 33 deletions.
49 changes: 25 additions & 24 deletions custom_components/powercalc/config_flow.py
Expand Up @@ -62,6 +62,7 @@
SensorType,
)
from .errors import ModelNotSupported, StrategyConfigurationError
from .power_profile.power_profile import PowerProfile
from .power_profile.library import ModelInfo, ProfileLibrary
from .power_profile.model_discovery import get_power_profile
from .sensors.daily_energy import DEFAULT_DAILY_UPDATE_FREQUENCY
Expand Down Expand Up @@ -184,6 +185,8 @@ def __init__(self):
self.name: str | None = None
self.source_entity: SourceEntity | None = None
self.source_entity_id: str | None = None
self.power_profile: PowerProfile | None = None
self.skip_advanced_step: bool = False

@staticmethod
@callback
Expand All @@ -196,32 +199,25 @@ async def async_step_integration_discovery(
) -> FlowResult:
"""Handle integration discovery."""

self.skip_advanced_step = True # We don't want to ask advanced option when discovered
self.sensor_config.update(discovery_info)
self.sensor_config.update({CONF_MODE: CalculationStrategy.LUT})
self.selected_sensor_type = SensorType.VIRTUAL_POWER
self.name = discovery_info[CONF_NAME]
unique_id = discovery_info[CONF_UNIQUE_ID]
await self.async_set_unique_id(unique_id)
self._abort_if_unique_id_configured()

return await self.async_step_discovery_confirm()

async def async_step_discovery_confirm(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
"""Confirm discovery."""
self.source_entity_id = discovery_info[CONF_ENTITY_ID]
self.source_entity = await create_source_entity(
self.source_entity_id, self.hass
)

if user_input is not None:
return self.create_config_entry()
self._set_confirm_only()
placeholders = {
self.context["title_placeholders"] = {
"name": self.sensor_config.get(CONF_NAME),
"manufacturer": self.sensor_config.get(CONF_MANUFACTURER),
"model": self.sensor_config.get(CONF_MODEL),
}
self.context["title_placeholders"] = placeholders
return self.async_show_form(
step_id="discovery_confirm", description_placeholders=placeholders
)
return await self.async_step_lut()

async def async_step_user(self, user_input=None) -> FlowResult:
"""Handle the initial step."""
Expand Down Expand Up @@ -356,25 +352,30 @@ async def async_step_wled(self, user_input: dict[str, str] = None) -> FlowResult
async def async_step_lut(self, user_input: dict[str, str] = None) -> FlowResult:
"""Try to autodiscover manufacturer/model first. Ask the user to confirm this or forward to manual configuration"""
if user_input is not None:
if user_input.get(CONF_CONFIRM_AUTODISCOVERED_MODEL):
if user_input.get(CONF_CONFIRM_AUTODISCOVERED_MODEL) and self.power_profile:
self.sensor_config.update(
{
CONF_MANUFACTURER: self.power_profile.manufacturer,
CONF_MODEL: self.power_profile.model
}
)
return await self.async_step_power_advanced()

return await self.async_step_lut_manufacturer()

power_profile = None
if self.source_entity.entity_entry:
try:
power_profile = await get_power_profile(
self.power_profile = await get_power_profile(
self.hass, {}, self.source_entity.entity_entry
)
except ModelNotSupported:
power_profile = None
if power_profile:
self.power_profile = None
if self.power_profile:
return self.async_show_form(
step_id="lut",
description_placeholders={
"manufacturer": power_profile.manufacturer,
"model": power_profile.model,
"manufacturer": self.power_profile.manufacturer,
"model": self.power_profile.model,
},
data_schema=SCHEMA_POWER_LUT_AUTODISCOVERED,
errors={},
Expand Down Expand Up @@ -456,8 +457,8 @@ async def async_step_power_advanced(
self, user_input: dict[str, str] = None
) -> FlowResult:
errors = {}
if user_input is not None:
self.sensor_config.update(user_input)
if user_input is not None or self.skip_advanced_step:
self.sensor_config.update(user_input or {})
return self.create_config_entry()

return self.async_show_form(
Expand Down
3 changes: 0 additions & 3 deletions custom_components/powercalc/strings.json
Expand Up @@ -28,9 +28,6 @@
"update_frequency": "time in seconds between state updates of the sensor"
}
},
"discovery_confirm": {
"description": "Do you want to setup power sensors for {name} ({manufacturer} {model})?"
},
"group": {
"title": "Create a group sensor",
"data": {
Expand Down
1 change: 1 addition & 0 deletions custom_components/powercalc/translations/de.json
@@ -1,5 +1,6 @@
{
"config": {
"flow_title": "{name} ({manufacturer} {model})",
"abort": {
"already_configured": "Der Sensor ist bereits konfiguriert, geben Sie eine Unique-Id an"
},
Expand Down
3 changes: 0 additions & 3 deletions custom_components/powercalc/translations/en.json
Expand Up @@ -33,9 +33,6 @@
},
"title": "Create a daily fixed sensor"
},
"discovery_confirm": {
"description": "Do you want to setup power sensors for {name} ({manufacturer} {model})?"
},
"fixed": {
"data": {
"power": "Power",
Expand Down
1 change: 1 addition & 0 deletions custom_components/powercalc/translations/nl.json
@@ -1,5 +1,6 @@
{
"config": {
"flow_title": "{name} ({manufacturer} {model})",
"abort": {
"already_configured": "Sensor is reeds geconfigureerd, specifieer een uniek ID"
},
Expand Down
1 change: 1 addition & 0 deletions custom_components/powercalc/translations/pl.json
@@ -1,5 +1,6 @@
{
"config": {
"flow_title": "{name} ({manufacturer} {model})",
"abort": {
"already_configured": "Czujnik jest już skonfigurowany, podaj Unikatowy identyfikator"
},
Expand Down
1 change: 1 addition & 0 deletions custom_components/powercalc/translations/pt-BR.json
@@ -1,5 +1,6 @@
{
"config": {
"flow_title": "{name} ({manufacturer} {model})",
"abort": {
"already_configured": "O sensor já está configurado, especifique um unique_id"
},
Expand Down
1 change: 1 addition & 0 deletions custom_components/powercalc/translations/ro.json
@@ -1,5 +1,6 @@
{
"config": {
"flow_title": "{name} ({manufacturer} {model})",
"abort": {
"already_configured": "Senzorul este deja configurat, specificați un ID unic"
},
Expand Down
1 change: 1 addition & 0 deletions custom_components/powercalc/translations/sv.json
@@ -1,5 +1,6 @@
{
"config": {
"flow_title": "{name} ({manufacturer} {model})",
"abort": {
"already_configured": "Sensor är redan konfigurerad, specificera ett unikt ID"
},
Expand Down
17 changes: 14 additions & 3 deletions tests/test_config_flow.py
Expand Up @@ -67,6 +67,11 @@


async def test_discovery_flow(hass: HomeAssistant):
light_entity = MockLight("test", STATE_ON, DEFAULT_UNIQUE_ID)
light_entity.manufacturer = "signify"
light_entity.model = "LCT010"
await create_mock_light_entity(hass, light_entity)

result: FlowResult = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_INTEGRATION_DISCOVERY},
Expand All @@ -78,16 +83,17 @@ async def test_discovery_flow(hass: HomeAssistant):
CONF_MODEL: "LCT010",
},
)

# Confirm selected manufacturer/model
assert result["type"] == data_entry_flow.FlowResultType.FORM
result = await hass.config_entries.flow.async_configure(result["flow_id"], {CONF_CONFIRM_AUTODISCOVERED_MODEL: True})

result = await hass.config_entries.flow.async_configure(result["flow_id"], {})
assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
assert result["data"] == {
CONF_ENTITY_ID: DEFAULT_ENTITY_ID,
CONF_SENSOR_TYPE: SensorType.VIRTUAL_POWER,
CONF_MANUFACTURER: "signify",
CONF_MODEL: "LCT010",
CONF_MODE: CalculationStrategy.LUT,
CONF_NAME: "test",
CONF_UNIQUE_ID: DEFAULT_UNIQUE_ID,
}
Expand Down Expand Up @@ -272,7 +278,12 @@ async def test_lut_autodiscover_flow(hass: HomeAssistant):

assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
_assert_default_virtual_power_entry_data(
CalculationStrategy.LUT, result["data"], {}
CalculationStrategy.LUT,
result["data"],
{
CONF_MANUFACTURER: light_entity.manufacturer,
CONF_MODEL: light_entity.model
}
)

await hass.async_block_till_done()
Expand Down

0 comments on commit 7a7f9fc

Please sign in to comment.