Skip to content

Commit dee0650

Browse files
committed
preparations for upcoming features
1 parent 2607e04 commit dee0650

File tree

6 files changed

+126
-69
lines changed

6 files changed

+126
-69
lines changed

custom_components/helium_solana/__init__.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,34 @@
11
from homeassistant import core, config_entries
2-
from .const import DOMAIN
2+
from .const import DOMAIN, CONF_VERSION, CONF_WALLET, CONF_WALLETS, CONF_WALLET_COUNT, CONF_INTEGRATION, CONF_INTEGRATION_VALUES
33
import logging
44
import asyncio
55

66
_LOGGER = logging.getLogger(__name__)
77

8+
9+
# Example migration function
10+
async def async_migrate_entry(hass: core.HomeAssistant, config_entry: config_entries.ConfigEntry):
11+
"""Migrate old entry."""
12+
_LOGGER.debug("Migrating from version %s", config_entry.version)
13+
14+
if config_entry.version == 1:
15+
16+
new = {**config_entry.data}
17+
# TODO: modify Config Entry data
18+
new[CONF_INTEGRATION] = 'wallet'
19+
new[CONF_WALLET] = new[CONF_WALLETS][0]
20+
new[CONF_VERSION] = 2
21+
del new[CONF_WALLETS]
22+
del new[CONF_WALLET_COUNT]
23+
24+
config_entry.version = 2
25+
config_entry.title = 'Wallet (Migrated) '+new[CONF_WALLET][0:4]
26+
hass.config_entries.async_update_entry(config_entry, data=new)
27+
28+
_LOGGER.info("Migration to version %s successful", config_entry.version)
29+
30+
return True
31+
832
async def async_setup(hass: core.HomeAssistant, config: dict) -> bool:
933
"""Set up the GitHub Custom component from yaml configuration."""
1034
hass.data.setdefault(DOMAIN, {})
Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,41 @@
11
from homeassistant import config_entries
2-
from .const import DOMAIN, CONF_WALLET, CONF_WALLET_COUNT, CONF_WALLETS
2+
from .const import DOMAIN, CONF_VERSION, CONF_WALLET, CONF_WALLET_COUNT, CONF_WALLETS, CONF_INTEGRATION, CONF_INTEGRATION_VALUES
33
import voluptuous as vol
44
import homeassistant.helpers.config_validation as cv
55

66
class HeliumSolanaConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
77
"""Example config flow."""
88
# The schema version of the entries that it creates
99
# Home Assistant will call your migrate method if the version changes
10-
VERSION = 1
10+
VERSION = 2
1111

12+
13+
1214
async def async_step_user(self, info):
1315
if info is not None:
14-
16+
selected_integration_label = info[CONF_INTEGRATION]
17+
selected_integration = [key for key, value in CONF_INTEGRATION_VALUES.items() if value == selected_integration_label]
18+
if len(selected_integration) != 1:
19+
return
20+
selected_integration = selected_integration[0]
1521
self.data = {}
16-
self.data[CONF_WALLET_COUNT] = info[CONF_WALLET_COUNT]
17-
self.data[CONF_WALLETS] = []
18-
19-
return self.async_show_form(
20-
step_id="wallets", data_schema=vol.Schema({vol.Required(CONF_WALLET): str})
21-
)
22+
self.data[CONF_VERSION] = self.VERSION
23+
self.data[CONF_INTEGRATION] = selected_integration
24+
self.title = selected_integration_label
2225

26+
if selected_integration == 'wallet':
27+
return self.async_show_form(
28+
step_id="wallet", data_schema=vol.Schema({vol.Required(CONF_WALLET): str})
29+
)
30+
31+
return self.async_create_entry(title=self.title, data=self.data)
2332
return self.async_show_form(
24-
step_id="user", data_schema=vol.Schema({vol.Required(CONF_WALLET_COUNT, default=1): int})
33+
step_id="user", data_schema=vol.Schema({vol.Required(CONF_INTEGRATION): vol.In(CONF_INTEGRATION_VALUES.values())})
2534
)
2635

27-
async def async_step_wallets(self, info):
36+
async def async_step_wallet(self, info):
2837

29-
self.data[CONF_WALLETS].append(info[CONF_WALLET])
30-
31-
if self.data[CONF_WALLET_COUNT] == len(self.data[CONF_WALLETS]):
32-
return self.async_create_entry(title="Helium Integration", data=self.data)
33-
34-
return self.async_show_form(
35-
step_id="wallets", data_schema=vol.Schema({vol.Required(CONF_WALLET): str})
36-
)
38+
self.data[CONF_WALLET] = info[CONF_WALLET]
39+
self.title = self.title+' '+self.data[CONF_WALLET][0:4]
40+
return self.async_create_entry(title=self.title, data=self.data)
3741

