예제 #1
0
class JVCRemote(remote.RemoteEntity):
    """Home assistant JVC remote representation"""
    def __init__(self, name, host):
        """Initialize the Remote."""
        from jvc_projector import JVCProjector
        self._name = name or DEVICE_DEFAULT_NAME
        self._host = host
        self._last_command_sent = None
        self._jvc = JVCProjector(host)
        self._state = None
        self._power_state = 'N/A'

    @property
    def should_poll(self):
        # poll the device so we know if it was state changed
        # via an external method, like the physical remote
        return True

    @property
    def name(self):
        """Return the name of the device if any."""
        return self._name

    async def async_update(self):
        await self.async_update_state()

    @property
    def is_on(self):
        """Return true if remote is on."""
        return self._state

    @property
    def device_state_attributes(self):
        """Return device state attributes."""

        if self._power_state in ['lamp_on', 'reserved']:
            self._state = True
        else:
            self._state = False

        return {
            'last_command_sent':
            self._last_command_sent
            if self._last_command_sent is not None else 'N/A',
            'power_state':
            self._power_state,
        }

    async def async_turn_on(self, **kwargs):
        """Turn the remote on."""
        await self.async_send_command('power_on')
        self._state = True

    async def async_turn_off(self, **kwargs):
        """Turn the remote off."""
        await self.async_send_command('power_off')
        self._state = False

    async def async_send_command(self, command, **kwargs):
        """Send a command to a device."""

        if type(command) != list:
            command = [command]

        for com in command:
            _LOGGER.info(f"sending command: {com}")
            try:
                command_sent = self._jvc.command(com)
            except Exception:
                # when an error occured during sending, command execution probably failed
                command_sent = False

            if not command_sent:
                self._last_command_sent = "N/A"
                continue
            else:
                self._last_command_sent = com

    async def async_update_state(self):
        """"Update the state with the Power Status (if available)"""

        try:
            self._power_state = self._jvc.power_state()
        except Exception:
            self._power_state = 'unknown'
예제 #2
0
class JVCRemote(remote.RemoteDevice):
    """Home assistant JVC remote representation"""
    def __init__(self, name, host):
        """Initialize the Remote."""
        from jvc_projector import JVCProjector
        self._name = name or DEVICE_DEFAULT_NAME
        self._host = host
        self._last_command_sent = None
        self._jvc = JVCProjector(host)
        self._state = None

    @property
    def should_poll(self):
        # poll the device so we know if it was state changed
        # via an external method, like the physical remote
        return True

    @property
    def name(self):
        """Return the name of the device if any."""
        return self._name

    def update(self):
        self._state = self._jvc.is_on()

    @property
    def is_on(self):
        """Return true if remote is on."""
        return self._state

    @property
    def device_state_attributes(self):
        """Return device state attributes."""
        if self._last_command_sent is not None:
            return {'last_command_sent': self._last_command_sent}

    async def async_turn_on(self, **kwargs):
        """Turn the remote on."""
        _LOGGER.info("powering on")
        self._jvc.power_on()
        self._state = True
        await asyncio.sleep(1)
        self.schedule_update_ha_state(True)

    async def async_turn_off(self, **kwargs):
        """Turn the remote off."""
        _LOGGER.info("powering off")
        self._jvc.power_off()
        self._state = False
        await asyncio.sleep(1)
        self.schedule_update_ha_state(True)

    async def async_send_command(self, command, **kwargs):
        """Send a command to a device."""
        for com in command:
            _LOGGER.info(f"sending command: {com}")
            command_sent = self._jvc.command(com)
            if not command_sent:
                self._last_command_sent = "N/A"
                continue
            else:
                self._last_command_sent = com

                await asyncio.sleep(1)
                self.schedule_update_ha_state()