Skip to content

Commit

Permalink
Merge pull request #114 from elad-bar/use-coordinator
Browse files Browse the repository at this point in the history
Use coordinator
elad-bar authored Jul 3, 2023
2 parents a11876d + 50320f8 commit 99d7598
Showing 76 changed files with 2,429 additions and 4,063 deletions.
25 changes: 25 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
# Changelog

## v0.4.0

- Refactor code to support HA coordinator
- Improve reconnection when AWS Broker gets disconnected
- Remove integration options (Edit configuration)
- Improve diagnostic data
- New components
- Sensor: Clean Mode
- Sensor: Cycle Count
- Sensor: Main Unit Status
- Sensor: Robot Status
- Sensor: Robot Type
- Sensor: RSSI
- Sensor: Network Name
- Sensor: Status (Calculated Status)
- Number: LED Intensity
- Support translation for
- Vacuum: Fan Speed
- Sensor: Filter Status
- Sensor: Robot Status
- Sensor: Main Unit Status
- Sensor: Clean Mode
- Sensor: Status
- Select: LED Mode

## v0.3.4

- Fix error upon restart caused by attempt to handle message from AWS MQTT Broker during restart of HA
71 changes: 31 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
@@ -43,13 +43,6 @@ to test you can run the CLI mode or within the HA.
| Username | Textbox | - | | Username of dashboard user for MyDolphin Plus |
| Password | Textbox | - | | Password of dashboard user for MyDolphin Plus |

###### Integration options (Configuration -> Integrations -> MyDolphin Plus Integration -> Options)

| Fields name | Type | Required | Default | Description |
| ----------- | ------- | -------- | -------------------- | --------------------------------------------- |
| Username | Textbox | - | Last stored username | Username of dashboard user for MyDolphin Plus |
| Password | Textbox | - | Last stored password | Password of dashboard user for MyDolphin Plus |

###### Configuration validations

Upon submitting the form of creating an integration or updating options,
@@ -88,18 +81,27 @@ Please remove the integration and re-add it to make it work again.

## HA Components

| Entity Name | Type | Description | Additional information |
| -------------------------------- | -------------- | -------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------- |
| {Robot Name} AWS Broker | Binary Sensors | Indicates whether the component synchronized with cloud or not | |
| {Robot Name} Schedule Delay | Binary Sensors | Indicates whether the delay cleaning is enabled or not | |
| {Robot Name} Schedule {Day Name} | Binary Sensors | Indicates whether the schedule cleaning is enabled or not | |
| {Robot Name} Weekly Schedule | Binary Sensor | Indicates whether the weekly scheduler is on or off | |
| {Robot Name} | Light | Turned on or off the led | |
| {Robot Name} Led Mode | Select | Select led mode | Blinking, Always on, Disco |
| {Robot Name} Filter | Sensors | Presents the status of the filter bag | |
| {Robot Name} Cycle Time | Sensor | Indicates the time the robot is cleaning | Measurement of duration in minutes |
| {Robot Name} Cycle Time Left | Sensor | Indicates the time left for the robot to complete the cycle | Measurement of duration in seconds |
| {Robot Name} | Vacuum | Provides functionality of vacuum to the robot | Features: State, Fan Speed (Cleaning Mode), Return Home (Pickup), Turn On, Turn Off, Send Command (Navigate, Schedule, Delay Clean) |
| Entity Name | Type | Description | Additional information |
| -------------------------------- | ------------- | -------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------- |
| {Robot Name} AWS Broker | Binary Sensor | Indicates whether the component synchronized with cloud or not | |
| {Robot Name} Schedule Delay | Binary Sensor | Indicates whether the delay cleaning is enabled or not | |
| {Robot Name} Schedule {Day Name} | Binary Sensor | Indicates whether the schedule cleaning is enabled or not | |
| {Robot Name} Weekly Schedule | Binary Sensor | Indicates whether the weekly scheduler is on or off | |
| {Robot Name} LED | Light | Turned on or off the led | |
| {Robot Name} LED Intensity | Number | Sets the LED intensity values between 0-100 | |
| {Robot Name} LED Mode | Select | Select led mode | Blinking, Always on, Disco |
| {Robot Name} Status | Sensor | Presents the calculated status of the device | |
| {Robot Name} RSSI | Sensor | Presents the WIFI signal strength in DB | |
| {Robot Name} Network Name | Sensor | Presents the name of the network (WIFI SSID) | |
| {Robot Name} Clean Mode | Sensor | Presents the current clean mode | |
| {Robot Name} Main Unit Status | Sensor | Presents the status of the main unit (PWS) | |
| {Robot Name} Robot Status | Sensor | Presents the status of the robot | |
| {Robot Name} Robot Model | Sensor | Presents the type of the robot | |
| {Robot Name} Cycle Count | Sensor | Presents the number of cycles ran | |
| {Robot Name} Filter Status | Sensor | Presents the status of the filter bag | |
| {Robot Name} Cycle Time | Sensor | Indicates the time the robot is cleaning | Measurement of duration in minutes |
| {Robot Name} Cycle Time Left | Sensor | Indicates the time left for the robot to complete the cycle | Measurement of duration in seconds |
| {Robot Name} | Vacuum | Provides functionality of vacuum to the robot | Features: State, Fan Speed (Cleaning Mode), Return Home (Pickup), Turn On, Turn Off, Send Command (Navigate, Schedule, Delay Clean) |

