Skip to content

Commit

Permalink
v2.0.0b13
Browse files Browse the repository at this point in the history
  • Loading branch information
Yiğit Topcu committed Apr 1, 2024
1 parent 4580d06 commit 3771ca3
Show file tree
Hide file tree
Showing 22 changed files with 380 additions and 181 deletions.
14 changes: 8 additions & 6 deletions custom_components/dreame_vacuum/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,17 @@
Platform.TIME,
)


async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up Dreame Vacuum from a config entry."""
#coordinator = hass.data[DOMAIN][entry.entry_id] if DOMAIN in hass.data else None
#if coordinator and coordinator.device:
# _unload(coordinator)

coordinator = DreameVacuumDataUpdateCoordinator(hass, entry=entry)
await coordinator.async_config_entry_first_refresh()

hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator

entry.async_on_unload(entry.add_update_listener(update_listener))

# Register frontend
#frontend_js = f"/{DOMAIN}/frontend.js"
#if DATA_EXTRA_MODULE_URL not in hass.data:
Expand All @@ -44,6 +45,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:

# Set up all platforms for this device/entry.
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)

entry.async_on_unload(entry.add_update_listener(update_listener))
return True


Expand All @@ -53,9 +56,8 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
coordinator: DreameVacuumDataUpdateCoordinator = hass.data[DOMAIN][
entry.entry_id
]
coordinator.device.listen(None)
coordinator.device.disconnect()
coordinator.async_set_updated_data()
coordinator._device.listen(None)
coordinator._device.disconnect()
del coordinator._device
coordinator._device = None
del hass.data[DOMAIN][entry.entry_id]
Expand Down
6 changes: 3 additions & 3 deletions custom_components/dreame_vacuum/button.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ def async_update_buttons(
DreameVacuumShortcutButtonEntity(
coordinator,
DreameVacuumButtonEntityDescription(
key="shortcut",
icon="mdi:play-speed",
available_fn=lambda device: not device.status.started
and not device.status.shortcut_task
Expand All @@ -266,8 +267,9 @@ def async_update_buttons(
DreameVacuumMapButtonEntity(
coordinator,
DreameVacuumButtonEntityDescription(
entity_category=EntityCategory.DIAGNOSTIC,
key="backup",
icon="mdi:content-save",
entity_category=EntityCategory.DIAGNOSTIC,
available_fn=lambda device: not device.status.started and not device.status.map_backup_status,
),
map_index,
Expand Down Expand Up @@ -345,7 +347,6 @@ def __init__(
break

super().__init__(coordinator, description)
self._attr_translation_key = None
self.id = shortcut_id
if self.id >= 32:
self.id = self.id - 31
Expand Down Expand Up @@ -410,7 +411,6 @@ def __init__(
map_data = coordinator.device.get_map(self.map_index)
self._map_name = map_data.custom_name if map_data else None
super().__init__(coordinator, description)
self._attr_translation_key = None
self._set_id()
self._attr_unique_id = f"{self.device.mac}_backup_map_{self.map_index}"
self.entity_id = f"button.{self.device.name.lower()}_backup_map_{self.map_index}"
Expand Down
18 changes: 14 additions & 4 deletions custom_components/dreame_vacuum/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
STATE_UNKNOWN,
STATUS_CODE_TO_NAME,
ATTR_CALIBRATION,
ATTR_SELECTED,
ATTR_CLEANING_HISTORY_PICTURE,
ATTR_CRUISING_HISTORY_PICTURE,
ATTR_OBSTACLE_PICTURE,
Expand Down Expand Up @@ -353,6 +354,7 @@ def async_update_map_cameras(
DreameVacuumCameraEntity(
coordinator,
DreameVacuumCameraEntityDescription(
key="saved_map",
entity_category=EntityCategory.CONFIG,
icon="mdi:map-search",
),
Expand All @@ -370,6 +372,7 @@ def async_update_map_cameras(
DreameVacuumCameraEntity(
coordinator,
DreameVacuumCameraEntityDescription(
key="wifi_map",
entity_category=EntityCategory.CONFIG,
icon="mdi:wifi-settings",
map_type=DreameVacuumMapType.WIFI_MAP,
Expand Down Expand Up @@ -501,13 +504,13 @@ def _handle_coordinator_update(self) -> None:
"""Fetch state from the device."""
self._last_map_request = 0
map_data = self._map_data
if map_data and self.device.cloud_connected and (self.map_index > 0 or self.device.status.located):
if map_data and self.device.cloud_connected and (self.map_index > 0 or self.device.status.located):
if map_data.last_updated:
self._state = datetime.fromtimestamp(int(map_data.last_updated))
elif map_data.timestamp_ms:
self._state = datetime.fromtimestamp(int(map_data.timestamp_ms))
self._state = datetime.fromtimestamp(int(map_data.timestamp_ms / 1000))
else:
self._state = STATE_UNAVAILABLE
self._state = datetime.now()

if self.map_index > 0:
if self._map_name != map_data.custom_name:
Expand Down Expand Up @@ -613,6 +616,10 @@ def update(self) -> None:
map_data = self._map_data
if map_data and self.device.cloud_connected and (self.map_index > 0 or self.device.status.located):
self._device_active = self.device.status.active
if map_data.last_updated:
self._state = datetime.fromtimestamp(int(map_data.last_updated))
elif map_data.timestamp_ms:
self._state = datetime.fromtimestamp(int(map_data.timestamp_ms / 1000))

if (
self.map_index == 0
Expand All @@ -638,6 +645,7 @@ def update(self) -> None:
)
)
elif not self._default_map:
self._state = STATE_UNAVAILABLE
self._image = self._default_map_image
self._default_map = True
self._frame_id = -1
Expand Down Expand Up @@ -865,9 +873,11 @@ def extra_state_attributes(self) -> Dict[str, Any]:
if not attributes:
attributes = {}

if self.map_index:
attributes[ATTR_SELECTED] = self.device.status.selected_map and self.device.status.selected_map.map_index == self.map_index

token = self.access_tokens[-1]
if self.map_index == 0:

def get_key(index, history):
return f"{index}: {time.strftime('%m/%d %H:%M', time.localtime(history.date.timestamp()))} - {'Second ' if history.second_cleaning else ''}{STATUS_CODE_TO_NAME.get(history.status, STATE_UNKNOWN).replace('_', ' ').title()} {'(Completed)' if history.completed else '(Interrupted)'}"

Expand Down
28 changes: 26 additions & 2 deletions custom_components/dreame_vacuum/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@
"dreame.vacuum.r2310",
"dreame.vacuum.r2310a",
"dreame.vacuum.r2310b",
"dreame.vacuum.r2310d",
"dreame.vacuum.r2310e",
"dreame.vacuum.r2310f",
"dreame.vacuum.r2310g",
"dreame.vacuum.r2312",
Expand All @@ -107,15 +109,18 @@
"dreame.vacuum.r2338a",
"dreame.vacuum.r2345a",
"dreame.vacuum.r2345h",
"dreame.vacuum.r2350",
"dreame.vacuum.r2355",
"dreame.vacuum.r2360",
"dreame.vacuum.r2360w",
"dreame.vacuum.r2361a",
"dreame.vacuum.r2363a",
"dreame.vacuum.r2364a",
"dreame.vacuum.r2367",
"dreame.vacuum.r2375",
"dreame.vacuum.r2377",
"dreame.vacuum.r2380",
"dreame.vacuum.r2380r",
#"dreame.vacuum.r2380",
#"dreame.vacuum.r2380r",
"dreame.vacuum.r2386",
"dreame.vacuum.r2388",
"dreame.vacuum.r2394a",
Expand All @@ -126,12 +131,31 @@
"dreame.vacuum.r2394s",
"dreame.vacuum.r2394u",
"dreame.vacuum.r2398",
"dreame.vacuum.r2412",
"dreame.vacuum.r2416",
"dreame.vacuum.r2416a",
"dreame.vacuum.r2416c",
"dreame.vacuum.r2416h",
"dreame.vacuum.r2416n",
"dreame.vacuum.r2421",
"dreame.vacuum.r2424",
"dreame.vacuum.r2426",
"dreame.vacuum.r2435",
"dreame.vacuum.r2449a",
"dreame.vacuum.r2449k",
"dreame.vacuum.r2450m",
"dreame.vacuum.r2455",
"dreame.vacuum.r2462",
"dreame.vacuum.r9301",
"dreame.vacuum.r9302",
"dreame.vacuum.r9304",
"dreame.vacuum.r9305",
"dreame.vacuum.r9309a",
"dreame.vacuum.r9311",
"dreame.vacuum.r9312",
"dreame.vacuum.r9316h",
"dreame.vacuum.r9316k",
"dreame.vacuum.r9317a",
]

MIJIA_MODELS = [
Expand Down
33 changes: 18 additions & 15 deletions custom_components/dreame_vacuum/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from __future__ import annotations

import math
import time
import traceback
from homeassistant.components import persistent_notification
from homeassistant.config_entries import ConfigEntry
Expand Down Expand Up @@ -140,7 +141,7 @@ def __init__(
super().__init__(
hass,
LOGGER,
name=DOMAIN,
name=DOMAIN
)

async_dispatcher_connect(
Expand Down Expand Up @@ -185,7 +186,7 @@ def _cleaning_paused_changed(self, previous_value=None) -> None:

def _task_status_changed(self, previous_value=None) -> None:
if previous_value is not None:
if self._device.cleanup_completed:
if self._device.status.cleanup_completed:
self._fire_event(EVENT_TASK_STATUS, self._device.status.job)
self._create_persistent_notification(NOTIFICATION_CLEANUP_COMPLETED, NOTIFICATION_ID_CLEANUP_COMPLETED)
self._check_consumables()
Expand Down Expand Up @@ -362,11 +363,12 @@ def _check_consumables(self):
)

def _create_persistent_notification(self, content, notification_id) -> None:
if self._notify or notification_id == NOTIFICATION_ID_2FA_LOGIN:
if not self.device.disconnected and self.device.device_connected and (self._notify or notification_id == NOTIFICATION_ID_2FA_LOGIN):
if isinstance(self._notify, list) and notification_id != NOTIFICATION_ID_2FA_LOGIN:
if notification_id == NOTIFICATION_ID_CLEANUP_COMPLETED:
if NOTIFICATION_ID_CLEANUP_COMPLETED not in self._notify:
return
notification_id = f'{notification_id}_{int(time.time())}'
elif NOTIFICATION_ID_WARNING in notification_id or NOTIFICATION_ID_LOW_WATER in notification_id:
if NOTIFICATION_ID_WARNING not in self._notify:
return
Expand Down Expand Up @@ -432,9 +434,10 @@ async def _async_update_data(self) -> DreameVacuumDevice:
try:
LOGGER.info("Integration starting...")
await self.hass.async_add_executor_job(self._device.update)
self._device.schedule_update()
self.async_set_updated_data()
return self._device
if self._device and not self._device.disconnected:
self._device.schedule_update()
self.async_set_updated_data()
return self._device
except Exception as ex:
LOGGER.warning("Integration start failed: %s", traceback.format_exc())
if self._device is not None:
Expand Down Expand Up @@ -467,17 +470,17 @@ def async_set_updated_data(self, device=None) -> None:
LOGGER.info("Update Host Config: %s", self._host)
self.hass.config_entries.async_update_entry(self._entry, data=data)

if self._two_factor_url != self._device.two_factor_url:
if self._device.two_factor_url:
self._create_persistent_notification(
f"{NOTIFICATION_2FA_LOGIN}[{self._device.two_factor_url}]({self._device.two_factor_url})",
NOTIFICATION_ID_2FA_LOGIN,
)
if self._device.two_factor_url:
self._create_persistent_notification(
f"{NOTIFICATION_2FA_LOGIN}[Click for 2FA Login]({self._device.two_factor_url})",
NOTIFICATION_ID_2FA_LOGIN,
)
if self._two_factor_url != self._device.two_factor_url:
self._fire_event(EVENT_2FA_LOGIN, {"url": self._device.two_factor_url})
else:
self._remove_persistent_notification(NOTIFICATION_ID_2FA_LOGIN)
else:
self._remove_persistent_notification(NOTIFICATION_ID_2FA_LOGIN)

self._two_factor_url = self._device.two_factor_url
self._two_factor_url = self._device.two_factor_url

self._available = self._device and self._device.available
super().async_set_updated_data(self._device)
Expand Down
Loading

0 comments on commit 3771ca3

Please sign in to comment.