2525import org .openhab .binding .freeboxos .internal .action .FreeplugActions ;
2626import org .openhab .binding .freeboxos .internal .api .FreeboxException ;
2727import org .openhab .binding .freeboxos .internal .api .rest .FreeplugManager ;
28+ import org .openhab .binding .freeboxos .internal .api .rest .FreeplugManager .Freeplug ;
2829import org .openhab .core .library .types .QuantityType ;
2930import org .openhab .core .library .unit .Units ;
3031import org .openhab .core .thing .Channel ;
3132import org .openhab .core .thing .Thing ;
33+ import org .openhab .core .thing .ThingStatus ;
34+ import org .openhab .core .thing .ThingStatusDetail ;
3235import org .openhab .core .thing .binding .ThingHandlerService ;
3336import org .slf4j .Logger ;
3437import org .slf4j .LoggerFactory ;
@@ -47,17 +50,21 @@ public class FreeplugHandler extends ApiConsumerHandler {
4750
4851 public FreeplugHandler (Thing thing ) {
4952 super (thing );
53+ statusDrivenByBridge = false ;
5054 }
5155
52- @ Override
53- void initializeProperties (Map <String , String > properties ) throws FreeboxException {
56+ private MACAddress getCheckMac () throws FreeboxException {
5457 MACAddress mac = getMac ();
5558 if (mac == null ) {
56- throw new FreeboxException (
57- "initializeProperties is not possible because MAC address is undefined for the thing "
58- + thing .getUID ());
59+ throw new FreeboxException ("MAC address is undefined for the thing " + thing .getUID ());
5960 }
60- getManager (FreeplugManager .class ).getPlug (mac ).ifPresent (plug -> {
61+ return mac ;
62+ }
63+
64+ @ Override
65+ void initializeProperties (Map <String , String > properties ) throws FreeboxException {
66+ MACAddress mac = getCheckMac ();
67+ if (getManager (FreeplugManager .class ).getPlug (mac ) instanceof Freeplug plug ) {
6168 properties .put (Thing .PROPERTY_MODEL_ID , plug .model ());
6269 properties .put (ROLE , plug .netRole ().name ());
6370 properties .put (NET_ID , plug .netId ());
@@ -73,25 +80,30 @@ void initializeProperties(Map<String, String> properties) throws FreeboxExceptio
7380 updateThing (editThing ().withChannels (channels ).build ());
7481 }
7582 }
76- });
83+ } else {
84+ throw new FreeboxException ("Freeplug is absent" );
85+ }
7786 }
7887
7988 @ Override
8089 protected void internalPoll () throws FreeboxException {
81- MACAddress mac = getMac ();
82- if (mac == null ) {
83- throw new FreeboxException (
84- "internalPoll is not possible because MAC address is undefined for the thing " + thing .getUID ());
85- }
86- getManager (FreeplugManager .class ).getPlug (mac ).ifPresent (plug -> {
90+ MACAddress mac = getCheckMac ();
91+ if (getManager (FreeplugManager .class ).getPlug (mac ) instanceof Freeplug plug ) {
8792 updateChannelDateTimeState (LAST_SEEN , Instant .now ().minusSeconds (plug .inactive ()));
8893
8994 updateChannelString (LINE_STATUS , plug .ethPortStatus ());
9095 updateChannelOnOff (REACHABLE , plug .hasNetwork ());
9196
9297 updateRateChannel (RATE + "-down" , plug .rxRate ());
9398 updateRateChannel (RATE + "-up" , plug .txRate ());
94- });
99+ if (plug .hasNetwork ()) {
100+ updateStatus (ThingStatus .ONLINE );
101+ } else {
102+ updateStatus (ThingStatus .OFFLINE , ThingStatusDetail .NONE , "@text/info-plug-not-reachable" );
103+ }
104+ } else {
105+ throw new FreeboxException ("Freeplug is absent" );
106+ }
95107 }
96108
97109 private void updateRateChannel (String channel , int rate ) {
@@ -100,17 +112,12 @@ private void updateRateChannel(String channel, int rate) {
100112 }
101113
102114 public void reset () {
103- MACAddress mac = getMac ();
104- if (mac == null ) {
105- logger .warn ("Freeplug restart is not possible because MAC address is undefined for the thing {}" ,
106- thing .getUID ());
107- return ;
108- }
109115 try {
116+ MACAddress mac = getCheckMac ();
110117 getManager (FreeplugManager .class ).reboot (mac );
111- logger .debug ("Freeplug {} succesfully restarted" , mac );
118+ logger .debug ("Freeplug {} successfully restarted" , mac );
112119 } catch (FreeboxException e ) {
113- logger .warn ("Error restarting freeplug {}: {}" , mac , e .getMessage ());
120+ logger .warn ("Error restarting freeplug {}: {}" , thing . getUID () , e .getMessage ());
114121 }
115122 }
116123
0 commit comments