Skip to content

Commit

Permalink
use selectors and choose utm select entity
Browse files Browse the repository at this point in the history
  • Loading branch information
dgomes committed Apr 9, 2022
1 parent 429a4d4 commit 281682f
Showing 1 changed file with 13 additions and 20 deletions.
33 changes: 13 additions & 20 deletions custom_components/erse/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import voluptuous as vol
from pyerse.comercializador import Comercializador
from pyerse.ciclos import Ciclo_Diario
from pyerse.comercializador import POTENCIA

from homeassistant.components.utility_meter.const import DOMAIN as UTILITY_METER_DOMAIN
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN

from homeassistant import config_entries
Expand All @@ -28,8 +28,11 @@
DOMAIN,
)

from homeassistant.helpers import selector

_LOGGER = logging.getLogger(__name__)

POTENCIAS = [{"value": str(p), "label": f"{p} kVA"} for p in Comercializador.potencias()]

class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
"""Handle a config flow for Entidade Reguladora dos Serviços Energéticos."""
Expand All @@ -51,8 +54,8 @@ async def async_step_user(self, user_input=None):
data_schema=vol.Schema(
{
vol.Required(CONF_OPERATOR): str,
vol.Required(CONF_INSTALLED_POWER): vol.In(
Comercializador.potencias()
vol.Required(CONF_INSTALLED_POWER, default=str(POTENCIA[0])): selector.selector(
{"select": {"options": POTENCIAS}}
),
vol.Required(CONF_PLAN): vol.In(
Comercializador.opcao_horaria()
Expand All @@ -64,6 +67,7 @@ async def async_step_user(self, user_input=None):
),
)

user_input[CONF_INSTALLED_POWER] = float(user_input[CONF_INSTALLED_POWER])
try:
self.operator = Comercializador(
user_input[CONF_OPERATOR],
Expand All @@ -87,12 +91,8 @@ async def async_step_utility_meter(self, user_input=None):
step_id="utility_meter",
data_schema=vol.Schema(
{
vol.Optional(CONF_UTILITY_METERS): cv.multi_select(
{
s.entity_id: s.name
for s in self.hass.states.async_all()
if s.domain == UTILITY_METER_DOMAIN
}
vol.Optional(CONF_UTILITY_METERS): selector.selector(
{"entity": {"domain": "select", "integration": "utility_meter", "multiple": True}},
),
}
),
Expand All @@ -115,17 +115,8 @@ async def async_step_costs(self, user_input=None):
for tariff in self.operator.plano.tarifas
},
**{
vol.Required(tariff.name + CONF_METER_SUFFIX): cv.multi_select(
{
s.entity_id: s.name
for s in self.hass.states.async_all()
if s.domain == SENSOR_DOMAIN
and s.attributes.get(ATTR_UNIT_OF_MEASUREMENT)
in [
ENERGY_WATT_HOUR,
ENERGY_KILO_WATT_HOUR,
]
}
vol.Required(tariff.name + CONF_METER_SUFFIX): selector.selector(
{"entity": {"domain": "sensor", "device_class": "energy", "integration": "utility_meter", "multiple": True}},
)
for tariff in self.operator.plano.tarifas
},
Expand Down Expand Up @@ -155,6 +146,8 @@ class ERSEOptionsFlow(config_entries.OptionsFlow):

def __init__(self, config_entry):
"""Initialize options flow."""
config_entry.data[CONF_INSTALLED_POWER] = float(config_entry.data[CONF_INSTALLED_POWER])

self.operator = Comercializador(
config_entry.data[CONF_OPERATOR],
config_entry.data[CONF_INSTALLED_POWER],
Expand Down

0 comments on commit 281682f

Please sign in to comment.