Skip to content

Commit

Permalink
Allow in time calculation per estimated arrival time
Browse files Browse the repository at this point in the history
  • Loading branch information
amaximus committed Feb 18, 2023
1 parent abf3738 commit 6c77638
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Define sensors with the following configuration parameters:<br />
| colors | **Y** | `false` | Display BKK's default color for the line and the text |
| ignoreNow | **Y** | `true` | Ignore vehicles already in the station |
| entity_id | **Y** | `` | Used instead of name for automatic generation of entity_id |
| inPredicted | **Y** | `false` | Calculate time in as per estimated arrival time, when available |
| routes | **Y** | `` | List of routes to consider. Those not listed will be discarded |
---

Expand Down
2 changes: 1 addition & 1 deletion custom_components/bkk_stop/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
"codeowners": ["@amaximus"],
"iot_class": "cloud_polling",
"requirements": [],
"version": "2.4.1"
"version": "2.5.0"
}
13 changes: 11 additions & 2 deletions custom_components/bkk_stop/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
CONF_STOPID = 'stopId'
CONF_WHEELCHAIR = 'wheelchair'
CONF_ROUTES = 'routes'
CONF_INPREDICTED = 'inPredicted'

DEFAULT_NAME = 'Budapest GO'
DEFAULT_ICON = 'mdi:bus'
Expand All @@ -42,6 +43,7 @@
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
vol.Optional(CONF_ROUTES, default=[]): vol.All(cv.ensure_list, [cv.string]),
vol.Optional(ATTR_ENTITY_ID, default=''): cv.string,
vol.Optional(CONF_INPREDICTED, default='false'): cv.boolean,
})

@asyncio.coroutine
Expand All @@ -57,13 +59,14 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
colors = config.get(CONF_COLORS)
ignorenow = config.get(CONF_IGNORENOW)
routes = config.get(CONF_ROUTES)
inpredicted = config.get(CONF_INPREDICTED)

async_add_devices(
[BKKPublicTransportSensor(hass, name, entityid, stopid, minsafter, wheelchair, bikes, colors, ignorenow, maxitems, routes)],update_before_add=True)
[BKKPublicTransportSensor(hass, name, entityid, stopid, minsafter, wheelchair, bikes, colors, ignorenow, maxitems, routes, inpredicted)],update_before_add=True)

class BKKPublicTransportSensor(Entity):

def __init__(self, hass, name, entityid, stopid, minsafter, wheelchair, bikes, colors, ignorenow, maxitems, routes):
def __init__(self, hass, name, entityid, stopid, minsafter, wheelchair, bikes, colors, ignorenow, maxitems, routes, inpredicted):
"""Initialize the sensor."""
self._name = name
self._hass = hass
Expand All @@ -74,6 +77,7 @@ def __init__(self, hass, name, entityid, stopid, minsafter, wheelchair, bikes, c
self._bikes = bikes
self._colors = colors
self._ignorenow = ignorenow
self._inpredicted = inpredicted
self._routes = routes
self._state = None
self._bkkdata = {}
Expand Down Expand Up @@ -103,6 +107,9 @@ def extra_state_attributes(self):
for stopTime in bkkdata["data"]["entry"]["stopTimes"]:
diff = 0
diff = int(( stopTime.get("departureTime", 0) - currenttime ) / 60)
if self._inpredicted and 'predictedDepartureTime' in stopTime:
diff = int(( stopTime.get("predictedDepartureTime", 0) - currenttime ) / 60)

if diff < 0:
diff = 0
if self._ignorenow and diff == 0:
Expand Down Expand Up @@ -143,6 +150,8 @@ def extra_state_attributes(self):
itemnr += 1
if itemnr >= int(self._maxitems):
break
dt_now = datetime.now()
bkkjson["updatedAt"] = dt_now.strftime("%Y/%m/%d %H:%M")

return bkkjson

Expand Down
1 change: 1 addition & 0 deletions info.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Define sensors with the following configuration parameters:<br />
| colors | **Y** | `false` | Display BKK's default color for the line and the text |
| ignoreNow | **Y** | `true` | Ignore vehicles already in the station |
| entity_id | **Y** | `` | Used instead of name for automatic generation of entity_id |
| inPredicted | **Y** | `false` | Calculate time in as per estimated arrival time, when available |
| routes | **Y** | `` | List of routes to consider. Those not listed will be discarded |
---

Expand Down

0 comments on commit 6c77638

Please sign in to comment.