Skip to content

Commit

Permalink
Added vacuum position
Browse files Browse the repository at this point in the history
  • Loading branch information
oven-lab committed Dec 6, 2023
1 parent b4b29e3 commit 72033bb
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 23 deletions.
14 changes: 6 additions & 8 deletions custom_components/tuya_cloud_map_extractor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

PLATFORMS = [Platform.CAMERA]

from .const import DOMAIN, CONF_PATH
from .const import DOMAIN, CONF_PATH, CONF_LAST
_LOGGER = logging.getLogger(__name__)

async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
Expand Down Expand Up @@ -46,16 +46,14 @@ async def async_migrate_entry(hass, config_entry: ConfigEntry):
async def update_listener(hass: HomeAssistant, config_entry: ConfigEntry):
"""Handle options update."""

if not await async_unload_entry(hass, config_entry):
return

data = {**config_entry.data}
options = {**config_entry.options}

data["path_enabled"] = options["path_enabled"]
data[CONF_PATH] = options[CONF_PATH]
data[CONF_LAST] = options[CONF_LAST]

hass.config_entries.async_update_entry(config_entry, data=data)
await async_reload_entry

async def async_reload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> None:
"""Reload the config entry."""
if not await async_unload_entry(hass, config_entry):
return
await async_setup_entry(hass, config_entry)
15 changes: 8 additions & 7 deletions custom_components/tuya_cloud_map_extractor/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import generate_entity_id

from .const import CONF_PATH
from .const import CONF_PATH, CONF_LAST

_LOGGER = logging.getLogger(__name__)

SCAN_INTERVAL = timedelta(minutes=1)
SCAN_INTERVAL = timedelta(seconds=10)


