HomeAssistant Python API Timing Out on set_state and call_service, but not get_state?

This is weird. When I see a timeout I expect that means I’m pointing at the wrong thing, but I can use the API to get the state of any and all of my entities so I know it is pointing in the right direction. Can anyone see some flaw in my code here? Why do I keep getting these timeout errors?

Running the latest HA on Ubuntu 16.04, Python 3.5.2 and I also tested it on 3.7.1.

import homeassistant.remote as remote
from homeassistant.const import STATE_OFF, STATE_ON

api = remote.API('127.0.0.1', 'my_pass_goes_here')

def get_light_status():
    data = remote.get_state(api, entity_id='light.keylight_level')
    if data.state == 'off':
        print('Turning on Key Light!')
        remote.call_service(api, 'light.turn_on', 'light.keylight_level',
                        {"rgb_color": [255, 255, 255],
                         "brightness": 75})
    else:
        print('Turning off Key Light!')
        remote.set_state(api, 'light.keylight_level', new_state=STATE_OFF)

So the get_state is returning useful data, and my code knows when it is supposed to be turning the light on or off. But it fails when I try to actually do either of those with a call_service or set_state. I get the same timeout error in both cases.

File “/usr/local/lib/python3.5/dist-packages/urllib3/connectionpool.py”, line 384, in _make_request
six.raise_from(e, None)
File “”, line 2, in raise_from
File “/usr/local/lib/python3.5/dist-packages/urllib3/connectionpool.py”, line 380, in _make_request
httplib_response = conn.getresponse()
File “/usr/lib/python3.5/http/client.py”, line 1197, in getresponse
response.begin()
File “/usr/lib/python3.5/http/client.py”, line 297, in begin
version, status, reason = self._read_status()
File “/usr/lib/python3.5/http/client.py”, line 258, in _read_status
line = str(self.fp.readline(_MAXLINE + 1), “iso-8859-1”)
File “/usr/lib/python3.5/socket.py”, line 575, in readinto
return self._sock.recv_into(b)
socket.timeout: timed out

Big thanks to anyone who can offer any insights! I’m totally stumped. I’ve tried varying things in the calls like changing the domain to just “light” and adding or removing an entitiy_id field from the call.

Hi

May I ask you where you found the “import homeassistant.remote as remote” file? The link on Python API page is not working.