Created
August 3, 2017 02:55
-
-
Save bachya/e6ebadab7b1d8742ac33888bfab5abb1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
File: denvertrash.py | |
Author: Aaron Bach | |
Email: [email protected] | |
""" | |
import asyncio | |
import datetime | |
import logging | |
import voluptuous as vol | |
import homeassistant.helpers.config_validation as cv | |
from homeassistant.components.sensor import PLATFORM_SCHEMA | |
from homeassistant.const import CONF_MONITORED_CONDITIONS | |
from homeassistant.helpers.entity import Entity | |
from homeassistant.util import Throttle | |
_LOGGER = logging.getLogger(__name__) | |
REQUIREMENTS = ['pyden==0.3.4'] | |
PICKUP_TYPES = { | |
'compost': ('Compost Pickup', 'mdi:food-apple'), | |
'extra_trash': ('Extra Trash Pickup', 'mdi:truck'), | |
'recycling': ('Recycling Pickup', 'mdi:recycle'), | |
'trash': ('Trash Pickup', 'mdi:delete') | |
} | |
MIN_TIME_BETWEEN_UPDATES = datetime.timedelta(minutes=10) | |
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ | |
vol.Optional(CONF_MONITORED_CONDITIONS, default=[]): | |
vol.All(cv.ensure_list, [vol.In(PICKUP_TYPES)]), | |
}) | |
class DenverTrashSensor(Entity): | |
""" A class representation of our sensor """ | |
def __init__(self, client, name, pickup_type, icon): | |
""" Initialize! """ | |
self._client = client | |
self._icon = icon | |
self._name = name | |
self._pickup_type = pickup_type | |
self._state = None | |
@property | |
def icon(self): | |
""" Returns the icon of the pickup type """ | |
return self._icon | |
@property | |
def name(self): | |
""" Returns the name of the pickup type """ | |
return self._name | |
@property | |
def state(self): | |
""" Returns the next pickup date of the pickup type """ | |
return self._state | |
@Throttle(MIN_TIME_BETWEEN_UPDATES) | |
def update(self): | |
""" Updates the status """ | |
_LOGGER.debug('Updating sensor: %s', self._name) | |
self._state = 42 | |
# present = maya.now() | |
# next_pickup = maya.when( | |
# self._client.next_pickup(self._pickup_type), | |
# timezone='America/Denver') | |
# if (next_pickup.datetime() - present.datetime()).days < 7: | |
# self._state = next_pickup.slang_time() | |
# else: | |
# self._state = next_pickup.datetime().strftime('%x') | |
@asyncio.coroutine | |
def async_setup_platform(hass, config, async_add_devices, discovery_info=None): | |
""" Setup! """ | |
import pyden | |
pickups_to_watch = config.get(CONF_MONITORED_CONDITIONS) | |
_LOGGER.debug('Pickup types being monitored: %s', pickups_to_watch) | |
client = None | |
latitude = hass.config.latitude | |
longitude = hass.config.longitude | |
try: | |
client = pyden.TrashClient.from_coordinates( | |
latitude, longitude) | |
except pyden.exceptions.GeocodingError as exc_info: | |
_LOGGER.error('An error occurred while geocoding: %s', str(exc_info)) | |
return False | |
except pyden.exceptions.HTTPError as exc_info: | |
_LOGGER.error('An HTTP error occurred: %s', str(exc_info)) | |
return False | |
except Exception as exc_info: # pylint: disable=broad-except | |
_LOGGER.error('An unknown error occurred...') | |
_LOGGER.debug(str(exc_info)) | |
return False | |
sensors = [] | |
for pickup_type in pickups_to_watch: | |
name, icon = PICKUP_TYPES[pickup_type] | |
sensors.append(DenverTrashSensor(client, name, pickup_type, icon)) | |
async_add_devices(sensors, True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment