@@ -1044,7 +1044,7 @@ async def async_update(self):
1044
1044
self ._state = attrs .get ('power' ) == 'on'
1045
1045
await self .async_update_attrs (attrs )
1046
1046
1047
- def _update_attr_sensor_entities (self , attrs , domain = 'sensor' , option = None ):
1047
+ async def async_update_attr_sensor_entities (self , attrs , domain = 'sensor' , option = None ):
1048
1048
add_sensors = self ._add_entities .get (domain )
1049
1049
opt = {** (option or {})}
1050
1050
for a in attrs :
@@ -1056,6 +1056,8 @@ def _update_attr_sensor_entities(self, attrs, domain='sensor', option=None):
1056
1056
opt ['dict_key' ] = kys [1 ]
1057
1057
if a not in self ._state_attrs :
1058
1058
continue
1059
+ if not add_sensors :
1060
+ continue
1059
1061
tms = self ._check_same_sub_entity (p , domain )
1060
1062
if p in self ._subs :
1061
1063
self ._subs [p ].update_from_parent ()
@@ -1064,8 +1066,6 @@ def _update_attr_sensor_entities(self, attrs, domain='sensor', option=None):
1064
1066
if tms <= 1 :
1065
1067
self .logger .info ('%s: Device sub entity %s: %s already exists.' , self .name_model , domain , p )
1066
1068
continue
1067
- elif not add_sensors :
1068
- pass
1069
1069
elif domain == 'sensor' :
1070
1070
from .sensor import BaseSensorSubEntity
1071
1071
option = {'unique_id' : f'{ self ._unique_did } -{ p } ' , ** opt }
@@ -1101,35 +1101,34 @@ def turn_off(self, **kwargs):
1101
1101
1102
1102
def update_attrs (self , attrs : dict , update_parent = False , update_subs = True ):
1103
1103
self ._state_attrs .update (attrs or {})
1104
- if self .hass and self .platform and update_subs :
1105
- tps = cv .ensure_list (self .custom_config ('attributes_template' ))
1106
- for tpl in tps :
1107
- if not tpl :
1108
- continue
1109
- tpl = CUSTOM_TEMPLATES .get (tpl , tpl )
1110
- tpl = cv .template (tpl )
1111
- tpl .hass = self .hass
1112
- adt = tpl .render ({'data' : self ._state_attrs }) or {}
1113
- if isinstance (adt , dict ):
1114
- if adt .pop ('_override' , False ):
1115
- self ._state_attrs = adt
1116
- else :
1117
- self ._state_attrs .update (adt )
1118
1104
if update_parent and hasattr (self , '_parent' ):
1119
1105
if self ._parent and hasattr (self ._parent , 'update_attrs' ):
1120
1106
getattr (self ._parent , 'update_attrs' )(attrs or {}, update_parent = False )
1107
+ return self ._state_attrs
1108
+
1109
+ async def async_update_attrs (self , attrs : dict , update_parent = False , update_subs = True ):
1110
+ self ._state_attrs .update (attrs or {})
1121
1111
if update_subs :
1112
+ if self .hass and self .platform :
1113
+ tps = cv .ensure_list (self .custom_config ('attributes_template' ))
1114
+ for tpl in tps :
1115
+ if not tpl :
1116
+ continue
1117
+ tpl = CUSTOM_TEMPLATES .get (tpl , tpl )
1118
+ tpl = cv .template (tpl )
1119
+ tpl .hass = self .hass
1120
+ adt = tpl .async_render ({'data' : self ._state_attrs }) or {}
1121
+ if isinstance (adt , dict ):
1122
+ if adt .pop ('_override' , False ):
1123
+ self ._state_attrs = adt
1124
+ else :
1125
+ self ._state_attrs .update (adt )
1122
1126
if pls := self .custom_config_list ('sensor_attributes' ):
1123
- self ._update_attr_sensor_entities (pls , domain = 'sensor' )
1127
+ await self .async_update_attr_sensor_entities (pls , domain = 'sensor' )
1124
1128
if pls := self .custom_config_list ('binary_sensor_attributes' ):
1125
- self ._update_attr_sensor_entities (pls , domain = 'binary_sensor' )
1129
+ await self .async_update_attr_sensor_entities (pls , domain = 'binary_sensor' )
1126
1130
return self ._state_attrs
1127
1131
1128
- async def async_update_attrs (self , * args , ** kwargs ):
1129
- return await self .hass .async_add_executor_job (
1130
- partial (self .update_attrs , * args , ** kwargs )
1131
- )
1132
-
1133
1132
1134
1133
class MiotEntityInterface :
1135
1134
_miot_service = None
@@ -1600,10 +1599,10 @@ async def async_update_for_main_entity(self):
1600
1599
1601
1600
# update miio prop/event in cloud
1602
1601
if cls := self .custom_config_list ('miio_cloud_records' ):
1603
- await self .hass . async_add_executor_job ( partial ( self . update_miio_cloud_records , cls ) )
1602
+ await self .async_update_miio_cloud_records ( pls )
1604
1603
1605
1604
if pls := self .custom_config_list ('miio_cloud_props' ):
1606
- await self .hass . async_add_executor_job ( partial ( self . update_miio_cloud_props , pls ) )
1605
+ await self .async_update_miio_cloud_props ( pls )
1607
1606
1608
1607
# update micloud statistics in cloud
1609
1608
cls = self .custom_config_list ('micloud_statistics' ) or []
@@ -1618,38 +1617,40 @@ async def async_update_for_main_entity(self):
1618
1617
}
1619
1618
cls = [* cls , dic ]
1620
1619
if cls :
1621
- await self .hass . async_add_executor_job ( partial ( self . update_micloud_statistics , cls ) )
1620
+ await self .async_update_micloud_statistics ( cls )
1622
1621
1623
1622
# update miio properties in lan
1624
1623
if pls := self ._vars .get ('miio_properties' , []):
1625
- await self .hass . async_add_executor_job ( partial ( self . update_miio_props , pls ) )
1624
+ await self .async_update_miio_props ( pls )
1626
1625
1627
1626
# update miio commands in lan
1628
1627
if cls := self .custom_config_json ('miio_commands' ):
1629
- await self .hass . async_add_executor_job ( partial ( self . update_miio_commands , cls ) )
1628
+ await self .async_update_miio_commands ( cls )
1630
1629
1631
- def update_miio_props (self , props ):
1630
+ async def async_update_miio_props (self , props ):
1632
1631
if not self .miot_device :
1633
1632
return
1634
1633
if self ._miio2miot :
1635
1634
attrs = self ._miio2miot .only_miio_props (props )
1636
1635
else :
1637
1636
try :
1638
1637
num = self .custom_config_integer ('chunk_properties' ) or 15
1639
- attrs = self ._device .get_properties (props , max_properties = num )
1638
+ attrs = await self .hass .async_add_executor_job (
1639
+ partial (self ._device .get_properties , props , max_properties = num )
1640
+ )
1640
1641
except DeviceException as exc :
1641
1642
self .logger .warning ('%s: Got miio properties %s failed: %s' , self .name_model , props , exc )
1642
1643
return
1643
1644
if len (props ) != len (attrs ):
1644
- self .update_attrs ({
1645
+ await self .async_update_attrs ({
1645
1646
'miio.props' : attrs ,
1646
1647
})
1647
1648
return
1648
1649
attrs = dict (zip (map (lambda x : f'miio.{ x } ' , props ), attrs ))
1649
1650
self .logger .debug ('%s: Got miio properties: %s' , self .name_model , attrs )
1650
- self .update_attrs (attrs )
1651
+ await self .async_update_attrs (attrs )
1651
1652
1652
- def update_miio_commands (self , commands ):
1653
+ async def async_update_miio_commands (self , commands ):
1653
1654
if not self .miot_device :
1654
1655
return
1655
1656
if isinstance (commands , dict ):
@@ -1663,7 +1664,9 @@ def update_miio_commands(self, commands):
1663
1664
cmd = cfg .get ('method' )
1664
1665
pms = cfg .get ('params' ) or []
1665
1666
try :
1666
- attrs = self ._device .send (cmd , pms )
1667
+ attrs = await self .hass .async_add_executor_job (
1668
+ partial (self ._device .send , cmd , pms )
1669
+ )
1667
1670
except DeviceException as exc :
1668
1671
self .logger .warning ('%s: Send miio command %s(%s) failed: %s' , self .name_model , cmd , cfg , exc )
1669
1672
continue
@@ -1675,9 +1678,9 @@ def update_miio_commands(self, commands):
1675
1678
else :
1676
1679
attrs = dict (zip (props , attrs ))
1677
1680
self .logger .debug ('%s: Got miio properties: %s' , self .name_model , attrs )
1678
- self .update_attrs (attrs )
1681
+ await self .async_update_attrs (attrs )
1679
1682
1680
- def update_miio_cloud_props (self , keys ):
1683
+ async def async_update_miio_cloud_props (self , keys ):
1681
1684
did = str (self .miot_did )
1682
1685
mic = self .xiaomi_cloud
1683
1686
if not did or not mic :
@@ -1691,7 +1694,7 @@ def update_miio_cloud_props(self, keys):
1691
1694
'did' : did ,
1692
1695
'props' : kls ,
1693
1696
}
1694
- rdt = mic .request_miot_api ('device/batchdevicedatas' , [pms ]) or {}
1697
+ rdt = await mic .async_request_api ('device/batchdevicedatas' , [pms ]) or {}
1695
1698
self .logger .debug ('%s: Got miio cloud props: %s' , self .name_model , rdt )
1696
1699
props = (rdt .get ('result' ) or {}).get (did , {})
1697
1700
@@ -1700,12 +1703,12 @@ def update_miio_cloud_props(self, keys):
1700
1703
tpl = CUSTOM_TEMPLATES .get (tpl , tpl )
1701
1704
tpl = cv .template (tpl )
1702
1705
tpl .hass = self .hass
1703
- attrs = tpl .render ({'props' : props })
1706
+ attrs = tpl .async_render ({'props' : props })
1704
1707
else :
1705
1708
attrs = props
1706
- self .update_attrs (attrs )
1709
+ await self .async_update_attrs (attrs )
1707
1710
1708
- def update_miio_cloud_records (self , keys ):
1711
+ async def async_update_miio_cloud_records (self , keys ):
1709
1712
did = self .miot_did
1710
1713
mic = self .xiaomi_cloud
1711
1714
if not did or not mic :
@@ -1723,13 +1726,13 @@ def update_miio_cloud_records(self, keys):
1723
1726
}
1724
1727
if gby :
1725
1728
kws ['group' ] = gby
1726
- rdt = mic .get_user_device_data (did , key , typ , ** kws ) or []
1729
+ rdt = await mic .async_get_user_device_data (did , key , typ , ** kws ) or []
1727
1730
tpl = self .custom_config (f'miio_{ typ } _{ key } _template' )
1728
1731
if tpl :
1729
1732
tpl = CUSTOM_TEMPLATES .get (tpl , tpl )
1730
1733
tpl = cv .template (tpl )
1731
1734
tpl .hass = self .hass
1732
- rls = tpl .render ({'result' : rdt })
1735
+ rls = tpl .async_render ({'result' : rdt })
1733
1736
else :
1734
1737
rls = [
1735
1738
v .get ('value' )
@@ -1741,9 +1744,9 @@ def update_miio_cloud_records(self, keys):
1741
1744
else :
1742
1745
attrs [f'{ typ } .{ key } ' ] = rls
1743
1746
if attrs :
1744
- self .update_attrs (attrs )
1747
+ await self .async_update_attrs (attrs )
1745
1748
1746
- def update_micloud_statistics (self , lst ):
1749
+ async def async_update_micloud_statistics (self , lst ):
1747
1750
did = self .miot_did
1748
1751
mic = self .xiaomi_cloud
1749
1752
if not did or not mic :
@@ -1762,14 +1765,14 @@ def update_micloud_statistics(self, lst):
1762
1765
'time_end' : now + 60 ,
1763
1766
'limit' : int (c .get ('limit' ) or 1 ),
1764
1767
}
1765
- rdt = mic .request_miot_api ('v2/user/statistics' , pms ) or {}
1768
+ rdt = await mic .async_request_api ('v2/user/statistics' , pms ) or {}
1766
1769
self .logger .debug ('%s: Got micloud statistics: %s' , self .name_model , rdt )
1767
1770
tpl = c .get ('template' )
1768
1771
if tpl :
1769
1772
tpl = CUSTOM_TEMPLATES .get (tpl , tpl )
1770
1773
tpl = cv .template (tpl )
1771
1774
tpl .hass = self .hass
1772
- rls = tpl .render (rdt )
1775
+ rls = tpl .async_render (rdt )
1773
1776
else :
1774
1777
rls = [
1775
1778
v .get ('value' )
@@ -1781,7 +1784,7 @@ def update_micloud_statistics(self, lst):
1781
1784
elif isinstance (rls , dict ):
1782
1785
attrs .update (rls )
1783
1786
if attrs :
1784
- self .update_attrs (attrs )
1787
+ await self .async_update_attrs (attrs )
1785
1788
1786
1789
def get_properties (self , mapping , update_entity = False , throw = False , ** kwargs ):
1787
1790
results = []
0 commit comments