Skip to content

Commit

Permalink
Merge pull request #8 from jfarmer08/Dev
Browse files Browse the repository at this point in the history
Fix issue with bulb not report the correct state add limited support for other Sengled products.
  • Loading branch information
jfarmer08 authored Aug 14, 2020
2 parents af66c49 + 8f7406f commit 1e13bb2
Show file tree
Hide file tree
Showing 6 changed files with 974 additions and 81 deletions.
5 changes: 4 additions & 1 deletion custom_components/sengledapi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

DOMAIN = "sengledapi"
CONF_COUNTRY = "country"
CONF_TYPE = "wifi"

CONFIG_SCHEMA = vol.Schema(
{
Expand All @@ -22,6 +23,7 @@
vol.Required(CONF_USERNAME): cv.string,
vol.Required(CONF_PASSWORD): cv.string,
vol.Required(CONF_COUNTRY): cv.string,
vol.Optional(CONF_TYPE, default=False): cv.boolean,
}
)
},
Expand All @@ -36,7 +38,7 @@ async def async_setup(hass, config):
-------------------------------------------------------------------
Sengled Bulb Home Assistant Integration
Version: v0.1-beta.6
Version: v0.1-beta.9
This is a custom integration
If you have any issues with this you need to open an issue here:
Expand All @@ -48,6 +50,7 @@ async def async_setup(hass, config):
config[DOMAIN].get(CONF_USERNAME),
config[DOMAIN].get(CONF_PASSWORD),
config[DOMAIN].get(CONF_COUNTRY),
config[DOMAIN].get(CONF_TYPE),
)
await sengledapi_account.async_init()

Expand Down
24 changes: 16 additions & 8 deletions custom_components/sengledapi/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from .sengledapi.sengledapi import SengledApi
from . import DOMAIN


import voluptuous as vol

import homeassistant.helpers.config_validation as cv
Expand Down Expand Up @@ -50,20 +49,24 @@ def __init__(self, light):
@property
def name(self):
"""Return the display name of this light."""
_LOGGER.debug("SengledApi Light " + self._name +" "+ str(self._state) +" "+ str(self._brightness) +" "+ str(self._avaliable) +" "+ str(self._device_mac) +" "+ str(self._device_model))
return self._name

@property
def unique_id(self):
_LOGGER.debug("SengledApi Light " + self._name +" "+ str(self._state) +" "+ str(self._brightness) +" "+ str(self._avaliable) +" "+ str(self._device_mac) +" "+ str(self._device_model))
return self._device_mac

@property
def available(self):
"""Return the connection status of this light"""
_LOGGER.debug("SengledApi Light " + self._name +" "+ str(self._state) +" "+ str(self._brightness) +" "+ str(self._avaliable) +" "+ str(self._device_mac) +" "+ str(self._device_model))
return self._avaliable