custom_components/helium_solana/const.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,24 @@
11
DOMAIN = "helium_solana"
2-
2+
CONF_VERSION = 'version'
3+
CONF_TITLE = 'title'
34
CONF_WALLETS = 'wallets'
45
CONF_WALLET = "wallet"
56
CONF_WALLET_COUNT = "wallet_count"
67
CONF_HOTSPOTS = 'hotspots'
78
CONF_PRICES = 'prices'
8-
9+
CONF_INTEGRATION = "integration"
10+
CONF_INTEGRATION_VALUES = {
11+
'general_stats': 'General Helium Stats',
12+
'general_token_price': 'Token Prices',
13+
'wallet' : 'Wallet',
14+
#'wallet_balance': 'Wallet Balance',
15+
#'wallet_staking': 'Wallet Staking',
16+
#'wallet_hotspots': 'Wallet Hotspots',
17+
18+
# 'governance_hnt': 'Governance HNT',
19+
# 'iot_device': 'IOT Device (Helium Console)'
20+
# 'hotspot_rewards': 'Specific Hotspot Rewards'
21+
}
922
HOTSPOTTY_STATS = "https://beta-api.hotspotty.net/api/v1/stats"
1023
HOTSPOTTY_PRICES = "https://beta-api.hotspotty.net/api/v1/tokens/prices"
1124
HOTSPOTTY_HOTSPOT_INFO = "https://beta-api.hotspotty.net/api/v1/wallets/hotspots/"

custom_components/helium_solana/sensor.py

Lines changed: 58 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828

2929
from .utility import http_client
3030

31-
from .api.heliumstats import HeliumStatsAPI
3231
from .api.backend import BackendAPI
3332
from .sensors.WalletBalance import WalletBalance
3433
from .sensors.HotspotReward import HotspotReward
@@ -40,6 +39,8 @@
4039

4140
from .const import (
4241
DOMAIN,
42+
CONF_VERSION,
43+
CONF_INTEGRATION,
4344
CONF_WALLET,
4445
CONF_WALLETS,
4546
CONF_HOTSPOTS,
@@ -64,7 +65,6 @@
6465
}
6566
)
6667

67-
heliumStatsAPI = HeliumStatsAPI(HOTSPOTTY_STATS, HOTSPOTTY_TOKEN)
6868
api_backend = BackendAPI()
6969

