async def async_setup(hass: HomeAssistantType, config: ConfigType) -> bool: """Create a Genius Hub system.""" hass.data[DOMAIN] = {} kwargs = dict(config[DOMAIN]) if CONF_HOST in kwargs: args = (kwargs.pop(CONF_HOST), ) else: args = (kwargs.pop(CONF_TOKEN), ) hub_uid = kwargs.pop(CONF_MAC, None) client = GeniusHub(*args, **kwargs, session=async_get_clientsession(hass)) broker = hass.data[DOMAIN]["broker"] = GeniusBroker(hass, client, hub_uid) try: await client.update() except aiohttp.ClientResponseError as err: _LOGGER.error("Setup failed, check your configuration, %s", err) return False broker.make_debug_log_entries() async_track_time_interval(hass, broker.async_update, SCAN_INTERVAL) for platform in [ "climate", "water_heater", "sensor", "binary_sensor", "switch" ]: hass.async_create_task( async_load_platform(hass, platform, DOMAIN, {}, config)) setup_service_functions(hass, broker) return True
async def main(loop): """Return the JSON as requested.""" args = _parse_args() # print(args) if args is None: return if args.debug_mode > 0: import ptvsd print( f"Debugging is enabled, listening on: {DEBUG_ADDR}:{DEBUG_PORT}.") ptvsd.enable_attach(address=(DEBUG_ADDR, DEBUG_PORT)) if args.debug_mode > 1: print("Waiting for debugger to attach...") ptvsd.wait_for_attach() print("Debugger is attached!") if args.debug_mode > 2: breakpoint() # Option of providing test data (as list of Dicts), or leave both as None if FILE_MODE: with open("raw_zones.json", mode="r") as fh: z = json.loads(fh.read()) # file from: ghclient zones -vvv # z = ast.literal_eval(fh.read()) # file from HA logs with open("raw_devices.json", mode="r") as fh: d = json.loads(fh.read()) # file from: ghclient zones -vvv # d = ast.literal_eval(fh.read()) # file from HA logs session = None hub = GeniusTestHub(zones_json=z, device_json=d, debug=True) else: session = aiohttp.ClientSession() hub = GeniusHub( hub_id=args.hub_id, username=args.username, password=args.password, session=session, debug=args.debug_mode, ) hub.verbosity = args.verbosity await hub.update() # initialise: enumerate all zones, devices & issues # ait hub.update() # for testing, do twice in a row to check for no duplicates # these can be used for debugging, above - save as files, above # z = await hub._zones # raw_zones.json # d = await hub._devices # raw_devices.json if args.device_id: try: # does a Device with this ID exist? device = hub.device_by_id[ args.device_id] # device_id is a str, not an int except KeyError: raise KeyError( f"Device '{args.device_id}' does not exist (by addr).") print(device.data) # v0 = device, v1 = device.data, v3 = device._raw elif args.zone_id: try: # was the zone_id given as a str, or an int? zone_id = int(args.zone_id) except ValueError: zone_id = args.zone_id find_zone_by_key = hub.zone_by_name else: find_zone_by_key = hub.zone_by_id try: # does a Zone with this ID exist? zone = find_zone_by_key[zone_id] except KeyError: raise KeyError( f"Zone '{args.zone_id}' does not exist (by name or ID).") if args.mode: await zone.set_mode(args.mode) elif args.temp: await zone.set_override(args.temp, args.secs) elif args.command == "devices": print(json.dumps(zone.devices)) elif args.command == "issues": print(json.dumps(zone.issues)) else: # args.command == "info" if DEBUG_NO_SCHEDULES: _info = {k: v for k, v in zone.data.items() if k != "schedule"} print(json.dumps(_info)) else: print(json.dumps(zone.data)) else: # as per: args.hub_id if args.command == "reboot": raise NotImplementedError() # await hub.reboot() elif args.command == "zones": if DEBUG_NO_SCHEDULES: _zones = [{k: v for k, v in z.items() if k != "schedule"} for z in hub.zones] print(json.dumps(_zones)) else: print(json.dumps(hub.zones)) elif args.command == "devices": print(json.dumps(hub.devices)) elif args.command == "issues": print(json.dumps(hub.issues)) else: # args.command == "info" print(f"VER = {json.dumps(hub.version)}") print(f"UID = {hub.uid}") if hub.api_version == 3: print(f"XXX =", {"weatherData": hub.zone_by_id[0]._raw["weatherData"]}) if session: await session.close()
async def main(loop): """Return the JSON as requested.""" args = docopt(__doc__) # print(args) session = aiohttp.ClientSession() # test with/without # Option of providing test data (as list of Dicts), or leave both as None if FILE_MODE: with open("raw_zones.json", mode="r") as fh: # z = json.loads(fh.read()) # for O/P from ghclient zones -vvv z = ast.literal_eval(fh.read()) # more forgiving with open("raw_devices.json", mode="r") as fh: # d = json.loads(fh.read()) d = ast.literal_eval(fh.read()) hub = GeniusTestHub(zones_json=z, device_json=d, session=session, debug=True) else: hub = GeniusHub( hub_id=args[HUB_ID], username=args[USERNAME], password=args[PASSWORD], session=session, debug=False, ) hub.verbosity = args[VERBOSE] await hub.update() # initialise: enumerate all zones, devices & issues # ait hub.update() # for testing, do twice in a row to check for no duplicates # these can be used for debugging, above - save as files, above # z = await hub._zones # raw_zones.json # d = await hub._devices # raw_devices.json if args[DEVICE_ID]: key = args[DEVICE_ID] # a device_id is always a str, never an int try: # does a Device with this ID exist? device = hub.device_by_id[key] except KeyError: raise KeyError(f"Device '{args[DEVICE_ID]}' does not exist (by addr).") print(device.info) # detail depends upon verbosity (v=0..3) # is: device (v=0), device.data (v=1), device._raw (v=3), and device.assigned_zone elif args[ZONE_ID]: try: # was the zone_id given as a str, or an int? key = int(args[ZONE_ID]) except ValueError: key = args[ZONE_ID] find_zone_by_key = hub.zone_by_name else: find_zone_by_key = hub.zone_by_id try: # does a Zone with this ID exist? zone = find_zone_by_key[key] except KeyError: raise KeyError(f"Zone '{args[ZONE_ID]}' does not exist (by name or ID).") if args[MODE]: await zone.set_mode(args[MODE]) elif args[TEMP]: await zone.set_override(args[TEMP], args[SECS]) elif args[DEVICES]: print(json.dumps(zone.devices)) elif args[ISSUES]: print(json.dumps(zone.issues)) else: # as per args[INFO] print(json.dumps({k: v for k, v in zone.info.items() if k != "schedule"})) # is: zone (v=0) zone.data (v=1) and zone._raw (v=3) else: # as per: args[HUB_ID] if args[REBOOT]: # pylint: disable=no-else-raise raise NotImplementedError() # await hub.reboot() elif args[ZONES]: # print( # json.dumps([{k: v for k, v in i.items() if k != "schedule"} for i in hub.zones]) # ) print(json.dumps(hub.zones)) # TODO: this is for CI elif args[DEVICES]: print(json.dumps(hub.devices)) elif args[ISSUES]: print(json.dumps(hub.issues)) else: # as per args[INFO] print(json.dumps(hub.version)) print(hub.uid) if hub.api_version == 3: # pylint: disable=protected-access print({"weatherData": hub.zone_by_id[0]._raw["weatherData"]}) if session: await session.close()
def __init__(self, hass, args, kwargs): """Initialize the geniushub client.""" self.hass = hass self.client = hass.data[DOMAIN]["client"] = GeniusHub( *args, **kwargs, session=async_get_clientsession(hass))