Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
iesus committed Oct 28, 2020
0 parents commit 34b0f72
Show file tree
Hide file tree
Showing 6 changed files with 49,917 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# home-assistant-resrobot
9 changes: 9 additions & 0 deletions custom_components/resrobot/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"domain": "resrobot",
"name": "resrobot",
"documentation": "",
"dependencies": [],
"after_dependencies": ["http", "rest"],
"codeowners": ["@iesus"],
"requirements": []
}
147 changes: 147 additions & 0 deletions custom_components/resrobot/sensor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
"""
Get data from trafiklab.se / resrobot.se
"""

import logging
import json

from collections import namedtuple
from datetime import timedelta

import voluptuous as vol

import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity import Entity
from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.components.rest.sensor import RestData
from homeassistant.const import (CONF_NAME)
from dateutil import parser
from datetime import datetime

_LOGGER = logging.getLogger(__name__)
_ENDPOINT = 'https://api.resrobot.se/v2/departureBoard?id=740051093&maxJourneys=5&format=json'

DEFAULT_NAME = 'ResRobot'
DEFAULT_INTERVAL = 5
DEFAULT_VERIFY_SSL = True
CONF_DEPARTURES = 'departures'
CONF_MAX_JOURNEYS = 'max_journeys'
CONF_STOP_ID = 'stop_id'
CONF_KEY = 'key'

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_KEY, default=0): cv.string,
vol.Optional(CONF_DEPARTURES): [{
vol.Required(CONF_STOP_ID): cv.string,
vol.Optional(CONF_MAX_JOURNEYS, default=5): cv.string,
vol.Optional(CONF_NAME): cv.string}],
})

SCAN_INTERVAL = timedelta(minutes=DEFAULT_INTERVAL)

def setup_platform(hass, config, add_devices, discovery_info=None):
sensors = []
depatures = config.get(CONF_DEPARTURES)

for departure in config.get(CONF_DEPARTURES):
add_sensors(
hass,
config,
add_devices,
departure.get(CONF_NAME),
departure.get(CONF_STOP_ID),
discovery_info
)

def add_sensors(hass, config, add_devices, name, location, discovery_info=None):
method = 'GET'
payload = ''
auth = ''
verify_ssl = DEFAULT_VERIFY_SSL
headers = {}
endpoint = _ENDPOINT + location
rest = RestData(method, endpoint, auth, headers, payload, verify_ssl)
rest.update()

if rest.data is None:
_LOGGER.error("Unable to fetch data from Trafiklab")
return False

restData = json.loads(rest.data)
sensors = []

for data in restData['Departure']:
transportInformation = {
name: data['name'],
direction: data['direction'],
time: data['time'],
date: data['date'],
}
sensors.append(entityRepresentation(rest, name, location, transportInformation))
add_devices(sensors, True)

# pylint: disable=no-member
class entityRepresentation(Entity):
"""Representation of a sensor."""

def __init__(self, rest, prefix, location, data):
"""Initialize a sensor."""
self._rest = rest
self._prefix = prefix
self._location = location
self._data = data
self._attributes = {}

@property
def name(self):
"""Return the name of the sensor."""
return self._name

@property
def state(self):
"""Return the state of the device."""
if self._state is not None:
return self._state
return None

@property
def device_state_attributes(self):
"""Return the state attributes of the monitored installation."""
if self._attributes is not None:
return self._attributes

@property
def unit_of_measurement(self):
"""Return the unit of measurement."""
if self._unit is not None:
return self._unit

@property
def icon(self):
return 'mdi:bus'

def update(self):
"""Get the latest data from the API and updates the state."""
try:
getAttributes = [
"name",
"direction",
"time",
"date",
]

self._rest.update()
self._result = json.loads(self._rest.data)
self._name = self._prefix + '_' + self._location + '_' + self._data['name']
for data in self._result['GetSingleStationResult']['Samples']:
if self._name == self._prefix + '_' + self._location + '_' + data['Name']:
self._unit = 'hours'
self._state = data['time']
# self._attributes.update({"last_modified": data['Updated']})
for attribute in data:
if attribute in getAttributes and data[attribute]:
self._attributes.update({attribute: data[attribute]})
except TypeError as e:
self._result = None
_LOGGER.error(
"Unable to fetch data from trafiklab. " + str(e))
6 changes: 6 additions & 0 deletions hacs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "ResRobot",
"country": "SE",
"domains": ["sensor"],
"homeassistant": "0.116.4"
}
21 changes: 21 additions & 0 deletions info.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# ResRobot integration

## Installation/Configuration:

Add the following to resources in your sensors.yaml:

```yaml
- platform: resrobot
location: 123
maxJourneys: 5
```
Or if you want multiple locations
```yaml
- platform: resrobot
location: 123,456,789
maxJourneys: 5
```
Replace location with number from from url from [stops.txt] after selecting stop
Loading

0 comments on commit 34b0f72

Please sign in to comment.