7070
async def async_setup_entry(
@@ -73,9 +73,19 @@ async def async_setup_entry(
7373
async_add_entities
7474
):
7575
config = hass.data[DOMAIN][config_entry.entry_id]
76-
wallets = config.get(CONF_WALLETS)
77-
sensors = await get_sensors(wallets, None)
78-
76+
integration = config.get(CONF_INTEGRATION)
77+
wallet = config.get(CONF_WALLET)
78+
sensors = await get_sensors(integration, wallet, None)
79+
80+
#version = config.get(CONF_VERSION)
81+
#if version == 2:
82+
# integration = config.get(CONF_INTEGRATION)
83+
# wallet = config.get(CONF_WALLET)
84+
# sensors = await get_sensors(integration, wallet, None)
85+
#elif version is None:
86+
# wallets = config.get(CONF_WALLETS)
87+
#print(wallets)
88+
# sensors = await get_sensors_legacy(wallets, None)
7989
async_add_entities(sensors, update_before_add=True)
8090

8191
async def async_setup_platform(
@@ -87,44 +97,44 @@ async def async_setup_platform(
8797
"""Set up the sensor platform."""
8898
wallets = config.get(CONF_WALLETS)
8999
prices = config.get(CONF_PRICES)
90-
sensors = await get_sensors(wallets, prices)
100+
sensors = await get_sensors_legacy(wallets, prices)
91101

92102
async_add_entities(sensors, update_before_add=True)
93103

94-
async def get_sensors(wallets, prices):
104+
async def get_sensors(integration, wallet, prices):
95105
sensors = []
96-
sensors.append(PriceSensor(http_client, ADDRESS_IOT, 'IOT', 'helium-iot'))
97-
sensors.append(PriceSensor(http_client, ADDRESS_MOBILE, 'MOBILE', 'helium-mobile'))
98-
sensors.append(PriceSensor(http_client, ADDRESS_HNT, 'HNT','helium'))
99-
sensors.append(PriceSensor(http_client, ADDRESS_SOLANA, 'SOLANA', 'wrapped-solana'))
100-
101-
102-
if prices:
103-
for price in prices:
104-
sensors.append(PriceSensor(price))
105-
106-
sensors.append(HeliumStats(api_backend, 'IOT', 'total_hotspots', 'Total Hotspots', ['stats', 'iot', 'total_hotspots'], 'mdi:router-wireless', 'Hotspots'))
107-
sensors.append(HeliumStats(api_backend, 'IOT', 'active_hotspots', 'Active Hotspots', ['stats', 'iot', 'active_hotspots'], 'mdi:router-wireless', 'Hotspots'))
108-
sensors.append(HeliumStats(api_backend, 'IOT', 'total_cities', 'Total Cities', ['stats', 'iot', 'total_cities'],'mdi:city', 'Cities'))
109-
sensors.append(HeliumStats(api_backend, 'IOT', 'total_countries', 'Total Countries', ['stats', 'iot', 'total_countries'], 'mdi:earth', 'Countries'))
110-
sensors.append(HeliumStats(api_backend, 'IOT', 'daily_average_rewards', 'Daily Average Rewards', ['stats', 'iot', 'daily_average_rewards'], 'mdi:hand-coin-outline', 'IOT', 'float'))
111106

112-
sensors.append(HeliumStats(api_backend, 'MOBILE', 'total_hotspots', 'Total Hotspots', ['stats', 'mobile', 'total_hotspots'],'mdi:router-wireless', 'Hotspots'))
113-
sensors.append(HeliumStats(api_backend, 'MOBILE', 'active_hotspots', 'Active Hotspots', ['stats', 'mobile', 'active_hotspots'],'mdi:router-wireless', 'Hotspots'))
114-
sensors.append(HeliumStats(api_backend, 'MOBILE', 'total_cities', 'Total Cities', ['stats', 'mobile', 'total_cities'], 'mdi:city', 'Cities'))
115-
sensors.append(HeliumStats(api_backend, 'MOBILE', 'total_countries', 'Total Countries', ['stats', 'mobile', 'total_countries'], 'mdi:earth', 'Countries'))
116-
sensors.append(HeliumStats(api_backend, 'MOBILE', 'daily_average_rewards', 'Daily Average Rewards', ['stats', 'mobile', 'daily_average_rewards'], 'mdi:hand-coin-outline', 'MOBILE' ,'float'))
117-
118-
for wallet in wallets:
119-
len_wallet = len(wallet)
120-
if len_wallet <32 or len_wallet > 44:
121-
continue
122-
107+
if integration == 'general_token_price':
108+
sensors.append(PriceSensor(http_client, ADDRESS_IOT, 'IOT', 'helium-iot'))
109+
sensors.append(PriceSensor(http_client, ADDRESS_MOBILE, 'MOBILE', 'helium-mobile'))
110+
sensors.append(PriceSensor(http_client, ADDRESS_HNT, 'HNT','helium'))
111+
sensors.append(PriceSensor(http_client, ADDRESS_SOLANA, 'SOLANA', 'wrapped-solana'))
112+
113+
if prices:
114+
for price in prices:
115+
sensors.append(PriceSensor(price))
116+
117+
if integration == 'general_stats':
118+
sensors.append(HeliumStats(api_backend, 'IOT', 'total_hotspots', 'Total Hotspots', ['stats', 'iot', 'total_hotspots'], 'mdi:router-wireless', 'Hotspots'))
119+
sensors.append(HeliumStats(api_backend, 'IOT', 'active_hotspots', 'Active Hotspots', ['stats', 'iot', 'active_hotspots'], 'mdi:router-wireless', 'Hotspots'))
120+
sensors.append(HeliumStats(api_backend, 'IOT', 'total_cities', 'Total Cities', ['stats', 'iot', 'total_cities'],'mdi:city', 'Cities'))
121+
sensors.append(HeliumStats(api_backend, 'IOT', 'total_countries', 'Total Countries', ['stats', 'iot', 'total_countries'], 'mdi:earth', 'Countries'))
122+
sensors.append(HeliumStats(api_backend, 'IOT', 'daily_average_rewards', 'Daily Average Rewards', ['stats', 'iot', 'daily_average_rewards'], 'mdi:hand-coin-outline', 'IOT', 'float'))
123+
124+
sensors.append(HeliumStats(api_backend, 'MOBILE', 'total_hotspots', 'Total Hotspots', ['stats', 'mobile', 'total_hotspots'],'mdi:router-wireless', 'Hotspots'))
125+
sensors.append(HeliumStats(api_backend, 'MOBILE', 'active_hotspots', 'Active Hotspots', ['stats', 'mobile', 'active_hotspots'],'mdi:router-wireless', 'Hotspots'))
126+
sensors.append(HeliumStats(api_backend, 'MOBILE', 'total_cities', 'Total Cities', ['stats', 'mobile', 'total_cities'], 'mdi:city', 'Cities'))
127+
sensors.append(HeliumStats(api_backend, 'MOBILE', 'total_countries', 'Total Countries', ['stats', 'mobile', 'total_countries'], 'mdi:earth', 'Countries'))
128+
sensors.append(HeliumStats(api_backend, 'MOBILE', 'daily_average_rewards', 'Daily Average Rewards', ['stats', 'mobile', 'daily_average_rewards'], 'mdi:hand-coin-outline', 'MOBILE' ,'float'))
129+
130+
if integration == 'wallet':
131+
#if integration == 'wallet_balance':
123132
sensors.append(WalletBalance(api_backend, wallet, 'hnt', ['balance', 'hnt'], 'HNT','mdi:wallet'))
124133
sensors.append(WalletBalance(api_backend, wallet, 'iot', ['balance', 'iot'], 'IOT','mdi:wallet'))
125134
sensors.append(WalletBalance(api_backend, wallet, 'sol', ['balance', 'solana'], 'SOL','mdi:wallet'))
126135
sensors.append(WalletBalance(api_backend, wallet, 'mobile', ['balance', 'mobile'], 'MOBILE','mdi:wallet'))
127136

137+
#if integration == 'wallet_hotspots':
128138
response = await api_backend.get_data('hotspot-rewards2/'+str(wallet))
129139
if response.status_code == 200:
130140
rewards = response.json()
@@ -142,6 +152,7 @@ async def get_sensors(wallets, prices):
142152
sensors.append(HotspotReward(api_backend, wallet, wallet, ['rewards_aggregated', token, 'unclaimed_rewards'], 'Unclaimed Rewards', token, 'mdi:hand-coin-outline'))
143153
sensors.append(HotspotReward(api_backend, wallet, wallet, ['rewards_aggregated', token, 'total_rewards'], 'Total Rewards', token, 'mdi:hand-coin-outline'))
144154

155+
#if integration == 'wallet_staking':
145156
response = await api_backend.get_data('staking-rewards/'+str(wallet))
146157
if response.status_code == 200:
147158
rewards = response.json()
@@ -153,11 +164,22 @@ async def get_sensors(wallets, prices):
153164
sensors.append(StakingRewardsToken(api_backend, wallet, token, 'mdi:hand-coin-outline'))
154165
#pass
155166

167+
return sensors
156168

157-
#_LOGGER.info(rewards)
169+
async def get_sensors_legacy(wallets, prices):
170+
sensors = []
171+
sensors += await get_sensors('general_token_price', None, prices)
172+
sensors += await get_sensors('general_stats', None, None)
158173

159174

160-
#sensors.append(HotspotReward(backendAPI, wallet, 'iot', ['rewards', ADDRESS_IOT], 'IOT', 'mdi:hand-coin-outline'))
161-
#sensors.append(HotspotReward(backendAPI, wallet, 'mobile', ['rewards', ADDRESS_MOBILE], 'MOBILE', 'mdi:hand-coin-outline'))
175+
for wallet in wallets:
176+
len_wallet = len(wallet)
177+
if len_wallet <32 or len_wallet > 44:
178+
continue
179+
180+
sensors += await get_sensors('wallet', wallet, None)
181+
#sensors += await get_sensors('wallet_balance', wallet, None)
182+
#sensors += await get_sensors('wallet_hotspots', wallet, None)
183+
#sensors += await get_sensors('wallet_staking', wallet, None)
162184

163185
return sensors

custom_components/helium_solana/translations/en.json

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,10 @@
22
"config": {
33
"step": {
44
"user": {
5-
"title": "Number of Wallets",
6-
"description": "Please enter the number of wallets, you would like to add. In the next step, you will be asked about the wallet addresses one by one.",
7-
"data": {
8-
"wallet_count": "Number of Wallets"
9-
}
5+
"title": "Select Integration",
6+
"description": "Please select the integration you want to add."
107
},
11-
"wallets": {
8+
"wallet": {
129
"title": "Solana Wallet Address",
1310
"description": "You can find this address in your Helium App (black icon) > Settings > Copy Address > Solana. After successful setup, once reloading the integration or restarting Home Assistant is needed.",
1411
"data": {

custom_components/helium_solana/translations/pt.json

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,8 @@
22
"config": {
33
"step": {
44
"user": {
5-
"title": "Numero de carteiras",
6-
"description": "Por Favor insira o número de carteiras, que deseja adicionar. No próximo passo irá ser pedido o endereço um de cada vez.",
7-
"data": {
8-
"wallet_count": "Número de Carteiras"
9-
}
5+
"title": "Selecionar Integração",
6+
"description": "Por favor, selecione a integração que deseja adicionar."
107
},
118
"wallets": {
129
"title": "Endreço da Carteira Solana",

0 commit comments

Comments
 (0)