Use EntityDescription - thinkingcleaner (#55068)
parent
354dbe91b7
commit
7314b1c8e1
|
@ -1,22 +1,39 @@
|
|||
"""Support for ThinkingCleaner sensors."""
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import timedelta
|
||||
|
||||
from pythinkingcleaner import Discovery, ThinkingCleaner
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant import util
|
||||
from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity
|
||||
from homeassistant.components.sensor import (
|
||||
PLATFORM_SCHEMA,
|
||||
SensorEntity,
|
||||
SensorEntityDescription,
|
||||
)
|
||||
from homeassistant.const import CONF_HOST, PERCENTAGE
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
|
||||
MIN_TIME_BETWEEN_SCANS = timedelta(seconds=10)
|
||||
MIN_TIME_BETWEEN_FORCED_SCANS = timedelta(milliseconds=100)
|
||||
|
||||
SENSOR_TYPES = {
|
||||
"battery": ["Battery", PERCENTAGE, "mdi:battery"],
|
||||
"state": ["State", None, None],
|
||||
"capacity": ["Capacity", None, None],
|
||||
}
|
||||
SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
|
||||
SensorEntityDescription(
|
||||
key="battery",
|
||||
name="Battery",
|
||||
native_unit_of_measurement=PERCENTAGE,
|
||||
icon="mdi:battery",
|
||||
),
|
||||
SensorEntityDescription(
|
||||
key="state",
|
||||
name="State",
|
||||
),
|
||||
SensorEntityDescription(
|
||||
key="capacity",
|
||||
name="Capacity",
|
||||
),
|
||||
)
|
||||
|
||||
STATES = {
|
||||
"st_base": "On homebase: Not Charging",
|
||||
|
@ -64,53 +81,34 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
|||
for device_object in devices:
|
||||
device_object.update()
|
||||
|
||||
dev = []
|
||||
for device in devices:
|
||||
for type_name in SENSOR_TYPES:
|
||||
dev.append(ThinkingCleanerSensor(device, type_name, update_devices))
|
||||
entities = [
|
||||
ThinkingCleanerSensor(device, update_devices, description)
|
||||
for device in devices
|
||||
for description in SENSOR_TYPES
|
||||
]
|
||||
|
||||
add_entities(dev)
|
||||
add_entities(entities)
|
||||
|
||||
|
||||
class ThinkingCleanerSensor(SensorEntity):
|
||||
"""Representation of a ThinkingCleaner Sensor."""
|
||||
|
||||
def __init__(self, tc_object, sensor_type, update_devices):
|
||||
def __init__(self, tc_object, update_devices, description: SensorEntityDescription):
|
||||
"""Initialize the ThinkingCleaner."""
|
||||
self.type = sensor_type
|
||||
|
||||
self.entity_description = description
|
||||
self._tc_object = tc_object
|
||||
self._update_devices = update_devices
|
||||
self._unit_of_measurement = SENSOR_TYPES[sensor_type][1]
|
||||
self._state = None
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
"""Return the name of the sensor."""
|
||||
return f"{self._tc_object.name} {SENSOR_TYPES[self.type][0]}"
|
||||
|
||||
@property
|
||||
def icon(self):
|
||||
"""Icon to use in the frontend, if any."""
|
||||
return SENSOR_TYPES[self.type][2]
|
||||
|
||||
@property
|
||||
def native_value(self):
|
||||
"""Return the state of the device."""
|
||||
return self._state
|
||||
|
||||
@property
|
||||
def native_unit_of_measurement(self):
|
||||
"""Return the unit of measurement of this entity, if any."""
|
||||
return self._unit_of_measurement
|
||||
self._attr_name = f"{tc_object.name} {description.name}"
|
||||
|
||||
def update(self):
|
||||
"""Update the sensor."""
|
||||
self._update_devices()
|
||||
|
||||
if self.type == "battery":
|
||||
self._state = self._tc_object.battery
|
||||
elif self.type == "state":
|
||||
self._state = STATES[self._tc_object.status]
|
||||
elif self.type == "capacity":
|
||||
self._state = self._tc_object.capacity
|
||||
sensor_type = self.entity_description.key
|
||||
if sensor_type == "battery":
|
||||
self._attr_native_value = self._tc_object.battery
|
||||
elif sensor_type == "state":
|
||||
self._attr_native_value = STATES[self._tc_object.status]
|
||||
elif sensor_type == "capacity":
|
||||
self._attr_native_value = self._tc_object.capacity
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
"""Support for ThinkingCleaner switches."""
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import timedelta
|
||||
import time
|
||||
|
||||
|
@ -9,7 +11,7 @@ from homeassistant import util
|
|||
from homeassistant.components.switch import PLATFORM_SCHEMA
|
||||
from homeassistant.const import CONF_HOST, STATE_OFF, STATE_ON
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers.entity import ToggleEntity
|
||||
from homeassistant.helpers.entity import ToggleEntity, ToggleEntityDescription
|
||||
|
||||
MIN_TIME_BETWEEN_SCANS = timedelta(seconds=10)
|
||||
MIN_TIME_BETWEEN_FORCED_SCANS = timedelta(milliseconds=100)
|
||||
|
@ -17,11 +19,20 @@ MIN_TIME_BETWEEN_FORCED_SCANS = timedelta(milliseconds=100)
|
|||
MIN_TIME_TO_WAIT = timedelta(seconds=5)
|
||||
MIN_TIME_TO_LOCK_UPDATE = 5
|
||||
|
||||
SWITCH_TYPES = {
|
||||
"clean": ["Clean", None, None],
|
||||
"dock": ["Dock", None, None],
|
||||
"find": ["Find", None, None],
|
||||
}
|
||||
SWITCH_TYPES: tuple[ToggleEntityDescription, ...] = (
|
||||
ToggleEntityDescription(
|
||||
key="clean",
|
||||
name="Clean",
|
||||
),
|
||||
ToggleEntityDescription(
|
||||
key="dock",
|
||||
name="Dock",
|
||||
),
|
||||
ToggleEntityDescription(
|
||||
key="find",
|
||||
name="Find",
|
||||
),
|
||||
)
|
||||
|
||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({vol.Optional(CONF_HOST): cv.string})
|
||||
|
||||
|
@ -41,28 +52,33 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
|||
for device_object in devices:
|
||||
device_object.update()
|
||||
|
||||
dev = []
|
||||
for device in devices:
|
||||
for type_name in SWITCH_TYPES:
|
||||
dev.append(ThinkingCleanerSwitch(device, type_name, update_devices))
|
||||
entities = [
|
||||
ThinkingCleanerSwitch(device, update_devices, description)
|
||||
for device in devices
|
||||
for description in SWITCH_TYPES
|
||||
]
|
||||
|
||||
add_entities(dev)
|
||||
add_entities(entities)
|
||||
|
||||
|
||||
class ThinkingCleanerSwitch(ToggleEntity):
|
||||
"""ThinkingCleaner Switch (dock, clean, find me)."""
|
||||
|
||||
def __init__(self, tc_object, switch_type, update_devices):
|
||||
def __init__(self, tc_object, update_devices, description: ToggleEntityDescription):
|
||||
"""Initialize the ThinkingCleaner."""
|
||||
self.type = switch_type
|
||||
self.entity_description = description
|
||||
|
||||
self._update_devices = update_devices
|
||||
self._tc_object = tc_object
|
||||
self._state = self._tc_object.is_cleaning if switch_type == "clean" else False
|
||||
self._state = (
|
||||
self._tc_object.is_cleaning if description.key == "clean" else False
|
||||
)
|
||||
self.lock = False
|
||||
self.last_lock_time = None
|
||||
self.graceful_state = False
|
||||
|
||||
self._attr_name = f"{tc_object} {description.name}"
|
||||
|
||||
def lock_update(self):
|
||||
"""Lock the update since TC clean takes some time to update."""
|
||||
if self.is_update_locked():
|
||||
|
@ -92,15 +108,10 @@ class ThinkingCleanerSwitch(ToggleEntity):
|
|||
|
||||
return True
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
"""Return the name of the sensor."""
|
||||
return f"{self._tc_object.name} {SWITCH_TYPES[self.type][0]}"
|
||||
|
||||
@property
|
||||
def is_on(self):
|
||||
"""Return true if device is on."""
|
||||
if self.type == "clean":
|
||||
if self.entity_description.key == "clean":
|
||||
return (
|
||||
self.graceful_state
|
||||
if self.is_update_locked()
|
||||
|
@ -111,22 +122,23 @@ class ThinkingCleanerSwitch(ToggleEntity):
|
|||
|
||||
def turn_on(self, **kwargs):
|
||||
"""Turn the device on."""
|
||||
if self.type == "clean":
|
||||
sensor_type = self.entity_description.key
|
||||
if sensor_type == "clean":
|
||||
self.set_graceful_lock(True)
|
||||
self._tc_object.start_cleaning()
|
||||
elif self.type == "dock":
|
||||
elif sensor_type == "dock":
|
||||
self._tc_object.dock()
|
||||
elif self.type == "find":
|
||||
elif sensor_type == "find":
|
||||
self._tc_object.find_me()
|
||||
|
||||
def turn_off(self, **kwargs):
|
||||
"""Turn the device off."""
|
||||
if self.type == "clean":
|
||||
if self.entity_description.key == "clean":
|
||||
self.set_graceful_lock(False)
|
||||
self._tc_object.stop_cleaning()
|
||||
|
||||
def update(self):
|
||||
"""Update the switch state (Only for clean)."""
|
||||
if self.type == "clean" and not self.is_update_locked():
|
||||
if self.entity_description.key == "clean" and not self.is_update_locked():
|
||||
self._tc_object.update()
|
||||
self._state = STATE_ON if self._tc_object.is_cleaning else STATE_OFF
|
||||
|
|
Loading…
Reference in New Issue