@property
def device_state_attributes(self):
"""Return device attributes of the entity."""
_LOGGER.debug("SengledApi Light " + self._name +" "+ str(self._state) +" "+ str(self._brightness) +" "+ str(self._avaliable) +" "+ str(self._device_mac) +" "+ str(self._device_model))
return {
ATTR_ATTRIBUTION: ATTRIBUTION,
"state": self._state,
Expand All @@ -79,11 +82,13 @@ def brightness(self):
This method is optional. Removing it indicates to Home Assistant
that brightness is not supported for this light.
"""
_LOGGER.debug("SengledApi Light " + self._name +" "+ str(self._state) +" "+ str(self._brightness) +" "+ str(self._avaliable) +" "+ str(self._device_mac) +" "+ str(self._device_model))
return self._brightness

@property
def is_on(self):
"""Return true if light is on."""
_LOGGER.debug("SengledApi Light " + self._name +" "+ str(self._state) +" "+ str(self._brightness) +" "+ str(self._avaliable) +" "+ str(self._device_mac) +" "+ str(self._device_model))
return self._state

@property
Expand All @@ -92,18 +97,21 @@ def supported_features(self):

async def async_turn_on(self, **kwargs):
"""Instruct the light to turn on. """
self._light._brightness = kwargs.get(ATTR_BRIGHTNESS)
await self._light.async_turn_on()
if self._device_model != "wificolora19":
self._light._brightness = kwargs.get(ATTR_BRIGHTNESS)
await self._light.async_turn_on()

async def async_turn_off(self, **kwargs):
"""Instruct the light to turn off."""
await self._light.async_turn_off()
if self._device_model != "wificolora19":
await self._light.async_turn_off()

async def async_update(self):
"""Fetch new state data for this light.
This is the only method that should fetch new data for Home Assistant.
"""
await self._light.async_update()
self._state = self._light.is_on()
self._avaliable = self._light._avaliable
self._brightness = self._light._brightness
if self._device_model != "wificolora19":
await self._light.async_update()
self._state = self._light.is_on()
self._avaliable = self._light._avaliable
self._brightness = self._light._brightness
229 changes: 200 additions & 29 deletions custom_components/sengledapi/sengledapi/sengled_bulb.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import asyncio
import logging

from .sengled_request import SengledRequest

_LOGGER = logging.getLogger(__name__)
_LOGGER.debug("SengledApi: sengled_bulb class")

Expand All @@ -14,7 +16,7 @@ def __init__(
state,
device_model,
brightness,
accesstoken,
jsession_id,
country,
):
_LOGGER.debug("SengledApi: Light " + friendly_name + " initializing.")
Expand All @@ -27,50 +29,44 @@ def __init__(
self._just_changed_state = False
self._device_model = device_model
self._brightness = int(brightness)
self._accesstoken = accesstoken
self._jsession_id = jsession_id
self._country = country

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

if self._brightness is not None:
url = (
"https://"
+ self._country
+ "-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}
payload = {
"deviceUuid": self._device_mac,
"brightness": brightness
}

else:
url = (
"https://"
+ self._country
+ "-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"}

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

loop.create_task(SengledRequest(url, payload).async_get_response(self._jsession_id))
self._state = True
self._just_changed_state = True

async def async_turn_off(self):
_LOGGER.debug("Light " + self._friendly_name + " turning off.")
url = (
"https://"
+ self._country
+ "-elements.cloud.sengled.com/zigbee/device/deviceSetOnOff.json"
)
_LOGGER.debug("Light " + self._friendly_name + " " + self._device_mac + " turning off.")
url =('https://'+ self._country+ '-elements.cloud.sengled.com/zigbee/device/deviceSetOnOff.json')
payload = {
"deviceUuid": self._device_mac,
"onoff": "0"
}

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

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

self._state = False
self._just_changed_state = True
Expand All @@ -79,16 +75,103 @@ def is_on(self):
return self._state

async def async_update(self):
_LOGGER.debug("Light " + self._friendly_name + " updating.")
_LOGGER.debug("Sengled Bulb " + self._friendly_name + " " + self._device_mac + " updating.")
if self._just_changed_state:
self._just_changed_state = False
else:
url = (
"https://element.cloud.sengled.com/zigbee/device/getDeviceDetails.json"
)
url = ('https://element.cloud.sengled.com/zigbee/device/getDeviceDetails.json')
payload = {}

data = await SengledRequest(url, payload).async_get_response(self._jsession_id)
_LOGGER.debug("Light " + self._friendly_name + " updating.")
for item in data["deviceInfos"]:
for items in item["lampInfos"]:
if items['deviceUuid'] == self._device_mac:
self._friendly_name = items["attributes"]["name"]
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
)

class SengledColorBulb:
def __init__(
self,
api,
device_mac,
friendly_name,
state,
device_model,
brightness,
jsession_id,
country,
):
_LOGGER.debug("SengledApi: Light " + 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._brightness = int(brightness)
self._jsession_id = jsession_id
self._country = country

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

if self._brightness is not None:
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://'+ 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(SengledRequest(url, payload).async_get_response(self._jsession_id))
self._state = True
self._just_changed_state = True

async def async_turn_off(self):
_LOGGER.debug("Light " + self._friendly_name + " " + self._device_mac + " 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(SengledRequest(url, payload).async_get_response(self._jsession_id))

self._state = False
self._just_changed_state = True

def is_on(self):
return self._state

async def async_update(self):
_LOGGER.debug("Light " + self._friendly_name + " " + self._device_mac + " 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)

data = await SengledRequest(url, payload).async_get_response(self._jsession_id)
_LOGGER.debug("Light " + self._friendly_name + " updating.")
for item in data["deviceInfos"]:
for items in item["lampInfos"]:
Expand All @@ -100,3 +183,91 @@ async def async_update(self):
self._avaliable = (
False if int(items["attributes"]["isOnline"]) == 0 else True
)

class SengledWifiColorBulb:
def __init__(
self,
api,
device_mac,
friendly_name,
state,
device_model,
brightness,
jsession_id,
country,
):
_LOGGER.debug("SengledApi: Wifi Color Bulb " + 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._brightness = int(brightness)
self._jsession_id = jsession_id
self._country = country

async def async_turn_on(self):
_LOGGER.debug("SengledApi: Wifi Color Bulb " + self._friendly_name + " " + self._device_mac + " turning on.")

if self._brightness is not None:
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://'+ 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(SengledRequest(url, payload).async_get_response(self._jsession_id))
self._state = True
self._just_changed_state = True

async def async_turn_off(self):
_LOGGER.debug("SengledApi: Wifi Color Bulb " + self._friendly_name + " " + self._device_mac + " 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(SengledRequest(url, payload).async_get_response(self._jsession_id))

self._state = False
self._just_changed_state = True

def is_on(self):
return self._state

async def async_update(self):
_LOGGER.debug("Light " + self._friendly_name + " " + self._device_mac + " 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 SengledRequest(url, payload).async_get_response(self._jsession_id)
_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 = 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
)
Loading

0 comments on commit 1e13bb2

Please sign in to comment.