From a832d9bbfb50353be1382596b926d345cc767808 Mon Sep 17 00:00:00 2001
From: Piotr Machowski <6118709+PiotrMachowski@users.noreply.github.com>
Date: Fri, 21 Oct 2022 23:14:55 +0200
Subject: [PATCH] Adjust to changes in API
---
.github/FUNDING.yml | 3 +-
README.md | 14 +++++---
custom_components/mpk_lodz/manifest.json | 2 +-
custom_components/mpk_lodz/sensor.py | 44 +++++++++++++++---------
4 files changed, 41 insertions(+), 22 deletions(-)
diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml
index 9f65311..f89c015 100644
--- a/.github/FUNDING.yml
+++ b/.github/FUNDING.yml
@@ -1 +1,2 @@
-custom: ["buymeacoffee.com/PiotrMachowski", "paypal.me/PiMachowski"]
+ko_fi: piotrmachowski
+custom: ["paypal.me/PiMachowski"]
diff --git a/README.md b/README.md
index c6dbc78..abf6487 100644
--- a/README.md
+++ b/README.md
@@ -1,7 +1,7 @@
[![HACS Custom][hacs_shield]][hacs]
[![GitHub Latest Release][releases_shield]][latest_release]
[![GitHub All Releases][downloads_total_shield]][releases]
-[![Buy me a coffee][buy_me_a_coffee_shield]][buy_me_a_coffee]
+[![Ko-Fi][ko_fi_shield]][ko_fi]
[![PayPal.Me][paypal_me_shield]][paypal_me]
@@ -14,6 +14,9 @@
[releases]: https://github.com/PiotrMachowski/Home-Assistant-custom-components-MPK-Lodz/releases
[downloads_total_shield]: https://img.shields.io/github/downloads/PiotrMachowski/Home-Assistant-custom-components-MPK-Lodz/total
+[ko_fi_shield]: https://img.shields.io/static/v1.svg?label=%20&message=Ko-Fi&color=F16061&logo=ko-fi&logoColor=white
+[ko_fi]: https://ko-fi.com/piotrmachowski
+
[buy_me_a_coffee_shield]: https://img.shields.io/static/v1.svg?label=%20&message=Buy%20me%20a%20coffee&color=6f4e37&logo=buy%20me%20a%20coffee&logoColor=white
[buy_me_a_coffee]: https://www.buymeacoffee.com/PiotrMachowski
@@ -35,11 +38,14 @@ This sensor uses unofficial API provided by MPK Łódź.
| Key | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
-| `id` | `positive integer` | `True` | - | ID of a stop |
+| `id` | `positive integer` | `False` | - | ID of a stop |
+| `num` | `positive integer` | `False` | - | Number of a stop |
| `name` | `string` | `False` | id | Name of a stop |
| `lines` | `list` | `False` | all available | List of monitored lines. |
| `directions` | `list` | `False` | all available | List of monitored directions. |
+One of `id` or `num` must be provided!
+
## Example usage
```
@@ -77,7 +83,7 @@ rm mpk_lodz.zip
## Hints
-* Value for `stop_id` can be retrieved from [*ITS Łódź*](http://rozklady.lodz.pl/). After choosing a desired stop open its electronical table. `stop_id` is a number visibile in URL.
+* Value for `id`/`num` can be retrieved from [*ITS Łódź*](http://rozklady.lodz.pl/). After choosing a desired stop open its electronical table. There should be a number visibile in URL. If URL contains `busStopId` you should use this number as `id`. If URL contains `busStopNum` you should use this number as `num`.
* These sensors provides attributes which can be used in [*HTML card*](https://github.com/PiotrMachowski/Home-Assistant-Lovelace-HTML-card) or [*HTML Template card*](https://github.com/PiotrMachowski/Home-Assistant-Lovelace-HTML-Template-card): `html_timetable`, `html_departures`
* HTML card:
@@ -102,5 +108,5 @@ rm mpk_lodz.zip
{{ state_attr('sensor.mpk_lodz_2873','html_departures') }}
```
-
+
diff --git a/custom_components/mpk_lodz/manifest.json b/custom_components/mpk_lodz/manifest.json
index 101af8c..ede1afb 100644
--- a/custom_components/mpk_lodz/manifest.json
+++ b/custom_components/mpk_lodz/manifest.json
@@ -6,6 +6,6 @@
"dependencies": [],
"codeowners": ["@PiotrMachowski"],
"requirements": ["requests"],
- "version": "v1.0.1",
+ "version": "v1.0.2",
"iot_class": "cloud_polling"
}
\ No newline at end of file
diff --git a/custom_components/mpk_lodz/sensor.py b/custom_components/mpk_lodz/sensor.py
index 71da761..acd00a5 100644
--- a/custom_components/mpk_lodz/sensor.py
+++ b/custom_components/mpk_lodz/sensor.py
@@ -11,6 +11,7 @@
DEFAULT_NAME = 'MPK Łódź'
+CONF_NUM = 'num'
CONF_STOPS = 'stops'
CONF_LINES = 'lines'
CONF_DIRECTIONS = 'directions'
@@ -19,7 +20,8 @@
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
vol.Required(CONF_STOPS): vol.All(cv.ensure_list, [
vol.Schema({
- vol.Required(CONF_ID): cv.positive_int,
+ vol.Optional(CONF_ID, default=0): cv.positive_int,
+ vol.Optional(CONF_NUM, default=0): cv.positive_int,
vol.Optional(CONF_NAME): cv.string,
vol.Optional(CONF_LINES, default=[]): cv.ensure_list,
vol.Optional(CONF_DIRECTIONS, default=[]): cv.ensure_list
@@ -31,25 +33,33 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
name = config.get(CONF_NAME)
stops = config.get(CONF_STOPS)
dev = []
- for stop in stops:
- stop_id = str(stop.get(CONF_ID))
- lines = stop.get(CONF_LINES)
- directions = stop.get(CONF_DIRECTIONS)
- real_stop_name = MpkLodzSensor.get_stop_name(stop_id)
+ for stop_cfg in stops:
+ stop_id = str(stop_cfg.get(CONF_ID))
+ stop_num = str(stop_cfg.get(CONF_NUM))
+ use_stop_num = stop_num != "0"
+ stop = stop_num if use_stop_num else stop_id
+ print(stop_id)
+ print(stop_num)
+ lines = stop_cfg.get(CONF_LINES)
+ directions = stop_cfg.get(CONF_DIRECTIONS)
+ real_stop_name = MpkLodzSensor.get_stop_name(stop, use_stop_num)
if real_stop_name is None:
- raise Exception("Invalid stop id: {}".format(stop_id))
- stop_name = stop.get(CONF_NAME) or stop_id
+ raise Exception(f"Invalid stop id/num: {stop_id} / {stop_num} / {use_stop_num} / {stop}")
+ stop_name = stop_cfg.get(CONF_NAME) or stop_id
+ if use_stop_num:
+ stop_name = stop_cfg.get(CONF_NAME) or f"num_{stop_num}"
uid = '{}_{}'.format(name, stop_name)
entity_id = async_generate_entity_id(ENTITY_ID_FORMAT, uid, hass=hass)
- dev.append(MpkLodzSensor(entity_id, name, stop_id, stop_name, real_stop_name, lines, directions))
+ dev.append(MpkLodzSensor(entity_id, name, stop, use_stop_num, stop_name, real_stop_name, lines, directions))
add_entities(dev, True)
class MpkLodzSensor(Entity):
- def __init__(self, entity_id, name, stop_id, stop_name, real_stop_name, watched_lines, watched_directions):
+ def __init__(self, entity_id, name, stop, use_stop_num, stop_name, real_stop_name, watched_lines, watched_directions):
self.entity_id = entity_id
self._name = name
- self._stop_id = stop_id
+ self._stop = stop
+ self._use_stop_num = use_stop_num
self._watched_lines = watched_lines
self._watched_directions = watched_directions
self._stop_name = stop_name
@@ -95,7 +105,7 @@ def extra_state_attributes(self):
def update(self):
now = datetime.now()
- data = MpkLodzSensor.get_data(self._stop_id)
+ data = MpkLodzSensor.get_data(self._stop, self._use_stop_num)
if data is None:
return
departures = data[0][0]
@@ -166,15 +176,17 @@ def group_by_line(departures):
return departures_by_line
@staticmethod
- def get_stop_name(stop_id):
- data = MpkLodzSensor.get_data(stop_id)
+ def get_stop_name(stop, use_stop_num):
+ data = MpkLodzSensor.get_data(stop, use_stop_num)
if data is None:
return None
return data[0].attrib["name"]
@staticmethod
- def get_data(stop_id):
- address = "http://rozklady.lodz.pl/Home/GetTimeTableReal?busStopId={}".format(stop_id)
+ def get_data(stop, use_stop_num):
+ address = "http://rozklady.lodz.pl/Home/GetTimeTableReal?busStopId={}".format(stop)
+ if use_stop_num:
+ address = "http://rozklady.lodz.pl/Home/GetTimeTableReal?busStopNum={}".format(stop)
headers = {
'referer': address,
}