Browse Source

smartir clone

Vassilis Panos 7 years ago
parent
commit
7be872b899
7 changed files with 1210 additions and 0 deletions
  1. 173 0
      smartir/__init__.py
  2. 385 0
      smartir/climate.py
  3. 102 0
      smartir/controller.py
  4. 260 0
      smartir/fan.py
  5. 22 0
      smartir/manifest.json
  6. 264 0
      smartir/media_player.py
  7. 4 0
      smartir/services.yaml

+ 173 - 0
smartir/__init__.py

@@ -0,0 +1,173 @@
+import asyncio
+import binascii
+from distutils.version import StrictVersion
+import json
+import logging
+import os.path
+import requests
+import struct
+import voluptuous as vol
+
+from homeassistant.const import (
+    ATTR_FRIENDLY_NAME, __version__ as current_ha_version)
+import homeassistant.helpers.config_validation as cv
+from homeassistant.helpers.typing import ConfigType
+
+_LOGGER = logging.getLogger(__name__)
+
+DOMAIN = 'smartir'
+VERSION = '1.6.2'
+MANIFEST_URL = (
+    "https://raw.githubusercontent.com/"
+    "smartHomeHub/SmartIR/{}/"
+    "custom_components/smartir/manifest.json")
+REMOTE_BASE_URL = (
+    "https://raw.githubusercontent.com/"
+    "smartHomeHub/SmartIR/{}/"
+    "custom_components/smartir/")
+COMPONENT_ABS_DIR = os.path.dirname(
+    os.path.abspath(__file__))
+
+CONF_CHECK_UPDATES = 'check_updates'
+CONF_UPDATE_BRANCH = 'update_branch'
+
+CONFIG_SCHEMA = vol.Schema({
+    DOMAIN: vol.Schema({
+        vol.Optional(CONF_CHECK_UPDATES, default=True): cv.boolean,
+        vol.Optional(CONF_UPDATE_BRANCH, default='master'): vol.In(
+            ['master', 'rc'])
+    })
+}, extra=vol.ALLOW_EXTRA)
+
+async def async_setup(hass, config):
+    """Set up the SmartIR component."""
+    conf = config.get(DOMAIN)
+
+    if conf is None:
+        return True
+
+    check_updates = conf[CONF_CHECK_UPDATES]
+    update_branch = conf[CONF_UPDATE_BRANCH]
+
+    async def _check_updates(service):
+        await _update(hass, update_branch)
+
+    async def _update_component(service):
+        await _update(hass, update_branch, True)
+
+    hass.services.async_register(DOMAIN, 'check_updates', _check_updates)
+    hass.services.async_register(DOMAIN, 'update_component', _update_component)
+
+    if check_updates:
+        await _update(hass, update_branch, False, False)
+
+    return True
+
+async def _update(hass, branch, do_update=False, notify_if_latest=True):
+    try:
+        request = requests.get(MANIFEST_URL.format(branch), stream=True, timeout=10)
+    except:
+        _LOGGER.error("An error occurred while checking for updates. "
+                      "Please check your internet connection.")
+        return
+
+    if request.status_code != 200:
+        _LOGGER.error("Invalid response from the server while "
+                      "checking for a new version")
+        return
+
+    data = request.json()
+    min_ha_version = data['homeassistant']
+    last_version = data['updater']['version']
+    release_notes = data['updater']['releaseNotes']
+
+    if StrictVersion(last_version) <= StrictVersion(VERSION):
+        if notify_if_latest:
+            hass.components.persistent_notification.async_create(
+                "You're already using the latest version!", title='SmartIR')
+        return
+
+    if StrictVersion(current_ha_version) < StrictVersion(min_ha_version):
+        hass.components.persistent_notification.async_create(
+            "There is a new version of SmartIR integration, but it is **incompatible** "
+            "with your system. Please first update Home Assistant.", title='SmartIR')
+        return
+
+    if do_update is False:
+        hass.components.persistent_notification.async_create(
+            "A new version of SmartIR integration is available ({}). "
+            "Call the ``smartir.update_component`` service to update "
+            "the integration. \n\n **Release notes:** \n{}"
+            .format(last_version, release_notes), title='SmartIR')
+        return
+
+    # Begin update
+    files = data['updater']['files']
+    has_errors = False
+
+    for file in files:
+        try:
+            source = REMOTE_BASE_URL.format(branch) + file
+            dest = os.path.join(COMPONENT_ABS_DIR, file)
+            os.makedirs(os.path.dirname(dest), exist_ok=True)
+            Helper.downloader(source, dest)
+        except:
+            has_errors = True
+            _LOGGER.error("Error updating %s. Please update the file manually.", file)
+
+    if has_errors:
+        hass.components.persistent_notification.async_create(
+            "There was an error updating one or more files of SmartIR. "
+            "Please check the logs for more information.", title='SmartIR')
+    else:
+        hass.components.persistent_notification.async_create(
+            "Successfully updated to {}. Please restart Home Assistant."
+            .format(last_version), title='SmartIR')
+
+class Helper():
+    @staticmethod
+    def downloader(source, dest):
+        req = requests.get(source, stream=True, timeout=10)
+
+        if req.status_code == 200:
+            with open(dest, 'wb') as fil:
+                for chunk in req.iter_content(1024):
+                    fil.write(chunk)
+        else:
+            raise Exception("File not found")
+
+    @staticmethod
+    def pronto2lirc(pronto):
+        codes = [int(binascii.hexlify(pronto[i:i+2]), 16) for i in range(0, len(pronto), 2)]
+
+        if codes[0]:
+            raise ValueError("Pronto code should start with 0000")
+        if len(codes) != 4 + 2 * (codes[2] + codes[3]):
+            raise ValueError("Number of pulse widths does not match the preamble")
+
+        frequency = 1 / (codes[1] * 0.241246)
+        return [int(round(code / frequency)) for code in codes[4:]]
+
+    @staticmethod
+    def lirc2broadlink(pulses):
+        array = bytearray()
+
+        for pulse in pulses:
+            pulse = int(pulse * 269 / 8192)
+
+            if pulse < 256:
+                array += bytearray(struct.pack('>B', pulse))
+            else:
+                array += bytearray([0x00])
+                array += bytearray(struct.pack('>H', pulse))
+
+        packet = bytearray([0x26, 0x00])
+        packet += bytearray(struct.pack('<H', len(array)))
+        packet += array
+        packet += bytearray([0x0d, 0x05])
+
+        # Add 0s to make ultimate packet size a multiple of 16 for 128-bit AES encryption.
+        remainder = (len(packet) + 4) % 16
+        if remainder:
+            packet += bytearray(16 - remainder)
+        return packet

