Skip to content

Commit

Permalink
Fix password for SkyFi devices (#21)
Browse files Browse the repository at this point in the history
* fix pass for skyfi devices

* update wait and exceptions in _get_resource
  • Loading branch information
fredrike authored Aug 28, 2024
1 parent 7d0bebd commit 8d0c508
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 28 deletions.
19 changes: 14 additions & 5 deletions pydaikin/daikin_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
from urllib.parse import unquote

from aiohttp import ClientSession
from aiohttp.client_exceptions import ServerDisconnectedError
from aiohttp.client_exceptions import ClientOSError, ServerDisconnectedError
from aiohttp.web_exceptions import HTTPError, HTTPForbidden
from tenacity import (
before_sleep_log,
retry,
retry_if_exception_type,
stop_after_attempt,
wait_fixed,
wait_random_exponential,
)

from .discovery import get_name
Expand Down Expand Up @@ -125,9 +125,14 @@ async def init(self):

@retry(
reraise=True,
wait=wait_fixed(1),
wait=wait_random_exponential(multiplier=0.2, max=1.2),
stop=stop_after_attempt(3),
retry=retry_if_exception_type(ServerDisconnectedError),
retry=retry_if_exception_type(
(
ServerDisconnectedError,
ClientOSError,
)
),
before_sleep=before_sleep_log(_LOGGER, logging.DEBUG),
)
async def _get_resource(self, path: str, params: Optional[dict] = None):
Expand All @@ -136,7 +141,11 @@ async def _get_resource(self, path: str, params: Optional[dict] = None):
params = {}

_LOGGER.debug(
"Calling: %s/%s %s [%s]", self.base_url, path, params, self.headers
"Calling: %s/%s %s [%s]",
self.base_url,
path,
params if "pass" not in params else {**params, **{"pass": "****"}},
self.headers,
)

# cannot manage session on outer async with or this will close the session
Expand Down
56 changes: 34 additions & 22 deletions pydaikin/daikin_skyfi.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import logging
from urllib.parse import unquote

from aiohttp import ClientSession

from .daikin_base import Appliance

_LOGGER = logging.getLogger(__name__)
Expand All @@ -11,7 +13,7 @@
class DaikinSkyFi(Appliance):
"""Daikin class for SkyFi units."""

HTTP_RESOURCES = ['ac.cgi?pass={}', 'zones.cgi?pass={}']
HTTP_RESOURCES = ['ac.cgi', 'zones.cgi']

INFO_RESOURCES = HTTP_RESOURCES

Expand Down Expand Up @@ -47,11 +49,15 @@ class DaikinSkyFi(Appliance):
},
}

def __init__(self, device_id, session=None, password=None) -> None:
def __init__(
self,
device_id: str,
session: ClientSession | None,
password: str,
) -> None:
"""Init the pydaikin appliance, representing one Daikin SkyFi device."""
super().__init__(device_id, session)
self.device_ip = f'{self.device_ip}:2000'
self.base_url = f"http://{self.device_ip}"
self.base_url = f"http://{self.device_ip}:2000"
self._password = password

def __getitem__(self, name):
Expand Down Expand Up @@ -102,6 +108,14 @@ def parse_response(response_body):
)
return response

async def _get_resource(self, path: str, params: dict | None = None):
"""Make the http request."""
if params is None:
params = {}
params["pass"] = self._password

return await super()._get_resource(path, params)

def represent(self, key):
"""Return translated value from key."""
k, val = super().represent(self.SKYFI_TO_DAIKIN.get(key, key))
Expand All @@ -115,7 +129,7 @@ def represent(self, key):
async def set(self, settings):
"""Set settings on Daikin device."""
_LOGGER.debug("Updating settings: %s", settings)
await self.update_status(['ac.cgi?pass={}'])
await self.update_status(['ac.cgi'])

# Merge current_val with mapped settings
self.values.update(
Expand All @@ -129,19 +143,20 @@ async def set(self, settings):
# we are using an extra mode "off" to power off the unit
if settings.get('mode', '') == 'off':
self.values['opmode'] = '0'
query_c = 'set.cgi?pass={}&p=0'
params = {
"p": self.values['opmode'],
}
else:
if 'mode' in settings:
self.values['opmode'] = '1'
query_c = (
f"set.cgi?pass={{}}"
f"&p={self.values['opmode']}"
f"&t={self.values['settemp']}"
f"&f={self.values['fanspeed']}"
f"&m={self.values['acmode']}"
)
params = {
"p": self.values['opmode'],
"t": self.values['settemp'],
"f": self.values['fanspeed'],
"m": self.values['acmode'],
}

await self.update_status([query_c])
self.values.update(await self._get_resource("set.cgi", params))

@property
def zones(self):
Expand All @@ -165,11 +180,8 @@ async def set_zone(self, zone_id, key, value):
return
zone_id += 1

path = "setzone.cgi"
params = {"pass": "HIDDEN", "z": zone_id, "s": value}
_LOGGER.debug("Sending request to %s with params: %s", path, params)

params["pass"] = self._password

current_state = await self._get_resource(path, params)
self.values.update(current_state)
params = {
"z": zone_id,
"s": value,
}
self.values.update(await self._get_resource("setzone.cgi", params))
2 changes: 1 addition & 1 deletion pydaikin/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ async def __init__(
"""Factory to init the corresponding Daikin class."""

if password is not None:
self._generated_object = DaikinSkyFi(device_id, session, password=password)
self._generated_object = DaikinSkyFi(device_id, session, password)
elif key is not None:
self._generated_object = DaikinBRP072C(
device_id,
Expand Down

0 comments on commit 8d0c508

Please sign in to comment.