Skip to content

Commit

Permalink
Merge pull request #21 from jfarmer08/Dev
Browse files Browse the repository at this point in the history
Support for Wifi Lights
  • Loading branch information
jfarmer08 committed Aug 27, 2020
2 parents 6b98b46 + 417d9bc commit 9dbe881
Show file tree
Hide file tree
Showing 9 changed files with 209 additions and 151 deletions.
54 changes: 24 additions & 30 deletions custom_components/sengledapi/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
)

# Add to support quicker update time. Is this to Fast?
SCAN_INTERVAL = timedelta(seconds=5)
SCAN_INTERVAL = timedelta(seconds=30)

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -84,37 +84,34 @@ def device_state_attributes(self):
"state": self._state,
"available": self._avaliable,
"device model": self._device_model,
"device rssi": self._device_rssi,
"rssi": self._device_rssi,
"mac": self._device_mac,
"brightness": self._brightness,
"colorTemp": self._color_temperature,
"color": self._color,
}

@property
def color_temp(self):
"""Return the color_temp of the light."""
color_temp = int(self._color_temperature)
if color_temp is None:
return None
return colorutil.color_temperature_kelvin_to_mired(color_temp)
return colorutil.color_temperature_kelvin_to_mired(self._color_temperature)

@property
def hs_color(self):
"""Return the hs_color of the light."""
_LOGGER.debug("FARMER::::::: %s", str(self._color))
#_LOGGER.debug("FARMER::::::: %s", str(self._color))
a, b, c = self._color.split(":")
_LOGGER.debug(a)
_LOGGER.debug(b)
_LOGGER.debug(c)
return colorutil.color_RGB_to_hs(int(a), int(b), int(c))

# @property
# def min_mireds(self):
# """Return color temperature min mireds."""
# return colorutil.color_temperature_kelvin_to_mired(2000)
@property
def min_mireds(self):
"""Return color temperature min mireds."""
return colorutil.color_temperature_kelvin_to_mired(6500)

# @property
## def max_mireds(self):
# """Return color temperature max mireds."""
# return colorutil.color_temperature_kelvin_to_mired(6500)
@property
def max_mireds(self):
"""Return color temperature max mireds."""
return colorutil.color_temperature_kelvin_to_mired(2000)

@property
def brightness(self):
Expand All @@ -137,27 +134,24 @@ def supported_features(self):
features = SUPPORT_BRIGHTNESS
if self._device_model == "wifia19":
features = SUPPORT_BRIGHTNESS
if self._device_model == "E11-N1EA":
features = SUPPORT_BRIGHTNESS | SUPPORT_COLOR_TEMP
return features

async def async_turn_on(self, **kwargs):
"""Instruct the light to turn on. """
"""Turn on or control the light."""
if (
ATTR_BRIGHTNESS not in kwargs
and ATTR_HS_COLOR not in kwargs
and ATTR_COLOR_TEMP not in kwargs
):
if (ATTR_BRIGHTNESS not in kwargs and ATTR_HS_COLOR not in kwargs and ATTR_COLOR_TEMP not in kwargs):
await self._light.async_turn_on()
if ATTR_BRIGHTNESS in kwargs:
await self._light.async_turn_on()
await self._light.set_brightness(kwargs[ATTR_BRIGHTNESS])
await self._light.async_set_brightness(kwargs[ATTR_BRIGHTNESS])
if ATTR_HS_COLOR in kwargs:
self._light.set_color(kwargs[ATTR_HS_COLOR])
hs = kwargs.get(ATTR_HS_COLOR)
color = colorutil.color_hs_to_RGB(hs[0], hs[1])
await self._light.async_set_color(color)
if ATTR_COLOR_TEMP in kwargs:
color_temp = colorutil.color_temperature_mired_to_kelvin(
kwargs[ATTR_COLOR_TEMP]
)
await self._light.set_color_temp(color_temp)
color_temp = colorutil.color_temperature_mired_to_kelvin(kwargs[ATTR_COLOR_TEMP])
await self._light.async_color_temperature(color_temp)

async def async_turn_off(self, **kwargs):
"""Instruct the light to turn off."""
Expand Down
46 changes: 25 additions & 21 deletions custom_components/sengledapi/sengledapi/bulbs/sengled_bulb.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,39 +41,44 @@ async def async_turn_on(self):
_LOGGER.debug(
"SengledApi: Bulb %s %s turning on.", self._friendly_name, self._device_mac
)
self._just_changed_state = True
url = (
"https://"
+ self._country
+ "-elements.cloud.sengled.com/zigbee/device/deviceSetOnOff.json"
)

if self._brightness is not None:
url = (
"https://"
+ self._country
+ "-elements.cloud.sengled.com/zigbee/device/deviceSetBrightness.json"
)
payload = {"deviceUuid": self._device_mac, "onoff": "1"}

if self._brightness:
brightness = self._brightness
loop = asyncio.get_running_loop()
loop.create_task(self._api.async_do_request(url, payload, self._jsession_id))

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

else:
url = (
"https://"
+ self._country
+ "-elements.cloud.sengled.com/zigbee/device/deviceSetOnOff.json"
)
async def async_set_brightness(self, brightness):
_LOGGER.debug(
"Bulb %s %s setting brightness.", self._friendly_name, self._device_mac
)
self._state = True
self._just_changed_state = True

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

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

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

self._state = True
self._just_changed_state = True

async def async_turn_off(self):
_LOGGER.debug(
"SengledApi: Bulb %s %s turning off.", self._friendly_name, self._device_mac
)

