async def test_http_error(event_loop, aresponses): """Test HTTP error response handling.""" aresponses.add( "api.fumis.si", "/", "GET", aresponses.Response(text="OMG PUPPIES!", status=404), ) aresponses.add( "api.fumis.si", "/", "GET", aresponses.Response(text="OMG PUPPIES!", status=500), ) async with aiohttp.ClientSession(loop=event_loop) as session: fumis = Fumis( mac="AABBCCDDEEFF", password="******", session=session, loop=event_loop, request_timeout=1, ) with pytest.raises(FumisError): assert await fumis._request("/") with pytest.raises(FumisError): assert await fumis._request("/")
async def test_turn_on(event_loop, aresponses): """Test turning on Fumis WiRCU device.""" # Handle to run asserts on request in async def response_handler(request): data = await request.json() assert data == { "unit": { "id": "AABBCCDDEEFF", "type": 0, "pin": "1234" }, "apiVersion": "1", "controller": { "command": 2, "type": 0 }, } return aresponses.Response( status=200, headers={"Content-Type": "application/json"}, text="", ) aresponses.add("api.fumis.si", "/v1/status/", "POST", response_handler) async with aiohttp.ClientSession(loop=event_loop) as session: fumis = Fumis( mac="AABBCCDDEEFF", password="******", session=session, loop=event_loop, ) await fumis.turn_on()
async def main(loop): """Show example on controlling your Fumis WiRCU device.""" async with Fumis(mac="AABBCCDDEEFF", password="******", loop=loop) as fumis: info = await fumis.update_info() print(info) await fumis.set_target_temperature(23.0)
async def test_request_user_agent(event_loop, aresponses): """Test client sending correct user agent headers.""" # Handle to run asserts on request in async def response_handler(request): assert request.headers["User-Agent"] == f"PythonFumis/{__version__}" return aresponses.Response( status=200, headers={"Content-Type": "application/json"}, text="{}", ) aresponses.add("api.fumis.si", "/", "GET", response_handler) async with aiohttp.ClientSession(loop=event_loop) as session: fumis = Fumis( mac="AABBCCDDEEFF", password="******", session=session, loop=event_loop, ) await fumis._request("/")
async def test_internal_eventloop(aresponses): """Test internal event loop creation is handled correctly.""" aresponses.add( "api.fumis.si", "/", "GET", aresponses.Response( status=200, headers={"Content-Type": "application/json"}, text='{"test": "ok"}', ), ) async with Fumis(mac="AABBCCDDEEFF", password="******") as fumis: response = await fumis._request("/") assert response["test"] == "ok"
async def test_json_request(event_loop, aresponses): """Test JSON response is handled correctly.""" aresponses.add( "api.fumis.si", "/", "GET", aresponses.Response( status=200, headers={"Content-Type": "application/json"}, text='{"test": "ok"}', ), ) async with aiohttp.ClientSession(loop=event_loop) as session: fumis = Fumis( mac="AABBCCDDEEFF", password="******", session=session, loop=event_loop, ) response = await fumis._request("/") assert response["test"] == "ok"
async def test_invalid_content_type(event_loop, aresponses): """Test invalid content type from Fumis WiRCU API.""" aresponses.add( "api.fumis.si", "/", "GET", aresponses.Response( status=200, headers={"Content-Type": "other/content"}, text="{}", ), ) async with aiohttp.ClientSession(loop=event_loop) as session: fumis = Fumis( mac="AABBCCDDEEFF", password="******", session=session, loop=event_loop, request_timeout=1, ) with pytest.raises(FumisError): await fumis._request("/")
async def test_timeout(event_loop, aresponses): """Test request timeout from Fumis WiRCU API.""" # Faking a timeout by sleeping async def response_handler(_): await asyncio.sleep(2) return aresponses.Response( status=200, headers={"Content-Type": "application/json"}, text="{}", ) aresponses.add("api.fumis.si", "/", "GET", response_handler) async with aiohttp.ClientSession(loop=event_loop) as session: fumis = Fumis( mac="AABBCCDDEEFF", password="******", session=session, loop=event_loop, request_timeout=1, ) with pytest.raises(FumisConnectionError): assert await fumis._request("/")
async def test_info_none(event_loop, aresponses): """Test info data is None when communication has occured.""" # Handle to run asserts on request in aresponses.add( "api.fumis.si", "/v1/status", "GET", aresponses.Response( status=500, headers={"Content-Type": "application/json"}, text="Invalid response", ), ) aresponses.add( "api.fumis.si", "/v1/status", "GET", aresponses.Response( status=200, headers={"Content-Type": "application/json"}, text="", ), ) async with aiohttp.ClientSession(loop=event_loop) as session: fumis = Fumis( mac="AABBCCDDEEFF", password="******", session=session, loop=event_loop, ) with pytest.raises(FumisError): await fumis.update_info() assert fumis.info is None with pytest.raises(FumisError): await fumis.update_info() assert fumis.info is None
async def test_info_update(event_loop, aresponses): """Test getting Fumis WiRCU device information and states.""" aresponses.add( "api.fumis.si", "/v1/status", "GET", aresponses.Response( status=200, headers={"Content-Type": "application/json"}, text=load_fixture("info.json"), ), ) async with aiohttp.ClientSession(loop=event_loop) as session: fumis = Fumis( mac="AABBCCDDEEFF", password="******", session=session, loop=event_loop, ) info = await fumis.update_info() assert info assert info.unit_id == "AABBCCDDEEFF" assert info.unit_version == "2.0.0" assert info.controller_version == "1.7.0" assert info.ip == "192.168.1.2" assert info.rssi == -48 assert info.signal_strength == 100 assert info.state == "off" assert info.state_id == 1 assert info.status == "off" assert info.status_id == 0 assert info.temperature == 19.9 assert info.target_temperature == 21.8 assert info.heating_time == 1823340 assert info.igniter_starts == 392 assert info.misfires == 0 assert info.overheatings == 0 assert info.uptime == 58184580
async def test_signal_strength_0(event_loop, aresponses): """Test retreiving Fumis WiRCU device WiFi signal strength with -100 dB.""" aresponses.add( "api.fumis.si", "/v1/status", "GET", aresponses.Response( status=200, headers={"Content-Type": "application/json"}, text='{"unit": {"rssi": -100}}', ), ) async with aiohttp.ClientSession(loop=event_loop) as session: fumis = Fumis( mac="AABBCCDDEEFF", password="******", session=session, loop=event_loop, ) info = await fumis.update_info() assert info assert info.rssi == -100 assert info.signal_strength == 0