@@ -34,13 +34,14 @@ def initialize(self) -> None:
3434 self .store = get_bat_value_store (self .component_config .id )
3535 self .fault_state = FaultState (ComponentInfo .from_component_config (self .component_config ))
3636
37- def total_power_from_shelly (self ) -> int :
38- total = 0
37+ def update (self ) -> None :
38+ power = 0
3939 if self .generation == 1 :
4040 status_url = "http://" + self .address + "/status"
4141 else :
4242 status_url = "http://" + self .address + "/rpc/Shelly.GetStatus"
4343 status = req .get_http_session ().get (status_url , timeout = 3 ).json ()
44+
4445 try :
4546 if self .generation == 1 :
4647 if 'meters' in status :
@@ -49,28 +50,33 @@ def total_power_from_shelly(self) -> int:
4950 meters = status ['emeters' ] # shellyEM & shelly3EM
5051 # shellyEM has one meter, shelly3EM has three meters:
5152 for meter in meters :
52- total = total + meter ['power' ]
53+ power = power + meter ['power' ]
54+ currents = [0 , 0 , 0 ]
5355 else :
54- if 'switch:0' in status :
55- total = status ['switch:0' ]['apower' ]
56+ if 'switch:0' in status and 'apower' in status ['switch:0' ]:
57+ power = status ['switch:0' ]['apower' ]
58+ currents = [status ['switch:0' ]['current' ], 0 , 0 ]
59+ elif 'em1:0' in status :
60+ power = status ['em1:0' ]['act_power' ] # shelly Pro EM Gen 2
61+ currents = [status ['em1:0' ]['current' ], 0 , 0 ]
5662 elif 'pm1:0' in status :
57- total = status ['pm1:0' ]['apower' ] # shelly PM Mini Gen 3
63+ power = status ['pm1:0' ]['apower' ] # shelly PM Mini Gen 3
64+ currents = [status ['pm1:0' ]['current' ], 0 , 0 ]
5865 else :
59- total = status ['em:0' ]['total_act_power' ] # shelly Pro3EM
60- except KeyError :
61- log .exception ("unsupported shelly device?" )
62- finally :
63- return int (total )
66+ power = status ['em:0' ]['total_act_power' ] # shelly Pro3EM
67+ currents = [meter [f'{ i } _current' ] for i in 'abc' ]
6468
65- def update (self ) -> None :
66- bat = self .total_power_from_shelly () * self .factor
67- imported , exported = self .sim_counter .sim_count (bat )
68- bat_state = BatState (
69- power = bat ,
70- imported = imported ,
71- exported = exported
72- )
73- self .store .set (bat_state )
69+ power = power * self .factor
70+ imported , exported = self .sim_counter .sim_count (power )
71+ bat_state = BatState (
72+ power = power ,
73+ imported = imported ,
74+ exported = exported ,
75+ currents = currents
76+ )
77+ self .store .set (bat_state )
78+ except KeyError :
79+ log .exception ("unsupported shelly device." )
7480
7581
7682component_descriptor = ComponentDescriptor (configuration_factory = ShellyBatSetup )
0 commit comments