+ 385 - 0
smartir/climate.py

@@ -0,0 +1,385 @@
+import asyncio
+import json
+import logging
+import os.path
+
+import voluptuous as vol
+
+from homeassistant.components.climate import ClimateDevice, PLATFORM_SCHEMA
+from homeassistant.components.climate.const import (
+    HVAC_MODE_OFF, HVAC_MODE_HEAT, HVAC_MODE_COOL,
+    HVAC_MODE_DRY, HVAC_MODE_FAN_ONLY, HVAC_MODE_AUTO,
+    SUPPORT_TARGET_TEMPERATURE, SUPPORT_FAN_MODE,
+    HVAC_MODES, ATTR_HVAC_MODE)
+from homeassistant.const import (
+    CONF_NAME, STATE_ON, STATE_UNKNOWN, ATTR_TEMPERATURE,
+    PRECISION_TENTHS, PRECISION_HALVES, PRECISION_WHOLE)
+from homeassistant.core import callback
+from homeassistant.helpers.event import async_track_state_change
+import homeassistant.helpers.config_validation as cv
+from homeassistant.helpers.restore_state import RestoreEntity
+from . import COMPONENT_ABS_DIR, Helper
+from .controller import Controller
+
+_LOGGER = logging.getLogger(__name__)
+
+DEFAULT_NAME = "SmartIR Climate"
+
+CONF_UNIQUE_ID = 'unique_id'
+CONF_DEVICE_CODE = 'device_code'
+CONF_CONTROLLER_DATA = "controller_data"
+CONF_TEMPERATURE_SENSOR = 'temperature_sensor'
+CONF_HUMIDITY_SENSOR = 'humidity_sensor'
+CONF_POWER_SENSOR = 'power_sensor'
+
+SUPPORT_FLAGS = (
+    SUPPORT_TARGET_TEMPERATURE | 
+    SUPPORT_FAN_MODE
+)
+
+PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
+    vol.Optional(CONF_UNIQUE_ID): cv.string,
+    vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
+    vol.Required(CONF_DEVICE_CODE): cv.positive_int,
+    vol.Required(CONF_CONTROLLER_DATA): cv.string,
+    vol.Optional(CONF_TEMPERATURE_SENSOR): cv.entity_id,
+    vol.Optional(CONF_HUMIDITY_SENSOR): cv.entity_id,
+    vol.Optional(CONF_POWER_SENSOR): cv.entity_id
+})
+
+async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
+    """Set up the IR Climate platform."""
+    device_code = config.get(CONF_DEVICE_CODE)
+    device_files_subdir = os.path.join('codes', 'climate')
+    device_files_absdir = os.path.join(COMPONENT_ABS_DIR, device_files_subdir)
+
+    if not os.path.isdir(device_files_absdir):
+        os.makedirs(device_files_absdir)
+
+    device_json_filename = str(device_code) + '.json'
+    device_json_path = os.path.join(device_files_absdir, device_json_filename)
+
+    if not os.path.exists(device_json_path):
+        _LOGGER.warning("Couldn't find the device Json file. The component will " \
+                        "try to download it from the GitHub repo.")
+
+        try:
+            codes_source = ("https://raw.githubusercontent.com/"
+                            "smartHomeHub/SmartIR/master/"
+                            "codes/climate/{}.json")
+
+            Helper.downloader(codes_source.format(device_code), device_json_path)
+        except:
+            _LOGGER.error("There was an error while downloading the device Json file. " \
+                          "Please check your internet connection or if the device code " \
+                          "exists on GitHub. If the problem still exists please " \
+                          "place the file manually in the proper directory.")
+            return
+
+    with open(device_json_path) as j:
+        try:
+            device_data = json.load(j)
+        except:
+            _LOGGER.error("The device Json file is invalid")
+            return
+
+    async_add_entities([SmartIRClimate(
+        hass, config, device_data
+    )])
+
+class SmartIRClimate(ClimateDevice, RestoreEntity):
+    def __init__(self, hass, config, device_data):
+        self.hass = hass
+        self._unique_id = config.get(CONF_UNIQUE_ID)
+        self._name = config.get(CONF_NAME)
+        self._device_code = config.get(CONF_DEVICE_CODE)
+        self._controller_data = config.get(CONF_CONTROLLER_DATA)
+        self._temperature_sensor = config.get(CONF_TEMPERATURE_SENSOR)
+        self._humidity_sensor = config.get(CONF_HUMIDITY_SENSOR)
+        self._power_sensor = config.get(CONF_POWER_SENSOR)
+
+        self._manufacturer = device_data['manufacturer']
+        self._supported_models = device_data['supportedModels']
+        self._supported_controller = device_data['supportedController']
+        self._commands_encoding = device_data['commandsEncoding']
+        self._min_temperature = device_data['minTemperature']
+        self._max_temperature = device_data['maxTemperature']
+        self._precision = device_data['precision']
+
+        valid_hvac_modes = [x for x in device_data['operationModes'] if x in HVAC_MODES]
+
+        self._operation_modes = [HVAC_MODE_OFF] + valid_hvac_modes
+        self._fan_modes = device_data['fanModes']
+        self._commands = device_data['commands']
+
+        self._target_temperature = self._min_temperature
+        self._hvac_mode = HVAC_MODE_OFF
+        self._current_fan_mode = self._fan_modes[0]
+        self._last_on_operation = None
+
+        self._current_temperature = None
+        self._current_humidity = None
+
+        self._unit = hass.config.units.temperature_unit
+        self._support_flags = SUPPORT_FLAGS
+
+        self._temp_lock = asyncio.Lock()
+        self._on_by_remote = False
+
+        #Init the IR/RF controller
+        self._controller = Controller(
+            self.hass,
+            self._supported_controller, 
+            self._commands_encoding,
+            self._controller_data)
+            
+    async def async_added_to_hass(self):
+        """Run when entity about to be added."""
+        await super().async_added_to_hass()
+    
+        last_state = await self.async_get_last_state()
+        
+        if last_state is not None:
+            self._hvac_mode = last_state.state
+            self._current_fan_mode = last_state.attributes['fan_mode']
+            self._target_temperature = last_state.attributes['temperature']
+
+            if 'last_on_operation' in last_state.attributes:
+                self._last_on_operation = last_state.attributes['last_on_operation']
+
+        if self._temperature_sensor:
+            async_track_state_change(self.hass, self._temperature_sensor, 
+                                     self._async_temp_sensor_changed)
+
+            temp_sensor_state = self.hass.states.get(self._temperature_sensor)
+            if temp_sensor_state and temp_sensor_state.state != STATE_UNKNOWN:
+                self._async_update_temp(temp_sensor_state)
+
+        if self._humidity_sensor:
+            async_track_state_change(self.hass, self._humidity_sensor, 
+                                     self._async_humidity_sensor_changed)
+
+            humidity_sensor_state = self.hass.states.get(self._humidity_sensor)
+            if humidity_sensor_state and humidity_sensor_state.state != STATE_UNKNOWN:
+                self._async_update_humidity(humidity_sensor_state)
+
+        if self._power_sensor:
+            async_track_state_change(self.hass, self._power_sensor, 
+                                     self._async_power_sensor_changed)
+
+    @property
+    def unique_id(self):
+        """Return a unique ID."""
+        return self._unique_id
+
+    @property
+    def name(self):
+        """Return the name of the climate device."""
+        return self._name
+
+    @property
+    def state(self):
+        """Return the current state."""
+        if self._on_by_remote:
+            return STATE_ON
+        if self.hvac_mode != HVAC_MODE_OFF:
+            return self.hvac_mode
+        return HVAC_MODE_OFF
+
+    @property
+    def temperature_unit(self):
+        """Return the unit of measurement."""
+        return self._unit
+
+    @property
+    def min_temp(self):
+        """Return the polling state."""
+        return self._min_temperature
+        
+    @property
+    def max_temp(self):
+        """Return the polling state."""
+        return self._max_temperature
+
+    @property
+    def target_temperature(self):
+        """Return the temperature we try to reach."""
+        return self._target_temperature
+
+    @property
+    def target_temperature_step(self):
+        """Return the supported step of target temperature."""
+        return self._precision
+
+    @property
+    def hvac_modes(self):
+        """Return the list of available operation modes."""
+        return self._operation_modes
+
+    @property
+    def hvac_mode(self):
+        """Return hvac mode ie. heat, cool."""
+        return self._hvac_mode
+
+    @property
+    def last_on_operation(self):
+        """Return the last non-idle operation ie. heat, cool."""
+        return self._last_on_operation
+
+    @property
+    def fan_modes(self):
+        """Return the list of available fan modes."""
+        return self._fan_modes
+
+    @property
+    def fan_mode(self):
+        """Return the fan setting."""
+        return self._current_fan_mode
+
+    @property
+    def current_temperature(self):
+        """Return the current temperature."""
+        return self._current_temperature
+
+    @property
+    def current_humidity(self):
+        """Return the current humidity."""
+        return self._current_humidity
+
+    @property
+    def supported_features(self):
+        """Return the list of supported features."""
+        return self._support_flags
+
+    @property
+    def device_state_attributes(self) -> dict:
+        """Platform specific attributes."""
+        return {
+            'last_on_operation': self._last_on_operation,
+            'device_code': self._device_code,
+            'manufacturer': self._manufacturer,
+            'supported_models': self._supported_models,
+            'supported_controller': self._supported_controller,
+            'commands_encoding': self._commands_encoding,
+        }
+
+    async def async_set_temperature(self, **kwargs):
+        """Set new target temperatures."""
+        hvac_mode = kwargs.get(ATTR_HVAC_MODE)  
+        temperature = kwargs.get(ATTR_TEMPERATURE)
+          
+        if temperature is None:
+            return
+            
+        if temperature < self._min_temperature or temperature > self._max_temperature:
+            _LOGGER.warning('The temperature value is out of min/max range') 
+            return
+
+        if self._precision == PRECISION_WHOLE:
+            self._target_temperature = round(temperature)
+        else:
+            self._target_temperature = round(temperature, 1)
+
+        if hvac_mode:
+            await self.async_set_hvac_mode(hvac_mode)
+            return
+        
+        if not self._hvac_mode.lower() == HVAC_MODE_OFF:
+            await self.send_command()
+
+        await self.async_update_ha_state()
+
+    async def async_set_hvac_mode(self, hvac_mode):
+        """Set operation mode."""
+        self._hvac_mode = hvac_mode
+        
+        if not hvac_mode == HVAC_MODE_OFF:
+            self._last_on_operation = hvac_mode
+
+        await self.send_command()
+        await self.async_update_ha_state()
+
+    async def async_set_fan_mode(self, fan_mode):
+        """Set fan mode."""
+        self._current_fan_mode = fan_mode
+        
+        if not self._hvac_mode.lower() == HVAC_MODE_OFF:
+            await self.send_command()      
+        await self.async_update_ha_state()
+
+    async def async_turn_off(self):
+        """Turn off."""
+        await self.async_set_hvac_mode(HVAC_MODE_OFF)
+        
+    async def async_turn_on(self):
+        """Turn on."""
+        if self._last_on_operation is not None:
+            await self.async_set_hvac_mode(self._last_on_operation)
+        else:
+            await self.async_set_hvac_mode(self._operation_modes[1])
+
+    async def send_command(self):
+        async with self._temp_lock:
+            self._on_by_remote = False
+            operation_mode = self._hvac_mode
+            fan_mode = self._current_fan_mode
+            target_temperature = '{0:g}'.format(self._target_temperature)
+
+            if operation_mode.lower() == HVAC_MODE_OFF:
+                command = self._commands['off']
+            else:
+                command = self._commands[operation_mode][fan_mode][target_temperature]
+
+            try:
+                await self._controller.send(command)
+            except Exception as e:
+                _LOGGER.exception(e)
+            
+    async def _async_temp_sensor_changed(self, entity_id, old_state, new_state):
+        """Handle temperature sensor changes."""
+        if new_state is None:
+            return
+
+        self._async_update_temp(new_state)
+        await self.async_update_ha_state()
+
+    async def _async_humidity_sensor_changed(self, entity_id, old_state, new_state):
+        """Handle humidity sensor changes."""
+        if new_state is None:
+            return
+
+        self._async_update_humidity(new_state)
+        await self.async_update_ha_state()
+
+    async def _async_power_sensor_changed(self, entity_id, old_state, new_state):
+        """Handle power sensor changes."""
+        if new_state is None:
+            return
+
+        if new_state.state == STATE_ON and self._hvac_mode == HVAC_MODE_OFF:
+            self._on_by_remote = True
+            await self.async_update_ha_state()
+
+        if new_state.state == HVAC_MODE_OFF:
+            self._on_by_remote = False
+            if self._hvac_mode != HVAC_MODE_OFF:
+                self._hvac_mode = HVAC_MODE_OFF
+            await self.async_update_ha_state()
+
+    @callback
+    def _async_update_temp(self, state):
+        """Update thermostat with latest state from temperature sensor."""
+        try:
+            if state.state != STATE_UNKNOWN:
+                self._current_temperature = float(state.state)
+        except ValueError as ex:
+            _LOGGER.error("Unable to update from temperature sensor: %s", ex)
+
+    @callback
+    def _async_update_humidity(self, state):
+        """Update thermostat with latest state from humidity sensor."""
+        try:
+            if state.state != STATE_UNKNOWN:
+                self._current_humidity = float(state.state)
+        except ValueError as ex:
+            _LOGGER.error("Unable to update from humidity sensor: %s", ex)

