Skip to content

Commit

Permalink
Adjust to changes in API
Browse files Browse the repository at this point in the history
  • Loading branch information
PiotrMachowski committed Oct 21, 2022
1 parent 5cc8934 commit a832d9b
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 22 deletions.
3 changes: 2 additions & 1 deletion .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
custom: ["buymeacoffee.com/PiotrMachowski", "paypal.me/PiMachowski"]
ko_fi: piotrmachowski
custom: ["paypal.me/PiMachowski"]
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -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]


Expand All @@ -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

Expand All @@ -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

```
Expand Down Expand Up @@ -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:
Expand All @@ -102,5 +108,5 @@ rm mpk_lodz.zip
{{ state_attr('sensor.mpk_lodz_2873','html_departures') }}
```

<a href="https://www.buymeacoffee.com/PiotrMachowski" target="_blank"><img src="https://bmc-cdn.nyc3.digitaloceanspaces.com/BMC-button-images/custom_images/orange_img.png" alt="Buy Me A Coffee" style="height: auto !important;width: auto !important;" ></a>
<a href='https://ko-fi.com/piotrmachowski' target='_blank'><img height='35' style='border:0px;height:46px;' src='https://az743702.vo.msecnd.net/cdn/kofi3.png?v=0' border='0' alt='Buy Me a Coffee at ko-fi.com' />
<a href="https://paypal.me/PiMachowski" target="_blank"><img src="https://www.paypalobjects.com/webstatic/mktg/logo/pp_cc_mark_37x23.jpg" border="0" alt="PayPal Logo" style="height: auto !important;width: auto !important;"></a>
2 changes: 1 addition & 1 deletion custom_components/mpk_lodz/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
"dependencies": [],
"codeowners": ["@PiotrMachowski"],
"requirements": ["requests"],
"version": "v1.0.1",
"version": "v1.0.2",
"iot_class": "cloud_polling"
}
44 changes: 28 additions & 16 deletions custom_components/mpk_lodz/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

DEFAULT_NAME = 'MPK Łódź'

CONF_NUM = 'num'
CONF_STOPS = 'stops'
CONF_LINES = 'lines'
CONF_DIRECTIONS = 'directions'
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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,
}
Expand Down

0 comments on commit a832d9b

Please sign in to comment.