Add bin sensor

This commit is contained in:
cyr-ius
2020-03-28 18:02:41 +01:00
parent c1e3e5c4e2
commit 20f2e83478

View File

@@ -0,0 +1,76 @@
"""Roomba binary sensor entities."""
import logging
from homeassistant.components.binary_sensor import BinarySensorDevice
from .const import DOMAIN
_LOGGER = logging.getLogger(__name__)
async def async_setup_entry(hass, config_entry, async_add_entities):
"""Set up the iRobot Roomba vacuum cleaner."""
roomba = hass.data[DOMAIN]["roomba"]
status = roomba.master_state.get("state", {}).get("reported", {}).get("bin", {})
if "full" in status:
roomba_vac = RoombaBinStatus(roomba)
async_add_entities([roomba_vac], True)
class RoombaBinStatus(BinarySensorDevice):
"""Class to hold Roomba Sensor basic info."""
ICON = "mdi:delete-variant"
def __init__(self, roomba):
"""Initialize the sensor object."""
self.vacuum = roomba
self.vacuum_state = self.vacuum.master_state.get("state", {}).get(
"reported", {}
)
self._mac = self.vacuum_state.get("mac")
self._name = self.vacuum_state.get("name")
self._identifier = f"roomba_{self._mac}"
self._bin_status = None
@property
def name(self):
"""Return the name of the sensor."""
return f"{self._name} bin full"
@property
def unique_id(self):
"""Return the ID of this sensor."""
return f"bin_{self._mac}"
@property
def icon(self):
"""Return the icon of this sensor."""
return self.ICON
@property
def state(self):
"""Return the state of the sensor."""
return self._bin_status == "Full"
@property
def device_info(self):
"""Return the device info of the vacuum cleaner."""
return {
"identifiers": {(DOMAIN, self._identifier)},
"name": str(self._name),
}
async def async_update(self):
"""Return the update info of the vacuum cleaner."""
# No data, no update
if not self.vacuum.master_state:
_LOGGER.debug("Roomba %s has no data yet. Skip update", self.name)
return
self._bin_status = (
self.vacuum.master_state.get("state", {})
.get("reported", {})
.get("bin", {})
.get("full", False)
)
_LOGGER.debug("Update Full Bin status from the vacuum: %s", self._bin_status)