Skip to content
This repository has been archived by the owner on Jul 23, 2018. It is now read-only.

Commit

Permalink
Removed the need for a serperate sensor file
Browse files Browse the repository at this point in the history
Removed the need for a serperate sensor file
  • Loading branch information
ludeeus committed Jul 22, 2018
1 parent 6d5d9a2 commit e35a7e5
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 52 deletions.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* @ludeeus
10 changes: 5 additions & 5 deletions .github/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ repository:
allow_rebase_merge: false
labels:
- name: "Feature Request"
color: fbca04
color: "fbca04"
- name: "Bug"
color: d73a4a
color: "b60205"
- name: "Wont Fix"
color: ffffff
color: "ffffff"
- name: "Enhancement"
color: a2eeef
- name: "Documentation"
color: 008672
color: "008672"
- name: "Stale"
color: 930191
color: "930191"
13 changes: 1 addition & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,14 @@ To get the best use for this card, use together with [tracker-card](https://gith
To get started put `/custom_components/custom_cards.py`
here: `<config directory>/custom_components/custom_cards.py`

## Configuration

| key | default | required | description
| --- | --- | --- | ---
| **hide_sensor** | False | no | Download and register the sensor used by the [tracker-card](https://github.com/ciotlosm/custom-lovelace/tree/master/tracker-card), can be `True`/`False`

☢️ It is strongly adviced to not have this auto update

## Example
## Example configuration

In your `configuration.yaml`:

```yaml
custom_cards:
```

ℹ️ The sensor will get discovered automatically if installation was done correctly.

## Debug logging

In your `configuration.yaml`
Expand All @@ -40,7 +30,6 @@ logger:
default: warn
logs:
custom_components.custom_cards: debug
custom_components.sensor.custom_cards: debug
```

## Update single card
Expand Down
43 changes: 8 additions & 35 deletions custom_components/custom_cards.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,25 @@
import os
import subprocess
from datetime import timedelta
import time

import requests
import voluptuous as vol
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.event import track_time_interval
from homeassistant.helpers.discovery import load_platform
from homeassistant.helpers.dispatcher import async_dispatcher_send

__version__ = '1.1.14'
__version__ = '2.0.0'

DOMAIN = 'custom_cards'
DATA_CC = 'custom_cards_data'
CONF_HIDE_SENSOR = 'hide_sensor'
SIGNAL_SENSOR_UPDATE = 'custom_cards_update'

ATTR_CARD = 'card'

INTERVAL = timedelta(days=1)

CONFIG_SCHEMA = vol.Schema({
DOMAIN: vol.Schema({
vol.Optional(CONF_HIDE_SENSOR, default=False): cv.boolean,
})
}, extra=vol.ALLOW_EXTRA)

_LOGGER = logging.getLogger(__name__)

BROWSE_REPO = 'https//github.com/ciotlosm/custom-lovelace/master/'
VISIT_REPO = 'https://github.com/ciotlosm/custom-lovelace/blob/master/%s/changelog.md'
BASE_REPO = 'https://raw.githubusercontent.com/ciotlosm/custom-lovelace/master/'
SENSOR_URL = 'https://raw.githubusercontent.com/custom-components/sensor.custom_cards/master/custom_components/sensor/custom_cards.py'

def setup(hass, config):
"""Set up the component."""
Expand All @@ -47,7 +35,6 @@ def setup(hass, config):
__version__, __name__.split('.')[1])
conf_dir = str(hass.config.path())
controller = CustomCards(hass, conf_dir)
hide_sensor = config[DOMAIN][CONF_HIDE_SENSOR]

def update_all_service(call):
"""Set up service for manual trigger."""
Expand All @@ -64,23 +51,6 @@ def update_single_service(call):
DOMAIN, 'update_single', update_single_service)
hass.services.register(
DOMAIN, 'check_all', controller.cache_versions)
if not hide_sensor:
sensor_dir = str(hass.config.path("custom_components/sensor/"))
sensor_file = 'custom_cards.py'
sensor_full_path = sensor_dir + sensor_file
if not os.path.isfile(sensor_full_path):
_LOGGER.debug('Could not find %s in %s, trying to download.', sensor_file, sensor_dir)
response = requests.get(SENSOR_URL)
if response.status_code == 200:
_LOGGER.debug('Checking folder structure')
directory = os.path.dirname(sensor_dir)
if not os.path.exists(directory):
os.makedirs(directory)
with open(sensor_full_path, 'wb+') as sensorfile:
sensorfile.write(response.content)
else:
_LOGGER.critical('Failed to download sensor from %s', SENSOR_URL)
load_platform(hass, 'sensor', DOMAIN)
return True


Expand All @@ -93,7 +63,7 @@ def __init__(self, hass, conf_dir):
self.hass.data[DATA_CC] = {}
self.cache_versions(None) # Force a cache update on startup

def cache_versions(self, time):
def cache_versions(self, call):
"""Cache"""
self.cards = self.get_installed_cards()
self.hass.data[DATA_CC] = {} # Empty list to start from scratch
Expand All @@ -102,14 +72,17 @@ def cache_versions(self, time):
localversion = self.get_local_version(card[0])
remoteversion = self.get_remote_version(card[0])
has_update = (localversion != False and remoteversion != False and remoteversion != localversion)
not_local = (remoteversion != False and not localversion)
self.hass.data[DATA_CC][card[0]] = {
"local": localversion,
"remote": remoteversion,
"has_update": has_update,
"not_local": not_local,
}
self.hass.data[DATA_CC]['domain'] = DOMAIN
self.hass.data[DATA_CC]['repo'] = VISIT_REPO
async_dispatcher_send(self.hass, SIGNAL_SENSOR_UPDATE)
self.hass.states.set('sensor.custom_card_tracker', time.time(), self.hass.data[DATA_CC])


def update_all(self):
"""Update all cards"""
Expand All @@ -130,7 +103,7 @@ def update_single(self, card, card_dir=None):
_LOGGER.info('Upgrade of %s from version %s to version %s complete', card, self.hass.data[DATA_CC][card]['local'], self.hass.data[DATA_CC][card]['remote'])
self.hass.data[DATA_CC][card]['local'] = self.hass.data[DATA_CC][card]['remote']
self.hass.data[DATA_CC][card]['has_update'] = False
async_dispatcher_send(self.hass, SIGNAL_SENSOR_UPDATE)
self.hass.states.set('sensor.custom_card_tracker', time.time(), self.hass.data[DATA_CC])
else:
_LOGGER.debug('Skipping upgrade for %s, no update available', card)
else:
Expand Down

0 comments on commit e35a7e5

Please sign in to comment.