def update(self):
    "Update the TP-Link switch's state."
    try:
        if (not self._sysinfo):
            self._sysinfo = self.smartplug.sys_info
            self._mac = self.smartplug.mac
            self._model = self.smartplug.model
            if (self.smartplug.context is None):
                self._alias = self.smartplug.alias
                self._device_id = self._mac
            else:
                self._alias = [child for child in self.smartplug.sys_info['children'] if (child['id'] == self.smartplug.context)][0]['alias']
                self._device_id = self.smartplug.context
        if (self.smartplug.context is None):
            self._state = (self.smartplug.state == self.smartplug.SWITCH_STATE_ON)
        else:
            self._state = ([child for child in self.smartplug.sys_info['children'] if (child['id'] == self.smartplug.context)][0]['state'] == 1)
        if self.smartplug.has_emeter:
            emeter_readings = self.smartplug.get_emeter_realtime()
            self._emeter_params[ATTR_CURRENT_POWER_W] = '{:.2f}'.format(emeter_readings['power'])
            self._emeter_params[ATTR_TOTAL_ENERGY_KWH] = '{:.3f}'.format(emeter_readings['total'])
            self._emeter_params[ATTR_VOLTAGE] = '{:.1f}'.format(emeter_readings['voltage'])
            self._emeter_params[ATTR_CURRENT_A] = '{:.2f}'.format(emeter_readings['current'])
            emeter_statics = self.smartplug.get_emeter_daily()
            try:
                self._emeter_params[ATTR_TODAY_ENERGY_KWH] = '{:.3f}'.format(emeter_statics[int(time.strftime('%e'))])
            except KeyError:
                pass
        self._available = True
    except (SmartDeviceException, OSError) as ex:
        if self._available:
            _LOGGER.warning('Could not read state for %s: %s', self.smartplug.host, ex)
        self._available = False
Update the TP-Link switch's state.
def update(self):
    
    try:
        if (not self._sysinfo):
            self._sysinfo = self.smartplug.sys_info
            self._mac = self.smartplug.mac
            self._model = self.smartplug.model
            if (self.smartplug.context is None):
                self._alias = self.smartplug.alias
                self._device_id = self._mac
            else:
                self._alias = [child for child in self.smartplug.sys_info['children'] if (child['id'] == self.smartplug.context)][0]['alias']
                self._device_id = self.smartplug.context
        if (self.smartplug.context is None):
            self._state = (self.smartplug.state == self.smartplug.SWITCH_STATE_ON)
        else:
            self._state = ([child for child in self.smartplug.sys_info['children'] if (child['id'] == self.smartplug.context)][0]['state'] == 1)
        if self.smartplug.has_emeter:
            emeter_readings = self.smartplug.get_emeter_realtime()
            self._emeter_params[ATTR_CURRENT_POWER_W] = '{:.2f}'.format(emeter_readings['power'])
            self._emeter_params[ATTR_TOTAL_ENERGY_KWH] = '{:.3f}'.format(emeter_readings['total'])
            self._emeter_params[ATTR_VOLTAGE] = '{:.1f}'.format(emeter_readings['voltage'])
            self._emeter_params[ATTR_CURRENT_A] = '{:.2f}'.format(emeter_readings['current'])
            emeter_statics = self.smartplug.get_emeter_daily()
            try:
                self._emeter_params[ATTR_TODAY_ENERGY_KWH] = '{:.3f}'.format(emeter_statics[int(time.strftime('%e'))])
            except KeyError:
                pass
        self._available = True
    except (SmartDeviceException, OSError) as ex:
        if self._available:
            _LOGGER.warning('Could not read state for %s: %s', self.smartplug.host, ex)
        self._available = False