Skip to content

Commit

Permalink
Merge pull request #12 from tsunglung/main
Browse files Browse the repository at this point in the history
Add function of use selector to select entity and date in config flow
  • Loading branch information
cnstudio authored Apr 7, 2022
2 parents ec7c4df + 3e195d9 commit 28a83d3
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions custom_components/taipower_bimonthly_cost/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import callback
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers import selector

from .const import (
CONFIG_FLOW_VERSION,
Expand All @@ -26,11 +27,6 @@ async def validate_input(hass: core.HomeAssistant, data):
states_source = hass.states.get(data[CONF_BIMONTHLY_ENERGY])
if states_source is None:
raise EntityNotExist
try:
start_date = datetime.strptime(data[CONF_METER_START_DAY], '%Y/%m/%d')
cv.date(start_date)
except ValueError:
raise ValueError from ValueError
return True


Expand Down Expand Up @@ -75,11 +71,15 @@ async def async_step_user(self, user_input=None):

data_schema = vol.Schema(
{
vol.Required(CONF_BIMONTHLY_ENERGY): cv.string,
vol.Required(CONF_BIMONTHLY_ENERGY): selector.selector(
{"entity": {"domain": "sensor"}},
),
# pylint: disable=unnecessary-lambda
vol.Required(
CONF_METER_START_DAY,
default=lambda: datetime.now().strftime("%Y/%m/%d")): cv.string,
default=lambda: datetime.now().strftime("%Y/%m/%d")): selector.selector(
{"date": {}},
)
}
)

Expand Down Expand Up @@ -124,12 +124,16 @@ def _get_options_schema(self):
CONF_BIMONTHLY_ENERGY,
default=_get_config_value(
self.config_entry, CONF_BIMONTHLY_ENERGY, "")
): cv.string,
): selector.selector(
{"entity": {"domain": "sensor"}},
),
vol.Required(
CONF_METER_START_DAY,
default=_get_config_value(
self.config_entry, CONF_METER_START_DAY, 0)
): cv.string,
): selector.selector(
{"date": {}},
)
}
)

Expand Down

0 comments on commit 28a83d3

Please sign in to comment.