diff --git a/.gitignore b/.gitignore index 43030cb..f900351 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ .vscode/** **/__pycache__/** Config/** +.trunk/logs diff --git a/custom_components/vesync/light.py b/custom_components/vesync/light.py index 25083c0..cc4c865 100644 --- a/custom_components/vesync/light.py +++ b/custom_components/vesync/light.py @@ -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__) @@ -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) @@ -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) @@ -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): @@ -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): @@ -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)