+ 102 - 0
smartir/controller.py

@@ -0,0 +1,102 @@
+import asyncio
+from base64 import b64encode
+import binascii
+import logging
+
+from homeassistant.const import ATTR_ENTITY_ID
+from homeassistant.core import split_entity_id
+from . import Helper
+
+_LOGGER = logging.getLogger(__name__)
+
+BROADLINK_CONTROLLER = 'Broadlink'
+XIAOMI_CONTROLLER = 'Xiaomi'
+MQTT_CONTROLLER = 'MQTT'
+
+ENC_BASE64 = 'Base64'
+ENC_HEX = 'Hex'
+ENC_PRONTO = 'Pronto'
+ENC_RAW = 'Raw'
+
+BROADLINK_COMMANDS_ENCODING = [
+    ENC_BASE64, ENC_HEX, ENC_PRONTO]
+
+XIAOMI_COMMANDS_ENCODING = [
+    ENC_PRONTO, ENC_RAW]
+
+MQTT_COMMANDS_ENCODING = [ENC_RAW]
+
+class Controller():
+    def __init__(self, hass, controller, encoding, controller_data):
+        if controller not in [
+            BROADLINK_CONTROLLER, XIAOMI_CONTROLLER, MQTT_CONTROLLER]:
+            raise Exception("The controller is not supported.")
+
+        if controller == BROADLINK_CONTROLLER:
+            if encoding not in BROADLINK_COMMANDS_ENCODING:
+                raise Exception("The encoding is not supported "
+                                "by the Broadlink controller.")
+
+        if controller == XIAOMI_CONTROLLER:
+            if encoding not in XIAOMI_COMMANDS_ENCODING:
+                raise Exception("The encoding is not supported "
+                                "by the Xiaomi controller.")
+
+        if controller == MQTT_CONTROLLER:
+            if encoding not in MQTT_COMMANDS_ENCODING:
+                raise Exception("The encoding is not supported "
+                                "by the mqtt controller.")
+
+        self.hass = hass
+        self._controller = controller
+        self._encoding = encoding
+        self._controller_data = controller_data
+
+    async def send(self, command):
+        if self._controller == BROADLINK_CONTROLLER:
+            if self._encoding == ENC_HEX:
+                try:
+                    command = binascii.unhexlify(command)
+                    command = b64encode(command).decode('utf-8')
+                except:
+                    raise Exception("Error while converting "
+                                    "Hex to Base64 encoding")
+
+            if self._encoding == ENC_PRONTO:
+                try:
+                    command = command.replace(' ', '')
+                    command = bytearray.fromhex(command)
+                    command = Helper.pronto2lirc(command)
+                    command = Helper.lirc2broadlink(command)
+                    command = b64encode(command).decode('utf-8')
+                except:
+                    raise Exception("Error while converting "
+                                    "Pronto to Base64 encoding")
+
+            service_data = {
+                'host': self._controller_data,
+                'packet': command
+            }
+
+            await self.hass.services.async_call(
+                'broadlink', 'send', service_data)
+
+
+        if self._controller == XIAOMI_CONTROLLER:
+            service_data = {
+                ATTR_ENTITY_ID: self._controller_data,
+                'command':  self._encoding.lower() + ':' + command
+            }
+
+            await self.hass.services.async_call(
+               'remote', 'send_command', service_data)
+
+
+        if self._controller == MQTT_CONTROLLER:
+            service_data = {
+                'topic': self._controller_data,
+                'payload': command
+            }
+
+            await self.hass.services.async_call(
+               'mqtt', 'publish', service_data)

