Fix/regression from 0.1.11 on lights. (#42)

* ignore trunk log

* fix night light brightness for unsupported devices

* rename night light class
This commit is contained in:
Vincent Le Bourlot 2022-08-03 10:17:46 +02:00 committed by GitHub
parent f7d1947423
commit dc222cb03c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 23 deletions

1
.gitignore vendored
View File

@ -2,3 +2,4 @@
.vscode/** .vscode/**
**/__pycache__/** **/__pycache__/**
Config/** Config/**
.trunk/logs

View File

@ -14,7 +14,7 @@ from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity import EntityCategory from homeassistant.helpers.entity import EntityCategory
from homeassistant.helpers.entity_platform import AddEntitiesCallback 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 from .const import DEV_TYPE_TO_HA, DOMAIN, VS_DISCOVERY, VS_LIGHTS
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -51,7 +51,7 @@ def _setup_entities(devices, async_add_entities):
if DEV_TYPE_TO_HA.get(dev.device_type) in ("bulb-tunable-white",): if DEV_TYPE_TO_HA.get(dev.device_type) in ("bulb-tunable-white",):
entities.append(VeSyncTunableWhiteLightHA(dev)) entities.append(VeSyncTunableWhiteLightHA(dev))
if dev.night_light: if dev.night_light:
entities.append(VeSyncHumidifierNightLightHA(dev)) entities.append(VeSyncNightLightHA(dev))
async_add_entities(entities, update_before_add=True) async_add_entities(entities, update_before_add=True)
@ -66,7 +66,7 @@ def _vesync_brightness_to_ha(vesync_brightness):
"VeSync - received unexpected 'brightness' value from pyvesync api: %s", "VeSync - received unexpected 'brightness' value from pyvesync api: %s",
vesync_brightness, vesync_brightness,
) )
return 0 return None
# convert percent brightness to ha expected range # convert percent brightness to ha expected range
return round((max(1, brightness_value) / 100) * 255) return round((max(1, brightness_value) / 100) * 255)
@ -197,13 +197,13 @@ class VeSyncTunableWhiteLightHA(VeSyncBaseLight, LightEntity):
return [COLOR_MODE_COLOR_TEMP] return [COLOR_MODE_COLOR_TEMP]
class VeSyncHumidifierNightLightHA(VeSyncDimmableLightHA): class VeSyncNightLightHA(VeSyncDimmableLightHA):
"""Representation of the night light on a VeSync humidifier.""" """Representation of the night light on a VeSync device."""
def __init__(self, humidifier): def __init__(self, device):
"""Initialize the VeSync humidifier device.""" """Initialize the VeSync device."""
super().__init__(humidifier) super().__init__(device)
self.smarthumidifier = humidifier self.device = device
@property @property
def unique_id(self): def unique_id(self):
@ -218,18 +218,19 @@ class VeSyncHumidifierNightLightHA(VeSyncDimmableLightHA):
@property @property
def brightness(self): def brightness(self):
"""Get night light brightness.""" """Get night light brightness."""
# get value from pyvesync library api, return (
return _vesync_brightness_to_ha( _vesync_brightness_to_ha(self.device.details["night_light_brightness"])
self.smarthumidifier.details["night_light_brightness"] if has_feature(self.device, "details", "night_light_brightness")
else None
) )
@property @property
def is_on(self): def is_on(self):
"""Return True if night light is on.""" """Return True if night light is on."""
if self.device.config_dict["module"] == "VeSyncAirBypass": if has_feature(self.device, "details", "night_light"):
return self.smarthumidifier.details["night_light"] == "on" return self.device.details["night_light"] == "on"
else: if has_feature(self.device, "details", "night_light_brightness"):
return self.smarthumidifier.details["night_light_brightness"] > 0 return self.device.details["night_light_brightness"] > 0
@property @property
def entity_category(self): def entity_category(self):
@ -238,17 +239,17 @@ class VeSyncHumidifierNightLightHA(VeSyncDimmableLightHA):
def turn_on(self, **kwargs): def turn_on(self, **kwargs):
"""Turn the night light on.""" """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]) brightness = _ha_brightness_to_vesync(kwargs[ATTR_BRIGHTNESS])
self.smarthumidifier.set_night_light_brightness(brightness) self.device.set_night_light_brightness(brightness)
elif self.device.config_dict["module"] == "VeSyncAirBypass":
self.smarthumidifier.set_night_light("on")
else: else:
self.smarthumidifier.set_night_light_brightness(100) self.device.set_night_light_brightness(100)
def turn_off(self, **kwargs): def turn_off(self, **kwargs):
"""Turn the night light off.""" """Turn the night light off."""
if self.device.config_dict["module"] == "VeSyncAirBypass": if self.device.config_dict["module"] == "VeSyncAirBypass":
self.smarthumidifier.set_night_light("off") self.device.set_night_light("off")
else: else:
self.smarthumidifier.set_night_light_brightness(0) self.device.set_night_light_brightness(0)