Skip to content

Commit

Permalink
Fix adding sensors entities which did not use async_add_entities
Browse files Browse the repository at this point in the history
  • Loading branch information
niceboy committed Mar 7, 2024
1 parent a2ff161 commit cdb489c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 20 deletions.
12 changes: 6 additions & 6 deletions custom_components/aqara_gateway/core/gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def start(self, hass: HomeAssistant, config_entry: ConfigEntry):
else:
self.main_task = hass.loop.create_task(self.run())

async def run(self):
async def async_run(self):
""" Main thread loop. """
telnetshell = False
if "telnet" not in self.hass.data[DOMAIN]:
Expand All @@ -154,7 +154,7 @@ async def run(self):
devices = self._prepare_gateway(get_devices=True)
if isinstance(devices, list):
self._gw_topic = "gw/{}/".format(devices[0]['mac'][2:].upper())
await self.setup_devices(devices)
await self.async_setup_devices(devices)
break

if telnetshell:
Expand Down Expand Up @@ -330,7 +330,7 @@ def _get_devices(self, shell):

return devices

async def setup_devices(self, devices: list):
async def async_setup_devices(self, devices: list):
"""Add devices to hass."""
for device in devices:
timeout = 300
Expand Down Expand Up @@ -478,7 +478,7 @@ def on_disconnect(self, client, userdata, ret):
self.hass.data[DOMAIN]["mqtt"].remove(self.host)
self.available = False
# self.process_gateway_stats()
self.hass.create_task(self.run())
self.hass.create_task(self.async_run())

def on_message(self, client: Client, userdata, msg: MQTTMessage):
# pylint: disable=unused-argument
Expand Down Expand Up @@ -586,7 +586,7 @@ def _process_devices_info(self, prop, value):
'model_ver': dev['model_ver'],
'status': dev['status']
}
self.hass.create_task(self.setup_devices([device]))
self.hass.create_task(self.async_setup_devices([device]))
break

def _process_message(self, data: dict):
Expand Down Expand Up @@ -750,7 +750,7 @@ def _process_message(self, data: dict):
device['mac'] = '0x' + device['mac']
device['type'] = 'zigbee'
device['init'] = payload
self.hass.create_task(self.setup_devices([device]))
self.hass.create_task(self.async_setup_devices([device]))

async def _handle_device_remove(self, payload: dict):
"""Remove device from Hass. """
Expand Down
5 changes: 4 additions & 1 deletion custom_components/aqara_gateway/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,10 +366,13 @@
'params': [
['1.10.85', None, 'present_mode', None],
['0.12.85', 'load_power', 'power', 'sensor'],
['4.1.85', 'power_status', 'light', 'light'],
['14.1.85', 'light_level', 'brightness', None],
['14.2.85', 'colour_temperature', 'color_temp', None],
['14.5.85', 'rgb_color', 'rgb_color', None],
['4.1.85', 'power_status', 'light', 'light'],
['14.12.85', 'light_level', 'brightness', None],
['14.16.85', 'colour_temperature', 'color_temp', None],
['4.2.85', 'power_status', 'sub light', 'light'],
['14.46.85', None, 'dual_color_temperature_mode', None],
['8.0.2022', None, 'ambilight', None],
['8.0.2150', None, 'dynamic', None],
Expand Down
26 changes: 13 additions & 13 deletions custom_components/aqara_gateway/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,35 +69,35 @@
GATEWAY_PLATFORMS_NO_KEY = ["binary_sensor", "sensor"]


async def async_setup_entry(hass, entry, add_entities):
async def async_setup_entry(hass, entry, async_add_entities):
""" setup config entry """
def setup(gateway: Gateway, device: dict, attr: str):
if attr == 'gateway':
add_entities([GatewayStats(gateway, device, attr)])
async_add_entities([GatewayStats(gateway, device, attr)])
elif attr == 'zigbee':
add_entities([ZigbeeStats(gateway, device, attr)])
async_add_entities([ZigbeeStats(gateway, device, attr)])
elif attr == 'gas density':
add_entities([GatewayGasSensor(gateway, device, attr)])
async_add_entities([GatewayGasSensor(gateway, device, attr)])
elif attr == 'lock':
add_entities([GatewayLockSensor(gateway, device, attr)])
async_add_entities([GatewayLockSensor(gateway, device, attr)])
elif attr == 'key_id':
add_entities([GatewayKeyIDSensor(gateway, device, attr)])
async_add_entities([GatewayKeyIDSensor(gateway, device, attr)])
elif attr == 'lock_event':
add_entities([GatewayLockEventSensor(gateway, device, attr)])
async_add_entities([GatewayLockEventSensor(gateway, device, attr)])
elif attr in ('hear_rate', 'breath_rate', 'body_movements'):
add_entities([GatewaySleepMonitorSensor(gateway, device, attr)])
async_add_entities([GatewaySleepMonitorSensor(gateway, device, attr)])
elif attr == 'illuminance':
if (device['type'] == 'gateway' and
Utils.gateway_illuminance_supported(device['model'])):
add_entities([GatewaySensor(gateway, device, attr)])
async_add_entities([GatewaySensor(gateway, device, attr)])
elif device['type'] == 'zigbee':
add_entities([GatewaySensor(gateway, device, attr)])
async_add_entities([GatewaySensor(gateway, device, attr)])
elif attr == 'movements':
add_entities([GatewayMoveSensor(gateway, device, attr)])
async_add_entities([GatewayMoveSensor(gateway, device, attr)])
elif attr == 'occupancy_region':
add_entities([GatewayOccupancyRegionSensor(gateway, device, attr)])
async_add_entities([GatewayOccupancyRegionSensor(gateway, device, attr)])
else:
add_entities([GatewaySensor(gateway, device, attr)])
async_add_entities([GatewaySensor(gateway, device, attr)])

aqara_gateway: Gateway = hass.data[DOMAIN][entry.entry_id]
aqara_gateway.add_setup('sensor', setup)
Expand Down

0 comments on commit cdb489c

Please sign in to comment.