async def async_setup_entry(
Expand All @@ -34,7 +34,7 @@ async def async_setup_entry(
secret_key = config.data["client_secret"]
device_id = config.data["device_id"]
colors = config.data["colors"]
render_path = config.data[CONF_PATH]
path_settings = {CONF_PATH: config.data[CONF_PATH], "last": config.data[CONF_LAST]}
_LOGGER.debug("Adding entities")
async_add_entities(
[
Expand All @@ -46,7 +46,7 @@ async def async_setup_entry(
secret_key,
device_id,
should_poll,
render_path,
path_settings,
colors,
)
]
Expand All @@ -64,7 +64,7 @@ def __init__(
secret_key: str,
device_id: str,
should_poll: bool,
render_path: bool,
path_settings: dict,
colors: dict,
) -> None:
"""Initialized camera."""
Expand All @@ -86,7 +86,7 @@ def __init__(
self._device_id = device_id
self._attr_unique_id = client_id + device_id
self._colors = colors
self._render_path = render_path
self._path_settings = path_settings

async def async_added_to_hass(self) -> None:
self.async_schedule_update_ha_state(True)
Expand All @@ -97,6 +97,7 @@ def supported_features(self) -> int:

@property
def should_poll(self) -> bool:
print("should_poll: ", self._should_poll)
return self._should_poll

@property
Expand Down Expand Up @@ -129,7 +130,7 @@ def update(self):
self._secret_key,
self._device_id,
self._colors,
self._render_path,
self._path_settings,
)
_LOGGER.debug("Map data retrieved")
except Exception as error:
Expand Down
18 changes: 12 additions & 6 deletions custom_components/tuya_cloud_map_extractor/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
CONF_ROOM_NAME,
CONF_PATH,
CONF_PATH_COLOR,
CONF_LAST,
DEFAULT_BG_COLOR,
DEFAULT_ROOM_COLOR,
DEFAULT_WALL_COLOR,
Expand All @@ -59,7 +60,6 @@


class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):

VERSION = 2

def __init__(self) -> None:
Expand Down Expand Up @@ -157,9 +157,7 @@ async def async_step_colorconf(self, user_input=None):
CONF_WALL_COLOR: selector(
{"color_rgb": {}} # TODO: default value of [255, 255, 255]
),
CONF_PATH_COLOR: selector(
{"color_rgb": {}}
),
CONF_PATH_COLOR: selector({"color_rgb": {}}),
}

if "roominfo" in self.map_header:
Expand Down Expand Up @@ -194,15 +192,16 @@ async def async_step_room_colors(self, user_input=None):
return self.async_show_form(
step_id="room_colors", data_schema=vol.Schema(DATA_SCHEMA), errors=errors
)

@staticmethod
@callback
def async_get_options_flow(
config_entry: config_entries.ConfigEntry,
) -> config_entries.OptionsFlow:
"""Create the options flow."""
return OptionsFlowHandler(config_entry)



class OptionsFlowHandler(config_entries.OptionsFlow):
def __init__(self, config_entry: config_entries.ConfigEntry) -> None:
"""Initialize options flow."""
Expand All @@ -213,16 +212,19 @@ async def async_step_init(self, user_input=None):

data = {**self.config_entry.data}
path_default = data[CONF_PATH]
last_default = data[CONF_LAST]

DATA_SCHEMA = {
vol.Required(CONF_PATH, default=path_default): bool,
vol.Required(CONF_LAST, default=last_default): bool,
}

if user_input is not None:
return self.async_create_entry(title="", data=user_input)

return self.async_show_form(step_id="init", data_schema=vol.Schema(DATA_SCHEMA))


async def validate(hass: HomeAssistant, data: dict):
"""Validate the user input"""
return await hass.async_add_executor_job(
Expand All @@ -231,6 +233,8 @@ async def validate(hass: HomeAssistant, data: dict):
data["client_id"],
data["client_secret"],
data["device_id"],
{},
{CONF_PATH: data["path_enabled"], "last": True},
)


Expand Down Expand Up @@ -264,6 +268,8 @@ def create_entry_data(data: dict, header: dict):
if not CONF_PATH_COLOR in data:
data[CONF_PATH_COLOR] = DEFAULT_PATH_COLOR

data[CONF_LAST] = True

colors[CONF_BG_COLOR] = data.pop(CONF_BG_COLOR)
colors[CONF_WALL_COLOR] = data.pop(CONF_WALL_COLOR)
colors[CONF_PATH_COLOR] = data.pop(CONF_PATH_COLOR)
Expand Down
1 change: 1 addition & 0 deletions custom_components/tuya_cloud_map_extractor/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

CONF_PATH = "path_enabled"
CONF_PATH_COLOR = "path_color"
CONF_LAST = "last"

DEFAULT_BG_COLOR = [44, 50, 64]
DEFAULT_WALL_COLOR = [255, 255, 255]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,13 @@ def render_layout(raw_map: bytes, header: dict, colors: dict) -> Image.Image:


def get_map(
server: str, client_id: str, secret_key: str, device_id: str, colors={}, render_path=False
server: str, client_id: str, secret_key: str, device_id: str, colors={}, path_settings={}
) -> Image:
"""Downloads and parses vacuum map from tuya cloud."""

render_path = path_settings["path_enabled"]
last = path_settings["last"]

link = get_download_link(server, client_id, secret_key, device_id)
map_link = link["result"][0]["map_url"]
try:
Expand Down Expand Up @@ -113,7 +116,7 @@ def get_map(
if render_path:
_LOGGER.debug("Rendering path")

if colors == {}:
if "path_color" not in colors:
colors["path_color"] = [0, 255, 0]

scale = int(1080/image.size[0])
Expand All @@ -124,6 +127,14 @@ def get_map(
path = parse_path(response, scale=scale)
draw = ImageDraw.Draw(image)
draw.line(path, fill=tuple(colors["path_color"]), width=1)

if last:
x, y = path[-2], path[-1]
else:
x, y = path[0], path[1]

draw.ellipse([(x-5, y-5), (x+5, y+5)], outline=(255, 255, 255), fill=(0, 0, 255), width=1)

return header, image

return header, image
Expand Down

0 comments on commit 72033bb

Please sign in to comment.