homeassistant-vorwerk/sensor.py

87 lines
2.2 KiB
Python
Raw Normal View History

2021-03-20 16:11:04 +01:00
"""Support for Vorwerk sensors."""
import logging
2021-04-25 20:30:12 +02:00
from pybotvac.robot import Robot
2021-03-20 16:11:04 +01:00
from homeassistant.components.sensor import DEVICE_CLASS_BATTERY
from homeassistant.const import PERCENTAGE
from homeassistant.helpers.entity import Entity
2021-04-25 20:30:12 +02:00
from homeassistant.helpers.update_coordinator import (
CoordinatorEntity,
DataUpdateCoordinator,
)
from . import VorwerkState
2021-04-25 20:30:12 +02:00
from .const import (
VORWERK_DOMAIN,
VORWERK_ROBOT_API,
VORWERK_ROBOT_COORDINATOR,
VORWERK_ROBOTS,
)
2021-03-20 16:11:04 +01:00
_LOGGER = logging.getLogger(__name__)
BATTERY = "Battery"
async def async_setup_entry(hass, entry, async_add_entities):
"""Set up the Vorwerk sensor using config entry."""
_LOGGER.debug("Adding sensors for vorwerk robots")
async_add_entities(
[
2021-04-25 20:30:12 +02:00
VorwerkSensor(robot[VORWERK_ROBOT_API], robot[VORWERK_ROBOT_COORDINATOR])
2021-03-20 16:11:04 +01:00
for robot in hass.data[VORWERK_DOMAIN][entry.entry_id][VORWERK_ROBOTS]
],
True,
)
2021-04-25 20:30:12 +02:00
class VorwerkSensor(CoordinatorEntity, Entity):
2021-03-20 16:11:04 +01:00
"""Vorwerk sensor."""
2021-04-25 20:30:12 +02:00
def __init__(
self, robot_state: VorwerkState, coordinator: DataUpdateCoordinator
) -> None:
2021-03-20 16:11:04 +01:00
"""Initialize Vorwerk sensor."""
2021-04-25 20:30:12 +02:00
super().__init__(coordinator)
self.robot: Robot = robot_state.robot
self._state: VorwerkState = robot_state
2021-03-20 16:11:04 +01:00
self._robot_name = f"{self.robot.name} {BATTERY}"
self._robot_serial = self.robot.serial
@property
def name(self):
"""Return the name of this sensor."""
return self._robot_name
@property
def unique_id(self):
"""Return unique ID."""
return self._robot_serial
@property
def device_class(self):
"""Return the device class."""
return DEVICE_CLASS_BATTERY
@property
def available(self):
"""Return availability."""
2021-04-25 20:30:12 +02:00
return self._state.available
2021-03-20 16:11:04 +01:00
@property
def state(self):
"""Return the state."""
2021-04-25 20:30:12 +02:00
return self._state.battery_level
2021-03-20 16:11:04 +01:00
@property
def unit_of_measurement(self):
"""Return unit of measurement."""
return PERCENTAGE
@property
def device_info(self):
"""Device info for robot."""
2021-04-25 20:30:12 +02:00
return self._state.device_info