Vincent Le Bourlot 3498625de7
Add display switch to fan devices. (#32)
* Improve night_light feature discovery

* add display switch to fans

* revert night_light removal from humidifier

* Improve switch detection

* Update light.py

* fix style
2022-07-11 12:02:53 +02:00

206 lines
6.2 KiB
Python

"""Support for VeSync switches."""
import logging
from homeassistant.components.switch import SwitchEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity import EntityCategory
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .common import VeSyncBaseEntity, VeSyncDevice, is_humidifier
from .const import DEV_TYPE_TO_HA, DOMAIN, VS_DISCOVERY, VS_SWITCHES
_LOGGER = logging.getLogger(__name__)
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up switches."""
@callback
def discover(devices):
"""Add new devices to platform."""
_setup_entities(devices, async_add_entities)
config_entry.async_on_unload(
async_dispatcher_connect(hass, VS_DISCOVERY.format(VS_SWITCHES), discover)
)
_setup_entities(
hass.data[DOMAIN][config_entry.entry_id][VS_SWITCHES], async_add_entities
)
@callback
def _setup_entities(devices, async_add_entities):
"""Check if device is online and add entity."""
entities = []
for dev in devices:
if DEV_TYPE_TO_HA.get(dev.device_type) == "outlet":
entities.append(VeSyncSwitchHA(dev))
elif DEV_TYPE_TO_HA.get(dev.device_type) == "switch":
entities.append(VeSyncLightSwitch(dev))
elif is_humidifier(dev.device_type):
entities.extend(
(
VeSyncHumidifierDisplayHA(dev),
VeSyncHumidifierAutomaticStopHA(dev),
VeSyncHumidifierAutoOnHA(dev),
)
)
elif getattr(dev, "turn_on_display", None):
entities.append(VeSyncHumidifierDisplayHA(dev))
else:
_LOGGER.warning(
"%s - Unknown device type - %s", dev.device_name, dev.device_type
)
continue
async_add_entities(entities, update_before_add=True)
class VeSyncBaseSwitch(VeSyncDevice, SwitchEntity):
"""Base class for VeSync switch Device Representations."""
def turn_on(self, **kwargs):
"""Turn the device on."""
self.device.turn_on()
class VeSyncSwitchHA(VeSyncBaseSwitch, SwitchEntity):
"""Representation of a VeSync switch."""
def __init__(self, plug):
"""Initialize the VeSync switch device."""
super().__init__(plug)
self.smartplug = plug
@property
def extra_state_attributes(self):
"""Return the state attributes of the device."""
return (
{
"voltage": self.smartplug.voltage,
"weekly_energy_total": self.smartplug.weekly_energy_total,
"monthly_energy_total": self.smartplug.monthly_energy_total,
"yearly_energy_total": self.smartplug.yearly_energy_total,
}
if hasattr(self.smartplug, "weekly_energy_total")
else {}
)
def update(self):
"""Update outlet details and energy usage."""
self.smartplug.update()
self.smartplug.update_energy()
class VeSyncLightSwitch(VeSyncBaseSwitch, SwitchEntity):
"""Handle representation of VeSync Light Switch."""
def __init__(self, switch):
"""Initialize Light Switch device class."""
super().__init__(switch)
self.switch = switch
class VeSyncHumidifierSwitchEntity(VeSyncBaseEntity, SwitchEntity):
"""Representation of a switch for configuring a VeSync humidifier."""
def __init__(self, humidifier):
"""Initialize the VeSync humidifier device."""
super().__init__(humidifier)
self.smarthumidifier = humidifier
@property
def entity_category(self):
"""Return the configuration entity category."""
return EntityCategory.CONFIG
class VeSyncHumidifierDisplayHA(VeSyncHumidifierSwitchEntity):
"""Representation of the display on a VeSync humidifier."""
@property
def unique_id(self):
"""Return the ID of this display."""
return f"{super().unique_id}-display"
@property
def name(self):
"""Return the name of the display."""
return f"{super().name} display"
@property
def is_on(self):
"""Return True if display is on."""
return self.device.details["display"]
def turn_on(self, **kwargs):
"""Turn the display on."""
self.device.turn_on_display()
def turn_off(self, **kwargs):
"""Turn the display off."""
self.device.turn_off_display()
class VeSyncHumidifierAutomaticStopHA(VeSyncHumidifierSwitchEntity):
"""Representation of the automatic stop toggle on a VeSync humidifier."""
@property
def unique_id(self):
"""Return the ID of this device."""
return f"{super().unique_id}-automatic-stop"
@property
def name(self):
"""Return the name of the device."""
return f"{super().name} automatic stop"
@property
def is_on(self):
"""Return True if automatic stop is on."""
return self.device.config["automatic_stop"]
def turn_on(self, **kwargs):
"""Turn the automatic stop on."""
self.device.automatic_stop_on()
def turn_off(self, **kwargs):
"""Turn the automatic stop off."""
self.device.automatic_stop_off()
class VeSyncHumidifierAutoOnHA(VeSyncHumidifierSwitchEntity):
"""Provide switch to turn off auto mode and set manual mist level 1 on a VeSync humidifier."""
@property
def unique_id(self):
"""Return the ID of this device."""
return f"{super().unique_id}-auto-mode"
@property
def name(self):
"""Return the name of the device."""
return f"{super().name} auto mode"
@property
def is_on(self):
"""Return True if in auto mode."""
return self.device.details["mode"] == "auto"
def turn_on(self, **kwargs):
"""Turn auto mode on."""
self.device.set_auto_mode()
def turn_off(self, **kwargs):
"""Turn auto off by setting manual and mist level 1."""
self.device.set_manual_mode()
self.device.set_mist_level(1)