custom-components / sensor.avanza_stock

Custom component to get stock data from Avanza for Home Assistant
MIT License
46 stars 10 forks source link

Specify currency #73

Closed claha closed 4 years ago

claha commented 4 years ago

It would be useful to be able to specify the currency, i.e. to automatically convert from usd to sek. I see two possible solutions.

  1. For each stock define either a constant conversion factor or get it from som other sensor (it is possible to track currencies using this component), also add possibility to specify the new currency.
  2. Internally track all possiblie currency conversions and just let the user specify what currency they would like. This will create a lot of extra api calls since for example the usd->sek conversion can't be shared between sensors.

Option 1 seems like the best one, since it could use a constant (template sensor, input slider, ...) or another avanza_stock sensor, the only requirement would be that the sensor state is a number

claha commented 4 years ago

I think the most approperiate name for the new optional configuration variable should be 'conversion_rate'. Note that there should also be an optional 'currency' configuration, but it should be mandatory if conversion_rate is defined, and the other way around

claha commented 4 years ago

vol.Inclusive seems like the thing to use for data validation

claha commented 4 years ago

Example of how to get another sensor into a integration https://github.com/home-assistant/core/blob/dc52b858a40905c129684a8e964ce182c4ff00df/homeassistant/components/spaceapi/__init__.py (hass.states.get)

claha commented 4 years ago

The current solution does not convert amount per share for dividends

imist commented 4 years ago

For currency conversion, what about adding an optional "conversion_currency" attribute for each stock? For USDSEK the value would be 19000 taken from this url: https://www.avanza.se/index/om-indexet.html/19000/usd-sek. This in turn can, together with numberOfShares, be used to calculate total value in SEK. If you want, I can help test this as I own AAPL stocks ;-)

claha commented 4 years ago

That would probably be the cleanest way of doing this. I hope you bought your Apple stocks a long time ago :)

claha commented 4 years ago

@imist I've updated to component to support a conversion_currency attribute. It seems to work fine for me but would be happy if you could also test it out before I release it. If you use hacs you can select to install master or just copy the files manually.