Skip to content

Commit

Permalink
Update avanza stock to version 1.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
claha committed Aug 18, 2023
1 parent cdabda2 commit d019d38
Show file tree
Hide file tree
Showing 5 changed files with 444 additions and 408 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Constants for avanza_stock."""
__version__ = "1.2.0"
__version__ = "1.3.0"

DEFAULT_NAME = "Avanza Stock"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"domain": "avanza_stock",
"name": "Avanza Stock",
"version": "1.2.0",
"codeowners": ["@claha"],
"dependencies": [],
"documentation": "https://github.com/custom-components/sensor.avanza_stock",
"iot_class": "cloud_polling",
"issue_tracker": "https://github.com/custom-components/sensor.avanza_stock/issues",
"dependencies": [],
"codeowners": ["@claha"],
"requirements": ["pyavanza==0.6.0"],
"iot_class": "cloud_polling"
"version": "1.3.0"
}
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ def unit_of_measurement(self):
@property
def unique_id(self):
"""Return the unique id."""
return f"{self._stock}_stock"
return f"{self._stock}_{self._name}_stock"

@property
def state_class(self):
Expand All @@ -219,14 +219,38 @@ def device_class(self):

async def async_update(self):
"""Update state and attributes."""
data = await pyavanza.get_stock_async(self._session, self._stock)
if data["type"] == pyavanza.InstrumentType.ExchangeTradedFund:
data = await pyavanza.get_etf_async(self._session, self._stock)
data_conversion_currency = None
if self._conversion_currency:
data_conversion_currency = await pyavanza.get_stock_async(
self._session, self._conversion_currency
)
if self._stock == 0: # Non trackable, i.e. manual
data = {
"name": self._name.split(" ", 1)[1],
"unit_of_measurement": self._currency,
"quote": {
"last": self._purchase_price,
"change": 0,
"changePercent": 0,
},
"historicalClosingPrices": {
"oneWeek": self._purchase_price,
"oneMonth": self._purchase_price,
"threeMonths": self._purchase_price,
"oneYear": self._purchase_price,
"threeYears": self._purchase_price,
"fiveYears": self._purchase_price,
"tenYears": self._purchase_price,
"startOfYear": self._purchase_price,
},
"listing": {
"currency": self._currency,
},
}
else:
data = await pyavanza.get_stock_async(self._session, self._stock)
if data["type"] == pyavanza.InstrumentType.ExchangeTradedFund:
data = await pyavanza.get_etf_async(self._session, self._stock)
if self._conversion_currency:
data_conversion_currency = await pyavanza.get_stock_async(
self._session, self._conversion_currency
)
if data:
self._update_state(data)
self._update_unit_of_measurement(data)
Expand Down
6 changes: 6 additions & 0 deletions roles/homeassistant/templates/portfolio.yaml.j2
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
{% else %}
shares: {{ stock.shares|sum }}
{% endif %}
{%if stock.purchase_price is defined %}
purchase_price: {{ stock.purchase_price }}
{% endif%}
{%if stock.currency is defined %}
currency: {{ stock.currency }}
{% endif%}
{% endfor %}
monitored_conditions:
- change
Expand Down
Loading

0 comments on commit d019d38

Please sign in to comment.