+ 260 - 0
smartir/fan.py

@@ -0,0 +1,260 @@
+import asyncio
+import json
+import logging
+import os.path
+
+import voluptuous as vol
+
+from homeassistant.components.fan import (
+    FanEntity, PLATFORM_SCHEMA, ATTR_SPEED, 
+    SPEED_OFF, SPEED_LOW, SPEED_MEDIUM, SPEED_HIGH, 
+    DIRECTION_REVERSE, DIRECTION_FORWARD,
+    SUPPORT_SET_SPEED, SUPPORT_DIRECTION)
+from homeassistant.const import (
+    CONF_NAME, STATE_OFF, STATE_ON, STATE_UNKNOWN)
+from homeassistant.core import callback
+from homeassistant.helpers.event import async_track_state_change
+import homeassistant.helpers.config_validation as cv
+from homeassistant.helpers.restore_state import RestoreEntity
+from . import COMPONENT_ABS_DIR, Helper
+from .controller import Controller
+
+_LOGGER = logging.getLogger(__name__)
+
+DEFAULT_NAME = "SmartIR Fan"
+
+CONF_UNIQUE_ID = 'unique_id'
+CONF_DEVICE_CODE = 'device_code'
+CONF_CONTROLLER_DATA = "controller_data"
+CONF_POWER_SENSOR = 'power_sensor'
+
+PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
+    vol.Optional(CONF_UNIQUE_ID): cv.string,
+    vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
+    vol.Required(CONF_DEVICE_CODE): cv.positive_int,
+    vol.Required(CONF_CONTROLLER_DATA): cv.string,
+    vol.Optional(CONF_POWER_SENSOR): cv.entity_id
+})
+
+async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
+    """Set up the IR Fan platform."""
+    device_code = config.get(CONF_DEVICE_CODE)
+    device_files_subdir = os.path.join('codes', 'fan')
+    device_files_absdir = os.path.join(COMPONENT_ABS_DIR, device_files_subdir)
+
+    if not os.path.isdir(device_files_absdir):
+        os.makedirs(device_files_absdir)
+
+    device_json_filename = str(device_code) + '.json'
+    device_json_path = os.path.join(device_files_absdir, device_json_filename)
+
+    if not os.path.exists(device_json_path):
+        _LOGGER.warning("Couldn't find the device Json file. The component will " \
+                        "try to download it from the GitHub repo.")
+
+        try:
+            codes_source = ("https://raw.githubusercontent.com/"
+                            "smartHomeHub/SmartIR/master/"
+                            "codes/fan/{}.json")
+
+            Helper.downloader(codes_source.format(device_code), device_json_path)
+        except:
+            _LOGGER.error("There was an error while downloading the device Json file. " \
+                          "Please check your internet connection or if the device code " \
+                          "exists on GitHub. If the problem still exists please " \
+                          "place the file manually in the proper directory.")
+            return
+
+    with open(device_json_path) as j:
+        try:
+            device_data = json.load(j)
+        except:
+            _LOGGER.error("The device JSON file is invalid")
+            return
+
+    async_add_entities([SmartIRFan(
+        hass, config, device_data
+    )])
+
+class SmartIRFan(FanEntity, RestoreEntity):
+    def __init__(self, hass, config, device_data):
+        self.hass = hass
+        self._unique_id = config.get(CONF_UNIQUE_ID)
+        self._name = config.get(CONF_NAME)
+        self._device_code = config.get(CONF_DEVICE_CODE)
+        self._controller_data = config.get(CONF_CONTROLLER_DATA)
+        self._power_sensor = config.get(CONF_POWER_SENSOR)
+
+        self._manufacturer = device_data['manufacturer']
+        self._supported_models = device_data['supportedModels']
+        self._supported_controller = device_data['supportedController']
+        self._commands_encoding = device_data['commandsEncoding']
+        self._speed_list = [SPEED_OFF] + device_data['speed']
+        self._commands = device_data['commands']
+        
+        self._speed = SPEED_OFF
+        self._direction = None
+        self._last_on_speed = None
+
+        self._support_flags = SUPPORT_SET_SPEED
+
+        if (DIRECTION_REVERSE in self._commands and \
+            DIRECTION_FORWARD in self._commands):
+            self._direction = DIRECTION_REVERSE
+            self._support_flags = (
+                self._support_flags | SUPPORT_DIRECTION)
+
+        self._temp_lock = asyncio.Lock()
+        self._on_by_remote = False
+
+        #Init the IR/RF controller
+        self._controller = Controller(
+            self.hass,
+            self._supported_controller, 
+            self._commands_encoding,
+            self._controller_data)
+
+    async def async_added_to_hass(self):
+        """Run when entity about to be added."""
+        await super().async_added_to_hass()
+    
+        last_state = await self.async_get_last_state()
+
+        if last_state is not None:
+            if 'speed' in last_state.attributes:
+                self._speed = last_state.attributes['speed']
+
+            #If _direction has a value the direction controls appears 
+            #in UI even if SUPPORT_DIRECTION is not provided in the flags
+            if ('direction' in last_state.attributes and \
+                self._support_flags & SUPPORT_DIRECTION):
+                self._direction = last_state.attributes['direction']
+
+            if 'last_on_speed' in last_state.attributes:
+                self._last_on_speed = last_state.attributes['last_on_speed']
+
+            if self._power_sensor:
+                async_track_state_change(self.hass, self._power_sensor, 
+                                         self._async_power_sensor_changed)
+
+    @property
+    def unique_id(self):
+        """Return a unique ID."""
+        return self._unique_id
+
+    @property
+    def name(self):
+        """Return the display name of the fan."""
+        return self._name
+
+    @property
+    def state(self):
+        """Return the current state."""
+        if (self._on_by_remote or \
+            self.speed != SPEED_OFF):
+            return STATE_ON
+        return SPEED_OFF
+
+    @property
+    def speed_list(self):
+        """Get the list of available speeds."""
+        return self._speed_list
+
+    @property
+    def speed(self):
+        """Return the current speed."""
+        return self._speed
+
+    @property
+    def oscillating(self):
+        """Return the oscillation state."""
+        return None
+
+    @property
+    def direction(self):
+        """Return the oscillation state."""
+        return self._direction
+
+    @property
+    def last_on_speed(self):
+        """Return the last non-idle speed."""
+        return self._last_on_speed
+
+    @property
+    def supported_features(self):
+        """Return the list of supported features."""
+        return self._support_flags
+
+    @property
+    def device_state_attributes(self) -> dict:
+        """Platform specific attributes."""
+        return {
+            'last_on_speed': self._last_on_speed,
+            'device_code': self._device_code,
+            'manufacturer': self._manufacturer,
+            'supported_models': self._supported_models,
+            'supported_controller': self._supported_controller,
+            'commands_encoding': self._commands_encoding,
+        }
+
+    async def async_set_speed(self, speed: str):
+        """Set the speed of the fan."""
+        self._speed = speed
+
+        if not speed == SPEED_OFF:
+            self._last_on_speed = speed
+
+        await self.send_command()
+        await self.async_update_ha_state()
+
+    async def async_set_direction(self, direction: str):
+        """Set the direction of the fan"""
+        self._direction = direction
+
+        if not self._speed.lower() == SPEED_OFF:
+            await self.send_command()
+
+        await self.async_update_ha_state()
+
+    async def async_turn_on(self, speed: str = None, **kwargs):
+        """Turn on the fan."""
+        if speed is None:
+            speed = self._last_on_speed or self._speed_list[1]
+
+        await self.async_set_speed(speed)
+
+    async def async_turn_off(self):
+        """Turn off the fan."""
+        await self.async_set_speed(SPEED_OFF)
+
+    async def send_command(self):
+        async with self._temp_lock:
+            self._on_by_remote = False
+            speed = self._speed
+            direction = self._direction or 'default'
+
+            if speed.lower() == SPEED_OFF:
+                command = self._commands['off']
+            else:
+                command = self._commands[direction][speed] 
+
+            try:
+                await self._controller.send(command)
+            except Exception as e:
+                _LOGGER.exception(e)
+
+    async def _async_power_sensor_changed(self, entity_id, old_state, new_state):
+        """Handle power sensor changes."""
+        if new_state is None:
+            return
+
+        if new_state.state == STATE_ON and self._speed == SPEED_OFF:
+            self._on_by_remote = True
+            self._speed = None
+            await self.async_update_ha_state()
+
+        if new_state.state == STATE_OFF:
+            self._on_by_remote = False
+            if self._speed != SPEED_OFF:
+                self._speed = SPEED_OFF
+            await self.async_update_ha_state()

+ 22 - 0
smartir/manifest.json

@@ -0,0 +1,22 @@
+{
+  "domain": "smartir",
+  "name": "SmartIR",
+  "documentation": "https://github.com/smartHomeHub/SmartIR",
+  "dependencies": [],
+  "codeowners": ["@smartHomeHub"],
+  "requirements": [],
+  "homeassistant": "0.96.0b1",
+  "updater": {
+    "version": "1.6.2",
+    "releaseNotes": "* Add support for Fujitsu AR-JE5 remote control * Add support for Hualing KFR-45GW/JNV, KFR-45G/JNV AC units * Add support for Daitsu DS-9KIDT AC unit * Add support for Riello WSI XN, RAR-3U4 AC units",
+    "files": [
+      "__init__.py",
+      "climate.py",
+      "media_player.py",
+      "fan.py",
+      "controller.py",
+      "manifest.json",
+      "services.yaml"
+    ]
+  }
+}

+ 264 - 0
smartir/media_player.py

@@ -0,0 +1,264 @@
+import asyncio
+import json
+import logging
+import os.path
+
+import voluptuous as vol
+
+from homeassistant.components.media_player import (
+    MediaPlayerDevice, PLATFORM_SCHEMA)
+from homeassistant.components.media_player.const import (
+    SUPPORT_TURN_OFF, SUPPORT_TURN_ON, SUPPORT_PREVIOUS_TRACK,
+    SUPPORT_NEXT_TRACK, SUPPORT_VOLUME_STEP, SUPPORT_VOLUME_SET,
+    SUPPORT_VOLUME_MUTE, SUPPORT_SELECT_SOURCE, MEDIA_TYPE_CHANNEL)
+from homeassistant.const import (
+    CONF_NAME, STATE_OFF, STATE_ON, STATE_UNKNOWN)
+from homeassistant.core import callback
+import homeassistant.helpers.config_validation as cv
+from homeassistant.helpers.restore_state import RestoreEntity
+from . import COMPONENT_ABS_DIR, Helper
+from .controller import Controller
+
+_LOGGER = logging.getLogger(__name__)
+
+DEFAULT_NAME = "SmartIR Media Player"
+
+CONF_UNIQUE_ID = 'unique_id'
+CONF_DEVICE_CODE = 'device_code'
+CONF_CONTROLLER_DATA = "controller_data"
+CONF_POWER_SENSOR = 'power_sensor'
+
+PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
+    vol.Optional(CONF_UNIQUE_ID): cv.string,
+    vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
+    vol.Required(CONF_DEVICE_CODE): cv.positive_int,
+    vol.Required(CONF_CONTROLLER_DATA): cv.string,
+    vol.Optional(CONF_POWER_SENSOR): cv.entity_id
+})
+
+async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
+    """Set up the IR Media Player platform."""
+    device_code = config.get(CONF_DEVICE_CODE)
+    device_files_subdir = os.path.join('codes', 'media_player')
+    device_files_absdir = os.path.join(COMPONENT_ABS_DIR, device_files_subdir)
+
+    if not os.path.isdir(device_files_absdir):
+        os.makedirs(device_files_absdir)
+
+    device_json_filename = str(device_code) + '.json'
+    device_json_path = os.path.join(device_files_absdir, device_json_filename)
+
+    if not os.path.exists(device_json_path):
+        _LOGGER.warning("Couldn't find the device Json file. The component will " \
+                        "try to download it from the GitHub repo.")
+
+        try:
+            codes_source = ("https://raw.githubusercontent.com/"
+                            "smartHomeHub/SmartIR/master/"
+                            "codes/media_player/{}.json")
+
+            Helper.downloader(codes_source.format(device_code), device_json_path)
+        except:
+            _LOGGER.error("There was an error while downloading the device Json file. " \
+                          "Please check your internet connection or if the device code " \
+                          "exists on GitHub. If the problem still exists please " \
+                          "place the file manually in the proper directory.")
+            return
+
+    with open(device_json_path) as j:
+        try:
+            device_data = json.load(j)
+        except:
+            _LOGGER.error("The device JSON file is invalid")
+            return
+
+    async_add_entities([SmartIRMediaPlayer(
+        hass, config, device_data
+    )])
+
+class SmartIRMediaPlayer(MediaPlayerDevice, RestoreEntity):
+    def __init__(self, hass, config, device_data):
+        self.hass = hass
+        self._unique_id = config.get(CONF_UNIQUE_ID)
+        self._name = config.get(CONF_NAME)
+        self._device_code = config.get(CONF_DEVICE_CODE)
+        self._controller_data = config.get(CONF_CONTROLLER_DATA)
+        self._power_sensor = config.get(CONF_POWER_SENSOR)
+
+        self._manufacturer = device_data['manufacturer']
+        self._supported_models = device_data['supportedModels']
+        self._supported_controller = device_data['supportedController']
+        self._commands_encoding = device_data['commandsEncoding']
+        self._commands = device_data['commands']
+
+        self._state = STATE_OFF
+        self._sources_list = []
+        self._source = None
+        self._support_flags = 0
+
+        #Supported features
+        if 'off' in self._commands and self._commands['off'] is not None:
+            self._support_flags = self._support_flags | SUPPORT_TURN_OFF
+
+        if 'on' in self._commands and self._commands['on'] is not None:
+            self._support_flags = self._support_flags | SUPPORT_TURN_ON
+
+        if 'previousChannel' in self._commands and self._commands['previousChannel'] is not None:
+            self._support_flags = self._support_flags | SUPPORT_PREVIOUS_TRACK
+
+        if 'nextChannel' in self._commands and self._commands['nextChannel'] is not None:
+            self._support_flags = self._support_flags | SUPPORT_NEXT_TRACK
+
+        if ('volumeDown' in self._commands and self._commands['volumeDown'] is not None) \
+        or ('volumeUp' in self._commands and self._commands['volumeUp'] is not None):
+            self._support_flags = self._support_flags | SUPPORT_VOLUME_STEP | SUPPORT_VOLUME_SET
+
+        if 'mute' in self._commands and self._commands['mute'] is not None:
+            self._support_flags = self._support_flags | SUPPORT_VOLUME_MUTE
+
+        if 'sources' in self._commands and self._commands['sources'] is not None:
+            self._support_flags = self._support_flags | SUPPORT_SELECT_SOURCE
+
+            #Sources list
+            for key in self._commands['sources']:
+                self._sources_list.append(key)
+
+        self._temp_lock = asyncio.Lock()
+
+        #Init the IR/RF controller
+        self._controller = Controller(
+            self.hass,
+            self._supported_controller, 
+            self._commands_encoding,
+            self._controller_data)
+
+    async def async_added_to_hass(self):
+        """Run when entity about to be added."""
+        await super().async_added_to_hass()
+
+        last_state = await self.async_get_last_state()
+
+        if last_state is not None:
+            self._state = last_state.state
+
+    @property
+    def should_poll(self):
+        """Push an update after each command."""
+        return True
+
+    @property
+    def unique_id(self):
+        """Return a unique ID."""
+        return self._unique_id
+
+    @property
+    def name(self):
+        """Return the name of the media player."""
+        return self._name
+
+    @property
+    def state(self):
+        """Return the state of the player."""
+        return self._state
+
+    @property
+    def media_title(self):
+        """Return the title of current playing media."""
+        return None
+
+    @property
+    def media_content_type(self):
+        """Content type of current playing media."""
+        return MEDIA_TYPE_CHANNEL
+
+    @property
+    def source_list(self):
+        return self._sources_list
+        
+    @property
+    def source(self):
+        return self._source
+
+    @property
+    def supported_features(self):
+        """Flag media player features that are supported."""
+        return self._support_flags
+
+    @property
+    def device_state_attributes(self) -> dict:
+        """Platform specific attributes."""
+        return {
+            'device_code': self._device_code,
+            'manufacturer': self._manufacturer,
+            'supported_models': self._supported_models,
+            'supported_controller': self._supported_controller,
+            'commands_encoding': self._commands_encoding,
+        }
+
+    async def async_turn_off(self):
+        """Turn the media player off."""
+        await self.send_command(self._commands['off'])
+        
+        if self._power_sensor is None:
+            self._state = STATE_OFF
+            self._source = None
+            await self.async_update_ha_state()
+
+    async def async_turn_on(self):
+        """Turn the media player off."""
+        await self.send_command(self._commands['on'])
+
+        if self._power_sensor is None:
+            self._state = STATE_ON
+            await self.async_update_ha_state()
+
+    async def async_media_previous_track(self):
+        """Send previous track command."""
+        await self.send_command(self._commands['previousChannel'])
+        await self.async_update_ha_state()
+
+    async def async_media_next_track(self):
+        """Send next track command."""
+        await self.send_command(self._commands['nextChannel'])
+        await self.async_update_ha_state()
+
+    async def async_volume_down(self):
+        """Turn volume down for media player."""
+        await self.send_command(self._commands['volumeDown'])
+        await self.async_update_ha_state()
+
+    async def async_volume_up(self):
+        """Turn volume up for media player."""
+        await self.send_command(self._commands['volumeUp'])
+        await self.async_update_ha_state()
+    
+    async def async_mute_volume(self, mute):
+        """Mute the volume."""
+        await self.send_command(self._commands['mute'])
+        await self.async_update_ha_state()
+
+    async def async_select_source(self, source):
+        """Select channel from source."""
+        self._source = source
+        await self.send_command(self._commands['sources'][source])
+        await self.async_update_ha_state()
+
+    async def send_command(self, command):
+        async with self._temp_lock:
+            try:
+                await self._controller.send(command)
+            except Exception as e:
+                _LOGGER.exception(e)
+            
+    async def async_update(self):
+        if self._power_sensor is None:
+            return
+
+        power_state = self.hass.states.get(self._power_sensor)
+
+        if power_state:
+            if power_state.state == STATE_OFF:
+                self._state = STATE_OFF
+                self._source = None
+            elif power_state.state == STATE_ON:
+                self._state = STATE_ON

+ 4 - 0
smartir/services.yaml

@@ -0,0 +1,4 @@
+check_updates:
+  description: Check for SmartIR updates.
+update_component:
+  description: Update SmartIR component.