Advertisement
Skeem

CellarTracker only bottle-list __init__.py

Mar 2nd, 2021 (edited)
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.11 KB | None | 0 0
  1. # replace "__init__.py" from https://github.com/ahoernecke/ha_cellar_tracker to import all bottles in Home Assistant
  2.  
  3. from cellartracker import cellartracker
  4. import pandas as pd
  5. import numpy as np
  6. import logging
  7.  
  8. from random import seed
  9. from random import randint
  10. from datetime import timedelta
  11.  
  12. from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
  13. import voluptuous as vol
  14. import homeassistant.helpers.config_validation as cv
  15. from homeassistant.util import Throttle
  16.  
  17.  
  18. """Example Load Platform integration."""
  19. DOMAIN = 'cellar_tracker'
  20.  
  21. SCAN_INTERVAL = timedelta(seconds=3600)
  22. MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=3600)
  23.  
  24. _LOGGER = logging.getLogger(__name__)
  25.  
  26. CONFIG_SCHEMA = vol.Schema(
  27.     {
  28.         DOMAIN: vol.Schema(
  29.             {
  30.                 vol.Required(CONF_USERNAME): cv.string,
  31.                 vol.Required(CONF_PASSWORD): cv.string,
  32.             }
  33.         )
  34.     },
  35.     extra=vol.ALLOW_EXTRA,
  36. )
  37.  
  38.  
  39. def setup(hass, config):
  40.     """Your controller/hub specific code."""
  41.     # Data that you want to share with your platforms
  42.    
  43.     conf = config[DOMAIN]
  44.    
  45.     username = conf[CONF_USERNAME]
  46.     password = conf[CONF_PASSWORD]
  47.    
  48.     hass.data[DOMAIN] = WineCellarData(username, password)
  49.     hass.data[DOMAIN].update()
  50.    
  51.     hass.helpers.discovery.load_platform('sensor', DOMAIN, {}, config)
  52.    
  53.  
  54.     return True
  55.  
  56. class WineCellarData:
  57.     """Get the latest data and update the states."""
  58.  
  59.     def __init__(self, username, password):
  60.         """Init the Canary data object."""
  61.  
  62.         self._username = username
  63.         self._password = password
  64.        
  65.     def get_reading(self, key):
  66.       return self._data[key]
  67.  
  68.     def get_readings(self):
  69.       return self._data
  70.  
  71.     @Throttle(MIN_TIME_BETWEEN_UPDATES)
  72.     def update(self, **kwargs):
  73.       data = {}
  74.       username = self._username
  75.       password = self._password
  76.  
  77.       client = cellartracker.CellarTracker(username, password)
  78.  
  79.       list = client.get_list()
  80.       df = pd.DataFrame(list)
  81.       columns = ["Quantity", "Type", "Country", "Region", "Vintage", "Varietal", "Wine", "BeginConsume", "EndConsume", "CT", "Size", "Price", "Location", "Bin"] #define columns
  82.       group_data = df.reindex(columns=columns) #get columns
  83.       group_data[["Quantity"]] = group_data[["Quantity"]].apply(pd.to_numeric) #make Quantity a numeric value
  84.       group_data.columns = ['count', 'type', 'country', "region", "vintage", "varietal", "wine", "beginconsume", "endconsume", "score", "size", "price", "location", "bin"] #rename columns
  85.       group_data.index = df['iWine'] #create index (unique name)
  86.      
  87.       group='Flaske' #Add additional data to dict
  88.       data[group] = {}
  89.       for row, item in group_data.iterrows():
  90.           data[group][row] = item.to_dict()
  91.           data[group][row]["sub_type"] = row
  92.  
  93.       data["total_bottles"] = group_data['count'].sum()
  94.  
  95.       self._data = data
  96.  
  97.  
  98.  
  99. #Headers can be found here (remember to login to Cellartracker prior to):
  100. #https://www.cellartracker.com/xlquery.asp?Table=list
  101. #see also https://support.cellartracker.com/article/29-exporting-data  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement