Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for maps #19

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions custom_components/purei9/camera.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
"""Home Assistant camera entity"""
import homeassistant.helpers.config_validation as cv
import voluptuous as vol
from homeassistant.core import HomeAssistant
from homeassistant.config_entries import ConfigEntry
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.components.camera import Camera, PLATFORM_SCHEMA
from purei9_unofficial.cloud import CloudRobot, CloudMap, CloudClient
from homeassistant.const import CONF_PASSWORD, CONF_EMAIL
from . import purei9

def setup_platform(
hass: HomeAssistant, config: ConfigEntry, add_entities: AddEntitiesCallback, discovery_info=None
) -> None:
"""Register all Pure i9's in Home Assistant"""
if discovery_info is not None:
client = CloudClient(config[CONF_EMAIL], config[CONF_PASSWORD])

cameras = []
for robot in client.getRobots():
robotName = robot.getname()
for map in robot.getMaps():
cameras.append(PureI9Map.create(map, robotName))

if cameras:
add_entities(cameras, update_before_add=True)

class PureI9Map(Camera):
"""Map displaying the vacuums cleaning areas"""
def __init__(self, map: CloudMap, params: purei9.HomeAssistantCameraParams):
super().__init__()
self._map = map
self._params = params

@staticmethod
def create(map: CloudMap, robotName: str):
"""Named constructor for creating a new instance from a CloudMap"""
params = purei9.HomeAssistantCameraParams(map.id, robotName)
return PureI9Map(map, params)

@property
def unique_id(self) -> str:
"""Unique identifier to the vacuum that this map belongs to"""
return self._params.unique_id

@property
def name(self) -> str:
"""Name of the vacuum that this map belongs to"""
return self._params.name

def camera_image(self, width = None, height = None):
return self._params.image

def update(self) -> None:
self._map.get()
self._params.image = self._map.image

1 change: 0 additions & 1 deletion custom_components/purei9/const.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""Commonly used constants"""
DOMAIN = "purei9"
COMPONENTS = ["vacuum"]
MANUFACTURER = "Electrolux"
MODEL = "Pure i9"
15 changes: 13 additions & 2 deletions custom_components/purei9/purei9.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def battery_to_hass(pure_i9_battery: str) -> int:
POWER_MODE_QUIET = "QUIET"
POWER_MODE_SMART = "SMART"

class Params:
class BaseParams:
"""Data available in the state"""
battery: int = 100
state: str = STATE_IDLE
Expand All @@ -83,12 +83,23 @@ def __init__(self, unique_id: str, name: str, fan_speed_list: List[str]):
def unique_id(self) -> str:
"""Immutable unique identifier"""
return self._unique_id

@property
def fan_speed_list(self) -> str:
"""Immutable fan speed list"""
return self._fan_speed_list

class HomeAssistantVacuumParams(BaseParams):
"""Data available in the vacuum state"""
battery: int = 100
state: str = STATE_IDLE
available: bool = True
firmware: str = None

class HomeAssistantCameraParams(BaseParams):
"""Data available in the camera state"""
image = None

def is_power_mode_v2(fan_speed_list: List[str]) -> bool:
"""Determine if the robot supports the new or old fan speed list """
return len(fan_speed_list) == 3
Expand Down
18 changes: 14 additions & 4 deletions custom_components/purei9/vacuum.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
SUPPORT_START,
SUPPORT_STATE,
SUPPORT_STOP,
SUPPORT_MAP,
SUPPORT_FAN_SPEED,
StateVacuumEntity,
PLATFORM_SCHEMA,
Expand All @@ -20,6 +21,9 @@
STATE_IDLE
)
from homeassistant.const import CONF_PASSWORD, CONF_EMAIL
from homeassistant.core import HomeAssistant
from homeassistant.config_entries import ConfigEntry
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from purei9_unofficial.cloudv2 import CloudClient, CloudRobot
from purei9_unofficial.common import DustbinStates
from . import purei9, const
Expand All @@ -36,9 +40,12 @@
# Deprecated way to setup this integration. Will be removed in v2.x.
def setup_platform(_hass, config, add_entities, _discovery_info=None) -> None:
"""Register all Pure i9's in Home Assistant"""
client = CloudClient(config[CONF_EMAIL], config.get(CONF_PASSWORD))
client = CloudClient(config[CONF_EMAIL], config[CONF_PASSWORD])
entities = map(PureI9.create, client.getRobots())
add_entities(entities, update_before_add=True)

if entities:
add_entities(entities, update_before_add=True)
hass.helpers.discovery.load_platform('camera', const.DOMAIN, {}, config)

async def async_setup_entry(hass, config_entry, async_add_entities):
"""Initial setup for the workers. Download and identify all workers."""
Expand All @@ -61,8 +68,9 @@ class PureI9(StateVacuumEntity):
def __init__(
self,
robot: CloudRobot,
params: purei9.Params,
params: purei9.HomeAssistantVacuumParams,
):
super().__init__()
self._robot = robot
self._params = params
# The Pure i9 library caches results. When we do state updates, the next update
Expand All @@ -76,7 +84,8 @@ def create(robot: CloudRobot):
purei9_fan_speed_list = list(map(lambda x: x.name, robot.getsupportedpowermodes()))
fan_speed_list = purei9.fan_speed_list_to_hass(purei9_fan_speed_list)

params = purei9.Params(robot.getid(), robot.getname(), fan_speed_list)
params = purei9.HomeAssistantVacuumParams(robot.getid(), robot.getname(), fan_speed_list)

return PureI9(robot, params)

@property
Expand All @@ -91,6 +100,7 @@ def supported_features(self) -> int:
| SUPPORT_STOP
| SUPPORT_PAUSE
| SUPPORT_STATE
| SUPPORT_MAP
| SUPPORT_FAN_SPEED
)

Expand Down
19 changes: 14 additions & 5 deletions docs/helpful_links.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
# Helpful links

* Definitions of supported features: https://github.com/home-assistant/core/blob/dev/homeassistant/components/vacuum/services.yaml
* Source code to vacuum entity: https://github.com/home-assistant/core/tree/dev/homeassistant/components/vacuum
* Source code to entity: https://github.com/home-assistant/core/blob/adab367f0e8c48a68b4dffd0783351b0072fbd0a/homeassistant/helpers/entity.py
* Documentation to vacuum entity: https://developers.home-assistant.io/docs/core/entity/vacuum/
* Home Assistant developers: https://developers.home-assistant.io/
* [Home Assistant developers](https://developers.home-assistant.io/)
* [Entity](https://github.com/home-assistant/core/blob/adab367f0e8c48a68b4dffd0783351b0072fbd0a/homeassistant/helpers/entity.py)

## Vacuum

* [Vacuum entity](https://github.com/home-assistant/core/tree/dev/homeassistant/components/vacuum)
* [Vacuum supported features](https://github.com/home-assistant/core/blob/dev/homeassistant/components/vacuum/services.yaml)
* [Documentation to vacuum](https://developers.home-assistant.io/docs/core/entity/vacuum/)

## Camera

* [Camera entity](https://github.com/home-assistant/core/blob/adab367f0e8c48a68b4dffd0783351b0072fbd0a/homeassistant/components/camera)
* [Camera supported features](https://github.com/home-assistant/core/blob/adab367f0e8c48a68b4dffd0783351b0072fbd0a/homeassistant/components/camera/services.yaml)
* [Documentation to camera](https://developers.home-assistant.io/docs/core/entity/camera/)

## Tests

Expand Down
2 changes: 1 addition & 1 deletion hacs.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "Pure i9",
"render_readme": true,
"domains": ["vacuum"]
"domains": ["vacuum", "camera"]
}