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
a0a509ce
Commit
a0a509ce
authored
8 years ago
by
Fabian Affolter
Committed by
GitHub
8 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Add coinmarketcap sensor (#3064)
parent
40c71b5d
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/coinmarketcap.py
+125
-0
125 additions, 0 deletions
homeassistant/components/sensor/coinmarketcap.py
requirements_all.txt
+3
-0
3 additions, 0 deletions
requirements_all.txt
with
129 additions
and
0 deletions
.coveragerc
+
1
−
0
View file @
a0a509ce
...
@@ -205,6 +205,7 @@ omit =
...
@@ -205,6 +205,7 @@ omit =
homeassistant/components/scene/hunterdouglas_powerview.py
homeassistant/components/scene/hunterdouglas_powerview.py
homeassistant/components/sensor/arest.py
homeassistant/components/sensor/arest.py
homeassistant/components/sensor/bitcoin.py
homeassistant/components/sensor/bitcoin.py
homeassistant/components/sensor/coinmarketcap.py
homeassistant/components/sensor/cpuspeed.py
homeassistant/components/sensor/cpuspeed.py
homeassistant/components/sensor/deutsche_bahn.py
homeassistant/components/sensor/deutsche_bahn.py
homeassistant/components/sensor/dht.py
homeassistant/components/sensor/dht.py
...
...
This diff is collapsed.
Click to expand it.
homeassistant/components/sensor/coinmarketcap.py
0 → 100644
+
125
−
0
View file @
a0a509ce
"""
Details about crypto currencies from CoinMarketCap.
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/sensor.coinmarketcap/
"""
import
logging
from
datetime
import
timedelta
import
json
from
urllib.error
import
HTTPError
import
voluptuous
as
vol
from
homeassistant.components.sensor
import
PLATFORM_SCHEMA
import
homeassistant.helpers.config_validation
as
cv
from
homeassistant.helpers.entity
import
Entity
from
homeassistant.util
import
Throttle
REQUIREMENTS
=
[
'
coinmarketcap==2.0.1
'
]
_LOGGER
=
logging
.
getLogger
(
__name__
)
ATTR_24H_VOLUME_USD
=
'
24h_volume_usd
'
ATTR_AVAILABLE_SUPPLY
=
'
available_supply
'
ATTR_MARKET_CAP
=
'
market_cap_usd
'
ATTR_NAME
=
'
name
'
ATTR_PERCENT_CHANGE_24H
=
'
percent_change_24h
'
ATTR_PERCENT_CHANGE_7D
=
'
percent_change_7d
'
ATTR_PRICE
=
'
price_usd
'
ATTR_SYMBOL
=
'
symbol
'
ATTR_TOTAL_SUPPLY
=
'
total_supply
'
CONF_CURRENCY
=
'
currency
'
DEFAULT_CURRENCY
=
'
bitcoin
'
ICON
=
'
mdi:currency-usd
'
MIN_TIME_BETWEEN_UPDATES
=
timedelta
(
minutes
=
15
)
PLATFORM_SCHEMA
=
PLATFORM_SCHEMA
.
extend
({
vol
.
Optional
(
CONF_CURRENCY
,
default
=
DEFAULT_CURRENCY
):
cv
.
string
,
})
def
setup_platform
(
hass
,
config
,
add_devices
,
discovery_info
=
None
):
"""
Setup the CoinMarketCap sensor.
"""
currency
=
config
.
get
(
CONF_CURRENCY
)
try
:
CoinMarketCapData
(
currency
).
update
()
except
HTTPError
:
_LOGGER
.
warning
(
'
Currency
"
%s
"
is not available. Using
"
bitcoin
"'
,
currency
)
currency
=
DEFAULT_CURRENCY
add_devices
([
CoinMarketCapSensor
(
CoinMarketCapData
(
currency
))])
# pylint: disable=too-few-public-methods
class
CoinMarketCapSensor
(
Entity
):
"""
Representation of a CoinMarketCap sensor.
"""
def
__init__
(
self
,
data
):
"""
Initialize the sensor.
"""
self
.
data
=
data
self
.
_ticker
=
None
self
.
_unit_of_measurement
=
'
USD
'
self
.
update
()
@property
def
name
(
self
):
"""
Return the name of the sensor.
"""
return
self
.
_ticker
.
get
(
'
name
'
)
@property
def
state
(
self
):
"""
Return the state of the sensor.
"""
return
self
.
_ticker
.
get
(
'
price_usd
'
)
@property
def
unit_of_measurement
(
self
):
"""
Return the unit the value is expressed in.
"""
return
self
.
_unit_of_measurement
@property
def
icon
(
self
):
"""
Return the icon to use in the frontend, if any.
"""
return
ICON
@property
def
state_attributes
(
self
):
"""
Return the state attributes of the sensor.
"""
return
{
ATTR_24H_VOLUME_USD
:
self
.
_ticker
.
get
(
'
24h_volume_usd
'
),
ATTR_AVAILABLE_SUPPLY
:
self
.
_ticker
.
get
(
'
available_supply
'
),
ATTR_MARKET_CAP
:
self
.
_ticker
.
get
(
'
market_cap_usd
'
),
ATTR_PERCENT_CHANGE_24H
:
self
.
_ticker
.
get
(
'
percent_change_24h
'
),
ATTR_PERCENT_CHANGE_7D
:
self
.
_ticker
.
get
(
'
percent_change_7d
'
),
ATTR_SYMBOL
:
self
.
_ticker
.
get
(
'
symbol
'
),
ATTR_TOTAL_SUPPLY
:
self
.
_ticker
.
get
(
'
total_supply
'
),
}
# pylint: disable=too-many-branches
def
update
(
self
):
"""
Get the latest data and updates the states.
"""
self
.
data
.
update
()
self
.
_ticker
=
json
.
loads
(
self
.
data
.
ticker
.
decode
(
'
utf-8
'
).
strip
(
'
\n
'
))[
0
]
class
CoinMarketCapData
(
object
):
"""
Get the latest data and update the states.
"""
def
__init__
(
self
,
currency
):
"""
Initialize the data object.
"""
self
.
currency
=
currency
self
.
ticker
=
None
@Throttle
(
MIN_TIME_BETWEEN_UPDATES
)
def
update
(
self
):
"""
Get the latest data from blockchain.info.
"""
from
coinmarketcap
import
Market
self
.
ticker
=
Market
().
ticker
(
self
.
currency
)
This diff is collapsed.
Click to expand it.
requirements_all.txt
+
3
−
0
View file @
a0a509ce
...
@@ -52,6 +52,9 @@ boto3==1.3.1
...
@@ -52,6 +52,9 @@ boto3==1.3.1
# homeassistant.components.http
# homeassistant.components.http
cherrypy==7.1.0
cherrypy==7.1.0
# homeassistant.components.sensor.coinmarketcap
coinmarketcap==2.0.1
# homeassistant.scripts.check_config
# homeassistant.scripts.check_config
colorlog>2.1,<3
colorlog>2.1,<3
...
...
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