Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Bizkaibus, Biscays (Spain) bus service component #22934

Merged
merged 19 commits into from May 5, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .coveragerc
Expand Up @@ -53,6 +53,7 @@ omit =
homeassistant/components/bbox/sensor.py
homeassistant/components/bh1750/sensor.py
homeassistant/components/bitcoin/sensor.py
homeassistant/components/bizkaibus/sensor.py
homeassistant/components/blink/*
homeassistant/components/blinksticklight/light.py
homeassistant/components/blinkt/light.py
Expand Down
1 change: 1 addition & 0 deletions homeassistant/components/bizkaibus/__init__.py
@@ -0,0 +1 @@
"""The Bizkaibus, Biscays (Basque Country, Spain) bus service, bus tracker component."""
8 changes: 8 additions & 0 deletions homeassistant/components/bizkaibus/manifest.json
@@ -0,0 +1,8 @@
{
"domain": "bizkaibus",
"name": "Bizkaibus",
"documentation": "https://www.home-assistant.io/components/bizkaibus",
"dependencies": [],
"codeowners": ["@UgaitzEtxebarria"],
"requirements": []
}
136 changes: 136 additions & 0 deletions homeassistant/components/bizkaibus/sensor.py
@@ -0,0 +1,136 @@

"""Support for Bizkaibus, Biscay (Basque Country, Spain) Bus service."""

import logging
import requests
import json

import voluptuous as vol
import homeassistant.helpers.config_validation as cv

from homeassistant.const import CONF_NAME
from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.helpers.entity import Entity

import xml.etree.ElementTree as ET

_LOGGER = logging.getLogger(__name__)
_RESOURCE = 'http://apli.bizkaia.net/'
_RESOURCE += 'APPS/DANOK/TQWS/TQ.ASMX/GetPasoParadaMobile_JSON'

ATTR_ROUTE = 'Route'
ludeeus marked this conversation as resolved.
Show resolved Hide resolved
ATTR_ROUTE_NAME = 'Route name'
ludeeus marked this conversation as resolved.
Show resolved Hide resolved
ATTR_DUE_IN = 'Due in'

CONF_STOP_ID = 'stopid'
CONF_ROUTE = 'route'

DEFAULT_NAME = 'Next bus'

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_STOP_ID): cv.string,
vol.Optional(CONF_ROUTE): cv.string,
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
})


def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the Bizkaibus public transport sensor."""
name = config.get(CONF_NAME)
ludeeus marked this conversation as resolved.
Show resolved Hide resolved
stop = config.get(CONF_STOP_ID)
route = config.get(CONF_ROUTE)

data = BizkaibusData(stop, route)
add_entities([BizkaibusSensor(data, stop, route, name)], True)


class BizkaibusSensor(Entity):
"""The class for handling the data."""

def __init__(self, data, stop, route, name):
"""Initialize the sensor."""
self.data = data
self.stop = stop
self.route = route
self._name = name
self._state = None

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

@property
def state(self):
"""Return the state of the sensor."""
return self._state

@property
def unit_of_measurement(self):
"""Return the unit of measurement of the sensor."""
return 'min'
ludeeus marked this conversation as resolved.
Show resolved Hide resolved

def update(self):
"""Get the latest data from the webservice."""
self.data.update()
try:
self._state = self.data.info[0][ATTR_DUE_IN]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're trying to move new departure time type sensors over to use the timestamp device class. That means reporting an absolute utc ISO timestamp instead of a relative time as state.

We should keep this in mind for the future development of this platform.

https://developers.home-assistant.io/docs/en/entity_sensor.html#available-device-classes

https://www.home-assistant.io/lovelace/entities/#format

except TypeError:
pass


class BizkaibusData:
"""The class for handling the data retrieval."""

def __init__(self, stop, route):
"""Initialize the data object."""
self.stop = stop
self.route = route
self.info = [{ATTR_ROUTE_NAME: 'n/a',
ludeeus marked this conversation as resolved.
Show resolved Hide resolved
ATTR_ROUTE: self.route,
ATTR_DUE_IN: 'n/a'}]

def update(self):
"""Retrieve the information from API."""
params = {}
params['callback'] = ''
params['strLinea'] = self.route
params['strParada'] = self.stop

response = requests.get(_RESOURCE, params, timeout=10)

if response.status_code != 200:

self.info = [{ATTR_ROUTE_NAME: 'n/a',
ATTR_ROUTE: self.route,
ATTR_DUE_IN: 'n/a'}]
return

strJSON = response.text[1:-2].replace('\'', '"')
result = json.loads(strJSON)

if str(result['STATUS']) != 'OK':
self.info = [{ATTR_ROUTE_NAME: 'n/a',
ATTR_ROUTE: self.route,
ATTR_DUE_IN: 'n/a'}]
return

root = ET.fromstring(result['Resultado'])

self.info = []
for childBus in root.findall("PasoParada"):
route = childBus.find('linea').text
routeName = childBus.find('ruta').text
time = childBus.find('e1').find('minutos').text

if (routeName is not None and time is not None and
route is not None and route == self.route):
bus_data = {ATTR_ROUTE_NAME: routeName,
ATTR_ROUTE: route,
ATTR_DUE_IN: time}
self.info.append(bus_data)

if not self.info:
self.info = [{ATTR_ROUTE_NAME: 'n/a',
ATTR_ROUTE: self.route,
ATTR_DUE_IN: 'n/a'}]