diff --git a/custom_components/sengledapi/light.py b/custom_components/sengledapi/light.py index 8bb4c87..0420b86 100644 --- a/custom_components/sengledapi/light.py +++ b/custom_components/sengledapi/light.py @@ -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__) @@ -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): @@ -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.""" diff --git a/custom_components/sengledapi/sengledapi/bulbs/sengled_bulb.py b/custom_components/sengledapi/sengledapi/bulbs/sengled_bulb.py index b8e11e5..5483033 100644 --- a/custom_components/sengledapi/sengledapi/bulbs/sengled_bulb.py +++ b/custom_components/sengledapi/sengledapi/bulbs/sengled_bulb.py @@ -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 @@ -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 diff --git a/custom_components/sengledapi/sengledapi/bulbs/sengled_bulb_flood_motion.py b/custom_components/sengledapi/sengledapi/bulbs/sengled_bulb_flood_motion.py index 7e558e1..7d12a55 100644 --- a/custom_components/sengledapi/sengledapi/bulbs/sengled_bulb_flood_motion.py +++ b/custom_components/sengledapi/sengledapi/bulbs/sengled_bulb_flood_motion.py @@ -42,26 +42,13 @@ 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)) @@ -69,10 +56,28 @@ async def async_turn_on(self): 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 diff --git a/custom_components/sengledapi/sengledapi/bulbs/sengled_color_bulb.py b/custom_components/sengledapi/sengledapi/bulbs/sengled_color_bulb.py index a2048ea..3ee3d8e 100644 --- a/custom_components/sengledapi/sengledapi/bulbs/sengled_color_bulb.py +++ b/custom_components/sengledapi/sengledapi/bulbs/sengled_color_bulb.py @@ -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 diff --git a/custom_components/sengledapi/sengledapi/sengled_request.py b/custom_components/sengledapi/sengledapi/sengled_request.py index a70a63f..a1ead03 100644 --- a/custom_components/sengledapi/sengledapi/sengled_request.py +++ b/custom_components/sengledapi/sengledapi/sengled_request.py @@ -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##################################### diff --git a/custom_components/sengledapi/sengledapi/sengled_wifi_bulb.py b/custom_components/sengledapi/sengledapi/sengled_wifi_bulb.py index 20e7975..a5a0fc0 100644 --- a/custom_components/sengledapi/sengledapi/sengled_wifi_bulb.py +++ b/custom_components/sengledapi/sengledapi/sengled_wifi_bulb.py @@ -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", @@ -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 @@ -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 @@ -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 diff --git a/custom_components/sengledapi/sengledapi/sengled_wifi_bulb_property.py b/custom_components/sengledapi/sengledapi/sengled_wifi_bulb_property.py index 6757daf..11ceba3 100644 --- a/custom_components/sengledapi/sengledapi/sengled_wifi_bulb_property.py +++ b/custom_components/sengledapi/sengledapi/sengled_wifi_bulb_property.py @@ -38,16 +38,20 @@ def brightness(self): """Bulb brightness.""" for attr in self._attributes: if attr["name"] == "brightness": - return self.translate(int(attr["value"]), 0, 100, 0, 255) + return int(attr["value"], 10) return 0 @property def color_temperature(self): - """Bulb consumption time.""" + """Bulb Temperature.""" + """ + Set the color temperature of a light device. + temperature: 0 (warm) - 100 (cold) + """ for attr in self._attributes: if attr["name"] == "colorTemperature": - return self.translate(int(attr["value"]), 0, 100, 2000, 6500) - return 0 + return round(self.translate(int(attr["value"]), 0, 100, 2000, 6500)) + return 1 @property def color(self): @@ -55,6 +59,7 @@ def color(self): # This is being displayed as RGB for attr in self._attributes: if attr["name"] == "color": + _LOGGER.debug("SengledApi: Color From propertys %s", str(attr["value"])) return attr["value"] return "" diff --git a/custom_components/sengledapi/sengledapi/sengled_wifi_color_bulb.py b/custom_components/sengledapi/sengledapi/sengled_wifi_color_bulb.py index 331c32e..7235d0b 100644 --- a/custom_components/sengledapi/sengledapi/sengled_wifi_color_bulb.py +++ b/custom_components/sengledapi/sengledapi/sengled_wifi_color_bulb.py @@ -38,13 +38,14 @@ def __init__( self._avaliable = isonline self._just_changed_state = False self._device_model = device_model - self._brightness = int(brightness) + self._brightness = round((int(brightness) / 100) * 255) self._color_temperature = color_temperature self._color = color self._device_rssi = device_rssi self._jsession_id = jsession_id self._country = country + async def async_turn_on(self): _LOGGER.debug( "SengledApi: Wifi Color Bulb " @@ -66,65 +67,80 @@ async def async_turn_on(self): self._state = True self._just_changed_state = True - async def set_brightness(self, brightness): + 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" + + " setting Brightness " + + str(brightness_precentage) ) - _LOGGER.info("SengledApi: Turn on Brightness %s", self._brightness) + data_brightness = { "dn": self._device_mac, "type": "brightness", - "value": 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 set_color(self, color): + async def async_color_temperature(self, color_temp): _LOGGER.debug( "SengledApi: Wifi Color Bulb " + self._friendly_name + " " + self._device_mac - + " .Setting Color" + + " .Setting ColorTemp" ) - # need to convert to RGB I think - _LOGGER.info("SengledApi: Turn on Brightness %s", color) - data_color = { + _LOGGER.info("SengledApi: color Temp from HA %s", str(color_temp)) + # need to covert back to 0 -100 + color_temperature_precentage = round(self.translate(int(color_temp), 2000, 6500, 1, 100)) + _LOGGER.info("SengledApi: color Temp %s", color_temperature_precentage) + data_color_temperature = { "dn": self._device_mac, - "type": "color", - "value": "255:255:0", + "type": "colorTemperature", + "value": str(color_temperature_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_color), + "wifielement/{}/update".format(self._device_mac), + json.dumps(data_color_temperature), ) - async def set_color_temp(self, color_temp): + async def async_set_color(self, color): _LOGGER.debug( "SengledApi: Wifi Color Bulb " + self._friendly_name + " " + self._device_mac - + " .Setting ColorTemp" + + " .Setting Color" ) - # need to covert back to 0 -100 - _LOGGER.info("SengledApi: Turn on Brightness %s", color_temp) - data_brightness = { + + mycolor = str(color) + for r in ((" ", ""), (",", ":"), ("(",""),(")","")): + mycolor = mycolor.replace(*r) + + _LOGGER.info("SengledApi: Wifi Set Color %s", str(mycolor)) + data_color = { "dn": self._device_mac, - "type": "color_temperature", - "value": 10, + "type": "color", + "value": mycolor, "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), + "wifielement/{}/update".format(self._device_mac), json.dumps(data_color), ) async def async_turn_off(self): @@ -145,6 +161,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 @@ -174,17 +192,10 @@ async def async_update(self): 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 self._color_temperature = items.color_temperature - _LOGGER.debug( - "SengledApi: From Update brightness %s", items.brightness - ) - _LOGGER.debug( - "SengledApi: From Update color temp %s", - items.color_temperature, - ) def translate(self, value, leftMin, leftMax, rightMin, rightMax): # Figure out how 'wide' each range is @@ -195,4 +206,4 @@ def translate(self, value, leftMin, leftMax, rightMin, rightMax): valueScaled = float(value - leftMin) / float(leftSpan) # Convert the 0-1 range into a value in the right range. - return rightMin + (valueScaled * rightSpan) + return rightMin + (valueScaled * rightSpan) \ No newline at end of file diff --git a/custom_components/sengledapi/sengledapi/sengledapi.py b/custom_components/sengledapi/sengledapi/sengledapi.py index e35c58b..7054436 100644 --- a/custom_components/sengledapi/sengledapi/sengledapi.py +++ b/custom_components/sengledapi/sengledapi/sengledapi.py @@ -208,9 +208,7 @@ async def async_list_bulbs(self): _LOGGER.debug(device) if "lampInfos" in device: for light in device["lampInfos"]: - if ( - light["attributes"]["productCode"] == "E11-G13" - ): # Sengled Soft White A19 Bulb + if (light["attributes"]["productCode"] == "E11-G13"): # Sengled Soft White A19 Bulb bulbs.append( SengledBulb( self, @@ -225,9 +223,7 @@ async def async_list_bulbs(self): self._country, ) ) - if ( - light["attributes"]["productCode"] == "E11-G14" - ): # Sengled Daylight A19 Bulb + if (light["attributes"]["productCode"] == "E11-G14"): # Sengled Daylight A19 Bulb bulbs.append( SengledBulb( self, @@ -242,9 +238,7 @@ async def async_list_bulbs(self): self._country, ) ) - if ( - light["attributes"]["productCode"] == "E11-G33" - ): # Sengled Element Classic A60 B22 + if (light["attributes"]["productCode"] == "E11-G33"): # Sengled Element Classic A60 B22 bulbs.append( SengledBulb( self, @@ -259,9 +253,7 @@ async def async_list_bulbs(self): self._country, ) ) - if ( - light["attributes"]["productCode"] == "E12-N14" - ): # Sengled Soft White BR30 Bulb + if (light["attributes"]["productCode"] == "E12-N14"): # Sengled Soft White BR30 Bulb bulbs.append( SengledBulb( self, @@ -276,9 +268,7 @@ async def async_list_bulbs(self): self._country, ) ) - if ( - light["attributes"]["productCode"] == "E11-G23" - ): # Sengled Element Classic A60 E27 + if (light["attributes"]["productCode"] == "E11-G23"): # Sengled Element Classic A60 E27 bulbs.append( SengledBulb( self, @@ -293,9 +283,7 @@ async def async_list_bulbs(self): self._country, ) ) - if ( - light["attributes"]["productCode"] == "E1A-AC2" - ): # Sengled Element Downlight + if (light["attributes"]["productCode"] == "E1A-AC2"): # Sengled Element Downlight bulbs.append( SengledBulb( self, @@ -310,9 +298,7 @@ async def async_list_bulbs(self): self._country, ) ) - if ( - light["attributes"]["productCode"] == "E13-N11" - ): # Sengled Motion Sensor PAR38 Bulb + if (light["attributes"]["productCode"] == "E13-N11"): # Sengled Motion Sensor PAR38 Bulb bulbs.append( SengledBulbFloodMotion( self, @@ -500,6 +486,10 @@ async def async_list_bulbs(self): if self._wifi: for devicebulb in await self.get_devices(): if devicebulb.type_code == "wificolora19": + _LOGGER.debug("SengledAPI: uuid " + devicebulb.uuid) + _LOGGER.debug( + "SengledAPI: brightness " + str(devicebulb.brightness) + ) bulbs.append( SengledWifiColorBulb( self,