1818async def async_setup_entry (hass , entry , async_add_entities ):
1919 """Set up button platform."""
2020 host_ip = entry .data [CONF_HOST_IP ]
21- coordinator = BituoDataUpdateCoordinator (hass , host_ip )
2221 try :
23- await coordinator .async_config_entry_first_refresh ()
24- except UpdateFailed as e :
25- _LOGGER .error ("Error initializing button platform: %s" , e )
26- raise ConfigEntryNotReady from e
27-
28- # Fetch device model and firmware version
29- try :
30- device_info = await coordinator .fetch_device_info ()
31- except UpdateFailed :
32- _LOGGER .error ("Failed to fetch device info for %s" , host_ip )
33- device_info = {"model" : "Unknown Model" , "fw_version" : "Unknown" , "manufacturer" : "Unknown" , "MCUVersion" : "Unknown" , "manufacturer" : "Unknown" , "mcu_version" : "Unknown" }
34-
35- buttons = []
22+ response = await hass .async_add_executor_job (
23+ requests .get , f"http://{ host_ip } /data"
24+ )
25+ data = response .json ()
26+ device_info = {
27+ "model" : data .get ("productModel" ) or data .get ("ProductModel" , "Unknown Model" ),
28+ "fw_version" : data .get ("FWVersion" ) or data .get ("fwVersion" , "Unknown" ),
29+ "manufacturer" : data .get ("Manufactor" , "Unknown" ),
30+ "mcu_version" : data .get ("MCUVersion" , "Unknown" ),
31+ }
32+ except Exception as e :
33+ _LOGGER .error ("Failed to fetch device info for %s: %s" , host_ip , e )
34+ device_info = {
35+ "model" : "Unknown Model" ,
36+ "fw_version" : "Unknown" ,
37+ "manufacturer" : "Unknown" ,
38+ "mcu_version" : "Unknown" ,
39+ }
3640
3741 sensor_coordinator = hass .data [DOMAIN ][entry .entry_id ]['sensor_coordinator' ]
38- buttons .append (DataRefreshButton (sensor_coordinator , host_ip , device_info ["model" ], device_info ["fw_version" ], device_info ["manufacturer" ], device_info ["mcu_version" ]))
39-
40- if coordinator .data :
41- for field , action in coordinator .data .items ():
42- if "switch" in field .lower ():
43- continue # Skip buttons with 'switch' in the name
44- if field == "zero" :
45- field = "zero_Energy" # Rename 'zero' to 'zeroenergy'
46- buttons .append (BituoButton (coordinator , host_ip , field , action , device_info ["model" ], device_info ["fw_version" ], device_info ["manufacturer" ], device_info ["mcu_version" ]))
4742
48- async_add_entities (buttons , True )
49-
50- class BituoDataUpdateCoordinator (DataUpdateCoordinator ):
51- """Class to manage fetching data from the device."""
52-
53- def __init__ (self , hass , host_ip ):
54- """Initialize."""
55- self .host_ip = host_ip
56- super ().__init__ (hass , _LOGGER , name = DOMAIN , update_interval = SCAN_INTERVAL )
57-
58- async def _async_update_data (self ):
59- """Fetch data from the device."""
60- try :
61- response = await self .hass .async_add_executor_job (
62- requests .get , f"http://{ self .host_ip } /hadata"
63- )
64- return response .json ()
65- except Exception as err :
66- raise UpdateFailed (f"Error communicating with API: { err } " )
43+ buttons = [
44+ DataRefreshButton (sensor_coordinator , host_ip , device_info ["model" ], device_info ["fw_version" ], device_info ["manufacturer" ], device_info ["mcu_version" ]),
45+ DeviceLocatingButton (host_ip , device_info ["model" ], device_info ["fw_version" ], device_info ["manufacturer" ], device_info ["mcu_version" ])
46+ ]
6747
68- async def fetch_device_info (self ):
69- """Fetch device model and firmware version information."""
70- try :
71- response = await self .hass .async_add_executor_job (
72- requests .get , f"http://{ self .host_ip } /data"
73- )
74- data = response .json ()
75- return {
76- "model" : data .get ("productModel" ) or data .get ("ProductModel" , "Unknown Model" ),
77- "fw_version" : data .get ("FWVersion" ) or data .get ("fwVersion" , "Unknown" ),
78- "manufacturer" : data .get ("Manufactor" , "Unknown" ),
79- "mcu_version" : data .get ("MCUVersion" , "Unknown" ),
80- }
81- except Exception as err :
82- raise UpdateFailed (f"Error fetching device info: { err } " )
48+ async_add_entities (buttons , True )
8349
84- class BituoButton ( CoordinatorEntity , ButtonEntity ):
50+ class DeviceLocatingButton ( ButtonEntity ):
8551 """Representation of a Button."""
8652
87- def __init__ (self , coordinator , host_ip , field , action , model , fw_version , manufacturer , mcu_version ):
53+ def __init__ (self , host_ip , model , fw_version , manufacturer , mcu_version ):
8854 """Initialize the button."""
89- super ().__init__ (coordinator )
90- self ._field = field
91- self ._action = action
92- self ._attr_name = f"{ field .replace ('_' , ' ' ).title ()} "
93- self ._attr_unique_id = f"{ host_ip } _{ field } "
94- self .entity_id = f"button.{ host_ip .replace ('.' , '_' )} _{ self .format_field_entity_id (field )} "
55+ self ._attr_name = "Device Locating"
56+ self ._attr_unique_id = f"{ host_ip } _device_locating"
57+ self .entity_id = f"button.{ host_ip .replace ('.' , '_' )} _device_locating"
9558 self ._attr_device_info = DeviceInfo (
9659 identifiers = {(DOMAIN , host_ip )},
9760 name = f"{ model } - { host_ip } " ,
@@ -101,11 +64,12 @@ def __init__(self, coordinator, host_ip, field, action, model, fw_version, manuf
10164 configuration_url = f"http://{ host_ip } " # embed URL
10265 )
10366 self ._host_ip = host_ip
67+ self ._attr_icon = "mdi:map-marker"
10468
10569 async def async_press (self ):
10670 """Handle the button press."""
10771 await self .hass .async_add_executor_job (
108- requests .get , f"http://{ self ._host_ip } /{ self . _action } "
72+ requests .get , f"http://{ self ._host_ip } /location "
10973 )
11074
11175 @staticmethod
0 commit comments