Simplify setup of tradfri entities (#59343)

* Simplify detection of devices.
This commit is contained in:
jan iversen 2021-11-09 15:18:13 +01:00 committed by GitHub
parent 5177fabee0
commit ad91e4b417
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 25 additions and 27 deletions

View file

@ -26,9 +26,9 @@ async def async_setup_entry(
api = tradfri_data[KEY_API] api = tradfri_data[KEY_API]
devices = tradfri_data[DEVICES] devices = tradfri_data[DEVICES]
covers = [dev for dev in devices if dev.has_blind_control] async_add_entities(
if covers: TradfriCover(dev, api, gateway_id) for dev in devices if dev.has_blind_control
async_add_entities(TradfriCover(cover, api, gateway_id) for cover in covers) )
class TradfriCover(TradfriBaseDevice, CoverEntity): class TradfriCover(TradfriBaseDevice, CoverEntity):

View file

@ -31,10 +31,10 @@ async def async_setup_entry(
api = tradfri_data[KEY_API] api = tradfri_data[KEY_API]
devices = tradfri_data[DEVICES] devices = tradfri_data[DEVICES]
purifiers = [dev for dev in devices if dev.has_air_purifier_control]
if purifiers:
async_add_entities( async_add_entities(
TradfriAirPurifierFan(purifier, api, gateway_id) for purifier in purifiers TradfriAirPurifierFan(dev, api, gateway_id)
for dev in devices
if dev.has_air_purifier_control
) )

View file

@ -49,12 +49,12 @@ async def async_setup_entry(
api = tradfri_data[KEY_API] api = tradfri_data[KEY_API]
devices = tradfri_data[DEVICES] devices = tradfri_data[DEVICES]
lights = [dev for dev in devices if dev.has_light_control] entities: list[TradfriBaseClass] = [
if lights: TradfriLight(dev, api, gateway_id) for dev in devices if dev.has_light_control
async_add_entities(TradfriLight(light, api, gateway_id) for light in lights) ]
if config_entry.data[CONF_IMPORT_GROUPS] and (groups := tradfri_data[GROUPS]): if config_entry.data[CONF_IMPORT_GROUPS] and (groups := tradfri_data[GROUPS]):
async_add_entities(TradfriGroup(group, api, gateway_id) for group in groups) entities.extend([TradfriGroup(group, api, gateway_id) for group in groups])
async_add_entities(entities)
class TradfriGroup(TradfriBaseClass, LightEntity): class TradfriGroup(TradfriBaseClass, LightEntity):

View file

@ -27,17 +27,17 @@ async def async_setup_entry(
api = tradfri_data[KEY_API] api = tradfri_data[KEY_API]
devices = tradfri_data[DEVICES] devices = tradfri_data[DEVICES]
sensors = ( async_add_entities(
dev TradfriSensor(dev, api, gateway_id)
for dev in devices for dev in devices
if not dev.has_light_control if (
not dev.has_light_control
and not dev.has_socket_control and not dev.has_socket_control
and not dev.has_blind_control and not dev.has_blind_control
and not dev.has_signal_repeater_control and not dev.has_signal_repeater_control
and not dev.has_air_purifier_control and not dev.has_air_purifier_control
) )
if sensors: )
async_add_entities(TradfriSensor(sensor, api, gateway_id) for sensor in sensors)
class TradfriSensor(TradfriBaseDevice, SensorEntity): class TradfriSensor(TradfriBaseDevice, SensorEntity):

View file

@ -26,10 +26,8 @@ async def async_setup_entry(
api = tradfri_data[KEY_API] api = tradfri_data[KEY_API]
devices = tradfri_data[DEVICES] devices = tradfri_data[DEVICES]
switches = [dev for dev in devices if dev.has_socket_control]
if switches:
async_add_entities( async_add_entities(
TradfriSwitch(switch, api, gateway_id) for switch in switches TradfriSwitch(dev, api, gateway_id) for dev in devices if dev.has_socket_control
) )