Skip to content

Commit

Permalink
Add support for Country
Browse files Browse the repository at this point in the history
Fix issues with icon not showing in HA and added support for Country
  • Loading branch information
jfarmer08 committed Jun 18, 2020
1 parent 6ebceb4 commit e8682e9
Show file tree
Hide file tree
Showing 8 changed files with 227 additions and 47 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ This is a custom component to allow control of Sengled Bulbs in Homeassistant us
* On Hassbian the final location will be `/home/homeassistant/.homeassistant/custom_components/sengledapi`
3. Restart HA

## **SengledApi** NOTE: Configuration Changed Please update.
Country Code example:
us
au


## Configuration

Add the following to your configuration file.
Expand All @@ -35,6 +41,7 @@ Add the following to your configuration file.
sengledapi:
username: <email for sengled>
password: <password for sengled>
country: <country>
```

## Usage
Expand Down
9 changes: 7 additions & 2 deletions custom_components/sengledapi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@
_LOGGER = logging.getLogger(__name__)

DOMAIN = "sengledapi"
CONF_COUNTRY = "country"

CONFIG_SCHEMA = vol.Schema(
{
DOMAIN: vol.Schema(
{
vol.Required(CONF_USERNAME): cv.string,
vol.Required(CONF_PASSWORD): cv.string,
vol.Required(CONF_COUNTRY): cv.string,
}
)
},
Expand All @@ -33,7 +36,7 @@ async def async_setup(hass, config):
-------------------------------------------------------------------
Sengled Bulb Home Assistant Integration

Version: v0.1-beta.4
Version: v0.1-beta.5
This is a custom integration
If you have any issues with this you need to open an issue here:

Expand All @@ -42,7 +45,9 @@ async def async_setup(hass, config):
_LOGGER.debug("""Creating new SengledApi component""")

sengledapi_account = SengledApi(
config[DOMAIN].get(CONF_USERNAME), config[DOMAIN].get(CONF_PASSWORD)
config[DOMAIN].get(CONF_USERNAME),
config[DOMAIN].get(CONF_PASSWORD),
config[DOMAIN].get(CONF_COUNTRY),
)
await sengledapi_account.async_init()

Expand Down
6 changes: 1 addition & 5 deletions custom_components/sengledapi/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,7 @@ def supported_features(self):
return SUPPORT_BRIGHTNESS

async def async_turn_on(self, **kwargs):
"""Instruct the light to turn on.

You can skip the brightness part if your light does not support
brightness control.
"""
"""Instruct the light to turn on. """
self._light._brightness = kwargs.get(ATTR_BRIGHTNESS)
await self._light.async_turn_on()

Expand Down
36 changes: 23 additions & 13 deletions custom_components/sengledapi/sengledapi/sengled_bulb.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ def __init__(
device_model,
brightness,
accesstoken,
country,
):
_LOGGER.debug(
"SengledApi: Light " + friendly_name + " initializing." + str(accesstoken)
)
_LOGGER.debug("SengledApi: Light " + friendly_name + " initializing.")

self._api = api
self._device_mac = device_mac
Expand All @@ -27,21 +26,30 @@ def __init__(
self._avaliable = True
self._just_changed_state = False
self._device_model = device_model
self._brightness = brightness
self._brightness = int(brightness)
self._accesstoken = accesstoken
self._country = country

async def async_turn_on(self):
_LOGGER.debug("Farmer: Light " + self._friendly_name + " turning on.")
_LOGGER.debug("Light " + self._friendly_name + " turning on.")
if self._brightness is not None:
url = "https://us-elements.cloud.sengled.com/zigbee/device/deviceSetBrightness.json"
url = (
"https://"
+ self._country
+ "-elements.cloud.sengled.com/zigbee/device/deviceSetBrightness.json"
)

if self._brightness:
brightness = self._brightness

payload = {"deviceUuid": self._device_mac, "brightness": brightness}

else:
url = "https://us-elements.cloud.sengled.com/zigbee/device/deviceSetOnOff.json"
url = (
"https://"
+ self._country
+ "-elements.cloud.sengled.com/zigbee/device/deviceSetOnOff.json"
)

payload = {"deviceUuid": self._device_mac, "onoff": "1"}

Expand All @@ -53,7 +61,11 @@ async def async_turn_on(self):

async def async_turn_off(self):
_LOGGER.debug("Light " + self._friendly_name + " turning off.")
url = "https://us-elements.cloud.sengled.com/zigbee/device/deviceSetOnOff.json"
url = (
"https://"
+ self._country
+ "-elements.cloud.sengled.com/zigbee/device/deviceSetOnOff.json"
)

payload = {"deviceUuid": self._device_mac, "onoff": "0"}

Expand All @@ -74,19 +86,17 @@ async def async_update(self):
url = (
"https://element.cloud.sengled.com/zigbee/device/getDeviceDetails.json"
)
# url = "https://element.cloud.sengled.com/zigbee/room/getUserRoomsDetail.json"
payload = {}

payload = {}
data = await self._api.async_do_request(url, payload, self._accesstoken)
_LOGGER.debug("Light " + self._friendly_name + " updating." + str(data))
_LOGGER.debug("Light " + self._friendly_name + " updating.")
for item in data["deviceInfos"]:
for items in item["lampInfos"]:
self._friendly_name = items["attributes"]["name"]
self._brightness = items["attributes"]["brightness"]
self._brightness = int(items["attributes"]["brightness"])
self._state = (
True if int(items["attributes"]["onoff"]) == 1 else False
)
self._avaliable = (
False if int(items["attributes"]["isOnline"]) == 0 else True
)

4 changes: 3 additions & 1 deletion custom_components/sengledapi/sengledapi/sengled_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ def get_response(self, access_token):
"Cookie": access_token,
"Connection": "keep-alive",
}
_LOGGER.debug(
"SengledApi: get_response Sengled Request getting response NON-async."
)
r = requests.post(self._url, headers=self._header, data=self._payload)
data = r.json()
return data
Expand All @@ -44,7 +47,6 @@ async def async_get_response(self, access_token):
}
_LOGGER.debug(
"SengledApi: async_get_response Sengled Request getting response async."
+ str(access_token)
)
async with aiohttp.ClientSession() as session:
sslcontext = ssl.create_default_context(cafile=certifi.where())
Expand Down
80 changes: 80 additions & 0 deletions custom_components/sengledapi/sengledapi/sengled_switch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import asyncio
import logging

_LOGGER = logging.getLogger(__name__)


class SengledSwitch:
def __init__(
self, api, device_mac, friendly_name, state, device_model, accesstoken, country,
):
_LOGGER.debug("SengledApi: Switch " + friendly_name + " initializing.")

self._api = api
self._device_mac = device_mac
self._friendly_name = friendly_name
self._state = state
self._avaliable = True
self._just_changed_state = False
self._device_model = device_model
self._accesstoken = accesstoken
self._country = country

async def async_turn_on(self):
_LOGGER.debug("Switch " + self._friendly_name + " turning on.")

url = (
"https://"
+ self._country
+ "-elements.cloud.sengled.com/zigbee/device/deviceSetOnOff.json"
)

payload = {"deviceUuid": self._device_mac, "onoff": "1"}

loop = asyncio.get_running_loop()
loop.create_task(self._api.async_do_request(url, payload, self._accesstoken))

self._state = True
self._just_changed_state = True

async def async_turn_off(self):
_LOGGER.debug("Switch " + self._friendly_name + " turning off.")

url = (
"https://"
+ self._country
+ "-elements.cloud.sengled.com/zigbee/device/deviceSetOnOff.json"
)

payload = {"deviceUuid": self._device_mac, "onoff": "0"}

loop = asyncio.get_running_loop()
loop.create_task(self._api.async_do_request(url, payload, self._accesstoken))

self._state = False
self._just_changed_state = True

def is_on(self):
return self._state

async def async_update(self):
_LOGGER.debug("Switch " + self._friendly_name + " updating.")
if self._just_changed_state:
self._just_changed_state = False
else:
url = (
"https://element.cloud.sengled.com/zigbee/device/getDeviceDetails.json"
)

payload = {}
data = await self._api.async_do_request(url, payload, self._accesstoken)
_LOGGER.debug("Switch " + self._friendly_name + " updating.")
for item in data["deviceInfos"]:
for items in item["lampInfos"]:
self._friendly_name = items["attributes"]["name"]
self._state = (
True if int(items["attributes"]["onoff"]) == 1 else False
)
self._avaliable = (
False if int(items["attributes"]["isOnline"]) == 0 else True
)
69 changes: 48 additions & 21 deletions custom_components/sengledapi/sengledapi/sengledapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,20 @@

from .sengled_request import SengledRequest
from .sengled_bulb import SengledBulb
from .sengled_switch import SengledSwitch
from .sengledapi_exceptions import SengledApiAccessToken


class SengledApi:
def __init__(self, user_name, password):
def __init__(self, user_name, password, country):
_LOGGER.debug("Sengled Api initializing.")
self._user_name = user_name
self._password = password
self._device_id = "740447d2-eb6e-11e9-81b4-2a2ae2dbcce4"
self._in_error_state = False
self._invalid_access_tokens = []
self._access_token = None
self._country = country
# Create device array
self._all_devices = []

Expand All @@ -25,7 +28,6 @@ async def async_init(self):
self._access_token = await self.async_login(
self._user_name, self._password, self._device_id
)
_LOGGER.debug("sengledapi async_init" + self._access_token)

async def async_login(self, username, password, device_id):
_LOGGER.debug("Sengled Api log in async.")
Expand All @@ -41,7 +43,6 @@ async def async_login(self, username, password, device_id):

try:
access_token = "JSESSIONID=" + data["jsessionid"]
_LOGGER.debug("Sengled Api accessToken " + str(access_token))
self._access_token = access_token
return access_token
except:
Expand Down Expand Up @@ -75,48 +76,74 @@ async def async_list_bulbs(self):
for device in await self.async_get_devices():
_LOGGER.debug(device)
if "lampInfos" in device:
for device in device["lampInfos"]:
if(device["attributes"]["productCode"] == "E11-G13"):
for light in device["lampInfos"]:
if light["attributes"]["productCode"] == "E11-G13":
bulbs.append(
SengledBulb(
self,
device["deviceUuid"],
device["attributes"]["name"],
("on" if device["attributes"]["onoff"] == 1 else "off"),
device["attributes"]["productCode"],
device["attributes"]["brightness"],
light["deviceUuid"],
light["attributes"]["name"],
("on" if light["attributes"]["onoff"] == 1 else "off"),
light["attributes"]["productCode"],
light["attributes"]["brightness"],
self._access_token,
self._country,
)
)
if(device["attributes"]["productCode"] == "E11-G23"):
if light["attributes"]["productCode"] == "E11-G23":
bulbs.append(
SengledBulb(
self,
device["deviceUuid"],
device["attributes"]["name"],
("on" if device["attributes"]["onoff"] == 1 else "off"),
device["attributes"]["productCode"],
device["attributes"]["brightness"],
light["deviceUuid"],
light["attributes"]["name"],
("on" if light["attributes"]["onoff"] == 1 else "off"),
light["attributes"]["productCode"],
light["attributes"]["brightness"],
self._access_token,
self._country,
)
)
if(device["attributes"]["productCode"] == "E1A-AC2"):#Light Currently only one i have
if (
light["attributes"]["productCode"] == "E1A-AC2"
): # Light Currently only one i have
bulbs.append(
SengledBulb(
self,
light["deviceUuid"],
light["attributes"]["name"],
("on" if light["attributes"]["onoff"] == 1 else "off"),
light["attributes"]["productCode"],
light["attributes"]["brightness"],
self._access_token,
self._country,
)
)

return bulbs

async def async_list_switch(self):
_LOGGER.debug("Sengled Api listing switches.")
switch = []
# This is my room list
for device in await self.async_get_devices():
_LOGGER.debug(device)
if "lampInfos" in device:
for switch in device["lampInfos"]:
if switch["attributes"]["productCode"] == "E1E-G7F":
switch.append(
SengledSwitch(
self,
device["deviceUuid"],
device["attributes"]["name"],
("on" if device["attributes"]["onoff"] == 1 else "off"),
device["attributes"]["productCode"],
device["attributes"]["brightness"],
self._access_token,
self._country,
)
)

return bulbs
return switch

async def async_do_request(self, url, payload, accesstoken):
_LOGGER.debug("async_do_request - Sengled Api doing request.")
try:
return await SengledRequest(url, payload).async_get_response(accesstoken)
except:
Expand Down
Loading

0 comments on commit e8682e9

Please sign in to comment.