mirror of
https://github.com/home-assistant/core.git
synced 2025-07-11 15:27:08 +00:00
Add Sytadin Traffic component (#9524)
* Add Sytadin Traffic component * Add Sytadin Traffic component, update * Add Sytadin Traffic component, update * Add Sytadin Traffic component, update * Add Sytadin Traffic component, update * Add Sytadin Traffic component, update * implements @fabaff comments * Formatting * formatting * formatting * split monitored condition to be independent * formatting * add version * fix requirements part based on gen_requirements_all.py result * formatting * requirements stuff file * formatting * add missing file into .coveragerc * add me for code review * configuration management updated * implements @fabaff and @pvizeli comments * indentation * indentation * indentation * indentation * add const DEFAULT_UPDATE_INTERVAL * use const DEFAULT_UPDATE_INTERVAL * remove blank line * fixes debug call * clean for loop * use BeautifulSoup for html parsing * Update requirements * Add throttle and fix remaining issues * Don't add const
This commit is contained in:
parent
dd7d8d56bb
commit
b3e88d1f8f
@ -560,6 +560,7 @@ omit =
|
|||||||
homeassistant/components/sensor/swiss_public_transport.py
|
homeassistant/components/sensor/swiss_public_transport.py
|
||||||
homeassistant/components/sensor/synologydsm.py
|
homeassistant/components/sensor/synologydsm.py
|
||||||
homeassistant/components/sensor/systemmonitor.py
|
homeassistant/components/sensor/systemmonitor.py
|
||||||
|
homeassistant/components/sensor/sytadin.py
|
||||||
homeassistant/components/sensor/tank_utility.py
|
homeassistant/components/sensor/tank_utility.py
|
||||||
homeassistant/components/sensor/ted5000.py
|
homeassistant/components/sensor/ted5000.py
|
||||||
homeassistant/components/sensor/temper.py
|
homeassistant/components/sensor/temper.py
|
||||||
|
@ -54,6 +54,7 @@ homeassistant/components/media_player/monoprice.py @etsinko
|
|||||||
homeassistant/components/sensor/airvisual.py @bachya
|
homeassistant/components/sensor/airvisual.py @bachya
|
||||||
homeassistant/components/sensor/irish_rail_transport.py @ttroy50
|
homeassistant/components/sensor/irish_rail_transport.py @ttroy50
|
||||||
homeassistant/components/sensor/miflora.py @danielhiversen
|
homeassistant/components/sensor/miflora.py @danielhiversen
|
||||||
|
homeassistant/components/sensor/sytadin.py @gautric
|
||||||
homeassistant/components/sensor/tibber.py @danielhiversen
|
homeassistant/components/sensor/tibber.py @danielhiversen
|
||||||
homeassistant/components/sensor/waqi.py @andrey-git
|
homeassistant/components/sensor/waqi.py @andrey-git
|
||||||
homeassistant/components/switch/rainmachine.py @bachya
|
homeassistant/components/switch/rainmachine.py @bachya
|
||||||
|
140
homeassistant/components/sensor/sytadin.py
Normal file
140
homeassistant/components/sensor/sytadin.py
Normal file
@ -0,0 +1,140 @@
|
|||||||
|
"""
|
||||||
|
Support for Sytadin Traffic, French Traffic Supervision.
|
||||||
|
|
||||||
|
For more details about this platform, please refer to the documentation at
|
||||||
|
https://home-assistant.io/components/sensor.sytadin/
|
||||||
|
"""
|
||||||
|
import logging
|
||||||
|
import re
|
||||||
|
from datetime import timedelta
|
||||||
|
|
||||||
|
import requests
|
||||||
|
import voluptuous as vol
|
||||||
|
|
||||||
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
||||||
|
from homeassistant.const import (
|
||||||
|
LENGTH_KILOMETERS, CONF_MONITORED_CONDITIONS, CONF_NAME, ATTR_ATTRIBUTION)
|
||||||
|
from homeassistant.helpers.entity import Entity
|
||||||
|
from homeassistant.util import Throttle
|
||||||
|
|
||||||
|
REQUIREMENTS = ['beautifulsoup4==4.6.0']
|
||||||
|
|
||||||
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
URL = 'http://www.sytadin.fr/sys/barometres_de_la_circulation.jsp.html'
|
||||||
|
|
||||||
|
CONF_ATTRIBUTION = "Data provided by Direction des routes Île-de-France" \
|
||||||
|
"(DiRIF)"
|
||||||
|
|
||||||
|
DEFAULT_NAME = 'Sytadin'
|
||||||
|
REGEX = r'(\d*\.\d+|\d+)'
|
||||||
|
|
||||||
|
OPTION_TRAFFIC_JAM = 'traffic_jam'
|
||||||
|
OPTION_MEAN_VELOCITY = 'mean_velocity'
|
||||||
|
OPTION_CONGESTION = 'congestion'
|
||||||
|
|
||||||
|
SENSOR_TYPES = {
|
||||||
|
OPTION_TRAFFIC_JAM: ['Traffic Jam', LENGTH_KILOMETERS],
|
||||||
|
OPTION_MEAN_VELOCITY: ['Mean Velocity', LENGTH_KILOMETERS+'/h'],
|
||||||
|
OPTION_CONGESTION: ['Congestion', ''],
|
||||||
|
}
|
||||||
|
|
||||||
|
MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=5)
|
||||||
|
|
||||||
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||||
|
vol.Optional(CONF_MONITORED_CONDITIONS, default=OPTION_TRAFFIC_JAM):
|
||||||
|
vol.All(cv.ensure_list, [vol.In(SENSOR_TYPES)]),
|
||||||
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||||
|
"""Set up of the Sytadin Traffic sensor platform."""
|
||||||
|
name = config.get(CONF_NAME)
|
||||||
|
|
||||||
|
sytadin = SytadinData(URL)
|
||||||
|
|
||||||
|
dev = []
|
||||||
|
for option in config.get(CONF_MONITORED_CONDITIONS):
|
||||||
|
print(option)
|
||||||
|
dev.append(SytadinSensor(
|
||||||
|
sytadin, name, option, SENSOR_TYPES[option][0],
|
||||||
|
SENSOR_TYPES[option][1]))
|
||||||
|
add_devices(dev, True)
|
||||||
|
|
||||||
|
|
||||||
|
class SytadinSensor(Entity):
|
||||||
|
"""Representation of a Sytadin Sensor."""
|
||||||
|
|
||||||
|
def __init__(self, data, name, sensor_type, option, unit):
|
||||||
|
"""Initialize the sensor."""
|
||||||
|
self.data = data
|
||||||
|
self._state = None
|
||||||
|
self._name = name
|
||||||
|
self._option = option
|
||||||
|
self._type = sensor_type
|
||||||
|
self._unit = unit
|
||||||
|
|
||||||
|
@property
|
||||||
|
def name(self):
|
||||||
|
"""Return the name of the sensor."""
|
||||||
|
return '{} {}'.format(self._name, self._option)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def state(self):
|
||||||
|
"""Return the state of the sensor."""
|
||||||
|
return self._state
|
||||||
|
|
||||||
|
@property
|
||||||
|
def unit_of_measurement(self):
|
||||||
|
"""Return the unit of measurement."""
|
||||||
|
return self._unit
|
||||||
|
|
||||||
|
@property
|
||||||
|
def device_state_attributes(self):
|
||||||
|
"""Return the state attributes."""
|
||||||
|
return {
|
||||||
|
ATTR_ATTRIBUTION: CONF_ATTRIBUTION,
|
||||||
|
}
|
||||||
|
|
||||||
|
def update(self):
|
||||||
|
"""Fetch new state data for the sensor."""
|
||||||
|
self.data.update()
|
||||||
|
|
||||||
|
if self.data is None:
|
||||||
|
return
|
||||||
|
|
||||||
|
if self._type == OPTION_TRAFFIC_JAM:
|
||||||
|
self._state = self.data.traffic_jam
|
||||||
|
elif self._type == OPTION_MEAN_VELOCITY:
|
||||||
|
self._state = self.data.mean_velocity
|
||||||
|
elif self._type == OPTION_CONGESTION:
|
||||||
|
self._state = self.data.congestion
|
||||||
|
|
||||||
|
|
||||||
|
class SytadinData(object):
|
||||||
|
"""The class for handling the data retrieval."""
|
||||||
|
|
||||||
|
def __init__(self, resource):
|
||||||
|
"""Initialize the data object."""
|
||||||
|
self._resource = resource
|
||||||
|
self.data = None
|
||||||
|
self.traffic_jam = self.mean_velocity = self.congestion = None
|
||||||
|
|
||||||
|
@Throttle(MIN_TIME_BETWEEN_UPDATES)
|
||||||
|
def update(self):
|
||||||
|
"""Get the latest data from the Sytadin."""
|
||||||
|
from bs4 import BeautifulSoup
|
||||||
|
|
||||||
|
try:
|
||||||
|
raw_html = requests.get(self._resource, timeout=10).text
|
||||||
|
data = BeautifulSoup(raw_html, 'html.parser')
|
||||||
|
|
||||||
|
values = data.select('.barometre_valeur')
|
||||||
|
self.traffic_jam = re.search(REGEX, values[0].text).group()
|
||||||
|
self.mean_velocity = re.search(REGEX, values[1].text).group()
|
||||||
|
self.congestion = re.search(REGEX, values[2].text).group()
|
||||||
|
except requests.exceptions.ConnectionError:
|
||||||
|
_LOGGER.error("Connection error")
|
||||||
|
self.data = None
|
@ -115,6 +115,7 @@ batinfo==0.4.2
|
|||||||
# homeassistant.components.device_tracker.linksys_ap
|
# homeassistant.components.device_tracker.linksys_ap
|
||||||
# homeassistant.components.sensor.geizhals
|
# homeassistant.components.sensor.geizhals
|
||||||
# homeassistant.components.sensor.scrape
|
# homeassistant.components.sensor.scrape
|
||||||
|
# homeassistant.components.sensor.sytadin
|
||||||
beautifulsoup4==4.6.0
|
beautifulsoup4==4.6.0
|
||||||
|
|
||||||
# homeassistant.components.zha
|
# homeassistant.components.zha
|
||||||
|
Loading…
x
Reference in New Issue
Block a user