Skip to content

Commit

Permalink
Fix historical changes attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
claha committed Jan 12, 2023
1 parent d4e2476 commit fd54191
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 33 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ sensor:
## Changelog
- 1.1.1 - Fix historical changes
- 1.1.0 - Use the new api
- 1.0.12 - Add unique id
- 1.0.11 - Rename device state attributes
Expand Down
51 changes: 25 additions & 26 deletions custom_components/avanza_stock/const.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Constants for avanza_stock."""
__version__ = "1.1.0"
__version__ = "1.1.1"

DEFAULT_NAME = "Avanza Stock"

Expand Down Expand Up @@ -38,7 +38,6 @@
"priceOneMonthAgo",
"priceOneWeekAgo",
"priceOneYearAgo",
"priceSixMonthsAgo",
"priceThreeMonthsAgo",
"priceThreeYearsAgo",
"pushPermitted",
Expand Down Expand Up @@ -84,48 +83,48 @@
]

CHANGE_PRICE_MAPPING = [
("changeOneWeek", "priceOneWeekAgo"),
("changeOneMonth", "priceOneMonthAgo"),
("changeThreeMonths", "priceThreeMonthsAgo"),
("changeSixMonths", "priceSixMonthsAgo"),
("changeOneYear", "priceOneYearAgo"),
("changeThreeYears", "priceThreeYearsAgo"),
("changeFiveYears", "priceFiveYearsAgo"),
("changeCurrentYear", "priceAtStartOfYear"),
("changeOneWeek", "oneWeek"),
("changeOneMonth", "oneMonth"),
("changeThreeMonths", "threeMonths"),
("changeOneYear", "oneYear"),
("changeThreeYears", "threeYears"),
("changeFiveYears", "fiveYears"),
("changeTenYears", "tenYears"),
("changeCurrentYear", "startOfYear"),
]

TOTAL_CHANGE_PRICE_MAPPING = [
("totalChangeOneWeek", "priceOneWeekAgo"),
("totalChangeOneMonth", "priceOneMonthAgo"),
("totalChangeOneWeek", "oneWeek"),
("totalChangeOneMonth", "oneMonth"),
(
"totalChangeThreeMonths",
"priceThreeMonthsAgo",
"threeMonths",
),
("totalChangeSixMonths", "priceSixMonthsAgo"),
("totalChangeOneYear", "priceOneYearAgo"),
("totalChangeOneYear", "oneYear"),
(
"totalChangeThreeYears",
"priceThreeYearsAgo",
"threeYears",
),
("totalChangeFiveYears", "priceFiveYearsAgo"),
("totalChangeFiveYears", "fiveYears"),
("totalChangeTenYears", "tenYears"),
(
"totalChangeCurrentYear",
"priceAtStartOfYear",
"startOfYear",
),
]

CHANGE_PERCENT_PRICE_MAPPING = [
("changePercentOneWeek", "priceOneWeekAgo"),
("changePercentOneMonth", "priceOneMonthAgo"),
("changePercentOneWeek", "oneWeek"),
("changePercentOneMonth", "oneMonth"),
(
"changePercentThreeMonths",
"priceThreeMonthsAgo",
"threeMonths",
),
("changePercentSixMonths", "priceSixMonthsAgo"),
("changePercentOneYear", "priceOneYearAgo"),
("changePercentThreeYears", "priceThreeYearsAgo"),
("changePercentFiveYears", "priceFiveYearsAgo"),
("changePercentCurrentYear", "priceAtStartOfYear"),
("changePercentOneYear", "oneYear"),
("changePercentThreeYears", "threeYears"),
("changePercentFiveYears", "fiveYears"),
("changePercentTenYears", "tenYears"),
("changePercentCurrentYear", "startOfYear"),
]

CURRENCY_ATTRIBUTE = [
Expand Down
2 changes: 1 addition & 1 deletion custom_components/avanza_stock/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"domain": "avanza_stock",
"name": "Avanza Stock",
"version": "1.1.0",
"version": "1.1.1",
"documentation": "https://github.com/custom-components/sensor.avanza_stock",
"issue_tracker": "https://github.com/custom-components/sensor.avanza_stock/issues",
"dependencies": [],
Expand Down
25 changes: 19 additions & 6 deletions custom_components/avanza_stock/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,27 +255,40 @@ def _update_state_attributes(self, data):

if condition == "change":
for (change, price) in CHANGE_PRICE_MAPPING:
if price in data:
if price in data["historicalClosingPrices"]:
self._state_attributes[change] = round(
data["quote"]["last"] - data[price], 2
data["quote"]["last"]
- data["historicalClosingPrices"][price],
2,
)
else:
self._state_attributes[change] = "unknown"

if self._shares is not None:
for (change, price) in TOTAL_CHANGE_PRICE_MAPPING:
if price in data:
if price in data["historicalClosingPrices"]:
self._state_attributes[change] = round(
self._shares * (data["quote"]["last"] - data[price]), 2
self._shares
* (
data["quote"]["last"]
- data["historicalClosingPrices"][price]
),
2,
)
else:
self._state_attributes[change] = "unknown"

if condition == "changePercent":
for (change, price) in CHANGE_PERCENT_PRICE_MAPPING:
if price in data:
if price in data["historicalClosingPrices"]:
self._state_attributes[change] = round(
100 * (data["quote"]["last"] - data[price]) / data[price], 2
100
* (
data["quote"]["last"]
- data["historicalClosingPrices"][price]
)
/ data["historicalClosingPrices"][price],
2,
)
else:
self._state_attributes[change] = "unknown"
Expand Down

0 comments on commit fd54191

Please sign in to comment.