mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
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.
This commit is contained in:
parent
a7c74151bc
commit
6ff4ea1126
@ -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
|
||||
|
105
homeassistant/components/sensor/recollect_waste.py
Normal file
105
homeassistant/components/sensor/recollect_waste.py
Normal file
@ -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)
|
@ -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
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user