2019-04-03 15:40:03 +00:00
|
|
|
"""Support for Rova garbage calendar."""
|
2019-01-30 20:58:41 +00:00
|
|
|
|
|
|
|
from datetime import datetime, timedelta
|
|
|
|
import logging
|
|
|
|
|
2019-12-01 05:21:08 +00:00
|
|
|
from requests.exceptions import ConnectTimeout, HTTPError
|
|
|
|
from rova.rova import Rova
|
2019-01-30 20:58:41 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
2019-07-31 19:25:30 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
CONF_MONITORED_CONDITIONS,
|
|
|
|
CONF_NAME,
|
|
|
|
DEVICE_CLASS_TIMESTAMP,
|
|
|
|
)
|
2019-01-30 20:58:41 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
from homeassistant.helpers.entity import Entity
|
|
|
|
from homeassistant.util import Throttle
|
|
|
|
|
|
|
|
# Config for rova requests.
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_ZIP_CODE = "zip_code"
|
|
|
|
CONF_HOUSE_NUMBER = "house_number"
|
|
|
|
CONF_HOUSE_NUMBER_SUFFIX = "house_number_suffix"
|
2019-01-30 20:58:41 +00:00
|
|
|
|
|
|
|
UPDATE_DELAY = timedelta(hours=12)
|
|
|
|
SCAN_INTERVAL = timedelta(hours=12)
|
|
|
|
|
|
|
|
# Supported sensor types:
|
|
|
|
# Key: [json_key, name, icon]
|
|
|
|
SENSOR_TYPES = {
|
2019-07-31 19:25:30 +00:00
|
|
|
"bio": ["gft", "Biowaste", "mdi:recycle"],
|
|
|
|
"paper": ["papier", "Paper", "mdi:recycle"],
|
2020-01-06 13:34:15 +00:00
|
|
|
"plastic": ["pmd", "PET", "mdi:recycle"],
|
|
|
|
"residual": ["restafval", "Residual", "mdi:recycle"],
|
2019-07-31 19:25:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_ZIP_CODE): cv.string,
|
|
|
|
vol.Required(CONF_HOUSE_NUMBER): cv.string,
|
|
|
|
vol.Optional(CONF_HOUSE_NUMBER_SUFFIX, default=""): cv.string,
|
|
|
|
vol.Optional(CONF_NAME, default="Rova"): cv.string,
|
|
|
|
vol.Optional(CONF_MONITORED_CONDITIONS, default=["bio"]): vol.All(
|
|
|
|
cv.ensure_list, [vol.In(SENSOR_TYPES)]
|
|
|
|
),
|
|
|
|
}
|
|
|
|
)
|
2019-01-30 20:58:41 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
|
|
|
"""Create the Rova data service and sensors."""
|
|
|
|
|
|
|
|
zip_code = config[CONF_ZIP_CODE]
|
|
|
|
house_number = config[CONF_HOUSE_NUMBER]
|
2019-02-21 14:51:36 +00:00
|
|
|
house_number_suffix = config[CONF_HOUSE_NUMBER_SUFFIX]
|
2019-01-30 20:58:41 +00:00
|
|
|
platform_name = config[CONF_NAME]
|
|
|
|
|
|
|
|
# Create new Rova object to retrieve data
|
2019-02-21 14:51:36 +00:00
|
|
|
api = Rova(zip_code, house_number, house_number_suffix)
|
2019-01-30 20:58:41 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
if not api.is_rova_area():
|
|
|
|
_LOGGER.error("ROVA does not collect garbage in this area")
|
|
|
|
return
|
|
|
|
except (ConnectTimeout, HTTPError):
|
|
|
|
_LOGGER.error("Could not retrieve details from ROVA API")
|
|
|
|
return
|
|
|
|
|
|
|
|
# Create rova data service which will retrieve and update the data.
|
|
|
|
data_service = RovaData(api)
|
|
|
|
|
|
|
|
# Create a new sensor for each garbage type.
|
|
|
|
entities = []
|
|
|
|
for sensor_key in config[CONF_MONITORED_CONDITIONS]:
|
|
|
|
sensor = RovaSensor(platform_name, sensor_key, data_service)
|
|
|
|
entities.append(sensor)
|
|
|
|
|
|
|
|
add_entities(entities, True)
|
|
|
|
|
|
|
|
|
|
|
|
class RovaSensor(Entity):
|
|
|
|
"""Representation of a Rova sensor."""
|
|
|
|
|
|
|
|
def __init__(self, platform_name, sensor_key, data_service):
|
|
|
|
"""Initialize the sensor."""
|
|
|
|
self.sensor_key = sensor_key
|
|
|
|
self.platform_name = platform_name
|
|
|
|
self.data_service = data_service
|
|
|
|
|
|
|
|
self._state = None
|
|
|
|
|
|
|
|
self._json_key = SENSOR_TYPES[self.sensor_key][0]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name."""
|
2019-09-03 19:14:39 +00:00
|
|
|
return f"{self.platform_name}_{self.sensor_key}"
|
2019-01-30 20:58:41 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def icon(self):
|
|
|
|
"""Return the sensor icon."""
|
|
|
|
return SENSOR_TYPES[self.sensor_key][2]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_class(self):
|
|
|
|
"""Return the class of this sensor."""
|
|
|
|
return DEVICE_CLASS_TIMESTAMP
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Return the state of the sensor."""
|
|
|
|
return self._state
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Get the latest data from the sensor and update the state."""
|
|
|
|
self.data_service.update()
|
|
|
|
pickup_date = self.data_service.data.get(self._json_key)
|
|
|
|
if pickup_date is not None:
|
|
|
|
self._state = pickup_date.isoformat()
|
|
|
|
|
|
|
|
|
|
|
|
class RovaData:
|
|
|
|
"""Get and update the latest data from the Rova API."""
|
|
|
|
|
|
|
|
def __init__(self, api):
|
|
|
|
"""Initialize the data object."""
|
|
|
|
self.api = api
|
|
|
|
self.data = {}
|
|
|
|
|
|
|
|
@Throttle(UPDATE_DELAY)
|
|
|
|
def update(self):
|
|
|
|
"""Update the data from the Rova API."""
|
|
|
|
|
|
|
|
try:
|
|
|
|
items = self.api.get_calendar_items()
|
|
|
|
except (ConnectTimeout, HTTPError):
|
|
|
|
_LOGGER.error("Could not retrieve data, retry again later")
|
|
|
|
return
|
|
|
|
|
|
|
|
self.data = {}
|
|
|
|
|
|
|
|
for item in items:
|
2019-07-31 19:25:30 +00:00
|
|
|
date = datetime.strptime(item["Date"], "%Y-%m-%dT%H:%M:%S")
|
|
|
|
code = item["GarbageTypeCode"].lower()
|
2019-01-30 20:58:41 +00:00
|
|
|
|
|
|
|
if code not in self.data and date > datetime.now():
|
|
|
|
self.data[code] = date
|
|
|
|
|
|
|
|
_LOGGER.debug("Updated Rova calendar: %s", self.data)
|