Skip to content
This repository has been archived by the owner on Dec 21, 2023. It is now read-only.

Commit

Permalink
Fix/regression from 0.1.11 on lights. (#42)
Browse files Browse the repository at this point in the history
* ignore trunk log

* fix night light brightness for unsupported devices

* rename night light class
  • Loading branch information
vlebourl authored Aug 3, 2022
1 parent f7d1947 commit dc222cb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 23 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
.vscode/**
**/__pycache__/**
Config/**
.trunk/logs
47 changes: 24 additions & 23 deletions custom_components/vesync/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from homeassistant.helpers.entity import EntityCategory
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from .common import VeSyncDevice
from .common import VeSyncDevice, has_feature
from .const import DEV_TYPE_TO_HA, DOMAIN, VS_DISCOVERY, VS_LIGHTS

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -51,7 +51,7 @@ def _setup_entities(devices, async_add_entities):
if DEV_TYPE_TO_HA.get(dev.device_type) in ("bulb-tunable-white",):
entities.append(VeSyncTunableWhiteLightHA(dev))
if dev.night_light:
entities.append(VeSyncHumidifierNightLightHA(dev))
entities.append(VeSyncNightLightHA(dev))

async_add_entities(entities, update_before_add=True)

Expand All @@ -66,7 +66,7 @@ def _vesync_brightness_to_ha(vesync_brightness):
"VeSync - received unexpected 'brightness' value from pyvesync api: %s",
vesync_brightness,
)
return 0
return None
# convert percent brightness to ha expected range
return round((max(1, brightness_value) / 100) * 255)

Expand Down Expand Up @@ -197,13 +197,13 @@ def supported_color_modes(self):
return [COLOR_MODE_COLOR_TEMP]


class VeSyncHumidifierNightLightHA(VeSyncDimmableLightHA):
"""Representation of the night light on a VeSync humidifier."""
class VeSyncNightLightHA(VeSyncDimmableLightHA):
"""Representation of the night light on a VeSync device."""

def __init__(self, humidifier):
"""Initialize the VeSync humidifier device."""
super().__init__(humidifier)
self.smarthumidifier = humidifier
def __init__(self, device):
"""Initialize the VeSync device."""
super().__init__(device)
self.device = device

@property
def unique_id(self):
Expand All @@ -218,18 +218,19 @@ def name(self):
@property
def brightness(self):
"""Get night light brightness."""
# get value from pyvesync library api,
return _vesync_brightness_to_ha(
self.smarthumidifier.details["night_light_brightness"]
return (
_vesync_brightness_to_ha(self.device.details["night_light_brightness"])
if has_feature(self.device, "details", "night_light_brightness")
else None
)

@property
def is_on(self):
"""Return True if night light is on."""
if self.device.config_dict["module"] == "VeSyncAirBypass":
return self.smarthumidifier.details["night_light"] == "on"
else:
return self.smarthumidifier.details["night_light_brightness"] > 0
if has_feature(self.device, "details", "night_light"):
return self.device.details["night_light"] == "on"
if has_feature(self.device, "details", "night_light_brightness"):
return self.device.details["night_light_brightness"] > 0

@property
def entity_category(self):
Expand All @@ -238,17 +239,17 @@ def entity_category(self):

def turn_on(self, **kwargs):
"""Turn the night light on."""
if ATTR_BRIGHTNESS in kwargs:
if self.device.config_dict["module"] == "VeSyncAirBypass":
self.device.set_night_light("on")
elif ATTR_BRIGHTNESS in kwargs:
brightness = _ha_brightness_to_vesync(kwargs[ATTR_BRIGHTNESS])
self.smarthumidifier.set_night_light_brightness(brightness)
elif self.device.config_dict["module"] == "VeSyncAirBypass":
self.smarthumidifier.set_night_light("on")
self.device.set_night_light_brightness(brightness)
else:
self.smarthumidifier.set_night_light_brightness(100)
self.device.set_night_light_brightness(100)

def turn_off(self, **kwargs):
"""Turn the night light off."""
if self.device.config_dict["module"] == "VeSyncAirBypass":
self.smarthumidifier.set_night_light("off")
self.device.set_night_light("off")
else:
self.smarthumidifier.set_night_light_brightness(0)
self.device.set_night_light_brightness(0)

0 comments on commit dc222cb

Please sign in to comment.