Skip to content

Commit

Permalink
Round off to more decimals
Browse files Browse the repository at this point in the history
  • Loading branch information
claha committed Jun 7, 2024
1 parent 4838a02 commit 416910a
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions custom_components/avanza_stock/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ def _update_state_attributes(self, data):
self._state_attributes[change] = round(
data["quote"]["last"]
- data["historicalClosingPrices"][price],
2,
5,
)
else:
self._state_attributes[change] = "unknown"
Expand All @@ -308,7 +308,7 @@ def _update_state_attributes(self, data):
data["quote"]["last"]
- data["historicalClosingPrices"][price]
),
2,
5,
)
else:
self._state_attributes[change] = "unknown"
Expand All @@ -323,18 +323,18 @@ def _update_state_attributes(self, data):
- data["historicalClosingPrices"][price]
)
/ data["historicalClosingPrices"][price],
2,
3,
)
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["quote"]["last"], 2
self._shares * data["quote"]["last"], 5
)
self._state_attributes["totalChange"] = round(
self._shares * data["quote"]["change"], 2
self._shares * data["quote"]["change"], 5
)

self._update_profit_loss(data["quote"]["last"])
Expand Down Expand Up @@ -370,22 +370,22 @@ def _update_profit_loss(self, price):
if self._purchase_price is not None:
self._state_attributes["purchasePrice"] = self._purchase_price
self._state_attributes["profitLoss"] = round(
price - self._purchase_price, 2
price - self._purchase_price, 5
)
self._state_attributes["profitLossPercentage"] = round(
100 * (price - self._purchase_price) / self._purchase_price, 2
100 * (price - self._purchase_price) / self._purchase_price, 3
)

if self._shares is not None:
self._state_attributes["totalProfitLoss"] = round(
self._shares * (price - self._purchase_price), 2
self._shares * (price - self._purchase_price), 5
)

def _update_conversion_rate(self, data):
rate = data["quote"]["last"]
if self._invert_conversion_currency:
rate = 1.0 / rate
self._state = round(self._state * rate, 2)
self._state = round(self._state * rate, 5)
if self._invert_conversion_currency:
self._unit_of_measurement = data["name"].split("/")[0]
else:
Expand All @@ -397,7 +397,7 @@ def _update_conversion_rate(self, data):
and self._state_attributes[attribute] != "unknown"
):
self._state_attributes[attribute] = round(
self._state_attributes[attribute] * rate, 2
self._state_attributes[attribute] * rate, 5
)
self._update_profit_loss(self._state)

Expand Down

0 comments on commit 416910a

Please sign in to comment.