### Cleaning Modes

@@ -197,25 +199,14 @@ https://github.com/denysdovhan/vacuum-card
Copy the icons from www on the repository to /config/www. Below is a suggested configuration for the card
```yaml
entity: vacuum.my_vacuum
image: /local/robot_icon_c.svg
type: custom:vacuum-card
compact_view: false
stats:
default:
- attribute: turn_on_count
subtitle: Run Count
- entity_id: sensor.my_vacuum_filter
subtitle: Filter Status
- attribute: pws_status
subtitle: Base Status
- entity_id: binary_sensor.jmy_vacuum_broker
subtitle: AWS
cleaning:
- entity_id: sensor.my_vacuum_cycle_time_left
subtitle: Time Remaining
- attribute: robot_status
subtitle: Base Status
- entity_id: binary_sensor.my_vacuum_aws_broker
subtitle: AWS
type: tile
entity: vacuum.robot_name
show_entity_picture: true
features:
- type: vacuum-commands
commands:
- start_pause
- stop
- locate
- return_home
```
50 changes: 28 additions & 22 deletions custom_components/mydolphin_plus/__init__.py
Original file line number Diff line number Diff line change
@@ -9,56 +9,62 @@
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant

from .component.helpers import async_set_ha, clear_ha, get_ha
from .configuration.helpers.const import DOMAIN
from .common.consts import DEFAULT_NAME, DOMAIN, PLATFORMS
from .common.exceptions import LoginError
from .managers.config_manager import ConfigManager
from .managers.coordinator import MyDolphinPlusCoordinator

_LOGGER = logging.getLogger(__name__)


async def async_setup(hass, config):
async def async_setup(_hass, _config):
return True


