Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional configuration for refresh time #26

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
46 changes: 46 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Then go to `configuration.yaml` and add:
cellar_tracker:
username: !secret cellar_tracker_username
password: !secret cellar_tracker_password
scan_interval: 600 [Optional: Defaults to 1 hour]
```

# Dashboard visualization
Expand Down Expand Up @@ -153,5 +154,50 @@ columns:
modify: parseFloat(x).toFixed(0)
sort_by: count-
```

**Bottles by Appellation**
```
type: custom:flex-table-card
title: Bottles by Apellation
entities:
include: sensor.cellar_tracker_appellation*
columns:
- name: Country
data: sub_type
- name: Count
data: count
- name: Percentage
data: '%'
modify: parseFloat(x).toFixed(0)
suffix: '%'
- name: Average Value
data: value_avg
prefix: $
modify: parseFloat(x).toFixed(0)
sort_by: count-
```

**Bottles by Place of Purchase**
```
type: custom:flex-table-card
title: Bottles by Place of Purchase
entities:
include: sensor.cellar_tracker_storename*
columns:
- name: Country
data: sub_type
- name: Count
data: count
- name: Percentage
data: '%'
modify: parseFloat(x).toFixed(0)
suffix: '%'
- name: Average Value
data: value_avg
prefix: $
modify: parseFloat(x).toFixed(0)
sort_by: count-
```

# Contribute
Feel free to contribute by opening a PR, issue on this project
73 changes: 32 additions & 41 deletions custom_components/cellar_tracker/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,14 @@
from random import randint
from datetime import timedelta

from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME, CONF_SCAN_INTERVAL
import voluptuous as vol
import homeassistant.helpers.config_validation as cv
from homeassistant.util import Throttle




"""Example Load Platform integration."""
DOMAIN = 'cellar_tracker'

SCAN_INTERVAL = timedelta(seconds=3600)

MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=3600)

_LOGGER = logging.getLogger(__name__)

CONFIG_SCHEMA = vol.Schema(
Expand All @@ -30,67 +23,70 @@
{
vol.Required(CONF_USERNAME): cv.string,
vol.Required(CONF_PASSWORD): cv.string,
vol.Optional(CONF_SCAN_INTERVAL, default=3600): int
}
)
},
extra=vol.ALLOW_EXTRA,
)


def setup(hass, config):
"""Your controller/hub specific code."""
# Data that you want to share with your platforms

conf = config[DOMAIN]

username = conf[CONF_USERNAME]
password = conf[CONF_PASSWORD]

"""Your controller/hub specific code."""
# Data that you want to share with your platforms

conf = config[DOMAIN]


hass.data[DOMAIN] = WineCellarData(username, password)
hass.data[DOMAIN].update()
username = conf[CONF_USERNAME]
password = conf[CONF_PASSWORD]
# Enforce a low limit of 30
scan_interval_seconds = conf[CONF_SCAN_INTERVAL]
if scan_interval_seconds < 30:
_LOGGER.debug("Overriding scan interval to 30 due to low value of {scan_interval_seconds}")
scan_interval_seconds = 30
else:
_LOGGER.debug(f"Using configured scan_interval of {scan_interval_seconds}")
scan_interval = timedelta(seconds=scan_interval_seconds)

hass.data[DOMAIN] = WineCellarData(username, password, scan_interval)
hass.data[DOMAIN].update()


hass.helpers.discovery.load_platform('sensor', DOMAIN, {}, config)

hass.helpers.discovery.load_platform('sensor', DOMAIN, {}, config)

return True
return True

class WineCellarData:
"""Get the latest data and update the states."""

def __init__(self, username, password):
def __init__(self, username, password, scan_interval):
"""Init the Canary data object."""

# self._api = Api(username, password, timeout)

# self._locations_by_id = {}
# self._readings_by_device_id = {}
self._username = username
self._password = password

