Skip to content

Commit

Permalink
解决灯色温控制ha灯状态无变化问题
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardlcl committed Aug 26, 2022
1 parent 42cd41e commit 0cae7ec
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 19 deletions.
5 changes: 3 additions & 2 deletions custom_components/mhtzn/Gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
from homeassistant.core import HomeAssistant, Event
from homeassistant.helpers.dispatcher import async_dispatcher_send

from .const import MQTT_CLIENT_INSTANCE, CONF_LIGHT_DEVICE_TYPE, EVENT_ENTITY_REGISTER, MQTT_TOPIC_PREFIX
from .const import MQTT_CLIENT_INSTANCE, CONF_LIGHT_DEVICE_TYPE, EVENT_ENTITY_REGISTER, MQTT_TOPIC_PREFIX, \
EVENT_ENTITY_STATE_UPDATE

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -94,7 +95,7 @@ async def _async_mqtt_subscribe(self, msg):
stats_list = payload["data"]
for state in stats_list:
async_dispatcher_send(
self._hass, "mhtzn_device_state_{}".format(state["sn"]), state
self._hass, EVENT_ENTITY_STATE_UPDATE.format(state["sn"]), state
)
elif topic.endswith("p33"):
"""Basic data, including room information, light group information, curtain group information"""
Expand Down
39 changes: 23 additions & 16 deletions custom_components/mhtzn/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

from .const import DOMAIN, MQTT_CLIENT_INSTANCE, \
EVENT_ENTITY_REGISTER, EVENT_ENTITY_STATE_UPDATE, CACHE_ENTITY_STATE_UPDATE_KEY_DICT
from .util import color_temp_to_rgb

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -57,8 +58,6 @@ def turn_off(self, **kwargs: Any) -> None:
def __init__(self, hass: HomeAssistant, config: dict, config_entry: ConfigEntry) -> None:
self._attr_unique_id = config["unique_id"]

self._attr_entity_id = config["unique_id"]

self._attr_name = config["name"]

self._attr_max_mireds = LIGHT_MAX_KELVIN
Expand All @@ -69,23 +68,21 @@ def __init__(self, hass: HomeAssistant, config: dict, config_entry: ConfigEntry)

self.is_group = config["is_group"]

self._attr_color_mode = ColorMode.RGB

self._attr_supported_color_modes: set[ColorMode] = set()

self._attr_supported_color_modes.add(ColorMode.COLOR_TEMP)

if self.is_group:
self.room = int(config["room"])
self.subgroup = int(config["subgroup"])
self._attr_color_mode = ColorMode.RGB
self._attr_supported_color_modes.add(ColorMode.RGB)
else:
self.sn = config["sn"]
if "rgb" in config:
self._attr_color_mode = ColorMode.RGB
if ColorMode.RGB in config:
self._attr_supported_color_modes.add(ColorMode.RGB)

self._attr_color_mode = ColorMode.COLOR_TEMP

self.hass = hass

self.config_entry = config_entry
Expand Down Expand Up @@ -121,6 +118,14 @@ def device_info(self) -> DeviceInfo:
def is_on(self) -> bool | None:
return self.on_off

@property
def color_temp(self) -> int | None:
return self._attr_color_temp

@property
def rgb_color(self) -> tuple[int, int, int] | None:
return self._attr_rgb_color

def update_state(self, data):
"""Light event reporting changes the light state in HA"""
if "on" in data:
Expand All @@ -129,6 +134,15 @@ def update_state(self, data):
else:
self.on_off = True

if "kelvin" in data:
kelvin = int(data["kelvin"])
if kelvin > LIGHT_MAX_KELVIN:
kelvin = LIGHT_MAX_KELVIN
if kelvin < LIGHT_MIN_KELVIN:
kelvin = LIGHT_MIN_KELVIN
kelvin = LIGHT_MAX_KELVIN - (kelvin - LIGHT_MIN_KELVIN)
self._attr_color_temp = kelvin

if "rgb" in data:
rgb = data["rgb"]
blue = rgb & 255
Expand All @@ -139,15 +153,6 @@ def update_state(self, data):
if "level" in data:
self._attr_brightness = int(data["level"] * 255)

if "kelvin" in data:
kelvin = int(data["kelvin"])
if kelvin > LIGHT_MAX_KELVIN:
kelvin = LIGHT_MAX_KELVIN
if kelvin < LIGHT_MIN_KELVIN:
kelvin = LIGHT_MIN_KELVIN
kelvin = LIGHT_MAX_KELVIN - (kelvin - LIGHT_MIN_KELVIN)
self._attr_color_temp = kelvin

async def async_turn_on(self, **kwargs):
"""Turn on the light, switch color temperature, switch brightness, switch color operations"""
on = 1
Expand All @@ -167,6 +172,8 @@ async def async_turn_on(self, **kwargs):
kelvin = LIGHT_MIN_KELVIN
on = None
self._attr_color_temp = kwargs["color_temp"]
self._attr_rgb_color = color_temp_to_rgb(kelvin)

if "brightness" in kwargs:
brightness_normalized = kwargs["brightness"] / 255
level = round(brightness_normalized, 6)
Expand Down
55 changes: 54 additions & 1 deletion custom_components/mhtzn/util.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Utility functions for the MHTZN integration."""
import math

from homeassistant.const import CONF_NAME, CONF_PORT, CONF_USERNAME, CONF_PASSWORD, CONF_PROTOCOL

Expand All @@ -12,6 +13,59 @@ def get_connection_name(discovery_info):
return discovery_info.name.replace(f".{service_type}.", "")


def color_temp_to_rgb(color_temp) -> tuple[int, int, int] | None:
if color_temp < 1000:
color_temp = 1000.0

if color_temp > 40000:
color_temp = 40000.0

tempera = color_temp / 100.0
if tempera <= 66:
red = 255.0
else:
red = tempera - 60
red = 329.698727446 * math.pow(red, -0.1332047592)
if red < 0:
red = 0.0
elif red > 255.0:
red = 255.0

if tempera <= 66:
green = tempera
green = 99.4708025861 * math.log(green) - 161.1195681661
if green < 0:
green = 0.0

if green > 255:
green = 255.0
else:
green = tempera - 60
green = 288.1221695283 * math.pow(green, -0.0755148492)
if green < 0:
green = 0.0

if green > 255:
green = 255.0

if tempera >= 66:
blue = 255.0
else:
if tempera <= 19:
blue = 0.0
else:
blue = tempera - 10
blue = 138.5177312231 * math.log(blue) - 305.0447927307
if blue < 0:
blue = 0.0
if blue > 255:
blue = 255.0

color_rgb = (int(red), int(green), int(blue))

return color_rgb


def format_connection(discovery_info):
"""Parse and format mdns data"""

Expand Down Expand Up @@ -40,4 +94,3 @@ def format_connection(discovery_info):
}

return connection

0 comments on commit 0cae7ec

Please sign in to comment.