From 6ff4ea112657ea28902aeeadbe19975af1aa41a1 Mon Sep 17 00:00:00 2001 From: Stealth Hacker <42306475+stealthhacker@users.noreply.github.com> Date: Tue, 29 Jan 2019 00:38:01 -0800 Subject: [PATCH] Add Recollect Waste (#20121) * Added Recollect Waste Curbside Collection sensor for tracking next collection date and for which types of waste. * Added missing schema attributes. * Adding requirements and coverage entries for Recollect Waste platform. * Added exception handling, some other fixes and suggestions from code review. * Fixed reference to incorrect exception type. * Updated requirements_all.txt with new version of recollect-waste. * Added true to add_entities. Created constant for default time interval. Used different pylint exclusion comment. * Using HA's CONF_SCAN_INTERVAL now. Unique_id is now set in @property. * Changed parameter of timedelta from seconds to days. * Added test run of recollect client during setup_platform. Using built in SCAN_INTERVAL now. * Return nothing in setup_platform if there is an exception. --- .coveragerc | 1 + .../components/sensor/recollect_waste.py | 105 ++++++++++++++++++ requirements_all.txt | 3 + 3 files changed, 109 insertions(+) create mode 100644 homeassistant/components/sensor/recollect_waste.py diff --git a/.coveragerc b/.coveragerc index c60b39ca7ed..27fa0085d0d 100644 --- a/.coveragerc +++ b/.coveragerc @@ -851,6 +851,7 @@ omit = homeassistant/components/sensor/qnap.py homeassistant/components/sensor/radarr.py homeassistant/components/sensor/rainbird.py + homeassistant/components/sensor/recollect_waste.py homeassistant/components/sensor/ripple.py homeassistant/components/sensor/rtorrent.py homeassistant/components/sensor/ruter.py diff --git a/homeassistant/components/sensor/recollect_waste.py b/homeassistant/components/sensor/recollect_waste.py new file mode 100644 index 00000000000..9122973c919 --- /dev/null +++ b/homeassistant/components/sensor/recollect_waste.py @@ -0,0 +1,105 @@ +""" +Support for Recollect Waste curbside collection pickup. + +For more details about this platform, please refer to the documentation at +https://www.home-assistant.io/components/sensor.recollect_waste/ +""" +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_NAME) +from homeassistant.helpers.entity import Entity + +REQUIREMENTS = ['recollect-waste==1.0.1'] + +_LOGGER = logging.getLogger(__name__) +ATTR_PICKUP_TYPES = 'pickup_types' +ATTR_AREA_NAME = 'area_name' +CONF_PLACE_ID = 'place_id' +CONF_SERVICE_ID = 'service_id' +DEFAULT_NAME = 'recollect_waste' +ICON = 'mdi:trash-can-outline' +SCAN_INTERVAL = 86400 + + +PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ + vol.Required(CONF_PLACE_ID): cv.string, + vol.Required(CONF_SERVICE_ID): cv.string, + vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string +}) + + +def setup_platform(hass, config, add_entities, discovery_info=None): + """Set up the Recollect Waste platform.""" + import recollect_waste + + # pylint: disable=no-member + client = recollect_waste.RecollectWasteClient(config[CONF_PLACE_ID], + config[CONF_SERVICE_ID]) + + # Ensure the client can connect to the API successfully + # with given place_id and service_id. + try: + client.get_next_pickup() + # pylint: disable=no-member + except recollect_waste.RecollectWasteException as ex: + _LOGGER.error('Recollect Waste platform error. %s', ex) + return + + add_entities([RecollectWasteSensor( + config.get(CONF_NAME), + client)], True) + + +class RecollectWasteSensor(Entity): + """Recollect Waste Sensor.""" + + def __init__(self, name, client): + """Initialize the sensor.""" + self._attributes = {} + self._name = name + self._state = None + self.client = client + + @property + def name(self): + """Return the name of the sensor.""" + return self._name + + @property + def unique_id(self) -> str: + """Return a unique ID.""" + return "{}{}".format(self.client.place_id, self.client.service_id) + + @property + def state(self): + """Return the state of the sensor.""" + return self._state + + @property + def device_state_attributes(self): + """Return the state attributes.""" + return self._attributes + + @property + def icon(self): + """Icon to use in the frontend.""" + return ICON + + def update(self): + """Update device state.""" + import recollect_waste + + try: + pickup_event = self.client.get_next_pickup() + self._state = pickup_event.event_date + self._attributes.update({ + ATTR_PICKUP_TYPES: pickup_event.pickup_types, + ATTR_AREA_NAME: pickup_event.area_name + }) + # pylint: disable=no-member + except recollect_waste.RecollectWasteException as ex: + _LOGGER.error('Recollect Waste platform error. %s', ex) diff --git a/requirements_all.txt b/requirements_all.txt index dcb02d63895..6b9d6de32ab 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -1443,6 +1443,9 @@ raincloudy==0.0.5 # homeassistant.components.switch.raspyrfm raspyrfm-client==1.2.8 +# homeassistant.components.sensor.recollect_waste +recollect-waste==1.0.1 + # homeassistant.components.rainmachine regenmaschine==1.1.0