Skip to content

Commit

Permalink
Add cups selector to config
Browse files Browse the repository at this point in the history
  • Loading branch information
yinyang17 committed Mar 10, 2024
1 parent e7e9427 commit 1295986
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 38 deletions.
Binary file modified assets/config-bills-number.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 20 additions & 12 deletions custom_components/pvpc_energy/config_flow.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from homeassistant import config_entries
from homeassistant.helpers.selector import selector
from typing import Any, Dict, Optional
import homeassistant.helpers.config_validation as cv
import voluptuous as vol
Expand All @@ -8,13 +9,14 @@

_LOGGER = logging.getLogger(__name__)

AUTH_SCHEMA = vol.Schema({
AUTH_SCHEMA = {
vol.Required('ufd_login'): cv.string,
vol.Required('ufd_password'): cv.string
})
CUPS_SCHEMA = vol.Schema({
}
CUPS_SCHEMA = {
vol.Required('cups'): cv.string,
vol.Required('bills_number', default='5'): cv.positive_int
})
}

class PvpcEnergyConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
data: Optional[Dict[str, Any]]
Expand All @@ -25,22 +27,28 @@ async def async_step_user(self, user_input: Optional[Dict[str, Any]] = None):

UFD.User = user_input['ufd_login']
UFD.Password = user_input['ufd_password']
await UFD.supplypoints()
self.data = user_input
self.data['cups'] = UFD.cups
self.data['power_high'] = UFD.power_high
self.data['power_low'] = UFD.power_low
self.data['zip_code'] = UFD.zip_code

self.data['cups_list'] = await UFD.supplypoints()
return await self.async_step_cups()

return self.async_show_form(step_id="user", data_schema=AUTH_SCHEMA, errors=None)
return self.async_show_form(step_id="user", data_schema=vol.Schema(AUTH_SCHEMA), errors=None)

async def async_step_cups(self, user_input: Optional[Dict[str, Any]] = None):
_LOGGER.debug(f"cups: {list(self.data['cups_list'].keys())}")
CUPS_SCHEMA['cups'] = selector({
"select": {
"options": list(self.data['cups_list'].keys()),
}
})
if user_input is not None:
_LOGGER.debug(f"pvpc_energy: async_step_cups, user_input={user_input}")
supply_point = self.data['cups_list'][user_input['cups']]
self.data['cups'] = supply_point['cups']
self.data['power_high'] = supply_point['power_high']
self.data['power_low'] = supply_point['power_low']
self.data['zip_code'] = supply_point['zip_code']
self.data['bills_number'] = user_input['bills_number']

return self.async_create_entry(title=self.data['cups'], data=self.data)

return self.async_show_form(step_id="cups", data_schema=CUPS_SCHEMA, errors=None)
return self.async_show_form(step_id="cups", data_schema=vol.Schema(CUPS_SCHEMA), errors=None)
3 changes: 2 additions & 1 deletion custom_components/pvpc_energy/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
"name": "UFD Distribución Electricidad",
"codeowners": ["@yinyang17"],
"config_flow": true,
"single_config_entry": true,
"dependencies": ["recorder"],
"documentation":"https://github.com/yinyang17/pvpc_energy",
"iot_class": "cloud_polling",
"issue_tracker":"https://github.com/yinyang17/pvpc_energy/issues",
"requirements": [],
"version": "0.1.0"
"version": "0.2.0"
}
32 changes: 17 additions & 15 deletions custom_components/pvpc_energy/strings.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
{
"config": {
"step": {
"user": {
"title": "UFD",
"description": "Enter de UFD credentials and data. You need to have two-step login disabled",
"data": {
"ufd_login": "UFD login",
"ufd_password": "UFD password",
"cups": "CUPS",
"power_high": "High power",
"power_low": "Low power",
"zip_code": "Zip code",
"bills_number": "Number of bills to show in Markdown Card"
}
"config": {
"step": {
"user": {
"title": "UFD",
"description": "Enter de UFD credentials and data. You need to have two-step login disabled",
"data": {
"ufd_login": "UFD login",
"ufd_password": "UFD password"
}
},
"cups": {
"title": "UFD",
"description": "Enter the CUPS and the number of bills to show in Markdown Card",
"data": {
"bills_number": "Number of bills"
}
}
}
}
}
}
2 changes: 1 addition & 1 deletion custom_components/pvpc_energy/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
},
"cups": {
"title": "UFD",
"description": "Enter de number of bills to show in Markdown Card",
"description": "Enter the CUPS and the number of bills to show in Markdown Card",
"data": {
"bills_number": "Number of bills"
}
Expand Down
2 changes: 1 addition & 1 deletion custom_components/pvpc_energy/translations/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
},
"cups": {
"title": "UFD",
"description": "Introduce el número de facturas para la tarjeta Markdown",
"description": "Indica el CUPS y el número de facturas para la tarjeta Markdown",
"data": {
"bills_number": "Número de facturas"
}
Expand Down
4 changes: 2 additions & 2 deletions custom_components/pvpc_energy/translations/pt.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
},
"cups": {
"title": "UFD",
"description": "Inserir o número de contas para mostar",
"description": "Insira o CUPS e o número de contas a serem exibidas no Markdown Card",
"data": {
"bills_number": "Número de Contas"
}
}
}
}
}
}
14 changes: 8 additions & 6 deletions custom_components/pvpc_energy/ufd.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ async def billingPeriods(start_date, end_date):

async def supplypoints():
_LOGGER.debug(f"START - UFD.supplypoints()")
result = {}
async with aiohttp.ClientSession() as session:
headers = await UFD.getHeaders(session)
url = UFD.supplypoints_url.format(nif=UFD.nif)
Expand All @@ -162,11 +163,12 @@ async def supplypoints():
_LOGGER.debug(f"response={response}")
for supplyPoint in response['supplyPoints']['items']:
if supplyPoint['power1'] != '' and supplyPoint['power1'] != '':
UFD.cups = supplyPoint['cups']
UFD.power_high = float(supplyPoint['power1'])
UFD.power_low = float(supplyPoint['power2'])
UFD.zip_code = supplyPoint['address']['zipCode']
result[supplyPoint['cups']] = {
'cups': supplyPoint['cups'],
'power_high': float(supplyPoint['power1']),
'power_low': float(supplyPoint['power2']),
'zip_code': supplyPoint['address']['zipCode']
}
_LOGGER.debug(f"cups={UFD.cups}, power_high={UFD.power_high}, power_low={UFD.power_low}")
break
_LOGGER.debug(f"END - UFD.supplypoints()")

return result

0 comments on commit 1295986

Please sign in to comment.