mirror of
https://github.com/home-assistant/core.git
synced 2025-07-13 08:17:08 +00:00
Address late review of VeSync (#63945)
* Fast follow improvements to VeSync * Apply suggestions to other platforms, use async_on_unload * Rename dev_list to entities
This commit is contained in:
parent
0922627612
commit
f43c4d51e1
@ -14,7 +14,6 @@ from .const import (
|
|||||||
DOMAIN,
|
DOMAIN,
|
||||||
SERVICE_UPDATE_DEVS,
|
SERVICE_UPDATE_DEVS,
|
||||||
VS_DISCOVERY,
|
VS_DISCOVERY,
|
||||||
VS_DISPATCHERS,
|
|
||||||
VS_FANS,
|
VS_FANS,
|
||||||
VS_LIGHTS,
|
VS_LIGHTS,
|
||||||
VS_MANAGER,
|
VS_MANAGER,
|
||||||
@ -56,8 +55,6 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
|
|||||||
lights = hass.data[DOMAIN][VS_LIGHTS] = []
|
lights = hass.data[DOMAIN][VS_LIGHTS] = []
|
||||||
sensors = hass.data[DOMAIN][VS_SENSORS] = []
|
sensors = hass.data[DOMAIN][VS_SENSORS] = []
|
||||||
|
|
||||||
hass.data[DOMAIN][VS_DISPATCHERS] = []
|
|
||||||
|
|
||||||
if device_dict[VS_SWITCHES]:
|
if device_dict[VS_SWITCHES]:
|
||||||
switches.extend(device_dict[VS_SWITCHES])
|
switches.extend(device_dict[VS_SWITCHES])
|
||||||
hass.async_create_task(forward_setup(config_entry, Platform.SWITCH))
|
hass.async_create_task(forward_setup(config_entry, Platform.SWITCH))
|
||||||
@ -96,7 +93,7 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
|
|||||||
return
|
return
|
||||||
if new_switches and not switches:
|
if new_switches and not switches:
|
||||||
switches.extend(new_switches)
|
switches.extend(new_switches)
|
||||||
hass.async_create_task(forward_setup(config_entry, "switch"))
|
hass.async_create_task(forward_setup(config_entry, Platform.SWITCH))
|
||||||
|
|
||||||
fan_set = set(fan_devs)
|
fan_set = set(fan_devs)
|
||||||
new_fans = list(fan_set.difference(fans))
|
new_fans = list(fan_set.difference(fans))
|
||||||
@ -106,7 +103,7 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
|
|||||||
return
|
return
|
||||||
if new_fans and not fans:
|
if new_fans and not fans:
|
||||||
fans.extend(new_fans)
|
fans.extend(new_fans)
|
||||||
hass.async_create_task(forward_setup(config_entry, "fan"))
|
hass.async_create_task(forward_setup(config_entry, Platform.FAN))
|
||||||
|
|
||||||
light_set = set(light_devs)
|
light_set = set(light_devs)
|
||||||
new_lights = list(light_set.difference(lights))
|
new_lights = list(light_set.difference(lights))
|
||||||
@ -116,7 +113,7 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
|
|||||||
return
|
return
|
||||||
if new_lights and not lights:
|
if new_lights and not lights:
|
||||||
lights.extend(new_lights)
|
lights.extend(new_lights)
|
||||||
hass.async_create_task(forward_setup(config_entry, "light"))
|
hass.async_create_task(forward_setup(config_entry, Platform.LIGHT))
|
||||||
|
|
||||||
sensor_set = set(sensor_devs)
|
sensor_set = set(sensor_devs)
|
||||||
new_sensors = list(sensor_set.difference(sensors))
|
new_sensors = list(sensor_set.difference(sensors))
|
||||||
@ -126,7 +123,7 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
|
|||||||
return
|
return
|
||||||
if new_sensors and not sensors:
|
if new_sensors and not sensors:
|
||||||
sensors.extend(new_sensors)
|
sensors.extend(new_sensors)
|
||||||
hass.async_create_task(forward_setup(config_entry, "sensor"))
|
hass.async_create_task(forward_setup(config_entry, Platform.SENSOR))
|
||||||
|
|
||||||
hass.services.async_register(
|
hass.services.async_register(
|
||||||
DOMAIN, SERVICE_UPDATE_DEVS, async_new_device_discovery
|
DOMAIN, SERVICE_UPDATE_DEVS, async_new_device_discovery
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
"""Constants for VeSync Component."""
|
"""Constants for VeSync Component."""
|
||||||
|
|
||||||
DOMAIN = "vesync"
|
DOMAIN = "vesync"
|
||||||
VS_DISPATCHERS = "vesync_dispatchers"
|
|
||||||
VS_DISCOVERY = "vesync_discovery_{}"
|
VS_DISCOVERY = "vesync_discovery_{}"
|
||||||
SERVICE_UPDATE_DEVS = "update_devices"
|
SERVICE_UPDATE_DEVS = "update_devices"
|
||||||
|
|
||||||
|
@ -14,7 +14,7 @@ from homeassistant.util.percentage import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
from .common import VeSyncDevice
|
from .common import VeSyncDevice
|
||||||
from .const import DOMAIN, VS_DISCOVERY, VS_DISPATCHERS, VS_FANS
|
from .const import DOMAIN, VS_DISCOVERY, VS_FANS
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -44,30 +44,32 @@ async def async_setup_entry(
|
|||||||
) -> None:
|
) -> None:
|
||||||
"""Set up the VeSync fan platform."""
|
"""Set up the VeSync fan platform."""
|
||||||
|
|
||||||
async def async_discover(devices):
|
@callback
|
||||||
|
def discover(devices):
|
||||||
"""Add new devices to platform."""
|
"""Add new devices to platform."""
|
||||||
_async_setup_entities(devices, async_add_entities)
|
_setup_entities(devices, async_add_entities)
|
||||||
|
|
||||||
disp = async_dispatcher_connect(hass, VS_DISCOVERY.format(VS_FANS), async_discover)
|
config_entry.async_on_unload(
|
||||||
hass.data[DOMAIN][VS_DISPATCHERS].append(disp)
|
async_dispatcher_connect(hass, VS_DISCOVERY.format(VS_FANS), discover)
|
||||||
|
)
|
||||||
|
|
||||||
_async_setup_entities(hass.data[DOMAIN][VS_FANS], async_add_entities)
|
_setup_entities(hass.data[DOMAIN][VS_FANS], async_add_entities)
|
||||||
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def _async_setup_entities(devices, async_add_entities):
|
def _setup_entities(devices, async_add_entities):
|
||||||
"""Check if device is online and add entity."""
|
"""Check if device is online and add entity."""
|
||||||
dev_list = []
|
entities = []
|
||||||
for dev in devices:
|
for dev in devices:
|
||||||
if DEV_TYPE_TO_HA.get(dev.device_type) == "fan":
|
if DEV_TYPE_TO_HA.get(dev.device_type) == "fan":
|
||||||
dev_list.append(VeSyncFanHA(dev))
|
entities.append(VeSyncFanHA(dev))
|
||||||
else:
|
else:
|
||||||
_LOGGER.warning(
|
_LOGGER.warning(
|
||||||
"%s - Unknown device type - %s", dev.device_name, dev.device_type
|
"%s - Unknown device type - %s", dev.device_name, dev.device_type
|
||||||
)
|
)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
async_add_entities(dev_list, update_before_add=True)
|
async_add_entities(entities, update_before_add=True)
|
||||||
|
|
||||||
|
|
||||||
class VeSyncFanHA(VeSyncDevice, FanEntity):
|
class VeSyncFanHA(VeSyncDevice, FanEntity):
|
||||||
|
@ -14,7 +14,7 @@ from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
|||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
from .common import VeSyncDevice
|
from .common import VeSyncDevice
|
||||||
from .const import DOMAIN, VS_DISCOVERY, VS_DISPATCHERS, VS_LIGHTS
|
from .const import DOMAIN, VS_DISCOVERY, VS_LIGHTS
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -33,20 +33,20 @@ async def async_setup_entry(
|
|||||||
) -> None:
|
) -> None:
|
||||||
"""Set up lights."""
|
"""Set up lights."""
|
||||||
|
|
||||||
async def async_discover(devices):
|
@callback
|
||||||
|
def discover(devices):
|
||||||
"""Add new devices to platform."""
|
"""Add new devices to platform."""
|
||||||
_async_setup_entities(devices, async_add_entities)
|
_setup_entities(devices, async_add_entities)
|
||||||
|
|
||||||
disp = async_dispatcher_connect(
|
config_entry.async_on_unload(
|
||||||
hass, VS_DISCOVERY.format(VS_LIGHTS), async_discover
|
async_dispatcher_connect(hass, VS_DISCOVERY.format(VS_LIGHTS), discover)
|
||||||
)
|
)
|
||||||
hass.data[DOMAIN][VS_DISPATCHERS].append(disp)
|
|
||||||
|
|
||||||
_async_setup_entities(hass.data[DOMAIN][VS_LIGHTS], async_add_entities)
|
_setup_entities(hass.data[DOMAIN][VS_LIGHTS], async_add_entities)
|
||||||
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def _async_setup_entities(devices, async_add_entities):
|
def _setup_entities(devices, async_add_entities):
|
||||||
"""Check if device is online and add entity."""
|
"""Check if device is online and add entity."""
|
||||||
entities = []
|
entities = []
|
||||||
for dev in devices:
|
for dev in devices:
|
||||||
|
@ -14,7 +14,7 @@ from homeassistant.helpers.entity import EntityCategory
|
|||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
from .common import VeSyncBaseEntity
|
from .common import VeSyncBaseEntity
|
||||||
from .const import DOMAIN, VS_DISCOVERY, VS_DISPATCHERS, VS_SENSORS
|
from .const import DOMAIN, VS_DISCOVERY, VS_SENSORS
|
||||||
from .switch import DEV_TYPE_TO_HA
|
from .switch import DEV_TYPE_TO_HA
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
@ -27,31 +27,30 @@ async def async_setup_entry(
|
|||||||
) -> None:
|
) -> None:
|
||||||
"""Set up switches."""
|
"""Set up switches."""
|
||||||
|
|
||||||
async def async_discover(devices):
|
@callback
|
||||||
|
def discover(devices):
|
||||||
"""Add new devices to platform."""
|
"""Add new devices to platform."""
|
||||||
_async_setup_entities(devices, async_add_entities)
|
_setup_entities(devices, async_add_entities)
|
||||||
|
|
||||||
disp = async_dispatcher_connect(
|
config_entry.async_on_unload(
|
||||||
hass, VS_DISCOVERY.format(VS_SENSORS), async_discover
|
async_dispatcher_connect(hass, VS_DISCOVERY.format(VS_SENSORS), discover)
|
||||||
)
|
)
|
||||||
hass.data[DOMAIN][VS_DISPATCHERS].append(disp)
|
|
||||||
|
|
||||||
_async_setup_entities(hass.data[DOMAIN][VS_SENSORS], async_add_entities)
|
_setup_entities(hass.data[DOMAIN][VS_SENSORS], async_add_entities)
|
||||||
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def _async_setup_entities(devices, async_add_entities):
|
def _setup_entities(devices, async_add_entities):
|
||||||
"""Check if device is online and add entity."""
|
"""Check if device is online and add entity."""
|
||||||
dev_list = []
|
entities = []
|
||||||
for dev in devices:
|
for dev in devices:
|
||||||
if DEV_TYPE_TO_HA.get(dev.device_type) == "outlet":
|
if DEV_TYPE_TO_HA.get(dev.device_type) != "outlet":
|
||||||
dev_list.append(VeSyncPowerSensor(dev))
|
|
||||||
dev_list.append(VeSyncEnergySensor(dev))
|
|
||||||
else:
|
|
||||||
# Not an outlet that supports energy/power, so do not create sensor entities
|
# Not an outlet that supports energy/power, so do not create sensor entities
|
||||||
continue
|
continue
|
||||||
|
entities.append(VeSyncPowerSensor(dev))
|
||||||
|
entities.append(VeSyncEnergySensor(dev))
|
||||||
|
|
||||||
async_add_entities(dev_list, update_before_add=True)
|
async_add_entities(entities, update_before_add=True)
|
||||||
|
|
||||||
|
|
||||||
class VeSyncSensorEntity(VeSyncBaseEntity, SensorEntity):
|
class VeSyncSensorEntity(VeSyncBaseEntity, SensorEntity):
|
||||||
|
@ -8,7 +8,7 @@ from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
|||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
from .common import VeSyncDevice
|
from .common import VeSyncDevice
|
||||||
from .const import DOMAIN, VS_DISCOVERY, VS_DISPATCHERS, VS_SWITCHES
|
from .const import DOMAIN, VS_DISCOVERY, VS_SWITCHES
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -30,34 +30,34 @@ async def async_setup_entry(
|
|||||||
) -> None:
|
) -> None:
|
||||||
"""Set up switches."""
|
"""Set up switches."""
|
||||||
|
|
||||||
async def async_discover(devices):
|
@callback
|
||||||
|
def discover(devices):
|
||||||
"""Add new devices to platform."""
|
"""Add new devices to platform."""
|
||||||
_async_setup_entities(devices, async_add_entities)
|
_setup_entities(devices, async_add_entities)
|
||||||
|
|
||||||
disp = async_dispatcher_connect(
|
config_entry.async_on_unload(
|
||||||
hass, VS_DISCOVERY.format(VS_SWITCHES), async_discover
|
async_dispatcher_connect(hass, VS_DISCOVERY.format(VS_SWITCHES), discover)
|
||||||
)
|
)
|
||||||
hass.data[DOMAIN][VS_DISPATCHERS].append(disp)
|
|
||||||
|
|
||||||
_async_setup_entities(hass.data[DOMAIN][VS_SWITCHES], async_add_entities)
|
_setup_entities(hass.data[DOMAIN][VS_SWITCHES], async_add_entities)
|
||||||
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def _async_setup_entities(devices, async_add_entities):
|
def _setup_entities(devices, async_add_entities):
|
||||||
"""Check if device is online and add entity."""
|
"""Check if device is online and add entity."""
|
||||||
dev_list = []
|
entities = []
|
||||||
for dev in devices:
|
for dev in devices:
|
||||||
if DEV_TYPE_TO_HA.get(dev.device_type) == "outlet":
|
if DEV_TYPE_TO_HA.get(dev.device_type) == "outlet":
|
||||||
dev_list.append(VeSyncSwitchHA(dev))
|
entities.append(VeSyncSwitchHA(dev))
|
||||||
elif DEV_TYPE_TO_HA.get(dev.device_type) == "switch":
|
elif DEV_TYPE_TO_HA.get(dev.device_type) == "switch":
|
||||||
dev_list.append(VeSyncLightSwitch(dev))
|
entities.append(VeSyncLightSwitch(dev))
|
||||||
else:
|
else:
|
||||||
_LOGGER.warning(
|
_LOGGER.warning(
|
||||||
"%s - Unknown device type - %s", dev.device_name, dev.device_type
|
"%s - Unknown device type - %s", dev.device_name, dev.device_type
|
||||||
)
|
)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
async_add_entities(dev_list, update_before_add=True)
|
async_add_entities(entities, update_before_add=True)
|
||||||
|
|
||||||
|
|
||||||
class VeSyncBaseSwitch(VeSyncDevice, SwitchEntity):
|
class VeSyncBaseSwitch(VeSyncDevice, SwitchEntity):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user