mirror of
https://github.com/home-assistant/core.git
synced 2025-07-25 14:17:45 +00:00
Use EntityDescription - thinkingcleaner (#55068)
This commit is contained in:
parent
354dbe91b7
commit
7314b1c8e1
@ -1,22 +1,39 @@
|
|||||||
"""Support for ThinkingCleaner sensors."""
|
"""Support for ThinkingCleaner sensors."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
|
|
||||||
from pythinkingcleaner import Discovery, ThinkingCleaner
|
from pythinkingcleaner import Discovery, ThinkingCleaner
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant import util
|
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
|
from homeassistant.const import CONF_HOST, PERCENTAGE
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
|
||||||
MIN_TIME_BETWEEN_SCANS = timedelta(seconds=10)
|
MIN_TIME_BETWEEN_SCANS = timedelta(seconds=10)
|
||||||
MIN_TIME_BETWEEN_FORCED_SCANS = timedelta(milliseconds=100)
|
MIN_TIME_BETWEEN_FORCED_SCANS = timedelta(milliseconds=100)
|
||||||
|
|
||||||
SENSOR_TYPES = {
|
SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
|
||||||
"battery": ["Battery", PERCENTAGE, "mdi:battery"],
|
SensorEntityDescription(
|
||||||
"state": ["State", None, None],
|
key="battery",
|
||||||
"capacity": ["Capacity", None, None],
|
name="Battery",
|
||||||
}
|
native_unit_of_measurement=PERCENTAGE,
|
||||||
|
icon="mdi:battery",
|
||||||
|
),
|
||||||
|
SensorEntityDescription(
|
||||||
|
key="state",
|
||||||
|
name="State",
|
||||||
|
),
|
||||||
|
SensorEntityDescription(
|
||||||
|
key="capacity",
|
||||||
|
name="Capacity",
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
STATES = {
|
STATES = {
|
||||||
"st_base": "On homebase: Not Charging",
|
"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:
|
for device_object in devices:
|
||||||
device_object.update()
|
device_object.update()
|
||||||
|
|
||||||
dev = []
|
entities = [
|
||||||
for device in devices:
|
ThinkingCleanerSensor(device, update_devices, description)
|
||||||
for type_name in SENSOR_TYPES:
|
for device in devices
|
||||||
dev.append(ThinkingCleanerSensor(device, type_name, update_devices))
|
for description in SENSOR_TYPES
|
||||||
|
]
|
||||||
|
|
||||||
add_entities(dev)
|
add_entities(entities)
|
||||||
|
|
||||||
|
|
||||||
class ThinkingCleanerSensor(SensorEntity):
|
class ThinkingCleanerSensor(SensorEntity):
|
||||||
"""Representation of a ThinkingCleaner Sensor."""
|
"""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."""
|
"""Initialize the ThinkingCleaner."""
|
||||||
self.type = sensor_type
|
self.entity_description = description
|
||||||
|
|
||||||
self._tc_object = tc_object
|
self._tc_object = tc_object
|
||||||
self._update_devices = update_devices
|
self._update_devices = update_devices
|
||||||
self._unit_of_measurement = SENSOR_TYPES[sensor_type][1]
|
|
||||||
self._state = None
|
|
||||||
|
|
||||||
@property
|
self._attr_name = f"{tc_object.name} {description.name}"
|
||||||
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
|
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
"""Update the sensor."""
|
"""Update the sensor."""
|
||||||
self._update_devices()
|
self._update_devices()
|
||||||
|
|
||||||
if self.type == "battery":
|
sensor_type = self.entity_description.key
|
||||||
self._state = self._tc_object.battery
|
if sensor_type == "battery":
|
||||||
elif self.type == "state":
|
self._attr_native_value = self._tc_object.battery
|
||||||
self._state = STATES[self._tc_object.status]
|
elif sensor_type == "state":
|
||||||
elif self.type == "capacity":
|
self._attr_native_value = STATES[self._tc_object.status]
|
||||||
self._state = self._tc_object.capacity
|
elif sensor_type == "capacity":
|
||||||
|
self._attr_native_value = self._tc_object.capacity
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
"""Support for ThinkingCleaner switches."""
|
"""Support for ThinkingCleaner switches."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
import time
|
import time
|
||||||
|
|
||||||
@ -9,7 +11,7 @@ from homeassistant import util
|
|||||||
from homeassistant.components.switch import PLATFORM_SCHEMA
|
from homeassistant.components.switch import PLATFORM_SCHEMA
|
||||||
from homeassistant.const import CONF_HOST, STATE_OFF, STATE_ON
|
from homeassistant.const import CONF_HOST, STATE_OFF, STATE_ON
|
||||||
import homeassistant.helpers.config_validation as cv
|
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_SCANS = timedelta(seconds=10)
|
||||||
MIN_TIME_BETWEEN_FORCED_SCANS = timedelta(milliseconds=100)
|
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_WAIT = timedelta(seconds=5)
|
||||||
MIN_TIME_TO_LOCK_UPDATE = 5
|
MIN_TIME_TO_LOCK_UPDATE = 5
|
||||||
|
|
||||||
SWITCH_TYPES = {
|
SWITCH_TYPES: tuple[ToggleEntityDescription, ...] = (
|
||||||
"clean": ["Clean", None, None],
|
ToggleEntityDescription(
|
||||||
"dock": ["Dock", None, None],
|
key="clean",
|
||||||
"find": ["Find", None, None],
|
name="Clean",
|
||||||
}
|
),
|
||||||
|
ToggleEntityDescription(
|
||||||
|
key="dock",
|
||||||
|
name="Dock",
|
||||||
|
),
|
||||||
|
ToggleEntityDescription(
|
||||||
|
key="find",
|
||||||
|
name="Find",
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({vol.Optional(CONF_HOST): cv.string})
|
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:
|
for device_object in devices:
|
||||||
device_object.update()
|
device_object.update()
|
||||||
|
|
||||||
dev = []
|
entities = [
|
||||||
for device in devices:
|
ThinkingCleanerSwitch(device, update_devices, description)
|
||||||
for type_name in SWITCH_TYPES:
|
for device in devices
|
||||||
dev.append(ThinkingCleanerSwitch(device, type_name, update_devices))
|
for description in SWITCH_TYPES
|
||||||
|
]
|
||||||
|
|
||||||
add_entities(dev)
|
add_entities(entities)
|
||||||
|
|
||||||
|
|
||||||
class ThinkingCleanerSwitch(ToggleEntity):
|
class ThinkingCleanerSwitch(ToggleEntity):
|
||||||
"""ThinkingCleaner Switch (dock, clean, find me)."""
|
"""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."""
|
"""Initialize the ThinkingCleaner."""
|
||||||
self.type = switch_type
|
self.entity_description = description
|
||||||
|
|
||||||
self._update_devices = update_devices
|
self._update_devices = update_devices
|
||||||
self._tc_object = tc_object
|
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.lock = False
|
||||||
self.last_lock_time = None
|
self.last_lock_time = None
|
||||||
self.graceful_state = False
|
self.graceful_state = False
|
||||||
|
|
||||||
|
self._attr_name = f"{tc_object} {description.name}"
|
||||||
|
|
||||||
def lock_update(self):
|
def lock_update(self):
|
||||||
"""Lock the update since TC clean takes some time to update."""
|
"""Lock the update since TC clean takes some time to update."""
|
||||||
if self.is_update_locked():
|
if self.is_update_locked():
|
||||||
@ -92,15 +108,10 @@ class ThinkingCleanerSwitch(ToggleEntity):
|
|||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
@property
|
|
||||||
def name(self):
|
|
||||||
"""Return the name of the sensor."""
|
|
||||||
return f"{self._tc_object.name} {SWITCH_TYPES[self.type][0]}"
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_on(self):
|
def is_on(self):
|
||||||
"""Return true if device is on."""
|
"""Return true if device is on."""
|
||||||
if self.type == "clean":
|
if self.entity_description.key == "clean":
|
||||||
return (
|
return (
|
||||||
self.graceful_state
|
self.graceful_state
|
||||||
if self.is_update_locked()
|
if self.is_update_locked()
|
||||||
@ -111,22 +122,23 @@ class ThinkingCleanerSwitch(ToggleEntity):
|
|||||||
|
|
||||||
def turn_on(self, **kwargs):
|
def turn_on(self, **kwargs):
|
||||||
"""Turn the device on."""
|
"""Turn the device on."""
|
||||||
if self.type == "clean":
|
sensor_type = self.entity_description.key
|
||||||
|
if sensor_type == "clean":
|
||||||
self.set_graceful_lock(True)
|
self.set_graceful_lock(True)
|
||||||
self._tc_object.start_cleaning()
|
self._tc_object.start_cleaning()
|
||||||
elif self.type == "dock":
|
elif sensor_type == "dock":
|
||||||
self._tc_object.dock()
|
self._tc_object.dock()
|
||||||
elif self.type == "find":
|
elif sensor_type == "find":
|
||||||
self._tc_object.find_me()
|
self._tc_object.find_me()
|
||||||
|
|
||||||
def turn_off(self, **kwargs):
|
def turn_off(self, **kwargs):
|
||||||
"""Turn the device off."""
|
"""Turn the device off."""
|
||||||
if self.type == "clean":
|
if self.entity_description.key == "clean":
|
||||||
self.set_graceful_lock(False)
|
self.set_graceful_lock(False)
|
||||||
self._tc_object.stop_cleaning()
|
self._tc_object.stop_cleaning()
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
"""Update the switch state (Only for clean)."""
|
"""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._tc_object.update()
|
||||||
self._state = STATE_ON if self._tc_object.is_cleaning else STATE_OFF
|
self._state = STATE_ON if self._tc_object.is_cleaning else STATE_OFF
|
||||||
|
Loading…
x
Reference in New Issue
Block a user