Skip to content

Commit

Permalink
Add UserId/Passwod as authentication configuration (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
iKaew authored May 8, 2024
1 parent a7dba33 commit 012d65c
Show file tree
Hide file tree
Showing 10 changed files with 326 additions and 189 deletions.
81 changes: 45 additions & 36 deletions custom_components/lifesmart/__init__.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,24 @@
"""lifesmart by @ikaew"""
from typing import Final
from homeassistant.components.climate import FAN_HIGH, FAN_LOW, FAN_MEDIUM
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_URL, STATE_OFF, STATE_ON, Platform
from homeassistant.helpers.dispatcher import dispatcher_send
from homeassistant.helpers.typing import ConfigType
from homeassistant.util.dt import utcnow
from homeassistant.helpers.event import async_track_point_in_utc_time
from homeassistant.helpers.entity import DeviceInfo, Entity
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers import discovery
from homeassistant.core import HomeAssistant, callback
from homeassistant.components import climate
from homeassistant.const import (
CONF_FRIENDLY_NAME,
)
import subprocess
from unittest import case
import urllib.request
import json
import time
"""lifesmart by @ikaew."""
import asyncio
import datetime
import hashlib
import json
import logging
import threading
from .lifesmart_client import LifeSmartClient
import websocket
import asyncio
import struct
import subprocess
import sys
import threading
import time
from typing import Final
from unittest import case
import urllib.request

import aiohttp
import voluptuous as vol
import websocket

from homeassistant.components import climate
from homeassistant.components.climate import FAN_HIGH, FAN_LOW, FAN_MEDIUM
from homeassistant.components.light import (
ATTR_BRIGHTNESS,
ATTR_COLOR_TEMP,
Expand All @@ -38,7 +28,25 @@
ATTR_RGB_COLOR,
ATTR_RGBW_COLOR,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
CONF_FRIENDLY_NAME,
CONF_REGION,
CONF_URL,
STATE_OFF,
STATE_ON,
Platform,
)
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import discovery
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.dispatcher import dispatcher_send
from homeassistant.helpers.entity import DeviceInfo, Entity
from homeassistant.helpers.event import async_track_point_in_utc_time
from homeassistant.helpers.typing import ConfigType
import homeassistant.util.color as color_util
from homeassistant.util.dt import utcnow

from .const import (
AI_TYPES,
BINARY_SENSOR_TYPES,
Expand All @@ -47,16 +55,17 @@
CONF_AI_INCLUDE_ITEMS,
CONF_EXCLUDE_AGTS,
CONF_EXCLUDE_ITEMS,
CONF_LIFESMART_APPKEY,
CONF_LIFESMART_APPTOKEN,
CONF_LIFESMART_USERID,
CONF_LIFESMART_USERPASSWORD,
CONF_LIFESMART_USERTOKEN,
COVER_TYPES,
DEVICE_ID_KEY,
DEVICE_NAME_KEY,
DEVICE_TYPE_KEY,
DEVICE_WITHOUT_IDXNAME,
DOMAIN,
CONF_LIFESMART_APPKEY,
EV_SENSOR_TYPES,
GAS_SENSOR_TYPES,
GENERIC_CONTROLLER_TYPES,
Expand All @@ -78,9 +87,7 @@
SUPPORTED_SWTICH_TYPES,
UPDATE_LISTENER,
)

import voluptuous as vol
import sys
from .lifesmart_client import LifeSmartClient

sys.setrecursionlimit(100000)

Expand All @@ -93,9 +100,9 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry):

app_key = config_entry.data.get(CONF_LIFESMART_APPKEY)
app_token = config_entry.data.get(CONF_LIFESMART_APPTOKEN)
user_token = config_entry.data.get(CONF_LIFESMART_USERTOKEN)
user_id = config_entry.data.get(CONF_LIFESMART_USERID)
baseurl = config_entry.data.get(CONF_URL)
user_password = config_entry.data.get(CONF_LIFESMART_USERPASSWORD)
region = config_entry.data.get(CONF_REGION)
exclude_devices = config_entry.data.get(CONF_EXCLUDE_ITEMS)
exclude_hubs = config_entry.data.get(CONF_EXCLUDE_AGTS)
ai_include_hubs = config_entry.data.get(CONF_AI_INCLUDE_AGTS)
Expand All @@ -115,13 +122,15 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry):
update_listener = config_entry.add_update_listener(_async_update_listener)

lifesmart_client = LifeSmartClient(
baseurl,
region,
app_key,
app_token,
user_token,
user_id,
user_password,
)

await lifesmart_client.login_async()

devices = await lifesmart_client.get_all_device_async()

