Skip to content

Commit

Permalink
Upgrade to pyavanza v0.2.4
Browse files Browse the repository at this point in the history
The old api has been removed, monitored conditions will
not work since the api has a new structure on the
returned data.
  • Loading branch information
claha committed Dec 10, 2022
1 parent d6f7839 commit 6cb5bf2
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ repos:
- --quiet
files: ^((custom_components)/.+)?[^/]+\.py$

- repo: https://gitlab.com/pycqa/flake8
- repo: https://github.com/pycqa/flake8
rev: 4.0.1
hooks:
- id: flake8
Expand Down
2 changes: 1 addition & 1 deletion custom_components/avanza_stock/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
"issue_tracker": "https://github.com/custom-components/sensor.avanza_stock/issues",
"dependencies": [],
"codeowners": ["@claha"],
"requirements": ["pyavanza==0.2.2"],
"requirements": ["pyavanza==0.2.4"],
"iot_class": "cloud_polling"
}
18 changes: 9 additions & 9 deletions custom_components/avanza_stock/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,10 @@ async def async_update(self):
self._unit_of_measurement = self._currency

def _update_state(self, data):
self._state = data["lastPrice"]
self._state = data["quote"]["last"]

def _update_unit_of_measurement(self, data):
self._unit_of_measurement = data["currency"]
self._unit_of_measurement = data["listing"]["currency"]

def _update_state_attributes(self, data):
for condition in self._monitored_conditions:
Expand All @@ -254,7 +254,7 @@ def _update_state_attributes(self, data):
for (change, price) in CHANGE_PRICE_MAPPING:
if price in data:
self._state_attributes[change] = round(
data["lastPrice"] - data[price], 2
data["quote"]["last"] - data[price], 2
)
else:
self._state_attributes[change] = "unknown"
Expand All @@ -263,7 +263,7 @@ def _update_state_attributes(self, data):
for (change, price) in TOTAL_CHANGE_PRICE_MAPPING:
if price in data:
self._state_attributes[change] = round(
self._shares * (data["lastPrice"] - data[price]), 2
self._shares * (data["quote"]["last"] - data[price]), 2
)
else:
self._state_attributes[change] = "unknown"
Expand All @@ -272,21 +272,21 @@ def _update_state_attributes(self, data):
for (change, price) in CHANGE_PERCENT_PRICE_MAPPING:
if price in data:
self._state_attributes[change] = round(
100 * (data["lastPrice"] - data[price]) / data[price], 2
100 * (data["quote"]["last"] - data[price]) / data[price], 2
)
else:
self._state_attributes[change] = "unknown"

if self._shares is not None:
self._state_attributes["shares"] = self._shares
self._state_attributes["totalValue"] = round(
self._shares * data["lastPrice"], 2
self._shares * data["quote"]["last"], 2
)
self._state_attributes["totalChange"] = round(
self._shares * data["change"], 2
self._shares * data["quote"]["change"], 2
)

self._update_profit_loss(data["lastPrice"])
self._update_profit_loss(data["quote"]["last"])

def _update_key_ratios(self, data, attr):
key_ratios = data.get("keyRatios", {})
Expand Down Expand Up @@ -314,7 +314,7 @@ def _update_profit_loss(self, price):
)

def _update_conversion_rate(self, data):
rate = data["lastPrice"]
rate = data["quote"]["last"]
if self._invert_conversion_currency:
rate = 1.0 / rate
self._state = round(self._state * rate, 2)
Expand Down

0 comments on commit 6cb5bf2

Please sign in to comment.