url = (
"https://"
+ self._country
Expand All @@ -82,7 +87,6 @@ async def async_turn_off(self):
payload = {"deviceUuid": self._device_mac, "onoff": "0"}

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

self._state = False
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,37 +42,42 @@ async def async_turn_on(self):
"SengledApi: Bulb %s %s turning on.", self._friendly_name, self._device_mac
)

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"
)
url = (
"https://"
+ self._country
+ "-elements.cloud.sengled.com/zigbee/device/deviceSetOnOff.json"
)

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

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

self._state = True
self._just_changed_state = True

async def async_set_brightness(self, brightness):
_LOGGER.debug(
"Bulb %s %s setting brightness.", self._friendly_name, self._device_mac
)
self._state = True
self._just_changed_state = True
url = (
"https://"
+ self._country
+ "-elements.cloud.sengled.com/zigbee/device/deviceSetBrightness.json"
)

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

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

async def async_turn_off(self):
_LOGGER.debug(
"SengledApi: Bulb %s %s turning off.", self._friendly_name, self._device_mac
)
self._just_changed_state = True
url = (
"https://"
+ self._country
Expand Down
59 changes: 41 additions & 18 deletions custom_components/sengledapi/sengledapi/bulbs/sengled_color_bulb.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,35 +43,58 @@ def __init__(
async def async_turn_on(self):
_LOGGER.debug("Bulb %s %s turning on.", self._friendly_name, self._device_mac)

if self._brightness is not None:
url = (
"https://"
+ self._country
+ "-elements.cloud.sengled.com/zigbee/device/deviceSetBrightness.json"
)

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

payload = {"deviceUuid": self._device_mac, "brightness": brightness}
payload = {"deviceUuid": self._device_mac, "onoff": "1"}
self._state = True
self._just_changed_state = True
loop = asyncio.get_running_loop()
loop.create_task(self._api.async_do_request(url, payload, self._jsession_id))

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

payload = {"deviceUuid": self._device_mac, "onoff": "1"}
async def async_set_brightness(self, brightness):
_LOGGER.debug(
"Bulb %s %s setting brightness.", self._friendly_name, self._device_mac
)
self._just_changed_state = True
url = (
"https://"
+ self._country
+ "-elements.cloud.sengled.com/zigbee/device/deviceSetBrightness.json"
)

payload = {"deviceUuid": self._device_mac, "brightness": brightness}
self._state = True
self._just_changed_state = True
loop = asyncio.get_running_loop()
# loop.create_task(SengledRequest(url, payload).async_get_response(self._jsession_id))
loop.create_task(self._api.async_do_request(url, payload, self._jsession_id))

async def async_color_temperature(self, colorTemperature):
_LOGGER.debug(
"Bulb %s %s changing color.", self._friendly_name, self._device_mac
)
self._just_changed_state = True
url = (
"https://"
+ self._country
+ "-elements.cloud.sengled.com/zigbee/device/deviceSetColorTemperature.json"
)

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

self._state = True
self._just_changed_state = True

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

async def async_turn_off(self):
_LOGGER.debug("Bulb %s %s turning on.", self._friendly_name, self._device_mac)
self._just_changed_state = True
url = (
"https://"
+ self._country
Expand Down
2 changes: 1 addition & 1 deletion custom_components/sengledapi/sengledapi/sengled_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ async def async_get_response(self, jsession_id):
self._url, headers=self._header, data=self._payload, ssl=sslcontext
) as resp:
data = await resp.json()
_LOGGER.debug("SengledApi: data from async_get_response " + str(data))
# _LOGGER.debug("SengledApi: data from async_get_response " + str(data))
return data

########################Login#####################################
Expand Down
38 changes: 32 additions & 6 deletions custom_components/sengledapi/sengledapi/sengled_wifi_bulb.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,12 @@ def __init__(

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

data = {
"dn": self._device_mac,
"type": "switch",
Expand All @@ -64,9 +63,34 @@ async def async_turn_on(self):
self._state = True
self._just_changed_state = True

async def async_set_brightness(self, brightness):
brightness_precentage = round((brightness / 255) * 100)

_LOGGER.debug(
"SengledApi: Wifi Color Bulb "
+ self._friendly_name
+ " "
+ self._device_mac
+ " setting Brightness "
+ str(brightness_precentage)
)

data_brightness = {
"dn": self._device_mac,
"type": "brightness",
"value": str(brightness_precentage),
"time": int(time.time() * 1000),
}
self._state = True
self._just_changed_state = True
self._api._publish_mqtt(
"wifielement/{}/update".format(self._device_mac),
json.dumps(data_brightness),
)

async def async_turn_off(self):
_LOGGER.info(
"SengledApi: Wifi Bulb "
_LOGGER.debug(
"SengledApi: Wifi Color Bulb "
+ self._friendly_name
+ " "
+ self._device_mac
Expand All @@ -82,6 +106,8 @@ async def async_turn_off(self):
self._api._publish_mqtt(
"wifielement/{}/update".format(self._device_mac), json.dumps(data),
)
self._state = False
self._just_changed_state = True

def is_on(self):
return self._state
Expand All @@ -106,12 +132,12 @@ async def async_update(self):
)
_LOGGER.info("SengledApi: Wifi Bulb " + self._friendly_name + " updating.")
for item in data["deviceList"]:
_LOGGER.debug("SengledApi: Wifi Bulb update return " + str(item))
# _LOGGER.debug("SengledApi: Wifi Bulb update return " + str(item))
bulbs.append(SengledWifiBulbProperty(self, item))
for items in bulbs:
if items.uuid == self._device_mac:
self._friendly_name = items.name
self._brightness = items.brightness
self._brightness = round((items.brightness / 100) * 255)
self._state = items.switch
self._avaliable = items.online

Expand Down
Loading

0 comments on commit 9dbe881

Please sign in to comment.