hass.data[DOMAIN][config_entry.entry_id] = {
Expand Down Expand Up @@ -195,9 +204,9 @@ async def data_update_handler(msg):
attrs = hass.states.get(entity_id).attributes
hass.states.set(entity_id, data["val"], attrs)
elif device_type in SPOT_TYPES or device_type in LIGHT_SWITCH_TYPES:
#attrs = dict(hass.states.get(entity_id).attributes)
# attrs = dict(hass.states.get(entity_id).attributes)
_LOGGER.debug("websocket_light_msg: %s ", str(msg))
#_LOGGER.debug("websocket_light_attrs: %s", str(attrs))
# _LOGGER.debug("websocket_light_attrs: %s", str(attrs))
value = data["val"]
idx = sub_device_key

Expand Down
17 changes: 8 additions & 9 deletions custom_components/lifesmart/binary_sensor.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
"""Support for LifeSmart binary sensors."""
import datetime
import logging

from homeassistant.components.binary_sensor import (
ENTITY_ID_FORMAT,
BinarySensorDeviceClass,
BinarySensorEntity,
)
from homeassistant.const import STATE_OFF, STATE_ON
from homeassistant.helpers.dispatcher import async_dispatcher_connect

from homeassistant.helpers.entity import DeviceInfo

from . import LifeSmartDevice, generate_entity_id
from .const import (
BINARY_SENSOR_TYPES,
DEVICE_DATA_KEY,
Expand All @@ -20,13 +26,6 @@
MANUFACTURER,
MOTION_SENSOR_TYPES,
)
from homeassistant.components.binary_sensor import (
BinarySensorDeviceClass,
BinarySensorEntity,
ENTITY_ID_FORMAT,
)

from . import LifeSmartDevice, generate_entity_id

_LOGGER = logging.getLogger(__name__)

Expand Down
54 changes: 27 additions & 27 deletions custom_components/lifesmart/climate.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
"""Support for the LifeSmart climate devices."""
import logging
import time

from homeassistant.components.climate import ENTITY_ID_FORMAT, ClimateEntity
from homeassistant.components.climate.const import (
ClimateEntityFeature,
HVACMode,
FAN_HIGH,
FAN_LOW,
FAN_MEDIUM,
ClimateEntityFeature,
HVACMode,
)

from homeassistant.const import (
UnitOfTemperature,
PRECISION_WHOLE,
)
from homeassistant.const import PRECISION_WHOLE, UnitOfTemperature

from . import LifeSmartDevice

_LOGGER = logging.getLogger(__name__)
DEVICE_TYPE = "climate"

LIFESMART_STATE_LIST = [HVACMode.OFF,
HVACMode.AUTO,
HVACMode.FAN_ONLY,
HVACMode.COOL,
HVACMode.HEAT,
HVACMode.DRY,
]
LIFESMART_STATE_LIST = [
HVACMode.OFF,
HVACMode.AUTO,
HVACMode.FAN_ONLY,
HVACMode.COOL,
HVACMode.HEAT,
HVACMode.DRY,
]

LIFESMART_STATE_LIST2 = [HVACMode.OFF,
HVACMode.HEAT,]
LIFESMART_STATE_LIST2 = [
HVACMode.OFF,
HVACMode.HEAT,
]

FAN_MODES = [FAN_LOW, FAN_MEDIUM, FAN_HIGH]
GET_FAN_SPEED = {FAN_LOW: 15, FAN_MEDIUM: 45, FAN_HIGH: 76}
Expand Down Expand Up @@ -162,22 +162,22 @@ async def async_set_hvac_mode(self, hvac_mode):
await super().async_lifesmart_epset(
"0xCE", LIFESMART_STATE_LIST.index(hvac_mode), "MODE"
)
elif hvac_mode == HVACMode.OFF:
await super().async_lifesmart_epset("0x80", 0, "P1")
time.sleep(1)
await super().async_lifesmart_epset("0x80", 0, "P2")
return
elif await super().async_lifesmart_epset("0x81", 1, "P1") == 0:
time.sleep(2)
else:
if hvac_mode == HVACMode.OFF:
await super().async_lifesmart_epset("0x80", 0, "P1")
time.sleep(1)
await super().async_lifesmart_epset("0x80", 0, "P2")
return
else:
if await super().async_lifesmart_epset("0x81", 1, "P1") == 0:
time.sleep(2)
else:
return
return

@property
def supported_features(self):
"""Return the list of supported features."""
if self._devtype in AIR_TYPES:
return ClimateEntityFeature.TARGET_TEMPERATURE | ClimateEntityFeature.FAN_MODE
return (
ClimateEntityFeature.TARGET_TEMPERATURE | ClimateEntityFeature.FAN_MODE
)
else:
return ClimateEntityFeature.TARGET_TEMPERATURE
Loading

0 comments on commit 012d65c

Please sign in to comment.