diff --git a/README.md b/README.md index 8c39531..6002726 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/custom_components/avanza_stock/const.py b/custom_components/avanza_stock/const.py index 046e1c0..33defc7 100644 --- a/custom_components/avanza_stock/const.py +++ b/custom_components/avanza_stock/const.py @@ -1,5 +1,5 @@ """Constants for avanza_stock.""" -__version__ = "1.1.0" +__version__ = "1.1.1" DEFAULT_NAME = "Avanza Stock" @@ -38,7 +38,6 @@ "priceOneMonthAgo", "priceOneWeekAgo", "priceOneYearAgo", - "priceSixMonthsAgo", "priceThreeMonthsAgo", "priceThreeYearsAgo", "pushPermitted", @@ -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 = [ diff --git a/custom_components/avanza_stock/manifest.json b/custom_components/avanza_stock/manifest.json index 330eea9..5fc95d5 100644 --- a/custom_components/avanza_stock/manifest.json +++ b/custom_components/avanza_stock/manifest.json @@ -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": [], diff --git a/custom_components/avanza_stock/sensor.py b/custom_components/avanza_stock/sensor.py index 8aaf404..3373c47 100644 --- a/custom_components/avanza_stock/sensor.py +++ b/custom_components/avanza_stock/sensor.py @@ -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"