Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
Core
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
mirrored_repos
HomeAssistant
Core
Commits
dc02370b
Commit
dc02370b
authored
9 years ago
by
Paulus Schoutsen
Browse files
Options
Downloads
Plain Diff
Merge pull request #1355 from deisi/deutsche_bahn
New deutsche_bahn component
parents
35aa9aa8
b0960044
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
.coveragerc
+1
-0
1 addition, 0 deletions
.coveragerc
homeassistant/components/sensor/deutsche_bahn.py
+98
-0
98 additions, 0 deletions
homeassistant/components/sensor/deutsche_bahn.py
requirements_all.txt
+3
-0
3 additions, 0 deletions
requirements_all.txt
with
102 additions
and
0 deletions
.coveragerc
+
1
−
0
View file @
dc02370b
...
...
@@ -125,6 +125,7 @@ omit =
homeassistant/components/sensor/arest.py
homeassistant/components/sensor/bitcoin.py
homeassistant/components/sensor/cpuspeed.py
homeassistant/components/sensor/deutsche_bahn.py
homeassistant/components/sensor/dht.py
homeassistant/components/sensor/dweet.py
homeassistant/components/sensor/efergy.py
...
...
This diff is collapsed.
Click to expand it.
homeassistant/components/sensor/deutsche_bahn.py
0 → 100644
+
98
−
0
View file @
dc02370b
'''
homeassistant.components.sensor.bahn
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The deutsche_bahn sensor tells you if your next train is on time, or delayed.
'''
import
logging
from
datetime
import
timedelta
,
datetime
from
homeassistant.util
import
Throttle
from
homeassistant.helpers.entity
import
Entity
_LOGGER
=
logging
.
getLogger
(
__name__
)
REQUIREMENTS
=
[
'
schiene==0.14
'
]
ICON
=
'
mdi:train
'
# Return cached results if last scan was less then this time ago
MIN_TIME_BETWEEN_UPDATES
=
timedelta
(
seconds
=
120
)
def
setup_platform
(
hass
,
config
,
add_devices_callback
,
discovery_info
=
None
):
"""
Add the Bahn Sensor.
"""
start
=
config
.
get
(
'
from
'
)
goal
=
config
.
get
(
'
to
'
)
if
start
is
None
:
_LOGGER
.
error
(
'
Missing required variable:
"
from
"'
)
return
False
if
goal
is
None
:
_LOGGER
.
error
(
'
Missing required variable:
"
to
"'
)
return
False
dev
=
[]
dev
.
append
(
DeutscheBahnSensor
(
start
,
goal
))
add_devices_callback
(
dev
)
# pylint: disable=too-few-public-methods
class
DeutscheBahnSensor
(
Entity
):
"""
Implement a DeutscheBahn sensor
start: starting station
goal: target station
"""
def
__init__
(
self
,
start
,
goal
):
self
.
_name
=
start
+
'
to
'
+
goal
self
.
data
=
SchieneData
(
start
,
goal
)
self
.
update
()
@property
def
name
(
self
):
"""
return the name.
"""
return
self
.
_name
@property
def
icon
(
self
):
"""
Icon for the frontend
"""
return
ICON
@property
def
state
(
self
):
"""
Return the depature time of the next train
"""
return
self
.
_state
@property
def
state_attributes
(
self
):
return
self
.
data
.
connections
[
0
]
def
update
(
self
):
"""
Gets the latest delay from bahn.de and updates the state
"""
self
.
data
.
update
()
self
.
_state
=
self
.
data
.
connections
[
0
].
get
(
'
departure
'
,
'
Unknown
'
)
delay
=
self
.
data
.
connections
[
0
].
get
(
'
delay
'
,
{
'
delay_departure
'
:
0
,
'
delay_arrival
'
:
0
})
if
delay
[
'
delay_departure
'
]
!=
0
:
self
.
_state
+=
"
+ {}
"
.
format
(
delay
[
'
delay_departure
'
])
# pylint: disable=too-few-public-methods
class
SchieneData
(
object
):
"""
Pulls data from the bahn.de web page
"""
def
__init__
(
self
,
start
,
goal
):
import
schiene
self
.
start
=
start
self
.
goal
=
goal
self
.
schiene
=
schiene
.
Schiene
()
self
.
connections
=
[{}]
@Throttle
(
MIN_TIME_BETWEEN_UPDATES
)
def
update
(
self
):
"""
update connection data
"""
self
.
connections
=
self
.
schiene
.
connections
(
self
.
start
,
self
.
goal
,
datetime
.
now
())
for
con
in
self
.
connections
:
if
'
details
'
in
con
:
con
.
pop
(
'
details
'
)
# details info is not usefull
This diff is collapsed.
Click to expand it.
requirements_all.txt
+
3
−
0
View file @
dc02370b
...
...
@@ -225,6 +225,9 @@ radiotherm==1.2
# homeassistant.components.media_player.samsungtv
samsungctl==0.5.1
# homeassistant.components.sensor.deutsche_bahn
schiene==0.14
# homeassistant.components.scsgate
scsgate==0.1.0
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment