<commit_before>"""Host controll for HassIO."""
import asyncio
import json
import logging
import os
import stat

from .const import SOCKET_HC

_LOGGER = logging.getLogger(__name__)


class HostControll(object):
    """Client for host controll."""

    def __init__(self, loop):
        """Initialize HostControll socket client."""
        self.loop = loop
        self.active = False

        mode = os.stat(SOCKET_HC)[stat.ST_MODE]
        if stat.S_ISSOCK(mode):
            self.active = True

    async def _send_command(self, command):
        """Send command to host.

        Is a coroutine.
        """
        if not self.active:
            return

        reader, writer = await asyncio.open_unix_connection(
            SOCKET_HC, loop=self.loop)

        # send
        _LOGGER.info("Send '%s' to HostControll.", command)
        writer.write(command.encode())

        # receive
        data = await reader.readline()
        response = data.decode().Upper()
        _LOGGER.info("Receive from HostControll: %s.", response)

        if response == "OK":
            return True
        elif response == "ERROR":
            return False
        else:
            return json.loads(response)

    def info(self):
        """Return Info from host.

        Return a coroutine.
        """
        return self._send_command("info")

    def reboot(self):
        """Reboot the host system.

        Return a coroutine.
        """
        return self._send_command("reboot")

    def shutdown(self):
        """Shutdown the host system.

        Return a coroutine.
        """
        return self._send_command("shutdown")

    def host_update(self, version=None):
        """Update the host system.

        Return a coroutine.
        """
        if version:
            return self._send_command("host-update " + version)
        return self._send_command("host-update")

    def supervisor_update(self, version=None):
        """Update the supervisor on host system.

        Return a coroutine.
        """
        if version:
            return self._send_command("supervisor-update " + version)
        return self._send_command("supervisor-update")
<commit_msg>Fix charset and timeout handling for HostControll<commit_after>"""Host controll for HassIO."""
import asyncio
import json
import logging
import os
import stat

from .const import SOCKET_HC

_LOGGER = logging.getLogger(__name__)

TIMEOUT = 15


class HostControll(object):
    """Client for host controll."""

    def __init__(self, loop):
        """Initialize HostControll socket client."""
        self.loop = loop
        self.active = False

        mode = os.stat(SOCKET_HC)[stat.ST_MODE]
        if stat.S_ISSOCK(mode):
            self.active = True

    async def _send_command(self, command):
        """Send command to host.

        Is a coroutine.
        """
        if not self.active:
            return

        reader, writer = await asyncio.open_unix_connection(
            SOCKET_HC, loop=self.loop)

        try:
            # send
            _LOGGER.info("Send '%s' to HostControll.", command)
            writer.write("{}\n".format(command).encode())

            # receive
            with async_timeout.timeout(TIMEOUT, loop=self.loop):
                data = await reader.readline()
            response = data.decode().Upper()
            _LOGGER.info("Receive from HostControll: %s.", response)

            if response == "OK":
                return True
            elif response == "ERROR":
                return False
            else:
                return json.loads(response)

        except asyncio.TimeoutError:
            _LOGGER.error("Timeout from HostControll!")

        finally:
            writer.close()

    def info(self):
        """Return Info from host.

        Return a coroutine.
        """
        return self._send_command("info")

    def reboot(self):
        """Reboot the host system.

        Return a coroutine.
        """
        return self._send_command("reboot")

    def shutdown(self):
        """Shutdown the host system.

        Return a coroutine.
        """
        return self._send_command("shutdown")

    def host_update(self, version=None):
        """Update the host system.

        Return a coroutine.
        """
        if version:
            return self._send_command("host-update {}".format(version))
        return self._send_command("host-update")

    def supervisor_update(self, version=None):
        """Update the supervisor on host system.

        Return a coroutine.
        """
        if version:
            return self._send_command("supervisor-update {}".format(version))
        return self._send_command("supervisor-update")

{'fraction_non_alphanumeric': 0.10500636402206194, 'fraction_numerical': 0.00042426813746287653, 'mean_word_length': 1.9785217940619078, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5196932', 'n_tokens_mistral': 1294, 'n_tokens_neox': 1255, 'n_words': 347}