Skip to content

Commit f4dbf75

Browse files
author
heyajohnny
committed
Version: 1.0.2 20241121 - Added cryptocurrency_id, cryptocurrency_name, cryptocurrency_symbol and currency_name to attributes
THIS WILL BREAK current sensors because of the cryptocurrency_names to cryptocurrency_ids change!!!
1 parent 155afe5 commit f4dbf75

File tree

7 files changed

+79
-57
lines changed

7 files changed

+79
-57
lines changed

README.md

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,27 +39,35 @@ The next step is to add cryptoinfo sensors to your Home Assistant:
3939
![image](https://github.com/user-attachments/assets/8ad3d15c-78ce-4c0a-892c-f0a967595f9d)
4040

4141
### Properties
42+
```
4243
- Identifier Unique name for the sensor
43-
- Cryptocurrency names One or more of the 'id' values (seperated by a , character) that you can find on this <a href='https://api.coingecko.com/api/v3/coins/list' target='_blank'>page</a>
44-
- Multipliers The number of coins/tokens (seperated by a , character). The number of Multipliers must match the number of Cryptocurrency names
44+
- Cryptocurrency id's One or more of the 'id' values (seperated by a , character) that you can find on this <a href='https://api.coingecko.com/api/v3/coins/list' target='_blank'>page</a>
45+
- Multipliers The number of coins/tokens (seperated by a , character). The number of Multipliers must match the number of Cryptocurrency id's
4546
- Currency name One of the currency names that you can find on this <a href='https://api.coingecko.com/api/v3/simple/supported_vs_currencies' target='_blank'>page</a>
4647
- Unit of measurement You can use a currency symbol or you can make it empty. You can find some symbols on this <a href='https://en.wikipedia.org/wiki/Currency_symbol#List_of_currency_symbols_currently_in_use' target='_blank'>page</a>
4748
- Update frequency (minutes) How often should the value be refreshed? Beware of the <a href='https://support.coingecko.com/hc/en-us/articles/4538771776153-What-is-the-rate-limit-for-CoinGecko-API-public-plan' target='_blank'>CoinGecko rate limit</a> when using multiple sensors
4849
- Minimum time between requests (minutes) The minimum time between the other sensors and this sensor to make a data request to the API. (This property is shared and the same for every sensor). You can set this value to 0 if you only use 1 sensor
50+
```
4951

5052
### Attributes
5153
The entities have some important attributes:
52-
- last_update This will return the date and time of the last update
53-
- base_price This will return the price of 1 coin / token in 'currency_name'(default = "usd") of the 'cryptocurrency_name'
54-
- multiplier This will return the number of coins / tokens
55-
- 24h_volume This will return the 24 hour volume in 'currency_name'(default = "usd") of the 'cryptocurrency_name'(default = "bitcoin")
56-
- 1h_change This will return the 1 hour change in percentage of the 'cryptocurrency_name'(default = "bitcoin")
57-
- 24h_change This will return the 24 hour change in percentage of the 'cryptocurrency_name'(default = "bitcoin")
58-
- 7d_change This will return the 7 day change in percentage of the 'cryptocurrency_name'(default = "bitcoin")
59-
- 30d_change This will return the 30 day change in percentage of the 'cryptocurrency_name'(default = "bitcoin")
60-
- market_cap This will return the total market cap of the 'cryptocurrency_name'(default = "bitcoin") displayed in 'currency_name'(default = "usd")
61-
- circulating_supply This will return the circulating supply of the 'cryptocurrency_name'(default = "bitcoin")
62-
- total_supply This will return the total supply of the 'cryptocurrency_name'(default = "bitcoin")
54+
```
55+
- last_update This will return the date and time of the last update
56+
- cryptocurrency_id This will return the cryptocurrency id
57+
- cryptocurrency_name This will return the cryptocurrency name
58+
- cryptocurrency_symbol This will return the cryptocurrency symbol
59+
- currency_name This will return the currency name
60+
- base_price This will return the price of 1 coin / token in 'currency_name'(default = "usd") of the 'cryptocurrency_id'
61+
- multiplier This will return the number of coins / tokens
62+
- 24h_volume This will return the 24 hour volume in 'currency_name'(default = "usd") of the 'cryptocurrency_id'(default = "bitcoin")
63+
- 1h_change This will return the 1 hour change in percentage of the 'cryptocurrency_id'(default = "bitcoin")
64+
- 24h_change This will return the 24 hour change in percentage of the 'cryptocurrency_id'(default = "bitcoin")
65+
- 7d_change This will return the 7 day change in percentage of the 'cryptocurrency_id'(default = "bitcoin")
66+
- 30d_change This will return the 30 day change in percentage of the 'cryptocurrency_id'(default = "bitcoin")
67+
- market_cap This will return the total market cap of the 'cryptocurrency_id'(default = "bitcoin") displayed in 'currency_name'(default = "usd")
68+
- circulating_supply This will return the circulating supply of the 'cryptocurrency_id'(default = "bitcoin")
69+
- total_supply This will return the total supply of the 'cryptocurrency_id'(default = "bitcoin")
70+
```
6371

6472
Template example for usage of attributes.
6573
This example creates a new sensor with the attribute value '24h_volume' of the sensor 'sensor.cryptoinfo_main_wallet_ethereum_eur':

Version

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ Version: 0.1.5 20230323 - Use floating point precision for the update interval
1717
Version: 0.1.6 20230323 - Use floating point precision for the update interval (typo fix)
1818
Version: 0.1.7 20230330 - Better exception
1919
Version: 1.0.0 20241119 - Complete refactor
20-
Version: 1.0.1 20241120 - Updated hacs.json + removed multiplier from identifier
20+
Version: 1.0.1 20241120 - Updated hacs.json + removed multiplier from identifier
21+
Version: 1.0.2 20241121 - Added cryptocurrency_id, cryptocurrency_name, cryptocurrency_symbol and currency_name to attributes

custom_components/cryptoinfo/config_flow.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
from .const.const import (
1717
_LOGGER,
18-
CONF_CRYPTOCURRENCY_NAMES,
18+
CONF_CRYPTOCURRENCY_IDS,
1919
CONF_CURRENCY_NAME,
2020
CONF_ID,
2121
CONF_MIN_TIME_BETWEEN_REQUESTS,
@@ -29,9 +29,9 @@
2929
"description_help": "For more information, see the <a href='https://github.com/heyajohnny/cryptoinfo' target='_blank'>documentation</a>.",
3030
"id_help": "Unique name for the sensor",
3131
"currency_name_help": "One of the currency names in <a href='https://api.coingecko.com/api/v3/simple/supported_vs_currencies' target='_blank'>this list</a>.",
32-
"cryptocurrency_names_help": "The 'id' values from one or more of the coins/tokens in <a href='https://api.coingecko.com/api/v3/coins/list' target='_blank'>this list</a>. seperated by , characters",
32+
"cryptocurrency_ids_help": "The 'id' values from one or more of the coins/tokens in <a href='https://api.coingecko.com/api/v3/coins/list' target='_blank'>this list</a>. seperated by , characters",
3333
"unit_of_measurement_help": "Do you want to use a currency symbol? (<a href='https://en.wikipedia.org/wiki/Currency_symbol#List_of_currency_symbols_currently_in_use' target='_blank'>Symbol list</a>)",
34-
"multipliers_help": "The number of coins/tokens (seperated by a , character). The number of Multipliers must match the number of Cryptocurrency names",
34+
"multipliers_help": "The number of coins/tokens (seperated by a , character). The number of Multipliers must match the number of Cryptocurrency id's",
3535
"update_frequency_help": "How often should the value be refreshed? Beware of the <a href='https://support.coingecko.com/hc/en-us/articles/4538771776153-What-is-the-rate-limit-for-CoinGecko-API-public-plan' target='_blank'>CoinGecko rate limit</a> when tracking multiple cryptocurrencies.",
3636
"min_time_between_requests_help": "The minimum time between the other entities and this entity to make a data request to the API. (This property is shared and the same for every entity)",
3737
}
@@ -45,17 +45,17 @@ def _validate_input(self, user_input: dict[str, Any]) -> dict[str, str]:
4545
errors = {}
4646

4747
# Split and clean the values
48-
crypto_names = [
49-
name.strip() for name in user_input[CONF_CRYPTOCURRENCY_NAMES].split(",")
48+
crypto_ids = [
49+
name.strip() for name in user_input[CONF_CRYPTOCURRENCY_IDS].split(",")
5050
]
5151
multipliers = [mult.strip() for mult in user_input[CONF_MULTIPLIERS].split(",")]
5252

5353
# Check if the counts match
54-
if len(crypto_names) != len(multipliers):
54+
if len(crypto_ids) != len(multipliers):
5555
return {
5656
"base": "mismatch_values",
5757
"count_context": {
58-
"crypto_count": len(crypto_names),
58+
"crypto_count": len(crypto_ids),
5959
"multiplier_count": len(multipliers),
6060
},
6161
}
@@ -116,8 +116,8 @@ async def _redo_configuration(
116116
{
117117
vol.Optional(CONF_ID, default=entry_data[CONF_ID]): str,
118118
vol.Required(
119-
CONF_CRYPTOCURRENCY_NAMES,
120-
default=entry_data[CONF_CRYPTOCURRENCY_NAMES],
119+
CONF_CRYPTOCURRENCY_IDS,
120+
default=entry_data[CONF_CRYPTOCURRENCY_IDS],
121121
): str,
122122
vol.Required(
123123
CONF_CURRENCY_NAME, default=entry_data[CONF_CURRENCY_NAME]
@@ -162,7 +162,7 @@ async def async_step_user(self, user_input: dict[str, Any] | None = None):
162162
# Use user_input values as defaults if they exist, otherwise use the original defaults
163163
defaults = {
164164
CONF_ID: "Main btc stash",
165-
CONF_CRYPTOCURRENCY_NAMES: "bitcoin, ethereum",
165+
CONF_CRYPTOCURRENCY_IDS: "bitcoin, ethereum",
166166
CONF_MULTIPLIERS: "1, 32",
167167
CONF_CURRENCY_NAME: "usd",
168168
CONF_UNIT_OF_MEASUREMENT: "$",
@@ -178,8 +178,8 @@ async def async_step_user(self, user_input: dict[str, Any] | None = None):
178178
{
179179
vol.Optional(CONF_ID, default=defaults[CONF_ID]): str,
180180
vol.Required(
181-
CONF_CRYPTOCURRENCY_NAMES,
182-
default=defaults[CONF_CRYPTOCURRENCY_NAMES],
181+
CONF_CRYPTOCURRENCY_IDS,
182+
default=defaults[CONF_CRYPTOCURRENCY_IDS],
183183
): str,
184184
vol.Required(CONF_MULTIPLIERS, default=defaults[CONF_MULTIPLIERS]): str,
185185
vol.Required(

custom_components/cryptoinfo/const/const.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
DOMAIN = "cryptoinfo"
44

55
CONF_ID = "id"
6-
CONF_CRYPTOCURRENCY_NAMES = "cryptocurrency_names"
6+
CONF_CRYPTOCURRENCY_IDS = "cryptocurrency_ids"
77
CONF_CURRENCY_NAME = "currency_name"
88
CONF_MULTIPLIERS = "multipliers"
99
CONF_UPDATE_FREQUENCY = "update_frequency"
@@ -12,6 +12,10 @@
1212

1313
SENSOR_PREFIX = "Cryptoinfo "
1414
ATTR_LAST_UPDATE = "last_update"
15+
ATTR_CRYPTOCURRENCY_ID = "cryptocurrency_id"
16+
ATTR_CRYPTOCURRENCY_NAME = "cryptocurrency_name"
17+
ATTR_CRYPTOCURRENCY_SYMBOL = "cryptocurrency_symbol"
18+
ATTR_CURRENCY_NAME = "currency_name"
1519
ATTR_BASE_PRICE = "baseprice"
1620
ATTR_MULTIPLIER = "multiplier"
1721
ATTR_24H_VOLUME = "24h_volume"

custom_components/cryptoinfo/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"domain": "cryptoinfo",
33
"name": "Cryptoinfo",
4-
"version": "1.0.1",
4+
"version": "1.0.2",
55
"config_flow": true,
66
"iot_class": "cloud_polling",
77
"documentation": "https://github.com/heyajohnny/cryptoinfo",

custom_components/cryptoinfo/sensor.py

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,20 @@
2828
ATTR_BASE_PRICE,
2929
ATTR_CIRCULATING_SUPPLY,
3030
ATTR_LAST_UPDATE,
31+
ATTR_CRYPTOCURRENCY_ID,
32+
ATTR_CRYPTOCURRENCY_NAME,
33+
ATTR_CRYPTOCURRENCY_SYMBOL,
34+
ATTR_CURRENCY_NAME,
3135
ATTR_MARKET_CAP,
3236
ATTR_MULTIPLIER,
3337
ATTR_TOTAL_SUPPLY,
34-
CONF_CRYPTOCURRENCY_NAMES,
38+
CONF_CRYPTOCURRENCY_IDS,
3539
CONF_CURRENCY_NAME,
3640
CONF_ID,
3741
CONF_MIN_TIME_BETWEEN_REQUESTS,
3842
CONF_MULTIPLIERS,
3943
CONF_UNIT_OF_MEASUREMENT,
4044
CONF_UPDATE_FREQUENCY,
41-
DOMAIN,
4245
SENSOR_PREFIX,
4346
)
4447

@@ -53,7 +56,7 @@ async def async_setup_entry(
5356
config = config_entry.data
5457

5558
id_name = config.get(CONF_ID)
56-
cryptocurrency_names = config.get(CONF_CRYPTOCURRENCY_NAMES).lower().strip()
59+
cryptocurrency_ids = config.get(CONF_CRYPTOCURRENCY_IDS).lower().strip()
5760
currency_name = config.get(CONF_CURRENCY_NAME).strip()
5861
unit_of_measurement = config.get(CONF_UNIT_OF_MEASUREMENT).strip()
5962
multipliers = config.get(CONF_MULTIPLIERS).strip()
@@ -65,7 +68,7 @@ async def async_setup_entry(
6568
# Create coordinator for centralized data fetching
6669
coordinator = CryptoDataCoordinator(
6770
hass,
68-
cryptocurrency_names,
71+
cryptocurrency_ids,
6972
currency_name,
7073
update_frequency,
7174
min_time_between_requests,
@@ -76,24 +79,24 @@ async def async_setup_entry(
7679
await coordinator.async_config_entry_first_refresh()
7780

7881
entities = []
79-
crypto_list = [crypto.strip() for crypto in cryptocurrency_names.split(",")]
82+
crypto_list = [crypto.strip() for crypto in cryptocurrency_ids.split(",")]
8083
multipliers_list = [multiplier.strip() for multiplier in multipliers.split(",")]
8184

8285
multipliers_length = len(multipliers_list)
8386
crypto_list_length = len(crypto_list)
8487

8588
if multipliers_length != crypto_list_length:
8689
_LOGGER.error(
87-
f"Length mismatch: multipliers ({multipliers_length}) and cryptocurrency names ({crypto_list_length}) must have the same length"
90+
f"Length mismatch: multipliers ({multipliers_length}) and cryptocurrency id's ({crypto_list_length}) must have the same length"
8891
)
8992
return False
9093

91-
for i, cryptocurrency_name in enumerate(crypto_list):
94+
for i, cryptocurrency_id in enumerate(crypto_list):
9295
try:
9396
entities.append(
9497
CryptoinfoSensor(
9598
coordinator,
96-
cryptocurrency_name,
99+
cryptocurrency_id,
97100
currency_name,
98101
unit_of_measurement,
99102
multipliers_list[i],
@@ -116,7 +119,7 @@ class CryptoDataCoordinator(DataUpdateCoordinator):
116119
def __init__(
117120
self,
118121
hass: HomeAssistant,
119-
cryptocurrency_names: str,
122+
cryptocurrency_ids: str,
120123
currency_name: str,
121124
update_frequency: timedelta,
122125
min_time_between_requests: timedelta,
@@ -133,7 +136,7 @@ def __init__(
133136
) # Assign current count as instance ID
134137
CryptoDataCoordinator._instance_count += 1 # Increment the counter
135138
CryptoDataCoordinator._active_coordinators.add(self.instance_id)
136-
self.cryptocurrency_names = cryptocurrency_names
139+
self.cryptocurrency_ids = cryptocurrency_ids
137140
self.currency_name = currency_name
138141
self.id_name = id_name
139142
self.min_time_between_requests = min_time_between_requests
@@ -157,12 +160,12 @@ async def _async_update_data(self):
157160
CryptoDataCoordinator._last_updated_id = self.instance_id
158161

159162
_LOGGER.debug(
160-
f"First request, fetching data for sensor: {self.id_name} instance_id: {self.instance_id} cryptocurrency_names: {self.cryptocurrency_names}"
163+
f"First request, fetching data for sensor: {self.id_name} instance_id: {self.instance_id} cryptocurrency_ids: {self.cryptocurrency_ids}"
161164
)
162165

163166
url = (
164167
f"{API_ENDPOINT}coins/markets"
165-
f"?ids={self.cryptocurrency_names}"
168+
f"?ids={self.cryptocurrency_ids}"
166169
f"&vs_currency={self.currency_name}"
167170
f"&price_change_percentage=1h%2C24h%2C7d%2C30d"
168171
)
@@ -213,12 +216,12 @@ async def _async_update_data(self):
213216
return self.data if self.data else None
214217

215218
_LOGGER.debug(
216-
f"Fetch data from API endpoint, sensor: {self.id_name} instance_id: {self.instance_id} cryptocurrency_names: {self.cryptocurrency_names}"
219+
f"Fetch data from API endpoint, sensor: {self.id_name} instance_id: {self.instance_id} cryptocurrency_ids: {self.cryptocurrency_ids}"
217220
)
218221

219222
url = (
220223
f"{API_ENDPOINT}coins/markets"
221-
f"?ids={self.cryptocurrency_names}"
224+
f"?ids={self.cryptocurrency_ids}"
222225
f"&vs_currency={self.currency_name}"
223226
f"&price_change_percentage=1h%2C24h%2C7d%2C30d"
224227
)
@@ -242,14 +245,14 @@ class CryptoinfoSensor(CoordinatorEntity):
242245
def __init__(
243246
self,
244247
coordinator: CryptoDataCoordinator,
245-
cryptocurrency_name: str,
248+
cryptocurrency_id: str,
246249
currency_name: str,
247250
unit_of_measurement: str,
248251
multiplier: str,
249252
id_name: str,
250253
):
251254
super().__init__(coordinator)
252-
self.cryptocurrency_name = cryptocurrency_name
255+
self.cryptocurrency_id = cryptocurrency_id
253256
self.currency_name = currency_name
254257
self._unit_of_measurement = unit_of_measurement
255258
self.multiplier = multiplier
@@ -258,7 +261,7 @@ def __init__(
258261
(SENSOR_PREFIX + (id_name + " " if len(id_name) > 0 else ""))
259262
.lower()
260263
.replace(" ", "_")
261-
+ cryptocurrency_name
264+
+ cryptocurrency_id
262265
+ "_"
263266
+ currency_name
264267
)
@@ -267,7 +270,7 @@ def __init__(
267270
self._attr_unique_id = (
268271
SENSOR_PREFIX
269272
+ (id_name + " " if len(id_name) > 0 else "")
270-
+ cryptocurrency_name
273+
+ cryptocurrency_id
271274
+ currency_name
272275
)
273276

@@ -286,9 +289,9 @@ def unit_of_measurement(self):
286289
@property
287290
def state(self):
288291
"""Return the state of the sensor."""
289-
if self.coordinator.data and self.cryptocurrency_name in self.coordinator.data:
292+
if self.coordinator.data and self.cryptocurrency_id in self.coordinator.data:
290293
return float(
291-
self.coordinator.data[self.cryptocurrency_name]["current_price"]
294+
self.coordinator.data[self.cryptocurrency_id]["current_price"]
292295
) * float(self.multiplier)
293296
return None
294297

@@ -297,10 +300,12 @@ def extra_state_attributes(self):
297300
"""Return the state attributes."""
298301
if (
299302
not self.coordinator.data
300-
or self.cryptocurrency_name not in self.coordinator.data
303+
or self.cryptocurrency_id not in self.coordinator.data
301304
):
302305
return {
303306
ATTR_LAST_UPDATE: datetime.today().strftime("%d-%m-%Y %H:%M"),
307+
ATTR_CRYPTOCURRENCY_NAME: None,
308+
ATTR_CURRENCY_NAME: None,
304309
ATTR_BASE_PRICE: None,
305310
ATTR_MULTIPLIER: None,
306311
ATTR_24H_VOLUME: None,
@@ -313,9 +318,13 @@ def extra_state_attributes(self):
313318
ATTR_TOTAL_SUPPLY: None,
314319
}
315320

316-
data = self.coordinator.data[self.cryptocurrency_name]
321+
data = self.coordinator.data[self.cryptocurrency_id]
317322
return {
318323
ATTR_LAST_UPDATE: datetime.today().strftime("%d-%m-%Y %H:%M"),
324+
ATTR_CRYPTOCURRENCY_ID: self.cryptocurrency_id,
325+
ATTR_CRYPTOCURRENCY_NAME: data["name"],
326+
ATTR_CRYPTOCURRENCY_SYMBOL: data["symbol"],
327+
ATTR_CURRENCY_NAME: self.currency_name,
319328
ATTR_BASE_PRICE: data["current_price"],
320329
ATTR_MULTIPLIER: self.multiplier,
321330
ATTR_24H_VOLUME: data["total_volume"],

0 commit comments

Comments
 (0)