_LOGGER.debug(f"Initiating with scan interval of {scan_interval}")
self.update = Throttle(scan_interval)(self._update)
self._scan_interval = scan_interval

def get_reading(self, key):
return self._data[key]

def get_readings(self):
return self._data

@Throttle(MIN_TIME_BETWEEN_UPDATES)
def update(self, **kwargs):
def get_scan_interval(self):
return self._scan_interval

def _update(self, **kwargs):
_LOGGER.debug("Updating cellar tracker data")
data = {}
username = self._username
password = self._password


client = cellartracker.CellarTracker(username, password)
list = client.get_inventory()
df = pd.DataFrame(list)
inventory = client.get_inventory()
df = pd.DataFrame(inventory)
df[["Price","Valuation"]] = df[["Price","Valuation"]].apply(pd.to_numeric)

groups = ['Varietal', 'Country', 'Vintage', 'Producer', 'Type', 'Location']
groups = ['Varietal', 'Country', 'Vintage', 'Producer', 'Type', 'Location', 'Appellation', 'StoreName']

for group in groups:
group_data = df.groupby(group).agg({'iWine':'count','Valuation':['sum','mean']})
Expand All @@ -105,12 +101,7 @@ def update(self, **kwargs):
data[group][row] = item.to_dict()
data[group][row]["sub_type"] = row



data["total_bottles"] = len(df)
data["total_value"] = df['Valuation'].sum()
data["average_value"] = df['Valuation'].mean()
self._data = data



29 changes: 12 additions & 17 deletions custom_components/cellar_tracker/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
from homeassistant.helpers.entity import Entity
from homeassistant.util import Throttle
from datetime import timedelta
import logging
import time
import pandas as pd
import re
from . import DOMAIN

MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=30)
_LOGGER = logging.getLogger(__name__)

def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the sensor platform."""
Expand All @@ -16,10 +17,10 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
return

devs = []

master_data = hass.data[DOMAIN].get_readings()
scan_interval = hass.data[DOMAIN].get_scan_interval()



for sensor_type in master_data.keys():

Expand All @@ -30,20 +31,18 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
value = data[key]
sensor_data = data.copy()
sensor_data[sensor_type] = key


devs.append(WineCellarSensor(sensor_type, key, sensor_data))
devs.append(WineCellarSensor(sensor_type, key, sensor_data, scan_interval))
else:
devs.append(WineCellarSensor(sensor_type, None, data))

devs.append(WineCellarSensor(sensor_type, None, data, scan_interval))

add_entities(devs, True)


class WineCellarSensor(Entity):
"""Representation of a sensor."""

def __init__(self, sensor_type, sub_type, data):
def __init__(self, sensor_type, sub_type, data, scan_interval):
"""Initialize the sensor."""

self._sensor_type = sensor_type
Expand All @@ -56,6 +55,7 @@ def __init__(self, sensor_type, sub_type, data):
else:
self._slug = None
# self.update()
self.update = Throttle(scan_interval)(self._update)

@property
def name(self):
Expand All @@ -79,7 +79,6 @@ def icon(self):
return "mdi:currency-usd"

return "mdi:bottle-wine"


@property
def state(self):
Expand All @@ -95,7 +94,6 @@ def state(self):
@property
def unique_id(self):
return "cellar_tracker." + self.name


@property
def unit_of_measurement(self):
Expand All @@ -105,19 +103,16 @@ def unit_of_measurement(self):

return "bottles"

@Throttle(MIN_TIME_BETWEEN_UPDATES)
def update(self):
def _update(self):
"""Fetch new state data for the sensor.
This is the only method that should fetch new data for Home Assistant.
"""

_LOGGER.debug(f"Updating data for {self.name}")
self.hass.data[DOMAIN].update()
self._data = self.hass.data[DOMAIN].get_reading(self._sensor_type)

if(self._sub_type):
self._data = self._data[self._sub_type]
self._state = self._data["count"]
else:
self._state = self._data