Skip to content

Commit

Permalink
Add number of shares as optional conf
Browse files Browse the repository at this point in the history
  • Loading branch information
claha committed May 6, 2019
1 parent 4cd122f commit 8d33499
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Using your HA configuration directory (folder) as a starting point you should no
```text
custom_components/avanza_stock/__init__.py
custom_components/avanza_stock/sensor.py
custom_components/avanza_stock/manifest.json
```

## Configuration
Expand All @@ -30,6 +31,7 @@ key | type | description
**platform (Required)** | string | `avanza_stock`
**stock (Required)** | number | The stock id, see below how to find it.
**name (Optional)** | string | Custom name for the sensor. Default `avanza_stock_{stock}`.
**shares (Optional)** | number | The number of shares you own of this stock.
**monitored_conditions (Optional)** | list | Defines the attributes of the sensor, see below.

### Monitored conditions
Expand Down Expand Up @@ -148,6 +150,7 @@ sensor:
Note: This automation could be further improved by looping over all sensors and checking if their entity_id starts with `sensor.avanza_stock_` and then extract the information.

## Changelog
* 1.0.0 - Add number of shares as optional configuration
* 0.0.10 - Clean up monitored conditions
* 0.0.9 - Compare payment date with todays date, ignore time
* 0.0.8 - Ignore dividend if amount is zero, add resources.json and manfiest.json
Expand Down
17 changes: 12 additions & 5 deletions custom_components/avanza_stock/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
from homeassistant.const import CONF_MONITORED_CONDITIONS, CONF_NAME
from homeassistant.helpers.entity import Entity

__version__ = '0.0.10'
__version__ = '1.0.0'

_LOGGER = logging.getLogger(__name__)

DEFAULT_NAME = 'Avanza Stock'

CONF_STOCK = 'stock'
CONF_SHARES = 'shares'

SCAN_INTERVAL = timedelta(minutes=60)

Expand Down Expand Up @@ -96,6 +97,7 @@
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_STOCK): cv.positive_int,
vol.Optional(CONF_NAME): cv.string,
vol.Optional(CONF_SHARES): cv.positive_int,
vol.Optional(CONF_MONITORED_CONDITIONS,
default=MONITORED_CONDITIONS_DEFAULT):
vol.All(cv.ensure_list, [vol.In(MONITORED_CONDITIONS)]),
Expand All @@ -107,22 +109,24 @@ async def async_setup_platform(hass, config, async_add_entities,
"""Set up the Avanza Stock sensor."""
stock = config.get(CONF_STOCK)
name = config.get(CONF_NAME)
if config.get(CONF_NAME) is None:
shares = config.get(CONF_SHARES)
if name is None:
name = DEFAULT_NAME + ' ' + str(stock)
monitored_conditions = config.get(CONF_MONITORED_CONDITIONS)
entities = [
AvanzaStockSensor(stock, name, monitored_conditions)
AvanzaStockSensor(stock, name, shares, monitored_conditions)
]
async_add_entities(entities, True)


class AvanzaStockSensor(Entity):
"""Representation of a Avanza Stock sensor."""

def __init__(self, stock, name, monitored_conditions):
def __init__(self, stock, name, shares, monitored_conditions):
"""Initialize a Avanza Stock sensor."""
self._stock = stock
self._name = name
self._shares = shares
self._monitored_conditions = monitored_conditions
self._icon = "mdi:cash"
self._state = 0
Expand Down Expand Up @@ -156,7 +160,7 @@ def unit_of_measurement(self):
return self._unit_of_measurement

def update(self):
"""Update state and attributes.."""
"""Update state and attributes."""
data = self._api.get_stock(self._stock)
if data:
keyRatios = data.get('keyRatios', {})
Expand All @@ -177,6 +181,9 @@ def update(self):
self._state_attributes[condition] = data.get(
condition, None)

if self._shares is not None:
self._state_attributes['shares'] = self._shares

def update_dividends(self, dividends):
"""Update dividend attributes."""
# Create empty dividend attributes, will be overwritten with valid
Expand Down

0 comments on commit 8d33499

Please sign in to comment.