async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up a MyDolphin Plus component."""
"""Set up a Shinobi Video component."""
initialized = False

try:
_LOGGER.debug(f"Starting async_setup_entry of {DOMAIN}")
entry.add_update_listener(async_options_updated)
config_manager = ConfigManager(hass, entry)
await config_manager.initialize()

await async_set_ha(hass, entry)
coordinator = MyDolphinPlusCoordinator(hass, config_manager)
await coordinator.initialize()

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

await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)

_LOGGER.info(f"Start loading {DOMAIN} integration, Entry ID: {entry.entry_id}")

await coordinator.async_config_entry_first_refresh()

_LOGGER.info("Finished loading integration")

initialized = True

except LoginError:
_LOGGER.info(f"Failed to login {DEFAULT_NAME} API, cannot log integration")

except Exception as ex:
exc_type, exc_obj, tb = sys.exc_info()
line_number = tb.tb_lineno

_LOGGER.error(
f"Failed to load MyDolphin Plus, error: {ex}, line: {line_number}"
f"Failed to load {DEFAULT_NAME}, error: {ex}, line: {line_number}"
)

return initialized


async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
"""Unload a config entry."""
ha = get_ha(hass, entry.entry_id)
_LOGGER.info(f"Unloading {DOMAIN} integration, Entry ID: {entry.entry_id}")

if ha is not None:
await ha.async_remove(entry)
for platform in PLATFORMS:
await hass.config_entries.async_forward_entry_unload(entry, platform)

clear_ha(hass, entry.entry_id)
del hass.data[DOMAIN][entry.entry_id]

return True


async def async_options_updated(hass: HomeAssistant, entry: ConfigEntry):
"""Triggered by config entry options updates."""
_LOGGER.info(f"async_options_updated, Entry: {entry.as_dict()} ")

ha = get_ha(hass, entry.entry_id)

if ha is not None:
await ha.async_update_entry(entry)
69 changes: 48 additions & 21 deletions custom_components/mydolphin_plus/binary_sensor.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,61 @@
"""
Support for binary sensors.
"""
from __future__ import annotations

import logging

from .core.components.binary_sensor import CoreBinarySensor
from .core.helpers.setup_base_entry import async_setup_base_entry
from homeassistant.components.binary_sensor import (
BinarySensorEntity,
BinarySensorEntityDescription,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_ICON, Platform
from homeassistant.core import HomeAssistant

from .common.base_entity import MyDolphinPlusBaseEntity, async_setup_entities
from .common.consts import ATTR_ATTRIBUTES, ATTR_IS_ON
from .common.entity_descriptions import MyDolphinPlusDailyBinarySensorEntityDescription
from .managers.coordinator import MyDolphinPlusCoordinator

_LOGGER = logging.getLogger(__name__)

CURRENT_DOMAIN = Platform.SENSOR

async def async_setup_entry(hass, config_entry, async_add_devices):
"""Set up the component."""
await async_setup_base_entry(

async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities
):
await async_setup_entities(
hass,
config_entry,
async_add_devices,
CoreBinarySensor.get_domain(),
CoreBinarySensor.get_component,
entry,
CURRENT_DOMAIN,
BinarySensorEntityDescription,
MyDolphinPlusBinarySensorEntity,
async_add_entities,
)


async def async_unload_entry(hass, config_entry):
_LOGGER.info(
f"Unload entry for {CoreBinarySensor.get_domain()} domain: {config_entry}"
)
class MyDolphinPlusBinarySensorEntity(MyDolphinPlusBaseEntity, BinarySensorEntity):
"""Representation of a sensor."""

def __init__(
self,
entity_description: BinarySensorEntityDescription
| MyDolphinPlusDailyBinarySensorEntityDescription,
coordinator: MyDolphinPlusCoordinator,
):
super().__init__(entity_description, coordinator, CURRENT_DOMAIN)

self._attr_device_class = entity_description.device_class

def update_component(self, data):
"""Fetch new state parameters for the sensor."""
if data is not None:
is_on = data.get(ATTR_IS_ON)
attributes = data.get(ATTR_ATTRIBUTES)
icon = data.get(ATTR_ICON)

return True
self._attr_is_on = is_on
self._attr_extra_state_attributes = attributes

if icon is not None:
self._attr_icon = icon

async def async_remove_entry(hass, entry) -> None:
_LOGGER.info(f"Remove entry for {CoreBinarySensor.get_domain()} entry: {entry}")
else:
self._attr_is_on = None
Loading

0 comments on commit 99d7598

Please sign in to comment.