package com.bwssystems.hass.test; import java.util.Map.Entry; import com.bwssystems.HABridge.plugins.hass.State; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonElement; public class StateDataConstructor { public final static String TestData = "[{\"attributes\": {\"Vera Device Id\": 43, \"friendly_name\": \"Family Ceiling\", \"supported_features\": 1}, \"entity_id\": \"light.family_ceiling\", \"last_changed\": \"2016-12-09T15:29:33.069914+00:00\", \"last_updated\": \"2016-12-09T15:29:33.069914+00:00\", \"state\": \"off\"}, {\"attributes\": {\"Vera Device Id\": 46, \"friendly_name\": \"Living Room\", \"supported_features\": 1}, \"entity_id\": \"light.living_room\", \"last_changed\": \"2016-12-09T15:29:33.038648+00:00\", \"last_updated\": \"2016-12-09T15:29:33.038648+00:00\", \"state\": \"off\"}, {\"attributes\": {\"Vera Device Id\": 59, \"friendly_name\": \"Test Light on CM15 (RF) P1\"}, \"entity_id\": \"switch.test_light_on_cm15_rf_p1\", \"last_changed\": \"2016-12-09T15:29:33.205020+00:00\", \"last_updated\": \"2016-12-09T15:29:33.205020+00:00\", \"state\": \"off\"}, {\"attributes\": {\"Vera Device Id\": 60, \"friendly_name\": \"Test Light on CM15 (PL) N1\", \"supported_features\": 1}, \"entity_id\": \"light.test_light_on_cm15_pl_n1\", \"last_changed\": \"2016-12-09T15:29:33.062192+00:00\", \"last_updated\": \"2016-12-09T15:29:33.062192+00:00\", \"state\": \"off\"}, {\"attributes\": {\"Vera Device Id\": 55, \"friendly_name\": \"Topiary Tree\"}, \"entity_id\": \"switch.topiary_tree\", \"last_changed\": \"2016-12-09T15:29:33.192865+00:00\", \"last_updated\": \"2016-12-09T15:29:33.192865+00:00\", \"state\": \"off\"}, {\"attributes\": {\"Vera Device Id\": 45, \"friendly_name\": \"House B\"}, \"entity_id\": \"switch.house_b\", \"last_changed\": \"2016-12-09T15:29:33.185026+00:00\", \"last_updated\": \"2016-12-09T15:29:33.185026+00:00\", \"state\": \"off\"}, {\"attributes\": {\"attribution\": \"Weather forecast from yr.no, delivered by the Norwegian Meteorological Institute and the NRK.\", \"entity_picture\": \"//api.met.no/weatherapi/weathericon/1.1/?symbol=3;content_type=image/png\", \"friendly_name\": \"yr Symbol\"}, \"entity_id\": \"sensor.yr_symbol\", \"last_changed\": \"2016-12-09T15:29:32.774160+00:00\", \"last_updated\": \"2016-12-09T15:29:32.774160+00:00\", \"state\": \"3\"}, {\"attributes\": {\"azimuth\": 156.57, \"elevation\": 21.62, \"friendly_name\": \"Sun\", \"next_rising\": \"2016-12-10T13:08:09+00:00\", \"next_setting\": \"2016-12-09T22:19:14+00:00\"}, \"entity_id\": \"sun.sun\", \"last_changed\": \"2016-12-09T15:29:30.986518+00:00\", \"last_updated\": \"2016-12-09T16:08:30.700707+00:00\", \"state\": \"above_horizon\"}, {\"attributes\": {\"Vera Device Id\": 36, \"friendly_name\": \"Test Light on a CM11\", \"supported_features\": 1}, \"entity_id\": \"light.test_light_on_a_cm11\", \"last_changed\": \"2016-12-09T15:29:33.085475+00:00\", \"last_updated\": \"2016-12-09T15:29:33.085475+00:00\", \"state\": \"off\"}, {\"attributes\": {\"Vera Device Id\": 6, \"friendly_name\": \"Couch Left Lamp\", \"supported_features\": 1}, \"entity_id\": \"light.couch_left_lamp\", \"last_changed\": \"2016-12-09T15:29:33.088762+00:00\", \"last_updated\": \"2016-12-09T15:29:33.088762+00:00\", \"state\": \"off\"}, {\"attributes\": {\"Vera Device Id\": 48, \"current_power_mwh\": 99000.0, \"friendly_name\": \"Kitchen\"}, \"entity_id\": \"switch.kitchen\", \"last_changed\": \"2016-12-09T15:38:34.682844+00:00\", \"last_updated\": \"2016-12-09T16:08:38.946423+00:00\", \"state\": \"on\"}, {\"attributes\": {\"Vera Device Id\": 35, \"friendly_name\": \"Living Room Accent\", \"supported_features\": 1}, \"entity_id\": \"light.living_room_accent\", \"last_changed\": \"2016-12-09T15:29:33.046514+00:00\", \"last_updated\": \"2016-12-09T15:29:33.046514+00:00\", \"state\": \"off\"}, {\"attributes\": {\"Vera Device Id\": 63, \"friendly_name\": \"HRM: TiVo DVR\"}, \"entity_id\": \"switch.hrm_tivo_dvr\", \"last_changed\": \"2016-12-09T15:29:33.195235+00:00\", \"last_updated\": \"2016-12-09T15:29:33.195235+00:00\", \"state\": \"off\"}, {\"attributes\": {\"Vera Device Id\": 47, \"friendly_name\": \"Harmony Control\"}, \"entity_id\": \"switch.harmony_control\", \"last_changed\": \"2016-12-09T15:29:33.200190+00:00\", \"last_updated\": \"2016-12-09T15:29:33.200190+00:00\", \"state\": \"off\"}, {\"attributes\": {\"Vera Device Id\": 44, \"friendly_name\": \"Fridge Light\", \"supported_features\": 1}, \"entity_id\": \"light.fridge_light\", \"last_changed\": \"2016-12-09T15:29:33.054270+00:00\", \"last_updated\": \"2016-12-09T15:29:33.054270+00:00\", \"state\": \"off\"}, {\"attributes\": {\"Vera Device Id\": 51, \"friendly_name\": \"Counter\"}, \"entity_id\": \"switch.counter\", \"last_changed\": \"2016-12-09T15:29:33.202557+00:00\", \"last_updated\": \"2016-12-09T15:29:33.202557+00:00\", \"state\": \"off\"}, {\"attributes\": {\"Vera Device Id\": 64, \"friendly_name\": \"X10 G2\"}, \"entity_id\": \"switch.x10_g2\", \"last_changed\": \"2016-12-09T15:29:33.190470+00:00\", \"last_updated\": \"2016-12-09T15:29:33.190470+00:00\", \"state\": \"off\"}, {\"attributes\": {\"Vera Device Id\": 28, \"friendly_name\": \"Desk Lamp\", \"supported_features\": 1}, \"entity_id\": \"light.desk_lamp\", \"last_changed\": \"2016-12-09T15:29:33.093874+00:00\", \"last_updated\": \"2016-12-09T15:29:33.093874+00:00\", \"state\": \"off\"}, {\"attributes\": {\"Vera Device Id\": 58, \"friendly_name\": \"Desk Outlet\"}, \"entity_id\": \"switch.desk_outlet\", \"last_changed\": \"2016-12-09T15:29:33.187971+00:00\", \"last_updated\": \"2016-12-09T15:29:33.187971+00:00\", \"state\": \"on\"}, {\"attributes\": {\"Vera Device Id\": 9, \"friendly_name\": \"Couch Right Lamp\", \"supported_features\": 1}, \"entity_id\": \"light.couch_right_lamp\", \"last_changed\": \"2016-12-09T15:29:33.077672+00:00\", \"last_updated\": \"2016-12-09T15:29:33.077672+00:00\", \"state\": \"off\"}, {\"attributes\": {\"Vera Device Id\": 61, \"friendly_name\": \"Test Light on CM15 (RF) M1\", \"supported_features\": 1}, \"entity_id\": \"light.test_light_on_cm15_rf_m1\", \"last_changed\": \"2016-12-09T15:29:33.091354+00:00\", \"last_updated\": \"2016-12-09T15:29:33.091354+00:00\", \"state\": \"off\"}, {\"attributes\": {\"auto\": true, \"entity_id\": [\"light.family_ceiling\", \"light.test_light_on_a_cm11\", \"light.living_room\", \"light.test_light_on_cm15_pl_n1\", \"light.fridge_light\", \"light.couch_left_lamp\", \"light.living_room_accent\", \"light.couch_right_lamp\", \"light.desk_lamp\", \"light.test_light_on_cm15_rf_m1\"], \"friendly_name\": \"all lights\", \"hidden\": true, \"order\": 0}, \"entity_id\": \"group.all_lights\", \"last_changed\": \"2016-12-09T15:29:33.100850+00:00\", \"last_updated\": \"2016-12-09T15:29:33.100850+00:00\", \"state\": \"off\"}, {\"attributes\": {\"auto\": true, \"entity_id\": [\"switch.x10_g2\", \"switch.kitchen\", \"switch.test_light_on_cm15_rf_p1\", \"switch.desk_outlet\", \"switch.counter\", \"switch.topiary_tree\", \"switch.house_b\", \"switch.hrm_tivo_dvr\", \"switch.harmony_control\"], \"friendly_name\": \"all switches\", \"hidden\": true, \"order\": 1}, \"entity_id\": \"group.all_switches\", \"last_changed\": \"2016-12-09T15:29:33.212637+00:00\", \"last_updated\": \"2016-12-09T15:29:33.212637+00:00\", \"state\": \"on\"}]"; public static void main(String[] args){ StateDataConstructor aTestReference = new StateDataConstructor(); if(aTestReference.validateStructure()) System.out.println("Test Successful"); } public Boolean validateStructure() { Gson aGson; try { aGson = new GsonBuilder().create(); State[] aTestObject = aGson.fromJson(TestData, State[].class); for(int i = 0; i < aTestObject.length; i++) { System.out.println( "<" + aTestObject[i].getEntityId() + ">,<" + aTestObject[i].getLastChanged() + ">,<" + aTestObject[i].getLastUpdated() + ">,<" + aTestObject[i].getState() + ">"); if(aTestObject[i].getAttributes() != null) { for(Entry<String, JsonElement> theEntry:aTestObject[i].getAttributes().entrySet()) { try { System.out.println(" " + theEntry.getKey() + " <" + theEntry.getValue().getAsString() + ">"); } catch(IllegalStateException e) { System.out.println(" " + theEntry.getKey() + " <Array with size " + theEntry.getValue().getAsJsonArray().size() + ">"); } } } } } catch (Exception e) { return false; } return true; } }