"""WeatherLink Live Local Python API."""from__future__importannotationsimportjsonimporturllib.requestfromweatherlink_live_localimportconditions,discovery,units
[docs]defdiscover(timeout:int=1)->list[discovery.ServiceInfo]:""" Discover all WeatherLink Live services on local network(s). Args: timeout: Timeout in seconds Returns: List of found services """returndiscovery.Discovery.find(timeout=timeout)
[docs]defparse_response(json_str:str,units:units.Units)->conditions.Conditions:""" Parse JSON response from WeatherLink Live API. Args: json_str: Raw JSON response as str units: Units of the conditions Returns: Conditions of all available sensors """json_dict=json.loads(json_str)returnconditions.Conditions.from_dict(json_dict["data"],units)
[docs]defget_conditions(ip:str,units:units.Units,port:int=80,timeout:int=1,)->conditions.Conditions:""" Read conditions from WeatherLink Live device + all connected sensors. Args: ip: IP address of WeatherLink Live device. Use `discover` function to find devices with their IP address in the local network. units: Units of the conditions port: Port number of HTTP interface, should be 80 timeout: Maximum time to listen for WeatherLink Live services Returns: Conditions of all available sensors """url=f"http://{ip}:{port}/v1/current_conditions"withurllib.request.urlopen(url,timeout=timeout)asresp:# noqa: S310ifresp.status!=200:raiseRuntimeError(f"HTTP response code {resp.status}")json_str=resp.read().decode("utf-8")returnparse_response(json_str,units)