Skip to content

Commit

Permalink
fix multiple entities
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarsanchezdm committed Aug 15, 2024
1 parent 523d1cc commit 69a2659
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 40 deletions.
51 changes: 17 additions & 34 deletions custom_components/bicing/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
VERSION = 1
token: str
station_ids: []
station_names: []
#station_names: []

@staticmethod
@callback
Expand All @@ -54,21 +54,20 @@ async def async_step_user(self, user_input: dict[str, Any] | None = None) -> Flo
async def async_step_station(self, user_input: dict[str, Any] | None = None):
if user_input is not None:
self.station_ids = []
self.station_names = []

#self.station_names = []
for station_input in user_input[CONF_STATION_IDS]:
station = station_input.split(' - ', 1)
self.station_names.append(station[0])
self.station_ids.append(station[1])
#station = station_input.split(' - ', 1)
#self.station_names.append(station[1])
self.station_ids.append(station_input)

return self.async_create_entry(title="Bicing", data={
TOKEN: self.token,
CONF_STATION_IDS: self.station_ids,
CONF_STATION_NAMES: self.station_names
#CONF_STATION_NAMES: self.station_names
})

stations = await BikeStationApi.get_bike_stations(self.token)
options = list(map(lambda p: SelectOptionDict(label=str(p.id) + " - " + p.name, value=str(p.id) + " - " + p.name), stations))
options = list(map(lambda p: SelectOptionDict(label=str(p.id) + " - " + p.name, value=str(p.id)), stations))

schema = vol.Schema({
vol.Required(CONF_STATION_IDS): SelectSelector(
Expand All @@ -83,40 +82,24 @@ def __init__(self, config_entry):
self.config_entry = config_entry

async def async_step_init(self, user_input=None):
# """Manage the options."""
# # crear si no existeix
# if user_input is not None:
# return self.async_create_entry(title="Bicing", data=user_input)

# # configurar estació. convendria esborrar config de token
# token = self.config_entry.options.get(
# TOKEN, self.config_entry.data[TOKEN]
# )

# schema = vol.Schema({
# vol.Required(TOKEN, default=str(token)): TextSelector(),
# })

# return self.async_show_form(step_id="init", data_schema=schema)

if user_input is not None:
self.station_ids = []
self.station_names = []
station_ids = []
#self.station_names = []

for station_input in user_input[CONF_STATION_IDS]:
station = station_input.split(' - ', 1)
self.station_names.append(station[0])
self.station_ids.append(station[1])
#station = station_input.split(' - ', 1)
#self.station_names.append(station[0])
station_ids.append(station_input)

return self.async_create_entry(title="Bicing", data={
TOKEN: self.token,
CONF_STATION_IDS: self.station_ids,
CONF_STATION_NAMES: self.station_names
TOKEN: self.config_entry.data['TOKEN'],
CONF_STATION_IDS: station_ids,
#CONF_STATION_NAMES: self.station_names
})


stations = await BikeStationApi.get_bike_stations(self.token)
options = list(map(lambda p: SelectOptionDict(label=str(p.id) + " - " + p.name, value=str(p.id) + " - " + p.name), stations))
stations = await BikeStationApi.get_bike_stations(self.config_entry.data['TOKEN'])
options = list(map(lambda p: SelectOptionDict(label=str(p.id) + " - " + p.name, value=str(p.id)), stations))

schema = vol.Schema({
vol.Required(CONF_STATION_IDS): SelectSelector(
Expand Down
2 changes: 1 addition & 1 deletion custom_components/bicing/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
UPDATE_INTERVAL = 10
CONF_STATION = "station"
CONF_STATION_IDS = []
CONF_STATION_NAMES = []
#CONF_STATION_NAMES = []
#CONF_SHOW_IN_MAP = "show_in_map"
21 changes: 21 additions & 0 deletions custom_components/bicing/lib/bike_stations_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,27 @@ async def get_bike_stations(token):
)
stations.append(station)
return stations

@staticmethod
async def get_station_name(token, station_id):
headers = {
'Authorization': token,
}
session = aiohttp.ClientSession(headers=headers)
response = await session.get(const.STATION_INFO_ENDPOINT)
json = await response.json()
await session.close()

bike_station = None
for station in json['data']['stations']:
if str(station['station_id']) == str(station_id):
bike_station = station
break

if bike_station is None:
return f"Estació {station_id}"

return bike_station['name']

@staticmethod
async def get_station_status(token, station_id):
Expand Down
11 changes: 6 additions & 5 deletions custom_components/bicing/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import json
from .const import (
CONF_STATION_IDS,
CONF_STATION_NAMES,
#CONF_STATION_NAMES,
UPDATE_INTERVAL,
TOKEN,
#CONF_SHOW_IN_MAP
Expand All @@ -32,15 +32,16 @@
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback) -> None:
#show_in_map = entry.data[CONF_SHOW_IN_MAP]
stations = entry.data[CONF_STATION_IDS]
names = entry.data[CONF_STATION_NAMES]
#names = entry.data[CONF_STATION_NAMES]
token = entry.data[TOKEN]

_LOGGER.info(f"Creating Bicing stations {stations} ")

for station in stations:
coordinator = BicingStationCoordinator(hass, station, token)
for i, station in enumerate(stations):
coordinator = BicingStationCoordinator(hass, stations[i], token)
await coordinator.async_config_entry_first_refresh()
sensor = BicingStationSensor(entry.title, entry.unique_id, coordinator)
name = await BikeStationApi.get_station_name(token, station)
sensor = BicingStationSensor(name, entry.unique_id, coordinator)
async_add_entities([sensor])

class BicingStationCoordinator(DataUpdateCoordinator):
Expand Down

0 comments on commit 69a2659

Please sign in to comment.