Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
- Fix cutting border
- Fix reloading if login fails
- Adden turn on/off schedule
  • Loading branch information
Sdahl1234 authored Apr 14, 2024
1 parent 8b8d95b commit a62109c
Show file tree
Hide file tree
Showing 8 changed files with 799 additions and 320 deletions.
110 changes: 108 additions & 2 deletions custom_components/sunseeker/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
"""Sunseeker mower integration."""

import asyncio
import json
import logging
import os

from homeassistant.config_entries import ConfigEntry
from homeassistant.config_entries import ConfigEntry, ConfigEntryNotReady
from homeassistant.const import CONF_EMAIL, CONF_MODEL, CONF_PASSWORD, Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceInfo
Expand Down Expand Up @@ -47,6 +50,9 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):

data_handler = SunseekerRoboticmower(brand, email, password, language)
await hass.async_add_executor_job(data_handler.on_load)
if not data_handler.login_ok:
_LOGGER.error("Login error")
raise ConfigEntryNotReady("Login failed")
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = {DH: data_handler}

# robot = [1, 2]
Expand Down Expand Up @@ -86,6 +92,18 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
class SunseekerDataCoordinator(DataUpdateCoordinator): # noqa: D101
config_entry: ConfigEntry

jdata: None
data_loaded: bool = False
data_default = {
"Monday": "00:00 - 00:00",
"Tuesday": "00:00 - 00:00",
"Wednesday": "00:00 - 00:00",
"Thursday": "00:00 - 00:00",
"Friday": "00:00 - 00:00",
"Saturday": "00:00 - 00:00",
"Sunday": "00:00 - 00:00",
}

def __init__(
self, hass: HomeAssistant, data_handler: SunseekerRoboticmower, devicesn, brand
) -> None:
Expand All @@ -103,12 +121,100 @@ def __init__(
self.data_handler = data_handler
self._devicesn = devicesn
self.data_handler._dataupdated = self.dataupdated
self.filepath = os.path.join(
self.hass.config.config_dir,
"Schedule-{}.json".format(self._devicesn.replace(" ", "_")),
)
_LOGGER.info(self.filepath)
self.jdata = self.data_default
self.hass.add_job(self.set_schedule_data)
self.hass.add_job(self.file_exits)
self.hass.add_job(self.load_data)

async def set_schedule_data(self):
"""Set default."""
self.jdata["Monday"] = await self.GetSchedule(1)
self.jdata["Tuesday"] = await self.GetSchedule(2)
self.jdata["Wednesday"] = await self.GetSchedule(3)
self.jdata["Thursday"] = await self.GetSchedule(4)
self.jdata["Friday"] = await self.GetSchedule(5)
self.jdata["Saturday"] = await self.GetSchedule(6)
self.jdata["Sunday"] = await self.GetSchedule(7)

async def GetSchedule(self, daynumber: int) -> str:
"""Get schedule."""
b_trim = (
self.data_handler.get_device(self._devicesn).Schedule.GetDay(daynumber).trim
)
if b_trim:
s_trim = " Trim"
else:
s_trim = ""
retval = {
self.data_handler.get_device(self._devicesn)
.Schedule.GetDay(daynumber)
.start
+ " - "
+ self.data_handler.get_device(self._devicesn)
.Schedule.GetDay(daynumber)
.end
+ s_trim
}
return str(retval).replace("{", "").replace("}", "").replace("'", "")

async def file_exits(self):
"""Do file exists."""
try:
f = open(self.filepath, encoding="utf-8")
f.close()
except FileNotFoundError:
# save a new file
await self.save_data(False)

async def save_data(self, append: bool):
"""Save data."""
try:
if append:
cfile = open(self.filepath, "w", encoding="utf-8")
else:
cfile = open(self.filepath, "a", encoding="utf-8")
ocrdata = json.dumps(self.jdata)
self.data_handler.get_device(self._devicesn).Schedule.SavedData = self.jdata
cfile.write(ocrdata)
cfile.close()
except Exception as ex: # pylint: disable=broad-except
_LOGGER.debug(f"Save data failed: {ex}") # noqa: G004

async def load_data(self):
"""Load data."""
try:
cfile = open(self.filepath, encoding="utf-8")
ocrdata = cfile.read()
cfile.close()
_LOGGER.debug(f"ocrdata: {ocrdata}") # noqa: G004
_LOGGER.debug(f"jsonload: {json.loads(ocrdata)}") # noqa: G004

self.jdata = json.loads(ocrdata)
self.data_handler.get_device(self._devicesn).Schedule.SavedData = self.jdata
self.data_loaded = True
except Exception as ex: # pylint: disable=broad-except
_LOGGER.debug(f"load data failed: {ex}") # noqa: G004

async def save_schedule_data(self):
"""Update schedule data on disk."""
await self.set_schedule_data()
await self.save_data(True)

def dataupdated(self, devicesn: str):
def dataupdated(self, devicesn: str, schedule: bool):
"""Func Callback when data is updated."""
_LOGGER.debug(f"callback - Sunseeker {self._devicesn} data updated") # noqa: G004
if self._devicesn == devicesn:
self.hass.add_job(self.async_set_updated_data, None)
if (
schedule
and not self.data_handler.get_device(self._devicesn).Schedule.IsEmpty()
):
self.hass.add_job(self.save_schedule_data)

@property
def dsn(self):
Expand Down
12 changes: 11 additions & 1 deletion custom_components/sunseeker/config_flow.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Adds config flow for Sunseeker mower integration."""

import voluptuous as vol

from homeassistant import config_entries, core, exceptions
Expand All @@ -10,7 +11,16 @@

# _LOGGER = logging.getLogger(__name__)

brands = ["Adano", "Brucke", "Meec tools", "Orbex", "Scheppach", "Sunseeker", "Texas"]
brands = [
"Adano",
"Brucke",
"Meec tools",
"Orbex",
"Scheppach",
"Sunseeker",
"Texas",
"Grouw",
]
DATA_SCHEMA = vol.Schema(
{
vol.Required(CONF_MODEL, default=False): selector.SelectSelector(
Expand Down
4 changes: 2 additions & 2 deletions custom_components/sunseeker/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@
SUNSEEKER_DRY_COUNTDOWN = "Dry countdown"

# Max retries
MAX_UPDATE_RETRIES = 10
MAX_SET_CONFIG_RETRIES = 10
MAX_LOGIN_RETRIES = 10
MAX_SET_CONFIG_RETRIES = 15
2 changes: 1 addition & 1 deletion custom_components/sunseeker/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
"integration_type": "device",
"iot_class": "cloud_push",
"config_flow": true,
"version": "1.0.7"
"version": "1.0.9"
}
Loading

0 comments on commit a62109c

Please sign in to comment.