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:
Jonathan Keslin 2022-01-12 15:38:39 -08:00 committed by GitHub
parent 0922627612
commit f43c4d51e1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 49 additions and 52 deletions

View file

@ -14,7 +14,7 @@ from homeassistant.helpers.entity import EntityCategory
from homeassistant.helpers.entity_platform import AddEntitiesCallback
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
_LOGGER = logging.getLogger(__name__)
@ -27,31 +27,30 @@ async def async_setup_entry(
) -> None:
"""Set up switches."""
async def async_discover(devices):
@callback
def discover(devices):
"""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_SENSORS), async_discover
config_entry.async_on_unload(
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
def _async_setup_entities(devices, async_add_entities):
def _setup_entities(devices, async_add_entities):
"""Check if device is online and add entity."""
dev_list = []
entities = []
for dev in devices:
if DEV_TYPE_TO_HA.get(dev.device_type) == "outlet":
dev_list.append(VeSyncPowerSensor(dev))
dev_list.append(VeSyncEnergySensor(dev))
else:
if DEV_TYPE_TO_HA.get(dev.device_type) != "outlet":
# Not an outlet that supports energy/power, so do not create sensor entities
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):