Skip to content
This repository has been archived by the owner on Apr 3, 2024. It is now read-only.

Commit

Permalink
Rename VacuumBot to Device (#451)
Browse files Browse the repository at this point in the history
  • Loading branch information
edenhaus authored Oct 27, 2023
1 parent 7904f2f commit c1c133e
Show file tree
Hide file tree
Showing 13 changed files with 78 additions and 91 deletions.
2 changes: 1 addition & 1 deletion custom_components/deebot/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,5 @@ async def on_event(event: EventT) -> None:
self.async_write_ha_state()

self.async_on_remove(
self._vacuum_bot.events.subscribe(self._capability.event, on_event)
self._device.events.subscribe(self._capability.event, on_event)
)
14 changes: 7 additions & 7 deletions custom_components/deebot/button.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
from dataclasses import dataclass

from deebot_client.capabilities import CapabilityExecute
from deebot_client.device import Device
from deebot_client.events import LifeSpan
from deebot_client.vacuum_bot import VacuumBot
from homeassistant.components.button import ButtonEntity, ButtonEntityDescription
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
Expand Down Expand Up @@ -48,7 +48,7 @@ async def async_setup_entry(
)

def generate_reset_life_span(
device: VacuumBot,
device: Device,
) -> Sequence[DeebotResetLifeSpanButtonEntity]:
return [
DeebotResetLifeSpanButtonEntity(device, component)
Expand All @@ -66,7 +66,7 @@ class DeebotResetLifeSpanButtonEntity(
):
"""Deebot reset life span button entity."""

def __init__(self, vacuum_bot: VacuumBot, component: LifeSpan):
def __init__(self, device: Device, component: LifeSpan):
key = f"life_span_{component.name.lower()}_reset"
entity_description = ButtonEntityDescription(
key=key,
Expand All @@ -75,12 +75,12 @@ def __init__(self, vacuum_bot: VacuumBot, component: LifeSpan):
entity_registry_enabled_default=True, # Can be enabled as they don't poll data
entity_category=EntityCategory.CONFIG,
)
super().__init__(vacuum_bot, None, entity_description)
self._command = vacuum_bot.capabilities.life_span.reset(component)
super().__init__(device, None, entity_description)
self._command = device.capabilities.life_span.reset(component)

async def async_press(self) -> None:
"""Press the button."""
await self._vacuum_bot.execute_command(self._command)
await self._device.execute_command(self._command)


class DeebotButtonEntity(
Expand All @@ -91,4 +91,4 @@ class DeebotButtonEntity(

async def async_press(self) -> None:
"""Press the button."""
await self._vacuum_bot.execute_command(self._capability.execute())
await self._device.execute_command(self._capability.execute())
17 changes: 0 additions & 17 deletions custom_components/deebot/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,6 @@
StatsEvent,
WaterInfoEvent,
)
from deebot_client.models import VacuumState
from homeassistant.components.vacuum import (
STATE_CLEANING,
STATE_DOCKED,
STATE_ERROR,
STATE_IDLE,
STATE_PAUSED,
STATE_RETURNING,
)
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME, CONF_VERIFY_SSL

################################
Expand Down Expand Up @@ -62,14 +53,6 @@

DEEBOT_DEVICES = f"{DOMAIN}_devices"

VACUUMSTATE_TO_STATE = {
VacuumState.IDLE: STATE_IDLE,
VacuumState.CLEANING: STATE_CLEANING,
VacuumState.RETURNING: STATE_RETURNING,
VacuumState.DOCKED: STATE_DOCKED,
VacuumState.ERROR: STATE_ERROR,
VacuumState.PAUSED: STATE_PAUSED,
}

LAST_ERROR = "last_error"

Expand Down
8 changes: 4 additions & 4 deletions custom_components/deebot/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@

from deebot_client.api_client import ApiClient
from deebot_client.authentication import Authenticator
from deebot_client.device import Device
from deebot_client.exceptions import InvalidAuthenticationError
from deebot_client.models import ApiDeviceInfo, Configuration
from deebot_client.mqtt_client import MqttClient, MqttConfiguration
from deebot_client.util import md5
from deebot_client.vacuum_bot import VacuumBot
from homeassistant.const import (
CONF_DEVICES,
CONF_PASSWORD,
Expand All @@ -38,7 +38,7 @@ class DeebotController:
def __init__(self, hass: HomeAssistant, config: Mapping[str, Any]):
self._hass_config: Mapping[str, Any] = config
self._hass: HomeAssistant = hass
self._devices: list[VacuumBot] = []
self._devices: list[Device] = []
verify_ssl = config.get(CONF_VERIFY_SSL, True)
device_id = config.get(CONF_CLIENT_DEVICE_ID)

Expand Down Expand Up @@ -79,7 +79,7 @@ async def initialize(self) -> None:
if device.api_device_info["name"] in self._hass_config.get(
CONF_DEVICES, []
):
bot = VacuumBot(device, self._authenticator)
bot = Device(device, self._authenticator)
_LOGGER.debug(
"New vacbot found: %s", device.api_device_info["name"]
)
Expand Down Expand Up @@ -114,7 +114,7 @@ def register_platform_add_entities(
def register_platform_add_entities_generator(
self,
async_add_entities: AddEntitiesCallback,
func: Callable[[VacuumBot], Sequence[DeebotEntity[Any, EntityDescription]]],
func: Callable[[Device], Sequence[DeebotEntity[Any, EntityDescription]]],
) -> None:
"""Add entities generated through the provided function."""
new_entites: list[DeebotEntity[Any, EntityDescription]] = []
Expand Down
14 changes: 7 additions & 7 deletions custom_components/deebot/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
from typing import Any, Generic, TypeVar

from deebot_client.capabilities import Capabilities
from deebot_client.device import Device
from deebot_client.events import AvailabilityEvent
from deebot_client.events.base import Event
from deebot_client.vacuum_bot import VacuumBot
from homeassistant.helpers.entity import DeviceInfo, Entity, EntityDescription

from .const import DOMAIN
Expand Down Expand Up @@ -44,7 +44,7 @@ class DeebotEntity(Entity, Generic[CapabilityT, _EntityDescriptionT]): # type:

def __init__(
self,
vacuum_bot: VacuumBot,
device: Device,
capability: CapabilityT,
entity_description: _EntityDescriptionT | None = None,
**kwargs: Any,
Expand All @@ -58,10 +58,10 @@ def __init__(
'"entity_description" must be either set as class variable or passed on init!'
)

self._vacuum_bot: VacuumBot = vacuum_bot
self._device = device
self._capability = capability

device_info = self._vacuum_bot.device_info
device_info = self._device.device_info
self._attr_unique_id = device_info.did

if self.entity_description.key:
Expand All @@ -70,11 +70,11 @@ def __init__(
@property
def device_info(self) -> DeviceInfo | None:
"""Return device specific attributes."""
device = self._vacuum_bot.device_info
device = self._device.device_info
info = DeviceInfo(
identifiers={(DOMAIN, device.did)},
manufacturer="Ecovacs",
sw_version=self._vacuum_bot.fw_version,
sw_version=self._device.fw_version,
)

if nick := device.api_device_info.get("nick"):
Expand All @@ -96,5 +96,5 @@ async def on_available(event: AvailabilityEvent) -> None:
self.async_write_ha_state()

self.async_on_remove(
self._vacuum_bot.events.subscribe(AvailabilityEvent, on_available)
self._device.events.subscribe(AvailabilityEvent, on_available)
)
22 changes: 8 additions & 14 deletions custom_components/deebot/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
from typing import Any

from deebot_client.capabilities import CapabilityMap
from deebot_client.device import Device
from deebot_client.events.map import CachedMapInfoEvent, MapChangedEvent
from deebot_client.vacuum_bot import VacuumBot
from homeassistant.components.image import ImageEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
Expand All @@ -26,7 +26,7 @@ async def async_setup_entry(
controller: DeebotController = hass.data[DOMAIN][config_entry.entry_id]

def image_entity_generator(
device: VacuumBot,
device: Device,
) -> Sequence[DeebotMap]:
new_entities = []
if caps := device.capabilities.map:
Expand All @@ -47,9 +47,7 @@ class DeebotMap(

_attr_should_poll = True

def __init__(
self, hass: HomeAssistant, device: VacuumBot, capability: CapabilityMap
):
def __init__(self, hass: HomeAssistant, device: Device, capability: CapabilityMap):
super().__init__(
device,
capability,
Expand All @@ -64,13 +62,13 @@ def __init__(

def image(self) -> bytes | None:
"""Return bytes of image."""
return base64.decodebytes(self._vacuum_bot.map.get_base64_map())
return base64.decodebytes(self._device.map.get_base64_map())

async def async_added_to_hass(self) -> None:
"""Set up the event listeners now that hass is ready."""
await super().async_added_to_hass()

self._vacuum_bot.map.enable()
self._device.map.enable()

async def on_info(event: CachedMapInfoEvent) -> None:
self._attr_extra_state_attributes["map_name"] = event.name
Expand All @@ -80,17 +78,13 @@ async def on_changed(event: MapChangedEvent) -> None:
self.async_write_ha_state()

subscriptions = [
self._vacuum_bot.events.subscribe(
self._capability.chached_info.event, on_info
),
self._vacuum_bot.events.subscribe(
self._capability.changed.event, on_changed
),
self._device.events.subscribe(self._capability.chached_info.event, on_info),
self._device.events.subscribe(self._capability.changed.event, on_changed),
]

def on_remove() -> None:
for unsubscribe in subscriptions:
unsubscribe()
self._vacuum_bot.map.disable()
self._device.map.disable()

self.async_on_remove(on_remove)
2 changes: 1 addition & 1 deletion custom_components/deebot/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"deebot_client"
],
"requirements": [
"git+https://github.com/DeebotUniverse/client.py@dev#deebot-client==4.0.0dev1",
"git+https://github.com/DeebotUniverse/client.py@dev#deebot-client==4.0.0dev0",
"numpy>=1.23.2"
],
"version": "v0.0.0"
Expand Down
4 changes: 2 additions & 2 deletions custom_components/deebot/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,9 @@ async def on_event(event: EventT) -> None:
self.async_write_ha_state()

self.async_on_remove(
self._vacuum_bot.events.subscribe(self._capability.event, on_event)
self._device.events.subscribe(self._capability.event, on_event)
)

async def async_set_native_value(self, value: float) -> None:
"""Set new value."""
await self._vacuum_bot.execute_command(self._capability.set(int(value)))
await self._device.execute_command(self._capability.set(int(value)))
10 changes: 5 additions & 5 deletions custom_components/deebot/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from typing import Any, Generic

from deebot_client.capabilities import CapabilitySetTypes
from deebot_client.vacuum_bot import VacuumBot
from deebot_client.device import Device
from homeassistant.components.select import SelectEntity, SelectEntityDescription
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
Expand Down Expand Up @@ -79,12 +79,12 @@ class DeebotSelectEntity(

def __init__(
self,
vacuum_bot: VacuumBot,
device: Device,
capability: CapabilitySetTypes[EventT, str],
entity_description: DeebotSelectEntityDescription | None = None,
**kwargs: Any,
):
super().__init__(vacuum_bot, capability, entity_description, **kwargs)
super().__init__(device, capability, entity_description, **kwargs)
self._attr_options = self.entity_description.options_fn(capability)

async def async_added_to_hass(self) -> None:
Expand All @@ -96,9 +96,9 @@ async def on_water_info(event: EventT) -> None:
self.async_write_ha_state()

self.async_on_remove(
self._vacuum_bot.events.subscribe(self._capability.event, on_water_info)
self._device.events.subscribe(self._capability.event, on_water_info)
)

async def async_select_option(self, option: str) -> None:
"""Change the selected option."""
await self._vacuum_bot.execute_command(self._capability.set(option))
await self._device.execute_command(self._capability.set(option))
8 changes: 4 additions & 4 deletions custom_components/deebot/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from typing import Any, Generic, TypeVar

from deebot_client.capabilities import CapabilityEvent, CapabilityLifeSpan
from deebot_client.device import Device
from deebot_client.events import (
BatteryEvent,
CleanLogEvent,
Expand All @@ -15,7 +16,6 @@
StatsEvent,
TotalStatsEvent,
)
from deebot_client.vacuum_bot import VacuumBot
from homeassistant.components.sensor import (
SensorDeviceClass,
SensorEntity,
Expand Down Expand Up @@ -230,7 +230,7 @@ async def async_setup_entry(
)

def life_span_entity_generator(
device: VacuumBot,
device: Device,
) -> Sequence[LifeSpanSensor]:
new_entities = []
capability = device.capabilities.life_span
Expand Down Expand Up @@ -265,7 +265,7 @@ async def on_event(event: Event) -> None:
self.async_write_ha_state()

self.async_on_remove(
self._vacuum_bot.events.subscribe(self._capability.event, on_event)
self._device.events.subscribe(self._capability.event, on_event)
)


Expand All @@ -291,5 +291,5 @@ async def on_event(event: LifeSpanEvent) -> None:
self.async_write_ha_state()

self.async_on_remove(
self._vacuum_bot.events.subscribe(self._capability.event, on_event)
self._device.events.subscribe(self._capability.event, on_event)
)
6 changes: 3 additions & 3 deletions custom_components/deebot/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,13 @@ async def on_enable(event: EnableEvent) -> None:
self.async_write_ha_state()

self.async_on_remove(
self._vacuum_bot.events.subscribe(self._capability.event, on_enable)
self._device.events.subscribe(self._capability.event, on_enable)
)

async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn the entity on."""
await self._vacuum_bot.execute_command(self._capability.set(True))
await self._device.execute_command(self._capability.set(True))

async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn the entity off."""
await self._vacuum_bot.execute_command(self._capability.set(False))
await self._device.execute_command(self._capability.set(False))
Loading

0 comments on commit c1c133e

Please sign in to comment.